Python
Python
Python
2. x = ‘rahul’
print( x[0] )
print( x[-1] )
print( x[0 : 5] )
y = ‘verma’
print( x+y )
print( x * 3)
print( x[0] * 5 )
rahul
rahu
rahulverma
rv
hu
rahulrahulrahul
rrrrr
print( x[ - 1 : -5 ], x[ 0 : 4 ], x[ -1 : 0 ] )
y = [ ‘hello’, ‘world’ ]
print( x+y )
hello rahul
ooohhhhh Nooooooo
print( x[ - 1 : -5 ], x[ 0 : 4 ], x[ -1 : 0 ] )
y = ( ‘hello’, ‘world’ )
print( x+y )
hello rahul
ooohhhhh Nooooooo
import itertools.
x = {
‘Brand’ : ‘Hero’,
‘Model’ : ‘splender’,
y = x[ ‘Model’ ] + x[ ‘Owners’ ][ 3 ] )
print( y )
print( x[ ‘Color’ ] * 3 )
Hero rahul
Brand : Hero
splendersuresh
print( x )
5. import random
random.randint( 0, 99)
57
927
random.random()
0.5313843048739985
import math
import random
print( math.cos(random.randint(49,99) ) )
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
a = func1( 53, 89.2)
b = func2( ‘python’, 4)
print(a, b, c)
in func1
in func2
in func3
print( fp.read() )
print( fp.read( 5 ) )
line = fp.readline()
for I in line:
print( i )
fp.close()
y = input(‘input to le: ’)
fp.write( y )
fp.close()
fp = open(‘/Users/rahulverma/desktop/temp.txt’,’a’)
x = input(‘input to le: ’)
fp.write( x )
fp.close()
fi
fi
fi
fc.write( line )
fp.close()
fc.close()
x = np.array( [ [ 1, 2, 3, 4, 5 ] ,[ 6, 7, 8, 9, 10 ] ] )
2 10
[ 1, 2, 3, 4, 5 ] [ 1, 2, 3, 4, 5]
b = x.reshape( 5,2 )
( 2, 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.concatenate( ( x, y ) ) )
print( np.stack( ( x, y ) ) )
print( np.hstack( ( x, y ) ) )
[ [ 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 ] ]
[ [ 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 ]
[ 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 ] ]
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 )
print( np.sin( y[ 5 ] ) )
print( np.cos(y[ 5 ] ) )
print( np.tan( y[ 5 ] ) )
print( np.arccos( y[ 1 ] ) )
print( np.arcsin( y[ 4 ] ) )
print( np.arctan( y[ 5 ] ) )
print( np.sinh( y[ 5 ] ) )
print( np.cosh( y[ 5 ] ) )
print( np.tanh( y[ 5 ] ) )
print( np.rad2deg( y[ 5 ] ) )
print( np.rad2deg( x[ 5 ] ) )
0.8657598394923445
0.500459689008205
1.7299292200897907
1.2144110951818066
1.0597274793641782
nan
0.8081955160998497
1.248517659007662
1.5996238135430383
0.7805070469926899
59.96958255702618
3437.746770784939
print( np.mean( x ) )
print( np.median( x ) )
print( np.std( x ) )
print( np.var( x ) )
print( np.average( x ) )
print( np.amin( x ) )
print( np.amax( x ) )
print( np.percentile( x, 70 ) )
35.0
35.0
17.07825127659933
291.6666666666667
35.0
10
60
45.0
12. Import pandas as pd
print( data.mean( ) )
print( data.median( ) )
print( data[‘Count’].mode( ) ) #
print( data.max( ) )
print( data.min( ) )
print( data.std( ) )
print( data.var( ) )
print( data.count( ) )
data.isnull()
#Exception handeling
try:
k = 5 / 0
print( k )
except ZeroDivisionError:
nally:
#classes
class student:
self.name = name
self.class = class
self.marks = marks
def display(self):
print(‘Name : ‘,self.name,’\nClass:
’,self.class,’\nBranch: ’,self.marks)
obj.display()
fi
In [1]: import matplotlib.pyplot as plt
In [3]: x = np.linspace(0,10,1000)
In [5]: plt.legend()
Out[5]: <matplotlib.legend.Legend at 0x10509a280>
In [6]: plt.savefig('sample.png')
In [14]: plt.legend()
Out[14]: <matplotlib.legend.Legend at 0x10515d9a0>
In [15]: plt.ylabel('sin(x),cos(x)')
Out[15]: Text(0.5, 23.52222222222222, 'sin(x),cos(x)')
In [16]: plt.xlabel('x')
Out[16]: Text(22.347222222222214, 0.5, 'x')
plt.xlim(-10,10)
Out[9]: (-1, 10.0)
In [17]: plt.savefig('/Users/rahulverma/desktop/sample1.png')
In [3]: x=np.random.rand(100)
In [4]: y=np.random.rand(100)
In [5]: plt.scatter(x,y,color='g',s=500*np.random.randn(100),alpha=0.4,cmap='vir
...: idis')
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/
matplotlib/collections.py:980: RuntimeWarning: invalid value encountered in sqrt
scale = np.sqrt(self._sizes) * dpi / 72.0 * self._factor
Out[5]: <matplotlib.collections.PathCollection at 0x121c9c520>
In [6]: plt.colorbar()
Out[6]: <matplotlib.colorbar.Colorbar at 0x104c17700>
In [7]: plt.show()
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/
matplotlib/collections.py:980: RuntimeWarning: invalid value encountered in sqrt
scale = np.sqrt(self._sizes) * dpi / 72.0 * self._factor
In [8]: plt.savefig('/Users/rahulverma/desktop/sample3.png')
In [9]:
'
In [70]: x = np.random.randn(1000)
In [72]: plt.savefig('/Users/rahulverma/desktop/sample4.png')