Python Note 2
Python Note 2
##chr()
example:chr(65)
output:A
##file.read()
example:a = open("pattern2.py","r")
print(a.read(5))
a.close()
Output:# inv
##.read()
Example:a = open(r"C:\Users\SRI RAJAN\Documents\demo.txt")
print(a.read())
a.close()
Output:hello world
we love python
we love programming
&&overwrite
Example:a = open(r"C:\Users\SRI RAJAN\Documents\demo.txt","w")
a.close()
a = open(r"C:\Users\SRI RAJAN\Documents\demo.txt",)
print(a.read())
a.close()
Output:>>>
##.readline()
Example:a = open(r"C:\Users\SRI RAJAN\Documents\demo.txt","r")
print(a.readline())
Output:hello world
##.readlines()
Example:a = open(r"C:\Users\SRI RAJAN\Documents\demo.txt","r")
print(a.readlines())
Output:['hello world\n', 'i am sri rajan\n', 'learning python\n', 'thank you']
##loop in readline
Example:a = open(r"C:\Users\SRI RAJAN\Documents\demo.txt","r")
for line in a:
print(a.readlines())
a.close
Output:['hello world\n', 'i am sri rajan\n', 'learning python\n', 'thank you']
##.write
Example:a = open(r"C:\Users\SRI RAJAN\Documents\demo.txt",'w')
a.write('no man')
a.close
Output:no man (in file)
##append
Example:a = open(r"C:\Users\SRI RAJAN\Documents\demo.txt",'a')
a.write('no man')
a.close
Output:no manno man
## remove of file
Example:import os
os.remove(r"C:\Users\SRI RAJAN\Documents\demo2.txt")
Output: (file will be deleted)
## inner function
Example:def fun():
print('father function')
def fun2():
print('first child')
def fun3():
print('second child')
fun3()
fun2()
fun()
Output:father function
second child
first child
Example2:def fun(n):
def fun1():
return 'firt child'
def fun2():
return 'second child'
if n==1:
return fun1()
else:
return fun2()
a=fun(1)
b=fun(2)
print(a)
print(b)
Output:firt child
second child
DECORATORS IN PYTHON
## wrapper function
Example:def fun1(n):
def some():
print('hello')
n()
print('welcome')
return some
def fun2():
print('man')
ans=fun1(fun2)
ans()
Output:hello
man
welcome
## wrapper
Example:def fun1(n):
def some(*a,**b):
print('hello')
return some
@fun1
def fun2(name):
print(f'{name}')
fun2('sri')
Output:hello
fancy decorators
## class decorators
Example:
class Square:
def __init__(self,side):
self._side =side
@property
def side(self):
return self._side
@property
def area(self):
return self._side**2
s= Square(4)
print(s.side)
print(s.area)
Output:
4
16
Lambda Function
## lambda
Example:
x=lambda a:a*a
print(x(3))
Output:
9
Example2:
def a(x):
return(lambda y:x+y)
t = a(10)
print(t(4))
b = a(22)
print(b(3))
Output:
14
25
Example2:
def new(s,t):
return s*t
x=map(new,[1,2,3],[4,5,6])
print(x)
print(list(x))
Output:
<map object at 0x000002CF63DB0D90>
[4, 10, 18]
Generator function
Example:
def new(dict):
for x,y in dict.items():
yield x,y
a={1:"hi",2:"welcome"}
b=new(a)
print(b)
next(b)
Output:
<generator object new at 0x000001DAE1258BA0>
(1, 'hi')
(2, 'welcome')
Example2:
def new(a):
while a<=5:
a+=1
yield a
b=new(2)
print(next(b))
print(next(b))
print(next(b))
print(next(b))
print(next(b))
Output:
3
4
5
6
error: print(next(b))
StopIteration
Example2:
def new():
n=3
yield n
n=n*n
yield n
c=new()
print(next(c))
print(next(c))
Output:
3
9
Generator expression
Example:
a=range(1,6)
print("list",end=':')
b=[i+2 for i in a]
print(b)
c=(i+2 for i in a)
print(c)
print(next(c))
for j in c:
print(j)
g=range(1,6)
print("list",end=':')
d=(i+2 for i in g)
print(max(d)) #u also use min but after using one it became empty sequence
Output:
list:[3, 4, 5, 6, 7]
<generator object <genexpr> at 0x000001D7F53A8BA0>
3
4
5
6
7
list:7
stream of number
Example:
a=range(100)
b=(x for x in a)
print(b)
for y in b:
print(y,end=" ")
#to create even
c=range(2,100,2)
d=(x for x in c)
print(d)
for y in d:
print(y,end=" ")
output:
<generator object <genexpr> at 0x00000203D5858BA0>
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
<generator object <genexpr> at 0x00000203D58C3900>
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38
40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76
78 80 82 84 86 88 90 92 94 96 98
1..inheritance 6:22:50
Example:
see in python saved ..inheritance in python &2
*types of inheritance
1.single -involves one child class and parent class
2.multiple -involves more than one parent
3.multilevel -child class act as a parent class for another child class
4.hierarchical -more than one type of inheritance
Example:
1,2,3,4 ... see in python saved inheritence
*super function -calls a parent method directly
*method overriding - used to change functionality of parent function
Example: see in python saved inheritence
2.. Encapsulation 6:29:00
Example:
Exception handling
## self Exception making
Example:
#self exception making
a=10
if a>5:
raise Exception("a not be more than 5,value of a is {} ".format(a))
Output:
Traceback (most recent call last):
File "C:\Users\SRI RAJAN\AppData\Local\Programs\Python\Python39\
demo----------.py", line 4, in <module>
raise Exception("a not be more than 5,value of a is {} ".format(a))
Exception: a not be more than 5,value of a is 10
Example:
import sys
def linuxo():
assert("linux" in sys.platform),"only run in linux"
print('do something')
try:
linuxo()
except :
print("func not executed")
try:
linuxo()
except AssertionError as error:
print(error)
print("func not executed")
Output:
func not executed
only run in linux
func not executed
try:
print('hello')
except:
print('error')
try:
print('hello')
except:
print('error')
else:
print('the try is correct')
Output:
error
hello
hello
the try is correct
##error by try and except
Example:
try:
print(hello)
except NameError as Na:
print(Na)
Output:
name 'hello' is not defined
##finally clause
Example:
try:
print('hello')
except:
print('error')
else:
print('the try is correct')
finally:
print('print no matter right or wrong')
Output:
hello
the try is correct
print no matter right or wrong