2. 1) Python is an object-oriented programming language.
2) Means we can solve any problem by creating objects in our programs.
3) Python is a multi-paradigm programming language. Which means it
supports different programming approach.
OOPS Terminologies:
Object
An object is an entity that has attributes and behavior.
for e.g. Riya is an object who has attributes such as height, weight, etc. and has certain behaviors
such as walking, talking, eating etc.
Class
A class is a blueprint for the objects.
For e.g. Riya is object so we can define a class Human for her.
Methods
Object has attributes and behaviors. These behaviors are called methods. Methods are defined
inside the body of a class
3. Inheritance
Inheritance is a way of creating new class for using details of existing class without modifying it.
The newly formed class is a derived class similarly, the existing class is a base class.
types of Inheritance:
a) Single Inheritance
b) Multiple Inheritance
c) Multilevel Inheritance
d) Hierarchical Inheritance
e) Hybrid Inheritance
A) Single Inheritance:
A single Python inheritance is when a single class inherits from a class.
Syntax:
class A:
#properties of class A
class B(A):
#properties of class B
# class B inheriting the properties of class A
4. B) Multiple Inheritance:
means that you're inheriting the property of multiple classes into one.
Syntax:
class A:
# variable of class A
# functions of class A
class B:
# variable of class A
# functions of class A
class C(A, B):
# class C inheriting property of both class A and B
5. C) Multilevel Inheritance:
In multilevel inheritance, we inherit the classes at multiple separate levels.
Syntax:
class A:
# properties of class A
class B(A):
# class B inheriting property of class A
# more properties of class B
class C(B):
# class C inheriting property of class B
# thus, class C also inherits properties of class A
# more properties of class C
6. D) Hierarchical Inheritance:
When more than one class inherits from a class, it is hierarchical inheritance.
Syntax:
class A:
#properties of class A
class B(A):
#class B inheriting property of class A
# properties of class B
class C(A):
# class C inheriting property of class B
# thus, class C also inherits properties of class A
# properties of class C
7. E) Hybrid Inheritance :
Hybrid Python inheritance is a combination of any two kinds of inheritance.
Syntax:
class A:
# properties of class A
class B(A):
#class b inheriting the properties of class A
class C(A):
#Class C inheriting the properties of class A
class D(B,C):
# class D inheriting the properties of both class B and C
obj=D()
8. Encapsulation:
We can restrict access to methods and variables. This prevent data from direct
modification which is called encapsulation.
we denote private attribute using underscore as prefix i.e. single “ _ “ or double “ __“.
Polymorphism
Polymorphism is an ability to use common interface for multiple form.
For e.g. We need to color a shape, there are multiple shape like (rectangle, square, circle). So we
could use same method to color any shape. This concept is called Polymorphism.
9. Creation of Class and Objects :
A class is defined using the keyword class.
class Test:
def fun(self,x,y):
print(x+y)
obj = Test()
obj.fun(10,20)
10. The self
o Class’s methods must have an extra first parameter . We do not give a value for that
parameter when we call the method.
o When we call a method of this object as myobject.method(arg1, arg2), this is
automatically converted by Python into MyClass.method(myobject, arg1, arg2) – this is
all the special self is about.
11. Super Function:
super() allows us to call a method from the parent class.
class First:
def start(self):
print("Start")
def stop(self):
print("Stop")
class Second(First):
def execute(self):
super().start()
print("execute")
super().stop()
obj=Second()
obj.execute()
12. Constructors
A constructor is a special type of method (function) which is used to initialize the instance
members of the class. Constructor can be parameterized and non-parameterized as well.
A constructor is a class function that begins with double underscore (_). The name of the
constructor is always the same __init__().
When we create a class without a constructor, Python automatically creates a default constructor.
13. Non Parameterized Constructor
class Abc:
def __init__(self):
print("non parametrized constructor")
def show(self,name):
print("Hello",name)
bb = Abc()
bb.show(“welly")
Parameterized Constructor
class Student:
def __init__(self, name):
print("This is parametrized constructor")
self.n = name
def show(self):
print("Hello",self.n)
student = Student(“welly")
student.show()
14. Destructors
Destructors are called when an object gets destroyed.
The __del__() method is a known as a destructor method in Python.
class Employee:
def __init__(self):
print('Employee created.')
def __del__(self):
print('Destructor called, Employee deleted.')
obj = Employee()
del obj
15. File Handling
a) The key function for working with files is open() function.
b) open() function takes two parameters; filename, and mode.
There are four different modes for opening a file:
"r" - Read - Opens a file for reading, error occurs if file does not exist.
"w" - Write - Opens a file for writing, creates the file if it does not exist.
"x" - Create - Creates the specified file, returns an error if the file exists
"a" - Append - Opens a file for appending, creates the file if it does not exist.
‘r+’- Open a file for updating (reading and writing)