recursion iteration
used for fumctions loops
code size small large
write first 10 fibinacci series using recursion function
def fibo(n):
if n==0:
return 0
if n==1:
return 1
else:
return fibo(n-1)+fibo(n-2)
for i in range(10):
print(fibo(i))
important(files,classes)
unit3
oop
|--->groups related variables and functions into data sturctures called objects
>encapsulation
>polymorphism
>inheritance
class---it is a tempalate or blueprint from which objects are created
attributes----define the types of data the object can store
methods of a class----define the that object can perform,
it provide the way to modify the attributes of the object.
object----object is a instance of a class,under a class we can have any number of
class
Instantiation----process of creating an object from a class.
once an object is created for an class it has identity---unique address
state---data its store
behaviour---methods its contain
as you run the program bject state may change.
--init-- it is a builtin function ehich ia always executed when a class is
initiated this init is also called as constructor.
it is used to assign values to object properties or to perform any operation when
an object is created.
synatx for definining the class:
class python:(-->class name)
def --intit--(self,section,regno,rollno): #constructor
self.section=section
self.regno=regno
self.rollno=rollno
c1=python("A",112233,55) #c1--->object name
print(c1.section)
print(c1.regno)
print(c1.rollno)
class python:(-->class name)
def --intit--(self,section,regno,rollno): #constructor
ece.section=section
ece.regno=regno
ece.rollno=rollno
c1=python("A",112233,55) #c1--->object name
print(c1.section)
print(c1.regno)
print(c1.rollno)
self parameter is a reference to the current instance of the class and its used to
across the variable belongs to the class
the name 'self' can be replaced with any other name but has to be the first
parameter in any function in a class
syntax for making use of --str--()fn
when class object is represented as string
class python:
def __init__(self,section):
self.section=section
c1=python("A")
print(c1)
class python:
def __init__(self,section):
self.section=section
def __str__(self):
return f"{self.section}"
c1=python("A")
print(c1)
creating method in a class:
class python:
def __init__(self,section,rollno):
self.section=section
self.rollno=rollno
def first(self):
print("my class is "+ self.section)
A1=python("A",66)
A1.first()
create a class cant be empty. 'pass' is a keyword to create empty class
constructor that initializes 3 attributes
class product:
def __init__(self,name,price,discountpercent):
self.name=name
self.price=price
self.discountpercent=discountpercent
method uses 2 attributes to perform calculation
def getdiscountamount(self):
return self.price+self.discountpercent/100
method that calls another method
def getdiscountamount(self):
return self.price _self.discount amount()
):