1st Unit Notes
1st Unit Notes
a class is a user-defined blueprint of prototype from which objects are created. It allows
you to define a custom data structure by combining attributes[variables] and methods[functions]
into a single entity.
Syntax:
class classname:
2. what is object?
Syntax:
obj = classname()
print(obj.atrr)
3. Define self_method :
* Every instance method in Python needs to have self as its first parameter.
* It is used to initialize the attributes of the class with value paused to it.
5. What is Inheritance ?
Inheritance is a fundamental concept in object-orianted programming that allows to
create a hiearcy of classes that shares a set of properties and method by deriving a class from
another class.
Syntax:
class parentclass:
// code block
// code block
6. What is Polymorphism ?
* In Python, polymorphism refers to the ability to use a single interface or function name
to represent different types of objects or data.
7. What is Encapsulation ?
* This puts restrictions on accessing variables and methods and exposing only what is
necessary.
8. What is Assertion ?
* assertion is the boolean expression that checks if the statement is True or False.
* If the statement is true then it does nothing and continues the execution, but if the
statement is False then it stops the execution of the program and throws an error.
9. What is Generators ?
* generator is a function or expression that will process a given iterable one object at a
time, on demand.
* A generator will return a generator object, which can be cast as a list or some other
data structure as appropriate.
* Iterators are objects that allow you to traverse through all the elements of a collection
and return one element at a time.
5 Marks
MEMBER TYPES:
Most programming languages has three forms of access modifiers which are
function of a class.
The members of a class that are declared public are easily accessible fromany part of the program
Class Student:
self.stuName = name
self.stuAge = age
OUTPUT:
Name: SMITH
Age: 20
· The members of a class that are declared protected are only accessible to a
single underscore ‘_’ symbol before the data member of that class.
Example for protected access Modifier
class Student:
_name = None
_roll = None
_branch = None
# constructor
self._name = name
self._roll = roll
self._branch = branch
def _displayRollAndBranch(self):
# derived class
class College(Student):
# constructor
def displayDetails(self):
obj.displayDetails()
OUTPUT:
Name: SMITH
Roll: 17066
· The members of a class that are declared private are accessible within the
class only, private access modifier is the most secure access modifier.
class Student:
# private members
__name = None
__roll = None
__branch = None
# constructor
self.__name = name
self.__roll = roll
self.__branch = branch
def accessPrivateFunction(self):
self.__displayDetails()
# creating object
obj.accessPrivateFunction()
OUTPUT:
Name: SMITH
Roll: 17066
· Decorators are a very powerful and useful tool in Python since it allows
Syntax:
@gfg_decorator
def hello_decorator():
print("Gfg")
def hello_decorator():
print("Gfg")
hello_decorator = gfg_decorator(hello_decorator)'''
Example:
import time
import math
def calculate_time(func)
begin = time.time()
func(*args, **kwargs)
end = time.time()
return inner1
@calculate_time
def factorial(num):
# sleep 2 seconds because it takes very less time
time.sleep(2)
print(math.factorial(num))
factorial(10)
Output:
3628800
10 Marks
Method Overloading:
Two or more methods have the same name but different numbers of parameters or different types of
parameters, or both. These methods are called overloaded methods and this is called method overloading.
result = first*second
print(result)
print(result)
# you can also pass data type of any value as per requirement
Output:
12
17.985999999999997
Method Overriding:
Method overriding is an ability of any object-oriented programming language that allows a subclass or
child class to provide a specific implementation of a method that is already provided by one of its super-
classes or parent classes. When a method in a subclass has the same name, the same parameters or
signature, and same return type(or sub-type) as a method in its super-class, then the method in the
subclass is said to override the method in the super-class.
Example:
class Parent():
# Constructor
def _init_(self):
def show(self):
print(self.value)
def show(self):
print(self.value)
obj2 = Child()
obj1.show()
obj2.show()
Output:
Inside Parent
Inside Child
Parent class methods can also be called within the overridden methods. This
Example:
Python program to demonstrate calling the parent's class method inside the overridden method
class Parent():
def show(self):
print("Inside Parent")
class Child(Parent):
def show(self):
# method
Parent.show(self)
print("Inside Child")
# Driver's code
obj = Child()
obj.show()
Output:
Inside Parent
Inside Child
2.Using Super():
Python super() function provides us the facility to refer to the parent class
returns the proxy object that allows us to refer parent class by „super‟.
Python program to demonstrate calling the parent's class method inside the overridden method
using super()
class Parent():
def show(self):
print("Inside Parent")
class Child(Parent):
def show(self):
# method
super().show()
print("Inside Child")
# Driver's code
obj = Child()
obj.show()
Output:
Inside Parent
Inside Child
Syntax :
Class BaseClass:
{Body}
Class DerivedClass(BaseClass):
{Body}
· single inhertance
· multilevel inheritance
· multiple inheritance
· hierarchical inheritance
Single Inheritance:
When a child class inherits from only one parent class, it is called single inheritance. We saw an example
above.
Example:
class parent:
def display(self):
print("parent")
class child(parent):
def show(self):
print("child")
c1=child()
c1.show()
c1.display()
OUTPUT:
child
Parent
Multiple inheritances:
When a child class inherits from multiple parent classes, it is called multiple inheritances.
Example:
class Father:
def fdisplay(self):
print("Father")
class Mother:
def mdisplay(self):
print("Mother")
class child(Father,Mother):
def cdisplay(self):
print("child")
c1=child()
c1.cdisplay()
c1.mdisplay()
c1.fdisplay()
Output:
child
Mother
Father
Multilevel inheritance:
When we have a child and grandchild relationship. This means that a child class will inherit from its
parent class, which in turn is inheriting from its parent class.
Example:
class grandparent:
def display(self):
class parent(grandparent):
def show(self):
print("parent class")
class child(parent):
def show(self):
print("child")
c1=child()
c1.display()
c1.show()
c1.display()
OUTPUT:
child
Hierarchical inheritance:
More than one derived class can be created from a single base.
Example:
class parent:
def func1(self):
class Domestic(Animals):
def func2(self):
class Wild(Animals):
def func3(self):
print(“IT IS A WILD ANIMAL”)
Obj1=Domestic()
Obj2=Wild()
Obj1.func1()
Obj1.func2()
Obj2.func1()
Obj2.func3()
Output:
IT IS A DOMESTIC ANIMAL.
IT IS A WILD ANIMAL.
3.Explain polmorphism?
polymorphism means the same function name (but different signatures) being
used for different types. The key difference is the data types and number of
print(len("geeks"))
Output
# Driver code
print(add(2, 3))
print(add(2, 3, 4))
Output