OKPEDIA PYTHON EN

How to make classes in python

Create a class called Student

  • class Student:
  • def __init__(self, number, name, age):
  • self.number = number
  • self.name = name
  • self.age = name

The class has three properties: number, name, age.

How to assign objects to the class

To create the objects of the class, type

  • student1 = Student(3221, "Tom Smith", 18)
  • student2 = Student(3484, "John Williams",19)
  • student3 = Student(3123, "Sam Johnson",19)

These three objects (student1, student2, student3) contain 9 information.

Without object-oriented programming it would have been necessary to use 9 variables to hold the same information.

How to read information in objects

To get the content of the object student1

  • print("Student 1 Id ", student1.number)
  • print("Student 1 Name ", student1.name)
  • print("Student 1 Age ", student1.age)

In the same way you can read the information in the student2 and student3 objects.

https://how.okpedia.org/en/python/how-to-make-classes-in-python


Report an error or share a suggestion to enhance this page



FacebookTwitterLinkedinLinkedin