Tutorialslot.com

youtube facebook twitter gmail pinterest

Classes In Python


What is a class? Think of a class as a blueprint. It isn't something in itself, it simply describes how to make something. You can create lots of objects from that blueprint - known technically as an instance.
Since Python is Object oriented it manipulates programming by the construction of objects.
So how do you make these so-called 'classes'? very easily, with the class operator:

Defining a class
Classes can be defined by using the keyword "class" followed by class name and block of statements.
Syntax:
class class_name:
statements


Example:
class Emp:

count = 0

def __init__(self, name, salary):
self.name = name
self.salary = salary
Employee.count += 1

def Count(self):
print ("Total Employee %d" % Employee.Count)

def display(self):
print ("Name : ", self.name, ", Salary: ", self.salary)

#first object of class"
emp1 = Emp("Zara", 2000)
#second object of class"
emp2 = Emp("Manni", 5000)
emp1.display()
emp2.display()
print ("Total Number Of Employee %d" % Emp.Count)

Output:
Name : Zara ,Salary: 2000
Name : Manni ,Salary: 5000
Total Number Of Employee 2



Advertisement Powered By Google Adsense






IF YOU LIKE THIS TUTORIALS PLEASE SHARE

Share on whatsapp

POPULAR TAGS

python python tutorial learn python learning python python code learning java python language python documentation python tutor python basics python download python online python for dummies how to learn python