B.Tech Students: Apply for Live Programming Internship C, C++, Java, Python ,Web page Designing, PHP Python Classes and Objects | LiveCodeProgramming

Python Classes and Objects

In Python, a class is like a blueprint for creating objects. Each object is an instance of the class with its own data and functions.

Python में, class एक blueprint की तरह है जिससे आप object बना सकते हैं। हर object की अपनी अलग जानकारी और functions हो सकते हैं।

Example 1: Basic Object
class Employee:
    def __init__(self, name, age=0, salary=0):
        self.name = name
        self.age = age
        self.salary = salary

e1 = Employee('John', 25, 50000)

Output:

John 25 50000
Example 2: Multiple Objects
class Employee:
    def __init__(self, name, age=0, salary=0):
        self.name = name
        self.age = age
        self.salary = salary

e2 = Employee('Naincy', 30, 60000)
e3 = Employee('Aryan', 22, 45000)

Output:

Naincy 30 60000
Aryan 22 45000
Example 3: Access Attribute
class Employee:
    def __init__(self, name, age=0, salary=0):
        self.name = name
        self.age = age
        self.salary = salary

print(e1.name)

Output:

John
Example 4: Modify Attribute
class Employee:
    def __init__(self, name, age=0, salary=0):
        self.name = name
        self.age = age
        self.salary = salary

e1.salary += 5000
print(e1.salary)

Output:

55000
Example 5: Add Method
class Employee:
    def __init__(self, name, age=0, salary=0):
        self.name = name
        self.age = age
        self.salary = salary

class Employee:
    def __init__(self, name, age, salary):
        self.name = name
        self.age = age
        self.salary = salary
    def show(self):
        return f'{self.name} earns {self.salary}'
e4 = Employee('Rohit', 35, 70000)
print(e4.show())

Output:

Rohit earns 70000
Example 6: Class Variable
class Employee:
    def __init__(self, name, age=0, salary=0):
        self.name = name
        self.age = age
        self.salary = salary

class Employee:
    company = 'LiveCode'
    def __init__(self, name):
        self.name = name
e5 = Employee('Meena')
print(e5.company)

Output:

LiveCode
Example 7: isinstance Check
class Employee:
    def __init__(self, name, age=0, salary=0):
        self.name = name
        self.age = age
        self.salary = salary

e6 = Employee('Dev', 26, 55000)
print(isinstance(e6, Employee))

Output:

True
Example 8: Using __str__
class Employee:
    def __init__(self, name, age=0, salary=0):
        self.name = name
        self.age = age
        self.salary = salary

class Employee:
    def __init__(self, name):
        self.name = name
    def __str__(self):
        return f'Employee Name: {self.name}'
e7 = Employee('Kajal')
print(e7)

Output:

Employee Name: Kajal
Example 9: Delete Attribute
class Employee:
    def __init__(self, name, age=0, salary=0):
        self.name = name
        self.age = age
        self.salary = salary

e8 = Employee('Sumit', 32, 61000)
del e8.salary
print(hasattr(e8, 'salary'))

Output:

False
Example 10: Employee List
class Employee:
    def __init__(self, name, age=0, salary=0):
        self.name = name
        self.age = age
        self.salary = salary

team = [Employee('Shrutika', 24, 47000), Employee('Abhi', 29, 52000)]
for emp in team:
    print(emp.name)

Output:

Shrutika
Abhi