Python Lect1
Python Lect1
temp = 40
if(temp < 10):
print(‘Cold weather’)
elif(temp < 25):
print(‘Pleasant weather’)
else:
print(‘Hot weather’)
Execution Cycle of Python Program
Compilation
The program is converted into byte code. Byte code is a
fixed set of instructions that represent arithmetic,
comparison, memory operations, etc. It can run on any
operating system and hardware. The byte code
instructions are created in the .pyc file. The .pyc file is
not explicitly created as Python handles it internally but
it can be viewed with the following command:
class MyClass:
x = 5
p1 = Person("John", 36)
print(p1.name)
print(p1.age)
Python OOPs
Inheritance:
Python Inheritance
Inheritance allows us to define a class that inherits all the
methods and properties from another class.
Parent class is the class being inherited from, also called base
class.
Child class is the class that inherits from another class, also
called derived class.
Inheritance
class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
x = Person("John", "Doe")
x.printname()
Inheritance
Child class:
class Student(Person):
def __init__(self, fname, lname):
Person.__init__(self, fname, lname)
Or
class Student(Person):
def __init__(self, fname, lname, year):
super().__init__(fname, lname)
self.graduationyear = year
class Class1:
def m(self):
print("In Class1")
class Class2(Class1):
def m(self):
print("In Class2")
super().m()
class Class3(Class1):
def m(self):
print("In Class3")
super().m()
Multiple Inheritance
class Class4(Class2, Class3):
def m(self):
print("In Class4")
super().m()
obj = Class4()
obj.m()
Output:
In Class4
In Class2
In Class3
In Class1
Access modifiers
class Super:
# constructor
def __init__(self, var1, var2, var3):
self.var1 = var1
self._var2 = var2
self.__var3 = var3
Friend function
There is no option of friend function in python. you have an option to define a
protected variable by using a single underscore.
Run time polymorphism
Runtime polymorphism is nothing but method overriding. In Python, method overriding occurs
when a derived class provides its own implementation of a method that is already defined in its base
class.
class Animal:
def makeNoise(self):
raise NotImplementedError
class Cat(Animal):
def makeNoise(self):
print("Meoooowwwww")
class Dog(Animal):
def makeNoise(self):
print("Woooooof")
Run time polymorphism
a = Cat();
a.makeNoise() #Prints Meeeowwwwww
a = Dog();
a.makeNoise() #Prints WoooooofPythonCopy
Output:
Meoooowwwww
Woooooof
Function polymorphism
x = "Hello World!"
print(len(x))
print(len(mytuple))
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(len(thisdict))
Class polymorphism
class Car:
def __init__(self, brand, model):
self.brand = brand
self.model = model
def move(self):
print("Drive!")
class Boat:
def __init__(self, brand, model):
self.brand = brand
self.model = model
def move(self):
print("Sail!")
class Plane:
def __init__(self, brand, model):
self.brand = brand
self.model = model
def move(self):
print("Fly!")
def move(self):
print("Move!")
class Car(Vehicle):
pass
class Boat(Vehicle):
def move(self):
print("Sail!")
class Plane(Vehicle):
def move(self):
print("Fly!")