Python Lab
Python Lab
ipynb - Colaboratory
PYTHON LAB
19071A04G5 V.S.VASANTH.P
1.#finding gcd of 2 numbers
import math
print("enter the first number")
c=int(input())
print("enter the second number")
b=int(input())
a=math.gcd(c,b)
print("the gcb of {} and {} is {}".format(c,b,a))
2.#adding 2 numbers by taking inputs from user
print("enter the first number")
c=int(input())
print("enter the second number")
b=int(input())
print("the addition of {} and {} is {}".format(c,b,c+b))
3.#checking the number wheather its even or odd
c=int(input("enter a number: "))
if (c% 2) == 0:
print("{0} is Even".format(c))
else:
print("{0} is Odd".format(c))
enter a number: 5
5 is Odd
4.#write a program using for loop that loops a sequence
print("enter the number")
b=int(input())
https://colab.research.google.com/drive/1qgG6l_Filq6-fZ7K3m0ua9dq1Ys0_rP9#scrollTo=Axfi8b35HMjC&printMode=true 1/13
07/07/2021 Untitled1.ipynb - Colaboratory
print('the multiplication table of {} is'.format(b))
for i in range(1,21):
print("{} x {} = {}".format(b,i,b*i))
45024
45024 x 1 = 45024
45024 x 2 = 90048
45024 x 3 = 135072
45024 x 4 = 180096
45024 x 5 = 225120
45024 x 6 = 270144
45024 x 7 = 315168
45024 x 8 = 360192
45024 x 9 = 405216
45024 x 10 = 450240
45024 x 11 = 495264
45024 x 12 = 540288
45024 x 13 = 585312
45024 x 14 = 630336
45024 x 15 = 675360
45024 x 16 = 720384
45024 x 17 = 765408
45024 x 18 = 810432
45024 x 19 = 855456
45024 x 20 = 900480
5.#python program to print fibonacci sequence using while loop
nterms= int(input("how many terms?"))
a=0
b=1
count = 0
if nterms <= 0:
print("please enter a positive integer")
elif nterms == 1:
print("Fibonacci Sequence upto {}".format(nterms))
print(a)
else:
print("Fibonacci Sequence:")
while count < nterms:
print(a)
nth = a + b
a = b
b= nth
count += 1
Fibonacci Sequence:
https://colab.research.google.com/drive/1qgG6l_Filq6-fZ7K3m0ua9dq1Ys0_rP9#scrollTo=Axfi8b35HMjC&printMode=true 2/13
07/07/2021 Untitled1.ipynb - Colaboratory
13
6.#checking wheather the given word is pallindrome or not
print('enter the element')
a = input()
if a == a[::-1]:
print('It is a pallindrome')
else:
print('It is not a pallindrome')
11211
It is a pallindrome
7.#combining two strings
a='vasanth'
c=" "
b='perni'
print(a+c+b)
vasanth perni
8.#printing the number of chars present in a string
print('enter the name')
a = input()
chrs = 'barsjg'
if a[0] in chrs:
print('The start character is in' , list(chrs))
else:
print('The start character is not in' , list(chrs))
vasanth
The start character is not in ['b', 'a', 'r', 's', 'j', 'g']
9.#finding mean
import statistics
print('enter no of elements into the list')
n=int(input())
b=[]
for i in range(1,n+1):
print('enter the elements')
a=int(input())
b.append(a)
https://colab.research.google.com/drive/1qgG6l_Filq6-fZ7K3m0ua9dq1Ys0_rP9#scrollTo=Axfi8b35HMjC&printMode=true 3/13
07/07/2021 Untitled1.ipynb - Colaboratory
print(b)
c=sum(b)
d=len(b)
mean=c/d
print(mean)
e=statistics.mean(b)
print(e)
[5, 6, 7]
6.0
10.#finding median
import statistics
print('enter no of elements into the list')
n=int(input())
b=[]
for i in range(1,n+1):
print('enter the elements')
a=int(input())
b.append(a)
print(b)
b.sort()
n=len(b)
print(b)
if n % 2 == 0:
median1 = b[n//2]
median2 = b[n//2 - 1]
median = (median1 + median2)/2
else:
median =b[n//2]
print("Medain is: " + str(median))
e=statistics.median(b)
print("the median is {}".format(e))
https://colab.research.google.com/drive/1qgG6l_Filq6-fZ7K3m0ua9dq1Ys0_rP9#scrollTo=Axfi8b35HMjC&printMode=true 4/13
07/07/2021 Untitled1.ipynb - Colaboratory
[4, 6, 8, 9, 2]
[2, 4, 6, 8, 9]
Medain is: 6
the median is 6
11.#finding mode
import statistics
print('enter no of elements into the list')
n=int(input())
b=[]
for i in range(1,n+1):
print('enter the elements')
a=int(input())
b.append(a)
print(b)
print("the mode is {}".format(statistics.mode(b)))
[6, 6, 5, 3, 6]
the mode is 6
12.#displaying calendar
import calendar
print('enter the year')
n=int(input())
print("enter the month")
m=int(input())
print(calendar.month(n,m))
print(calendar.calendar(n))
2021
https://colab.research.google.com/drive/1qgG6l_Filq6-fZ7K3m0ua9dq1Ys0_rP9#scrollTo=Axfi8b35HMjC&printMode=true 5/13
07/07/2021 Untitled1.ipynb - Colaboratory
10
October 2021
Mo Tu We Th Fr Sa Su
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
2021
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 1 2 3 4 5 6 7 1 2 3 4 5 6 7
4 5 6 7 8 9 10 8 9 10 11 12 13 14 8 9 10 11 12 13 14
11 12 13 14 15 16 17 15 16 17 18 19 20 21 15 16 17 18 19 20 21
18 19 20 21 22 23 24 22 23 24 25 26 27 28 22 23 24 25 26 27 28
25 26 27 28 29 30 31 29 30 31
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 4 1 2 1 2 3 4 5 6
5 6 7 8 9 10 11 3 4 5 6 7 8 9 7 8 9 10 11 12 13
12 13 14 15 16 17 18 10 11 12 13 14 15 16 14 15 16 17 18 19 20
19 20 21 22 23 24 25 17 18 19 20 21 22 23 21 22 23 24 25 26 27
26 27 28 29 30 24 25 26 27 28 29 30 28 29 30
31
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 4 1 1 2 3 4 5
5 6 7 8 9 10 11 2 3 4 5 6 7 8 6 7 8 9 10 11 12
12 13 14 15 16 17 18 9 10 11 12 13 14 15 13 14 15 16 17 18 19
19 20 21 22 23 24 25 16 17 18 19 20 21 22 20 21 22 23 24 25 26
26 27 28 29 30 31 23 24 25 26 27 28 29 27 28 29 30
30 31
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 1 2 3 4 5 6 7 1 2 3 4 5
4 5 6 7 8 9 10 8 9 10 11 12 13 14 6 7 8 9 10 11 12
11 12 13 14 15 16 17 15 16 17 18 19 20 21 13 14 15 16 17 18 19
18 19 20 21 22 23 24 22 23 24 25 26 27 28 20 21 22 23 24 25 26
25 26 27 28 29 30 31 29 30 27 28 29 30 31
13.#python program to print all prime numbers in a given interval(use break)
c=0
print("enter starting limit")
n=int(input())
print("enter ending limit")
m=int(input())
for i in range(n,m+1):
for n in range(1,21):
if i%n==0:
https://colab.research.google.com/drive/1qgG6l_Filq6-fZ7K3m0ua9dq1Ys0_rP9#scrollTo=Axfi8b35HMjC&printMode=true 6/13
07/07/2021 Untitled1.ipynb - Colaboratory
if i%n 0:
c=c+1
if c==2:
print(i)
c=0
100
11
13
17
19
25
46
49
58
62
69
74
82
86
87
93
94
14.#write a program to convert a list and tuple into arrays
import numpy
a=[1,2,3,4,5,6,7,8,9,10]
print('list:',a)
print('list to array',numpy.asarray(a))
b=([3,5,8,0,9],[58,51,43,12],)
print('tuple:',b)
print('tuple to array',numpy.asarray(b))
/usr/local/lib/python3.7/dist-packages/numpy/core/_asarray.py:83: VisibleDeprecationWarn
return array(a, dtype, copy=False, order=order)
15.#write a program to count the numbers of characters in the string and store threm in a dic
print("enter the string :")
s=input()
a=len(s)
d={'characters':a}
https://colab.research.google.com/drive/1qgG6l_Filq6-fZ7K3m0ua9dq1Ys0_rP9#scrollTo=Axfi8b35HMjC&printMode=true 7/13
07/07/2021 Untitled1.ipynb - Colaboratory
print(d)
i am ironman
{'characters': 12}
16.#write a program combine lists into a dictionary
l1=[1,2,3,4,5,6,7,8,9,10]
l2=['when','you','play','game','of','thrones','u','win','or','die']
d={l1[i]:l2[i] for i in range(len(l1))}
print(d)
17.#write a program to check wheather a string starts with specified characters
a=input('enter the string :')
if (a[0]=='a' or a[0]=='t'):
print('Yes')
else:
print('No')
Yes
18.#python program to split and join a string
a="python program to split a string"
print(a.split())
#tuples
a=("tony", "fucking", "stark")
x=''.join(a)
print(x)
#dictionary
a={"name": "tony", "pp": "ongole"}
b= " TEST "
x= b.join(a)
print(x)
tonyfuckingstark
name TEST pp
19.#python program to sort words in alphabetic order
https://colab.research.google.com/drive/1qgG6l_Filq6-fZ7K3m0ua9dq1Ys0_rP9#scrollTo=Axfi8b35HMjC&printMode=true 8/13
07/07/2021 Untitled1.ipynb - Colaboratory
b=input('enter the string:')
a = b.split()
a.sort()
for i in a:
print(i)
enter the string:ned catelyn robb sansa arya bron rickon jon
arya
bron
catelyn
jon
ned
rickon
robb
sansa
20.#simple calculator program by making use of functions
def cal():
a=int(input('enter a number\t'))
b=int(input('enter a number\t'))
print('add-1,subtract-2,multiply-3,divide-4')
c=int(input('choose a choice\t'))
if c==1:
print(a+b)
if c==2:
print(a-b)
if c==3:
print(a*b)
if c==4:
print(a/b)
cal()
enter a number 4
enter a number 6
add-1,subtract-2,multiply-3,divide-4
choose a choice 1
10
21.#find the factorial of a number using recursion
def fact(n):
if(n>1):
return(n*(fact(n-1)))
else:
return 1
n=int(input('enter a number\t'))
fact(n)
enter a number 8
40320
https://colab.research.google.com/drive/1qgG6l_Filq6-fZ7K3m0ua9dq1Ys0_rP9#scrollTo=Axfi8b35HMjC&printMode=true 9/13
07/07/2021 Untitled1.ipynb - Colaboratory
22.#write a function dups to find all duplicates in the list
a=[1,2,6,42,1,32,18,'y','a','j','j']
b=[]
for i in a:
if i not in b:
b.append(i)
else:
print(i,end='')
1j
23.#write a function unique to find all the unique elements of a list
a=[1,3,9,1,2,8,'b','k','g','k']
b=[]
c=0
for i in a:
for j in a:
if (i==j):
c=c+1
if(c==1):
print(i,end='')
else:
c=0
32bg
24.#write a function cumulative product to compute cumulative product of a list of numbers
def stark():
a=[9,2,5,7]
b=[]
c=1
for i in range(0,len(a)):
c=c*a[i]
b.append(c)
return(b)
stark()
25.#write a function reverse to print the given list in the reverse order
def reverselist():
l=['stark',53,12.75,-67j,]
return(l[::-1])
reverselist()
26.#write a program that defines a matrix and prints
https://colab.research.google.com/drive/1qgG6l_Filq6-fZ7K3m0ua9dq1Ys0_rP9#scrollTo=Axfi8b35HMjC&printMode=true 10/13
07/07/2021 Untitled1.ipynb - Colaboratory
6. te a p og a t at de es a at a d p ts
import numpy as js
b=js.array([[1,2,3],[3,4,6],[8,9,10]])
print(b)
[[ 1 2 3]
[ 3 4 6]
[ 8 9 10]]
27.#write a program to perform addition multplication subtraction on matrices
c=js.array([[1,2,3],[4,5,6],[8,9,10]])
print('first matrix\n',c)
d=js.array([[6,8,9],[13,14,15],[2,3,4]])
print('second matrix\n',d)
print('addition of two matrices\n',c+d)
print('subtraction of two matrices\n',c-d)
print('multiplication of two matrices\n',c*d)
first matrix
[[ 1 2 3]
[ 4 5 6]
[ 8 9 10]]
second matrix
[[ 6 8 9]
[13 14 15]
[ 2 3 4]]
[[ 7 10 12]
[17 19 21]
[10 12 14]]
[[-5 -6 -6]
[-9 -9 -9]
[ 6 6 6]]
[[ 6 16 27]
[52 70 90]
[16 27 40]]
28.#install NumPypackage with pip and explore it
import numpy as np
print(np.version)
a=[[1,2],[3,4]]
np.reshape(a,(2,2))
b=np.array([[1,2,3],[4,5,6],[7,8,9]])
print('array dimension',b.ndim)
print('array shape',b.shape)
print('datatype',b.dtype)
print('\nexploring the array items in b')
print('2nd element in 1st dimension is',b[0][1])
print('1st element in 3rd dimension is',b[2][-3])
print('addition of above two elements is',b[0][1]+b[2][-3])
print('multiplication of above two elements is' b[0][1]*b[2][ 3])
https://colab.research.google.com/drive/1qgG6l_Filq6-fZ7K3m0ua9dq1Ys0_rP9#scrollTo=Axfi8b35HMjC&printMode=true 11/13
07/07/2021 Untitled1.ipynb - Colaboratory
print( multiplication of above two elements is ,b[0][1]*b[2][-3])
l=[10,20,15,50,60,40]
print('nlist -->>',1)
print('median value:',np.median(1))
print('mean value:',np.mean(1))
print('sorted list -->>',np.sort(1))
array dimension 2
datatype int64
nlist -->> 1
---------------------------------------------------------------------------
<ipython-input-57-616807722f4c> in <module>()
17 print('median value:',np.median(1))
18 print('mean value:',np.mean(1))
989 else:
990 a = asanyarray(a).copy(order="K")
992 return a
993
https://colab.research.google.com/drive/1qgG6l_Filq6-fZ7K3m0ua9dq1Ys0_rP9#scrollTo=Axfi8b35HMjC&printMode=true 12/13
07/07/2021 Untitled1.ipynb - Colaboratory
https://colab.research.google.com/drive/1qgG6l_Filq6-fZ7K3m0ua9dq1Ys0_rP9#scrollTo=Axfi8b35HMjC&printMode=true 13/13