Python

Download as pdf or txt
Download as pdf or txt
You are on page 1of 32

1.

x = input(“Enter any value: ”); print(x)

2. x = ‘rahul’

print( x[0] )

print( x[-1] )

print( x[0 : 5] )

print( x[0 : -1] )

print( x[-1 : -2] )

y = ‘verma’

print( x+y )

print( x[0] + y[0] )

print( x[2 : 4] + y[-1 : 0] )

print( x * 3)

print( x[0] * 5 )

rahul

rahu

rahulverma

rv

hu

rahulrahulrahul

rrrrr

x = [ 5, ‘rahul’, 3.14, 759, 0.13 ]

print( x[-2], x[0], x[1], x[4] )

print( x[ - 1 : -5 ], x[ 0 : 4 ], x[ -1 : 0 ] )

y = [ ‘hello’, ‘world’ ]

print( x+y )

print( y[0]+’ ’+x[1])

print( ‘smile’ + ’ ’ +y[1][0] + ’it’+ y[0][0] +’ ’+ x[1][4] + y[0][4]+’ve’ )

print( y[0][4]*3 + y[0][0]*5 + ‘ ’ + ’N’ + y[0][4]*7)

759 5 rahul 0.13

[0.13, 759, 3.14, ‘rahul’ 5] [5, ‘rahul’, 3.14, 759, 0.13]

[5, ‘rahul’, 3.14, 759, 0.13, ‘hello’, ‘world’]

hello rahul

smile with love

ooohhhhh Nooooooo

x = ( 5, ‘rahul’, 3.14, 759, 0.13 )

print( x[-2], x[0], x[1], x[4] )

print( x[ - 1 : -5 ], x[ 0 : 4 ], x[ -1 : 0 ] )

y = ( ‘hello’, ‘world’ )

print( x+y )

print( y[0]+’ ’+x[1])

print( ‘smile’ + ’ ’ +y[1][0] + ’it’+ y[0][0] +’ ’+ x[1][4] + y[0][4]+’ve’ )

print( y[0][4]*3 + y[0][0]*5 + ‘ ’ + ’N’ + y[0][4]*7)

759 5 rahul 0.13

(0.13, 759, 3.14, ‘rahul’ 5) (5, ‘rahul’, 3.14, 759, 0.13)

(5, ‘rahul’, 3.14, 759, 0.13, ‘hello’, ‘world’)

hello rahul

smile with love

ooohhhhh Nooooooo

import itertools.

x = {

‘Brand’ : ‘Hero’,

‘Model’ : ‘splender’,

‘Number’ : ‘RJ14 0000’,

‘Owners’ : [‘rahul’, ‘mukesh’, ‘ramesh’, ‘suresh’] }

print( x[ ‘Brand’ ], x[ ‘Owners’ ][ 0 ] )

res = dict( itertools.islice(x.item(), 2) )

y = x[ ‘Model’ ] + x[ ‘Owners’ ][ 3 ] )

print( y )

print( x[ ‘Color’ ] * 3 )

Hero rahul

Brand : Hero

{‘Brand’ : ‘Hero’, ‘Model’ : ‘splender’}

splendersuresh

x = { ‘hello’, 99, .39, 8, ‘ankit’, 2}7

print( x )

{ ‘hello’, 99, .39, 8, ‘ankit’, 2}

5. import random

random.randint( 0, 99)

57

random.randrang( 57, 999, 3)

927

random.random()

0.5313843048739985

import math

import random

print( math.sin( random.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)

c = func3( math.log(5), random.random())

print(a, b, c)

in func1

in func2

in func3

142.2 pythonpythonpythonpython 0.9013081295104165

7. fp = open( ‘/Users/rahulverma/desktop/temp.txt’, ‘r’)

print( fp.read() )

print( fp.read( 5 ) )

line = fp.readline()

for I in line:

print( i )

fp.close()

fp = open( ‘/Users/rahulverma/desktop/temp.txt’, ‘w’)

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()

fp = open( ‘/Users/rahulverma/desktop/temp.txt’, ‘r’ )

fc = open( ‘/Users/rahulverma/desktop/coied_ le.txt’, ‘a’)

for line in fp:

fi
fi
fi
fc.write( line )

fp.close()

fc.close()

10. Import numpy as np

x = np.array( [ [ 1, 2, 3, 4, 5 ] ,[ 6, 7, 8, 9, 10 ] ] )

print( x[0][1], x[1][4] )

2 10

print( x[ 0 ][ 0:4 ], x[ 0, 0:4] , x[1, 0:-1] )

[ 1, 2, 3, 4, 5 ] [ 1, 2, 3, 4, 5]

print( ‘shape before reshaping ‘, x.shape )

b = x.reshape( 5,2 )

print( ‘array after reshaping ’, b.shape, b )

( 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.concatenate( ( x, y ), axis =1 ) )

print( np.stack( ( x, y ) ) )

print( np.vstack( (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 ) )

[ array( [1, 2] ), array( [3, 4] ), array( [5, 6] ) ]

[ array( [1, 2] ), array( [3, 4] ), array( [5] ), array( [6] ) ]

[[12345]]

[ [ 6 7 8 9 10 ] ]

[ array( [ [1, 2, 3, 4, 5 ] ] ), array( [ [6, 7, 8, 9, 10] ] ), array( [ ],


shape=(0,5), dtype=int64 ), array( [ ] ), shape=(0,5), dtype=int64 ]
11. import numpy as np

x = np.array( [ 10, 20, 30, 40, 50, 60 ] )

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

data = pd.read_csv( ‘/Users/rahulverma/downloads/Births-and-


deaths/births-deaths-by-region.csv’)

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:

print( ‘exception caught - divide by zero error')

nally:

print( ‘this is always execute’ )

#classes

class student:

def __init__(self, name, branch, marks):

self.name = name

self.class = class

self.marks = marks

def display(self):

print(‘Name : ‘,self.name,’\nClass:
’,self.class,’\nBranch: ’,self.marks)

obj = class(‘Rahul’, ‘AInDS’, 100)

obj.display()


fi
In [1]: import matplotlib.pyplot as plt

In [2]: import numpy as np

In [3]: x = np.linspace(0,10,1000)

In [4]: plt.plot(x,np.sin(x), label = 'sin(x)', color='g')


Out[4]: [<matplotlib.lines.Line2D at 0x128359910>]

In [5]: plt.legend()
Out[5]: <matplotlib.legend.Legend at 0x10509a280>

In [6]: plt.savefig('sample.png')

In [12]: plt.plot(x,np.sin(x),label='sin(x)', color=‘g’)


Out[12]: [<matplotlib.lines.Line2D at 0x10515de50>]

In [13]: plt.plot(x,np.cos(x),label=‘cos(x)’, linestyle=‘—-’)


Out[13]: [<matplotlib.lines.Line2D at 0x13c538190>]

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 [71]: plt.hist(x, bins=6)


Out[71]:
(array([ 17., 134., 359., 349., 127., 14.]),
array([-3.11820055, -2.06408888, -1.00997721, 0.04413446, 1.09824613,
2.1523578 , 3.20646947]),
<BarContainer object of 6 artists>)

In [72]: plt.savefig('/Users/rahulverma/desktop/sample4.png')

You might also like