Advanced Python Notes
Advanced Python Notes
-------------------------------
PROGRAMMING APPROACHES ARE TWO TYPES.
}
M2()
{
}
MAIN()
{
M1()
M2()
}
EX: C
OOPS PRINCIPLES
1) ENCAPSULATION
2) ABSTRACTION
3) INHERITENCE
4) POLYMORPHISM
class <classname>:
states
behaviours
what is state?
state represents some value
state can be variable or constant
what is variable
a state which represent some value but value can be chaged is called as variable
what is constant
what is state? Ans:- state represnts some value.. state can be variable or
constant..
what is variable? ANs: a state which represnt some value but value can be changed
is called as variable..
what is constant? state which represnt some value but value cant be changed is
called as constant..
types of variables
------------------
in python we have 3 types of variables
1) local varibale
2) instance variable
3) static varibale
1)A varibale which is declared within the function or method is called local
variables.
local varibales we can access only within that function.
2) Instance variable
MyClass.a=10 when
note:
when we r declaring static variable with in class prefixing class name is
optional..but when r declaring static variable inside the static method prefixing
class name is compulosry..
what is Behvaiour?
Ans: .
->in python we can define 2 types of behaviours.
1)method or function or procedure.. 2)cosntructor..
what is method?
Ans: A behvaiour which represent some functionality, will execute when ever
programmer will call is called as method.
Types of methods
in python we will have 3 types of methods 1)instances method 2)static method
3)class method
def<method name(self)>:
<logic>
Types of construcotrs..
-----------------------------
constructors are two types..
1)default constructor or parameter less construcotr.
2)parameterized construcotor
----------------------------------------------
1)what is default constructor or parameter less construcotr.?
------------------------------------
Ans:
while defining a constructor..if we didn't declared any parameter except self
keyword.. which is called as default constructor..
----------------------------------------
<syntax to define default constructor>
---------------------------------------
def __init__(self):
<self.instance variablename>=<value>
task1
------------
define student class
states are:
----------------
stid
stname
stloc
behvaiours are:
-----------------
->parameterized constructor
->DisplayStudentInfo()
----------------------------
task2:
-------------------
define faculty class
states are:
------------------
fid
fname
fphno
behvaiours are:
------------------
->parameterized construcotr
->DisplayFacultyInfo()
Q: one dept can contain more than one employee. But one employee may not belong to
more than one department..
class Department:
def __init__(self,dno,dname):
self.deptno=dno
self.deptname=dname
def DisplayDeptInfo(self):
print("Department Number is:",self.deptno)
print("Department name is:",self.deptname)
class Employee(Department):
def __init__(self,eno,ename,esal):
super().__init__(111,'Technical')
self.empno=eno
self.empname=ename
self.empsal=esal
def DisplayEmpInfo(self):
super().DisplayDeptInfo()
print("Employee No is:",self.empno)
print("Employee Name is:",self.empname)
print("Employee Salary is:",self.empsal)
obj=Employee(111,'rama',1000)
obj.DisplayEmpInfo()
class Department:
def __init__(self,dno,dname):
self.deptno=dno
self.deptname=dname
def DisplayDeptInfo(self):
print("Department Number is:",self.deptno)
print("Department name is:",self.deptname)
class Employee(Department):
def __init__(self,eno,ename,esal):
sup
class Department:
def __init__(self,dno,dname):
self.deptno=dno
self.deptname=dname
def DisplayDeptInfo(self):
print("Department Number is:",self.deptno)
print("Department name is:",self.deptname)
obj=Employee(111,'rama',1000)
obj.DisplayEmpInfo()
8:24 PM: hw
task1
--------------
parrent class
Course
CHild class
student
task2:
---------------
parrent class
course
child class
faculty
task3:
------------------
parrent class
Shop
child class
customer
what is self?
Ans:
self is a keyword which represnt current
class object
super is a keyword.which rerepesnt super
class object
super() is using for
parrent class construcotr or instance method..
class BC:
def __init__(self,x,y):
self.a=x
self.b=y
def DisplayBcInfo(self):
print("a value is:",self.a)
print("b value is:",self.b)
class DC(BC):
def __init__(self,m,n):
super().__init__(10,20)
self.i=m
self.j=n
def DisplayDcInfo(self):
super().DisplayBcInfo()
print("i value is:",self.i)
print("j value is:",self.j)
class TC(DC):
def __init__(self,q,r):
super().__init__(100,200)
self.o=q
self.p=r
def DisplayTcInfo(self):
super().DisplayDcInfo()
print("o value is:",self.o)
print("p value is:",self.p)
obj=TC(1000,2000)
obj.DisplayTcInfo()
===================================
class Department:
def __init__(self,dno,dname):
self.dno=dno
self.dname=dname
def DisplayDeptInfo(self):
print("department no is:",self.dno)
print("department name is:",self.dname)
class Branch(Department):
def __init__(self,bid,bname):
super().__init__(10,'HR')
self.branchid=bid
self.branchname=bname
def DisplayBranchInfo(self):
super().DisplayDeptInfo()
print("branch id is:",self.branchid)
print("branch name is:",self.branchname)
class Employee(Branch):
def __init__(self,eno,ename,esal):
super().__init__(123,'Hyd')
self.empno=eno
self.empname=ename
self.empsal=esal
def DisplayEmpInfo(self):
super().DisplayBranchInfo()
print("Employee No is:",self.empno)
print("Employee Name is:",self.empname)
print("Employee salary is:",self.empsal)
obj=Employee(111,'rama',1000)
obj.DisplayEmpInfo()
task1
University class
College class
Student clas
impliment multi level inheritence
task2
------------------------------------
University class
College class
Faculty clas
impliment multi level inheritence
7:25 PM:
5)hierarchial inheritence
-------------------------------
inheriting one base class into multiple
derived classes is called as
hierarchial inheritence
example
--------------------------
class BC:
logic
class DC1(BC):
LOGIC\
class DC2(BC):
LOGIC
class DC3(BC):
LOGIC
Audience Question
Q: yes sir
Audience Question
A: what is Poymorphism?
------------------------
Ans:
polymorphism means one name many forms..
->implimenting multiple functionalities
with the same name is called as
polymorphsim..
that means implimneting multiple methods.\
with same name and different behvaiour
is called as polymorphsim..
what is Poymorphism?
------------------------
Ans:
polymorphism means one name many forms..
->implimenting multiple functionalities
with the same name is called as
polymorphsim..
that means implimneting multiple methods.\
with same name and different behvaiour
is called as polymorphsim..
Q: yes sir
Audience Question
Q: yes sir
A: what is overloading?
----------------------------
Ans:
implimenting multiple methods..with the
same name but diffrent signature
(number of argument) is called as
overloading..
types of overloading
---------------------------------
in python we can impliment three types of
overloading
1)method overloading
2)operator overloading
3)cosntructor overloading..
-----------------end---------------------------
Audience Question
Q: submitted sir
A: 1what is Poymorphism?
------------------------
2write the Tyepes of polymorphsim?
------------------------------------
3what is overloading?
-------------------
4write the types of overloading
------------------------
5)what is overriding..
Audience Question
Q: yes sir
A: 2)what is overriding..?
Ans:
impliment one method in super class and another
method in sub class with same name and same
\signature is called as overriding..
A: class Calculate:
def Add(self,a=None,b=None,c=None):
if a!=None and b!=None and c!=None:
print("sum of three numbers are:",a+b+c)
elif a!=None and b!=None:
print("sum of 2 numbers are:",a+b)
else:
print("Enter 2 or 3 numbers..")
obj=Calculate()
obj.Add(10,20)
obj.Add(10,20,30)
Audience Question
A: 1.what is Poymorphism?
------------------------
polymorphism means one name many forms..
->implimenting multiple functionalities
with the same name is called as
polymorphsim..
that means implimneting multiple methods.\
with same name and different behvaiour
is called as polymorphsim..
Tyepes of polymorphsim
---------------------------
in python polymorphsim are two types..
1)overloading
2)overriding
what is overloading?
----------------------------
Ans:
implimenting multiple methods..with the
same name but diffrent signature
(number of argument) is called as
overloading..
types of overloading
---------------------------------
in python we can impliment three types of
overloading
-------------------------------------------
1)what is
method overloading?
Ans:-
if 2 methods are having same name with
different arguments list.. then these methods
are called as overloaded methods..
----------------------------------------------------
Example 1 for method overloading..
------------------------------------------
Audience Question
Q: 2 sie
A: class Calculate:
def Add(self,*a):
sum=0
for i in a:
sum=sum+i
print("sum is:",sum)
obj=Calculate()
obj.Add(10,20)
obj.Add(10,20,30)
obj.Add(10,20,30,40)
============================================
Audience Question
Q: clearly understand
A: class Calculate:
def __init__(self,*a):
sum=0
for i in a:
sum=sum+i
print("sum is:",sum)
obj1=Calculate(10,20)
obj2=Calculate(10,20,30)
obj3=Calculate(10,20,30,40)
==================================================================
Audience Question
Q: 1 constructor
Audience Question
Q: we took sir
A: Exception Handling
----------------------------
what is an excpetion?
Ans:
an excpetion is a run time error..
------------------------------------------
what is an exception handling?
Ans:
Exception handling is a predeifned mechanism
to handle run time errors..
by using try,except,finally blocks..
----------------------------end-----------
pythin is providing various predefined
execeotion classes..to handle
various runtime errors..
for example
------------------
for every error it will provide one
predefined excpetion class
for example
to handle divide by zero error
class name is
ZeroDivisionError
to handle arthametic error
class name is
ArthemericError
like that..
---------------end--------------------------
<syntax of try,except,finally block>
--------------------------------------
Audience Question
Q: we took sir
A: Exception Handling
----------------------------
what is an excpetion?
Ans:
an excpetion is a run time error..
------------------------------------------
what is an exception handling?
Ans:
Exception handling is a predeifned mechanism
to handle run time errors..
by using try,except,finally blocks..
----------------------------end-----------
pythin is providing various predefined
execeotion classes..to handle
various runtime errors..
for example
------------------
for every error it will provide one
predefined excpetion class
for example
to handle divide by zero error
class name is
ZeroDivisionError
to handle arthametic error
class name is
ArthemericError
like that..
---------------end--------------------------
<syntax of try,except,finally block>
--------------------------------------