Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 25

INTRODUCTION TO PYTHON

NAME: TANAY CHAKRABORTY


ROLL NO.: 430220010021
REGISTRATION NO.: 201270100310003

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

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

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=[[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

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

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

>>>[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=[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

>>>Enter the no.:12


>>>divisors of 12 is [1, 2, 3,
4, 6, 12]

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

>>>Enter the string:mom Given


string is palindrome

10
8 Exp-8

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

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

>>>{’mango’: 2, ’apple’: 1, ’orange’: 2, ’banana’: 3}

11
9 Exp-9

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

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

>>>[’shreyan’, ’shubhradip’, ’shubhajit’,’sandip’,’sneha’, ’subarna’,


’sounak’, ’saptarshi’, ’shubhajit’, ’shuvam’]

12
10 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

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

>>>My name is Amal

13
11 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

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

Figure 1: The way programme reaches the minimum position

15
12 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 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

>>>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
13 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

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

>>>[31, 19, 18, 72, 65]

P.T.O

17
14 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

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

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

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

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

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

Figure 4: Histogram of an image

END

24

You might also like