Python Practical
Python Practical
print(x)
print( x[-1] )
y = ‘verma’
print( x[0] * 5 )
Output:- r
rahul
rahu
rahulverma
rv
hu
rahulrahulrahul
rrrrr
PYTHON PRACTICAL ASSIGNMENT SESSION 2021-22
y = [ ‘hello’, ‘world’ ]
hello rahul
ooohhhhh Nooooooo
y = ( ‘hello’, ‘world’ )
hello rahul
ooohhhhh Nooooooo
x = {
‘Brand’ : ‘Hero’,
‘Model’ : ‘splender’,
y = x[ ‘Model’ ] + x[ ‘Owners’ ][ 3 ] )
print( y )
Brand : Hero
splendersuresh
print( x )
Output :- 57
Output :- 927
Output :- 0.5313843048739985
import math
import random
print( math.cos(random.randint(49,99) ) )
Output :- 0.7739405962953605
0.7301735609948197
6. def func1( x, y ):
print(‘In func1\n')
a = x + y
return a
def func2( x, y ):
print(‘in func2\n’)
a = x * y
return a
def func3( x, y ):
print(‘in func3\n’)
return a
a = x - y
b = func2( ‘python’, 4)
print(a, b, c)
Output :- in func1
in func2
in func3
# fp open() function is use for open a file of given location and mode
line = fp.readline()
print( i )
y = input(‘input to file: ’)
fp.write( y ) #this will input write on file but remove all existing
content of the file
fp = open(‘/Users/rahulverma/desktop/temp.txt’,’a’) #appending
x = input(‘input to file: ’)
fp.close(). #input will print on the file after the file close
fc.write( line )
fp.close()
fc.close()
x = np.array( [ [ 1, 2, 3, 4, 5 ] ,[ 6, 7, 8, 9, 10 ] ] )
Output :- 2 10
Output :- [ 1, 2, 3, 4, 5 ] [ 1, 2, 3, 4, 5]
[[ 1 2 ]
[ 3 4 ]
[ 5 6 ]
[ 7 8 ]
[ 9 10 ]]
y = np.array( [ [ 11, 12, 13, 14, 15], [16, 17, 18, 19, 20] ] )
print( np.hstack( ( x, y ) ) ) # x y
Output :- [ [ 1 2 3 4 5 ]
[ 6 7 8 9 10 ]
[ 11 12 13 14 15 ]
[ 16 17 18 19 20 ] ]
[ [ 1 2 3 4 5 11 12 13 14 15 ]
[ 6 7 8 9 10 16 17 18 19 20 ] ]
PYTHON PRACTICAL ASSIGNMENT SESSION 2021-22
[ [ 1 2 3 4 5 ]
[ 6 7 8 9 10 ] ]
[ [ 11 12 13 14 15 ]
[ 16 17 18 19 20 ] ] #stack
[ [ 1 2 3 4 5 ]
[ 6 7 8 9 10 ]
[ 11 12 13 14 15 ]
[ 16 17 18 19 20 ] ] #vstack
[ [ 1 2 3 4 5 11 12 13 14 15 ]
[ 6 7 8 9 10 16 17 18 19 20 ] ] #hsatck
z = np.array( [1, 2, 3, 4, 5, 6 ] )
print( np.array_split( z, 3 ) )
print( np.array_split( z, 4 ) )
a = np.array_split( x, 2 )
print( a[ 0 ] )
print( a[ 1 ] )
print( np.array_split( x, 4 ) )
[[12345]]
[ [ 6 7 8 9 10 ] ]
y = x * ( 3.14 / 180 )
Output :- 0.8657598394923445
0.500459689008205
1.7299292200897907
1.2144110951818066
1.0597274793641782
nan
0.8081955160998497
1.248517659007662
1.5996238135430383
0.7805070469926899
59.96958255702618
3437.746770784939
PYTHON PRACTICAL ASSIGNMENT SESSION 2021-22
Output :- 35.0
35.0
17.07825127659933
291.6666666666667
35.0
10
60
45.0
PYTHON PRACTICAL ASSIGNMENT SESSION 2021-22
PYTHON PRACTICAL ASSIGNMENT SESSION 2021-22
print( data.min( ) ) #returns minimum value of every
column
PYTHON PRACTICAL ASSIGNMENT SESSION 2021-22
PYTHON PRACTICAL ASSIGNMENT SESSION 2021-22
PYTHON PRACTICAL ASSIGNMENT SESSION 2021-22
4. Bar plot
PYTHON PRACTICAL ASSIGNMENT SESSION 2021-22
PYTHON PRACTICAL ASSIGNMENT SESSION 2021-22
3. problem - find, he/she is voter or not
else:
a = x %10
sum = sum + a
x = x//10
8. Exception handling - try, except, finally and raise keywords are use
to handle exception
finally - this keywords blocks execute always after the try and except’s
block
try:
print( k )
except ZeroDivisionError:
finally:
9. class py:
if x==-1:
if n%2 ==0:
return 1
else:
return -1
if n==0:
return 1
if n<0:
return 1/self.pow(x,-n)
val = self.pow(x,n//2)
PYTHON PRACTICAL ASSIGNMENT SESSION 2021-22
if n%2 ==0:
return val*val
return val*val*x
print(py().pow(2, -3));
print(py().pow(3, 5));
print(py().pow(100, 0));
PYTHON PRACTICAL ASSIGNMENT SESSION 2021-22
4. # function to check if two strings are
# anagram or not
s1 ="listen"
s2 ="silent"
if(sorted(s1)== sorted(s2)):
else:
test_list3 = [1, 4, 5, 6, 5]
test_list4 = [3, 5, 7, 2, 5]
+ str(test_list3))
# initialising _list
print("intial_list", str(ini_tuple))
result = []
for i in ini_tuple:
result.append(i)
set1 = set()
set1 = set("GeeksForGeeks")
print(set1)
set1 = set(String)
print(set1)
print(set1)
#dictionary
if key in test_dict2:
test_dict1[key] = test_dict2[key]