The Object Oriented Programming (OOP) is a technique in the programming which organises multiples lines of code into reusable objects to interact among themselves to give the best solution for problems. In this article, we will see some of the python OOPs examples and solutions.

python OPPs examples and solutions

1. Create a class and object

class student:
    marks = 89
    name = 'Sarah'
    
stu1 = student()
print(stu1.marks)
print(stu1.name)
89
Sarah

2. Create empty class

Now we will create an empty class with no data methods and attributes.

class student:
    pass

s1 = student()
print(s1)

s1.name='Sarah'
print(s1.name)
<__main__.student object at 0x000002280EC12D88>
Sarah

3. Create class using type keyword

Through type keyword you can create a class and then instantiate it.

s1 = type('student',(),{})()
print(s1)

s1.name = 'sarah'
print(s1.name )
<__main__.student object at 0x000002280EB4FF08>
sarah

4. Create and Call methods of a class

The methods present in class are defined as the function which belongs to class.

class student:
    marks = 98
    name = 'sarah'
    
    def avg(self):
        print(self.marks/100*100)

stu1 = student()
print(stu1.marks)
print(stu1.name)
stu1.avg
98
sarah
<bound method student.avg of <__main__.student object at 0x000002280EC1FE08>>

5. Using __init__() method to assign values

The classes in the python have a special method _init_() which will execute when an instance of the memory is created.

class student:
    def __init__(self, marks,name):
        self.marks = marks
        self.name = name
        
stu1 = student(89,'Sarah')
print(stu1.marks)
print(stu1.name)
89
Sarah

6. Update object properties

Let’s see how we can update the properties of an object in python.

class student:
    def __init__(self, marks,name):
        self.marks = marks
        self.name = name
        
stu1 = student(89,'Sarah')
print(stu1.marks)



stu1.marks = 90
print(stu1.marks)

89
90

7. Deleting the object and object properties

You can make use of del keyword to delete the properties of object and object itself.

class student:
    def __init__(self, marks,name):
        self.marks = marks
        self.name = name
        
stu1 = student(89,'Sarah')

del stu1.marks
del stu1

It will delete the mentioned records…

8. Iterate over object attributes

You can use ‘dir’ to get back all the values in that. You can even go through list comprehension to get more methods.

class A():
    m = 1
    n = 2
 
    def __int__(self, x=1, y=2, z=3):
        self.x = x
        self._y = y
        self.__z__ = z
 
    def xyz(self):
        print(x, y, z)
 
 
obj = A()
print(dir(obj))
print([a for a in dir(obj) if not a.startswith('__')])
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__int__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'm', 'n', 'xyz']
['m', 'n', 'xyz']

9. Create data attributes of a dynamic class

You can use the ‘setattr’ to set the attribute name of the given object in python.

class student:
    pass

stu1 = student()
setattr(stu1,'Marks',89)

stu2 = student()
setattr(stu2,'Age',23)

print(stu1.Marks)
print(stu2.Age)
89
23

10. Create static value variable

We are declaring a variable inside the class definition.

class student:
    age = 23
    
print(student.age)

s = student()
print(s.age)

s.age = 24
print(student.age)
print(s.age)
23
23
23
24

Wrapping Up

The python OOPS examples and solution is provided for the most popular questions. If you are familiar with classes and objects then you will get this easily.

There are tons of examples are there for OOPs in python. Object oriented is bit complex but solves majority of the real-world tech problems.

This article is all about python OOPs examples and solutions. Hope this is useful to you people. Happy learning

More read: OOPs in python

Categorized in:

Tagged in:

,