Chapter-10
1. Exception try except finally try is try to solve proble error means go error based except
Finally is always work
a = 10
b=0
try:
c = a/d
except NameError:
d=2
c=a/d
print(c)
except ZeroDivisionError:
b=5
c=a/b
print(c)
finally:
print("success")
2. Customize exception
Class customizEx(Exception):// exception is base class customizeex is inherit
Pass
Eg:
class cusomEx(Exception):
pass
class ElementNotFound(cusomEx):
pass
class FirstElementIsSmallerThanSecond(cusomEx):
pass
l=[38,40,49]
try:
if l[0]<40:
raise FirstElementIsSmallerThanSecond
except ElementNotFound:
print("Element is not found")
except FirstElementIsSmallerThanSecond:
print("first element is smaller")
else:
print("element is found")
finally:
print("watched all the element")
chapter-11 lambda
1. Lambda is a important feature in python it useful for maps.filters,list_comprhention
Lamda is similar to the normal function but syntax look like
Lambda par or arugm = expression
x = lambda x: x/10
print(x(100))
2. Filter, map
friends = ['jaga','niyaz','danny','raga']
weekdays = list(filter(lambda x: x != 'jaga' and x!= 'danny',friends)) // filter to list only show
element
print(weekdays)
sat_sun = list(map(lambda x: x+'_raga_house',friends))
print(sat_sun)
chapter12
1. Oops in python to clean and reuasability for code
2. In class la property – variable, method-function
class motor:
def __init__(self):
pass
def buttonON(self):
print("on")
def speedCHANGE(self):
print("Speed is change")
engine = motor()
engine.buttonON()
3. Example using class object constructor
Attendance
class attendance():
def __init__(self,danny,niyaz):
self._danny = danny
self._niyaz = niyaz
def check_danny(self):
if self._danny == 0:
self._danny = 1
print("danny is present "+str(self._danny))
def check_niyaz(self):
if self._niyaz == 0:
self._niyaz = 1
elif self._niyaz == 2:
self._niyaz = 0
print("niyaz is present "+str(self._niyaz))
at = attendance(0,2)
print(at._danny)
at.check_danny()
print(at._niyaz)
at.check_niyaz()
inhertiance
derived class access the property of base class
class lecturer:
def __init__(self):
pass
def class_room(self):
print("classroom")
def principal_room(self):
print("principal")
class supervisor(lecturer):
def supervisor_cabin():
print("supervisor_cabin")
super = supervisor()
super.principal_room()
SUPER()key is used during inhertiance derived class hve extra properity menans we use init +
super key to add value not rewrite
def __init__(self,pro3,*args,**kwargs):
self._pro3 = pro3
super().__init__(*args,**kwargs)
animal & brred good example for super key using
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return f"My name is {self.name}."
class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name)
self.breed = breed
def speak(self):
return super().speak() + f" I am a {self.breed}."
dog = Dog("Fido", "Labrador")
print(dog.speak())
Example
class staff:
def __init__(self,pro1,pro2):
self._pro1 = pro1
self._pro2 = pro2
class principle(staff):
def __init__(self,pro3,*args,**kwargs):
self._pro3 = pro3
super().__init__(*args,**kwargs)
p = principle("dismiss",'class','work')
print(p._pro1)
print(p._pro2)
print(p._pro3)
example cricketer
class sachin:
def __init__(self,bat):
self._bat = bat
class arjun(sachin):
def __init__(self,bowl,field,*args,**kwargs):
self._bowl = bowl
self._field = field
super().__init__(*args,**kwargs)
a = arjun("bowling-145","field-covers","right-arm batting")
print("arjun is ",a._bowl,a._field,a._bat)
example:
class gun_shoter:
def __init__(self,name,work):
self._name=name
self._work=work
class polic_man(gun_shoter):
def __init__(self,desig,*args,**kwargs):
self._desig=desig
super().__init__(*args,**kwargs)
police = polic_man("arrest","danny","shot")
print(police._name,police._work,police._desig)
chapter-13 decorators
decorators is wrap the function in inside the function
def mark(a,b):
print(a+b)
def check(funct):
def inner(a,b):
if a == -1:
a=0
return funct(a,b)
return inner
data = check(mark)
data(-1,2)
eassy way using @wrapper name to pass function
def check(funct):
def inner(a,b):
if a == -1:
a=0
return funct(a,b)
return inner
@check// decorator
def mark(a,b):
print(a-b)
mark(-1,2)
example for decoration- put a pass mark
#wrap function
def pass_mark(marks):
def para(a,b):
c=40-(a+b)
d=c/2
a,b=a+d,b+d
return marks(a,b)
return para
@pass_mark
def mark(a,b):
print("your total is: "+str(a+b))
if a+b >= 40:
print("your are pass")
else:
print("fail")
mark(16,19)
example for decoraters -// show data element and postion and total
#wrapper
def data_show(data_func):
def n_cont_par(*args,**kwargs):
total =0
for i,item in enumerate(args):
print("your "+str(i)+" item is "+str(item))
total = data_func
print("your total is: "+str(total(*args,**kwargs)))
return n_cont_par
@data_show
def data(a,b):
return a+b
data(10,10)
CHAPTER-14 READ WRITE ZIP FILE
with open("readme.txt","r") as f:
read = f.read(8)
f.seek(0)
readline = f.readline()
print(read)
print(readline)
read , write ,append
lists =['line1','line2','line3']
with open('readme.txt','w') as f: //write
f.writelines('\n'.join(lists))
lists =['\n','new line1','new line2','new line3']
with open('readme.txt','a') as f: //append
f.writelines('\n'.join(lists))
readinng with split
using split(‘/n} or plit(‘,’)
to remove
with open('readme.txt','r') as file:
data = file.read()
co = data.split('\n')
for c in co:
x,y=c.split(',')
print(x)
print(y)
we extract zip file
from zipfile import ZipFile
with ZipFile("readme.zip",'r') as zip:
zip.extractall()
print("successfully extract")
unzip
"""#zip
from zipfile import ZipFile
with ZipFile("readme.zip",'w') as z:
z.write('readme.txt')