Python Lab Report SANDIP PODDAR
Python Lab Report SANDIP PODDAR
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
7.2 THEORY ...................................................................................................................... 10
1
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
13.3 CODE ........................................................................................................................... 17
13.4 OUTPUT ...................................................................................................................... 17
2
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
4 Exp-1
1.1 PROBLEM
1.2 CODE
1. List comprehension
1 N=int(input("Enter␣the␣no.:"))
2 a=[[0 for i in range(N+1)],[1 for i in range(N+1)],[2 for i in
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
5 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
f=1
print("Exist")
break
P.T.O
5
2. Python Method
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=[j for j in x if j>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=[column for row in x for column in row]
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=[i for i in range(1,n+1) if n%i==0]
3 print(f"divisors␣of␣{n}␣is␣\n{divisor_list}")
5.3 OUTPUT
P.T.O
8
9 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=[]
in b and not in d:
d.append(i)
6.4 OUTPUT
>>>[1, 2, 3, 5, 8, 13]
9
1 Exp-7
0
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
i=-1
break
6 i==-1:
7.4 OUTPUT
10
1 Exp-8
1
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
2 "banana"]
3 d={}
d[i]=1
d[i]+=1
8.4 OUTPUT
11
1 Exp-9
2
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
5 d={}
9.4 OUTPUT
12
13 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
10.4 OUTPUT
13
14 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
y=(x_new-2)**2
14
else:
x_old=x_new
x_new=x_old-l*2*(x_old-2)
11.4 OUTPUT
>>>Iteration= 107
>>>Desired Value= 1.9999999996240665
15
16 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 import numpy 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
17 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
13.4 OUTPUT
P.T.O
17
18 Exp-14
14.1 PROBLEM
14.2 THEORY
14.3 CODE
n<=1:
return n
else:
14.4 OUTPUT
>>>Enter a no.:6
>>>0 1 1 2 3 5
P.T.O
18
19 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
d.append(np.sqrt(np.sum((a[element[0]]-a[element[1]])**2)))
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
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 for i in text:
6 if i not in d:
7 d[i]=1
8 else:
9 d[i]+=1
10 l=[]
11 for i in d.keys():
12 if ord(i)>=65 and ord(i)<=90:
21
15 d1={}
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 import cv2
3 a=cv2.imread("E:\Pictures\SD_photo.jpg")
4 d={}
d[k]+=1
23
17.4 OUTPUT
END
24