INTRODUCTION TO PYTHON
NAME: SANDIP PODDAR
ROLL NO.: 430220010005
REGISTRETION NO.: 201270100310069
SUB. CODE: EC 694C
DEPT.: ELECTRONICS AND
COMMUNICATION ENGINEERING
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
Write a program to Generate the following output
[ [0,0,0,...N],[1,1,1,...N],[2,2,2,2,....N] ]
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
Take a list of numbers of 10 elements and check the existence
of a number ”n” in that list
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
>>>[10, 30, 6, 7, 9, 10]
4 Exp-4
4.1 PROBLEM
Flatten the list containing the sub-list as below
Given: x=[[1,2,3],[4,5,6],[7,8,9]]
4.2 THEORY
If we have multidimensional array and we need a single dimensional array. So we
need to write a program to convert a multidimensional array into a single
dimensional array.
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
>>>Enter the no.:12
>>>divisors of 12 is
[1, 2, 3, 4, 6, 12]
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
>>>Enter the string:mom
Given string is palindrome
10
1 Exp-8
1
8.1 PROBLEM
Take a list of names of different fruits and then make a
database to count the number of fruits. [Use dictionary]
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
>>>{’mango’: 2, ’apple’: 1, ’orange’: 2, ’banana’: 3}
11
1 Exp-9
2
9.1 PROBLEM
Take list of names and create a database to search a list of
names with a first character
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
>>>[’shreyan’, ’shubhradip’, ’shubhajit’,’sandip’,’sneha’,
’subarna’, ’sounak’, ’saptarshi’, ’shubhajit’, ’shuvam’]
12
13 Exp-10
10.1 PROBLEM
Take the string “MyNameIsAmal” as input and re-write the
string as “My name is Amal”
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
>>>My name is Amal
13
14 Exp-11
11.1 PROBLEM
Find the minima of the function
f (x) = y = (x − 2)2
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
Figure 1: The way programme reaches the minimum position
15
16 Exp-12
12.1 PROBLEM
Create a random matrix of size 3x3 with column wise sum is 1
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
>>>Desired Matrix= [[0.02340555 0.30186028 0.32624207]
[0.46098329 0.3818838 0.5503718 ]
[0.51561116 0.31625592 0.12338613]]
>>>Column wise sum= [1. 1. 1.]
16
17 Exp-13
13.1 PROBLEM
Find the difference of successive elements of a list as
given: x=[12,43,24,6,78,13]
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
>>>[31, 19, 18, 72, 65]
P.T.O
17
18 Exp-14
14.1 PROBLEM
Generate Fibonacci sequence of n elements using recursive call
14.2 THEORY
Recursive function is a function which calls itself repetitively until a terminating
condition satisfies. So by using this powerful function we need to print the
Fibonacci Sequence upto the term user wants.
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
Effect of high dimensions on the Euclidean distance
15.2 THEORY
In co-ordinate geometry,Euclidean distance is a distance measured between 2 points
belong to euclidean plane. The formula for calculating the distance is
√
dist = (x1 − x2)2 + (y1 − y2)2
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
com com and
6 d=[]
d.append(np.sqrt(np.sum((a[element[0]]-a[element[1]])**2)))
P.T.O
19
15.4 OUTPUT
(a) 2 dimension (b) 10 dimension
(c) 100 dimension (d) 1000 dimension
(e) 10000 dimension
Figure 2: Changes observed for increasing dimension
20
16 Exp-16
16.1 PROBLEM
Study the frequency of occurrence of English alphabets
16.2 THEORY
We have to find out the number of occurrence of english alphabets in any given text.
16.3 CODE
1 import matplotlib.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 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}
Figure 3: Frequency of English alphabets in a given text
22
17 Exp-17
17.1 PROBLEM
Study the frequency of pixels of an image
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
Figure 4: Histogram of an image
END
24