Professional Documents
Culture Documents
Python Lab Report
Python Lab Report
NARULA INSTITUTE
OF
TECHNOLOGY
Contents
1 Exp-1 4
1.1 PROBLEM.................................................................................................................................4
1.2 CODE.........................................................................................................................................4
1.3 OUTPUT..................................................................................................................................4
2 Exp-2 5
2.1 PROBLEM.................................................................................................................................5
2.2 THEORY....................................................................................................................................5
2.3 CODE.........................................................................................................................................5
2.4 OUTPUT....................................................................................................................................6
3 Exp-3 6
3.1 PROBLEM.................................................................................................................................6
3.2 THEORY....................................................................................................................................6
3.3 CODE.........................................................................................................................................7
3.4 OUTPUT....................................................................................................................................7
4 Exp-4 7
4.1 PROBLEM.................................................................................................................................7
4.2 THEORY....................................................................................................................................7
4.3 CODE.........................................................................................................................................7
4.4 OUTPUT....................................................................................................................................8
5 Exp-5 8
5.1 PROBLEM.................................................................................................................................8
5.2 CODE.........................................................................................................................................8
5.3 OUTPUT....................................................................................................................................8
6 Exp-6 9
6.1 PROBLEM.................................................................................................................................9
6.2 THEORY....................................................................................................................................9
6.3 CODE.........................................................................................................................................9
6.4 OUTPUT....................................................................................................................................9
7 Exp-7 10
7.1 PROBLEM.................................................................................................................................10
1
7.2 THEORY....................................................................................................................................10
7.3 CODE.........................................................................................................................................10
7.4 OUTPUT....................................................................................................................................10
8 Exp-8 11
8.1 PROBLEM.................................................................................................................................11
8.2 THEORY....................................................................................................................................11
8.3 CODE.........................................................................................................................................11
8.4 OUTPUT....................................................................................................................................11
9 Exp-9 12
9.1 PROBLEM.................................................................................................................................12
9.2 THEORY....................................................................................................................................12
9.3 CODE.........................................................................................................................................12
9.4 OUTPUT....................................................................................................................................12
10 Exp-10 13
10.1 PROBLEM.................................................................................................................................13
10.2 THEORY....................................................................................................................................13
10.3 CODE.........................................................................................................................................13
10.4 OUTPUT....................................................................................................................................13
11 Exp-11 14
11.1 PROBLEM.................................................................................................................................14
11.2 THEORY....................................................................................................................................14
11.3 CODE.........................................................................................................................................14
11.4 OUTPUT....................................................................................................................................15
12 Exp-12 16
12.1 PROBLEM.................................................................................................................................16
12.2 THEORY....................................................................................................................................16
12.3 CODE.........................................................................................................................................16
12.4 OUTPUT....................................................................................................................................16
13 Exp-13 17
13.1 PROBLEM.................................................................................................................................17
13.2 THEORY....................................................................................................................................17
2
13.3 CODE.........................................................................................................................................17
13.4 OUTPUT....................................................................................................................................17
14 Exp-14 18
14.1 PROBLEM.................................................................................................................................18
14.2 THEORY....................................................................................................................................18
14.3 CODE.........................................................................................................................................18
14.4 OUTPUT....................................................................................................................................18
15 Exp-15 19
15.1 PROBLEM.................................................................................................................................19
15.2 THEORY....................................................................................................................................19
15.3 CODE.........................................................................................................................................19
15.4 OUTPUT....................................................................................................................................20
16 Exp-16 21
16.1 PROBLEM.................................................................................................................................21
16.2 THEORY....................................................................................................................................21
16.3 CODE.........................................................................................................................................21
16.4 OUTPUT....................................................................................................................................22
17 Exp-17 23
17.1 PROBLEM.................................................................................................................................23
17.2 THEORY....................................................................................................................................23
17.3 CODE.........................................................................................................................................23
17.4 OUTPUT....................................................................................................................................24
3
1 Exp-1
1.1 PROBLEM
1.2 CODE
1.List comprehension
1 N=int(input("Enter␣the␣no.:"))
2 a=[[0foriinrange(N+1)],[1foriinrange(N+1)],[2foriin
range(N+1)]]
3 print(a)
2.Python Method
1 N=int(input("Enter␣the␣no.:"))
2 a=[[0,]*N,[1,]*N,[2,]*N]
3 print(a)
1.3 OUTPUT
1.[ [0,0,0,...N],[1,1,1,...N],[2,2,2,2,....N] ]
2.[ [0,0,0,...N],[1,1,1,...N],[2,2,2,2,....N] ]
4
2 Exp-2
2.1 PROBLEM
2.2 THEORY
There are some numbers in a container. We need to write a program to find a specific
number is in the container or not.
2.3 CODE
1.Normal Method
1 l=[1,2,3,4,5,6,7,8,9,10]
2 n=15
3 f=0
4 foriinl:
ifn==i:
5
6
f=1 print("Exist") break
97 if(f==0):
10 print("Not␣Exist")
P.T.O
5
2.Python Method
1 l=[1,2,3,4,5,6,7,8,9,10]
2 n=5
3 ifninl:
p4 rint("Exist")
5 else:
6 print("Not␣Exist")
2.4 OUTPUT
1. Not Exist
2. Exist
3 Exp-3
3.1 PROBLEM
Take all elements from the given list which are greater than 5
Given: x=[10,30,2,4,5,6,7,9,10]
3.2 THEORY
There are some numbers in a container. We need to write a program to find the numbers
which are greater than 5.
6
3.3 CODE
1 x=[10,30,2,4,5,6,7,9,10]
2 new=[jforjinxifj>5]
3 print(new)
3.4 OUTPUT
4 Exp-4
4.1 PROBLEM
4.2 THEORY
4.3 CODE
1 x=[[1,2,3],[4,5,6],[7,8,9]]
2 new=[columnforrowinxforcolumninrow]
3 print(new)
7
4.4 OUTPUT
>>>[1, 2, 3, 4, 5, 6, 7, 8, 9]
5 Exp-5
5.1 PROBLEM
Write a program to ask a number from user and then print all
the divisor of that number.
5.2 CODE
1 n=int(input("Enter␣the␣no.:"))
2 divisor_list=[iforiinrange(1,n+1)ifn i==0]
3 print(f"divisors␣of␣{n}␣is␣\n{divisor_list}")
5.3 OUTPUT
P.T.O
8
6 Exp-6
6.1 PROBLEM
Write a program that returns a list that contains only the elements that are common
between the lists (with out duplicates).
Given:
a=[1,1,2,3,5,8,13,21,34,55,89],b=[1,2,3,4,5,6,7,8,9,10,11,12,13
]
6.2 THEORY
If we have two containers of different numbers and we want to make a list of the
numbers which are common in both the containers without putting any duplicate value
in the list.
6.3 CODE
1 a=[1,1,2,3,5,8,13,21,34,55,89]
2 b=[1,2,3,4,5,6,7,8,9,10,11,12,13]
3 d=[]
4 foriina:
5 ifiinbandinotind:
6 d.append(i)
7 print(d)
6.4 OUTPUT
>>>[1, 2, 3, 5, 8, 13]
9
7 Exp-7
7.1 PROBLEM
Ask the user for a string and check whether the string is a
palindrome or not.
7.2 THEORY
We have to take a string from user. Then we need to write a code to check whether the
string is same from both the sides(front and reverse)or not. If same then that’s a
palindrome string else not.
7.3 CODE
1 n=input("Enter␣the␣string:")
2 foriinrange(len(n)//2):
ifn[i]!=n[len(n)-1-i]:
3
i=-1
4
break
5
6 ifi==-1:
p7 rint("Given␣string␣is␣not␣palindrome")
8 else:
9 print("Given␣string␣is␣palindrome")
7.4 OUTPUT
10
8 Exp-8
8.1 PROBLEM
8.2 THEORY
If we have a basket full of different fruits and we are trying to count the number of
different fruits according to their name. Then make a list of the fruit’s names and the
number of the that fruit available in the basket.
8.3 CODE
1 l=["mango","apple","orange","orange","banana","mango","banana",
2 "banana"]
3 d={}
4 foriinl:
5 ifinotind:
6
d[i]=1 else:
7 d[i]+=1
8
9 print(d)
8.4 OUTPUT
11
9 Exp-9
9.1 PROBLEM
9.2 THEORY
To search the names of people who have the same first character in their name, we want
a database which store all the names under the first character of their name.
9.3 CODE
1 l=["shreyan","shubhradip","shubhajit","dona","sandip","adrish",
2 "tanmoy","ankita","subarna","trisrota","bhaskar","ratul","hrisit",
3 "koushik","sounak","pradip","tanay","avishek","prasanjit","anurupa",
4 "saptarshi","shubhajit","rebanta","shuvam","mainak","ananya","sneha"]
5 d={}
6 foriinl:
7 ifi[0]notind:
8 d[i[0]]=[i]
9 else:
10 d[i[0]].append(i)
11 print(d[’s’])
9.4 OUTPUT
12
10 Exp-10
10.1 PROBLEM
10.2 THEORY
We have to write a code which can formate the above mentioned string. First, we need
to separate each word then make the appropriate word in small-case and at last join
them again with space between them.
10.3 CODE
1 s="MyNameIsAmal"
2 l=list(s)
3 lis="".join([iifi.islower()else"␣"+iforiinl])[1:]
4 lis1=lis.split()
5 forjinrange(len(lis1)):
6 ifj!=0andj!=len(lis1)-1:
7 lis1[j]=lis1[j].lower()
8 f_s="␣".join(lis1)
9 print(f_s)
10.4 OUTPUT
13
11 Exp-11
11.1 PROBLEM
11.2 THEORY
We are trying to find the lowest position of the given function i.e. a particular value for
which the function gives the lowest value or minimum value. So we need to find that
particular value of the independent variable for which the function has the minimum
value.
11.3 CODE
1 importnumpy as np
2 importmatplotlib.pyplot as plt
3 x=np.arange(-10,10,.2)
4 y=(x-2)**2
5 plt.plot(x,y,’-’)
6 x_old=-9
7 l=.1
8 y=(x_old-2)**2
9 plt.plot(x_old,y,’*’)
10 x_new=x_old-l*2*(x_old-2)
11 foriinrange(1000):
12 y=(x_new-2)**2
13
plt.plot(x_new,y,’*r’) ifnp.abs(x_old-
14 x_new)<10**-10:
15 print("Iteration=",i)
14
16 print("Desired␣Value=",x_new)
17
break else:
18 x_old=x_new
19 x_new=x_old-l*2*(x_old-2)
20
21 plt.show()
11.4 OUTPUT
>>>Iteration= 107
>>>Desired Value= 1.9999999996240665
15
12 Exp-12
12.1 PROBLEM
12.2 THEORY
We need to generate a 3x3 matrix. The elements of the matrix are random. Then from
there we have to make that matrix, a matrix whose column wise sums are equal to 1.
12.3 CODE
1 importnumpy as np
2 a=np.random.rand(3,3)
3 b=np.sum(a,axis=0)
4 c=a/b
5 print("Desired␣Matrix=",c)
6 print("Column␣wise␣sum=",np.sum(c,axis=0))
12.4 OUTPUT
16
13 Exp-13
13.1 PROBLEM
13.2 THEORY
We need to write a code which can find the difference between 2 successive digits
within a list. And return a list which contain the differences of the digits.
13.3 CODE
1 importnumpy as np
2 x=[12,43,24,6,78,13]
3 s=[]
4 foriinrange(len(x)):
5 ifi>0:
6 s.append(np.abs(x[i]-x[i-1]))
7 print(s)
13.4 OUTPUT
P.T.O
17
14 Exp-14
14.1 PROBLEM
14.2 THEORY
14.3 CODE
1 deffibo(n):
2ifn<=1:
3
returnn else:
4return(fibo(n-1)+fibo(n-2))
65 n=int(input("Enter␣a␣no.:"))
7 foriinrange(n):
8 print(fibo(i),end="␣")
14.4 OUTPUT
>>>Enter a no.:6
>>>0 1 1 2 3 5
P.T.O
18
15 Exp-15
15.1 PROBLEM
15.2 THEORY
for 2 dimensional point. The formula can be extended for more dimension. But the
limitation of this formula for calculating distance between points is, in high dimension the
distance between any 2 points are nearly same.
15.3 CODE
1 importnumpy as np
2 importmatplotlib.pyplot as plt
3 a=np.random.random((5,1000))
4 com=[iforiinrange(5)]
5 l=[(i,j)foriincomforjincomifi!=jandi<j]
6 d=[]
7 forelementinl:
8 d.append(np.sqrt(np.sum((a[element[0]]-a[element[1]])**2)))
9 plt.stem(d)
10 plt.show()
P.T.O
19
15.4 OUTPUT
20
16 Exp-16
16.1 PROBLEM
16.2 THEORY
We have to find out the number of occurrence of english alphabets in any given text.
16.3 CODE
1 importmatplotlib.pyplot as plt
2 text="Cryptography␣is␣the␣study␣of␣secure␣communications␣
techniques␣that␣allow␣only␣the␣sender␣and␣intended␣recipient␣
of␣a␣message␣to␣view␣its␣contents.␣The␣term␣is␣derived␣from␣
the␣Greek␣word␣kryptos,␣which␣means␣hidden.␣It␣is␣closely␣
associated␣to␣encryption,␣which␣is␣the␣act␣of␣scrambling␣
ordinary␣text␣into␣what’s␣known␣as␣ciphertext␣and␣then␣back␣
again␣upon␣arrival.␣In␣addition,␣cryptography␣also␣covers␣the␣
obfuscation␣of␣information␣in␣images␣using␣techniques␣such␣as␣ microdots␣or␣merging."
3 text=text.upper()
4 d={}
5 foriintext:
6 ifinotind:
7 d[i]=1
8 else:
9 d[i]+=1
10 l=[]
11 foriind.keys():
12 iford(i)>=65andord(i)<=90:
21
13 l.append(i)
14 l=sorted(l)
15 d1={}
16 forjinl:
17 d1[j]=d[j]
18 print(d1)
19 plt.stem(d1.keys(),d1.values())
20 plt.show()
16.4 OUTPUT
>>>{’A’: 28, ’B’: 3, ’C’: 22, ’D’: 16, ’E’: 39, ’F’: 7,
’G’: 10, ’H’: 20, ’I’: 37, ’K’: 4, ’L’: 8, ’M’: 11, ’N’:
33, ’O’: 33, ’P’: 9, ’Q’: 2, ’R’: 25, ’S’: 30, ’T’: 38,
’U’: 9, ’V’: 4, ’W’: 7, ’X’: 2, ’Y’: 10}
22
17 Exp-17
17.1 PROBLEM
17.2 THEORY
We have to collect the number i.e. how many pixels have the same intensity level(starting
from 0 to 255) in an image. Then we need to make the histogram of it to visualize the
collected data.
17.3 CODE
1 importcv2
2 importmatplotlib.pyplot as plt
3 a=cv2.imread("E:\Pictures\SD_photo.jpg")
4 d={}
5 foriina:
6 forjini:
7 forkinj:
8 ifknotind:
9
d[k]=1 else:
10 d[k]+=1
11
12 plt.bar(d.keys(),d.values())
13 plt.title("Histogram")
14 plt.xlabel("Pixel␣Intensity")
15 plt.ylabel("Frequency")
16 plt.show()
23
17.4 OUTPUT
END
24