/    /  Python Classes

Python Classes

In this tutorial, we will learn python in detail.

 

Since Python is an object-oriented programming language all the code is written using a special construct called class. Developers use classes to make related things together. This can be done using the keyword ‘class’. A Class is an object constructor. It is blueprint for creating objects.

 

Create a class:

 

Use the keyword ‘class’ to create class.

 

Example:

 

class MyClass:
  x = 10

 

Create object:

 

class MyClass:
  x = 5

p1 = MyClass()
print(p1.x)

 

Output:

 

5

 

The _init_() function:

 

Every class has a function called _init_(), which will be executed when the class is being initiated.

Values can be assigned to object properties using _init_().

 

Example:

 

class Person:
def __init__(self, name, age):
   self.name = name
    self.age = age

p1 = Person("John", 36)

print(p1.name)
print(p1.age)

 

Output:

 

John
36