Python DataStructure
Python DataStructure
https://colab.research.google.com/drive/1OOQLIsjPx5OK7NW94nh4IEv_W61pl2JS Page 1 of 11
Python-DataStructure.ipynb - Colab 06/08/24, 3:17 PM
q.append(20)
1 # Queue demonstration using collections.deque
q.append(30)
2
3 from collections import deque
4
5 q = deque() # Creation of a queue
6
7 q.append(10)
8 q.append(20)
9 q.append(30)
10 print(q)
11
12 print(q.popleft()) # Deletes first element i.e. 10
13 print(q)
14 print(q[0]) # Peek
15
16 print(len(q))
17 print(len(q)==0)
1 # map() function
2
3 def f(x):
4 return 2*x
5
6 L = [1, 6, 4, 9, 7]
7
8 M = list(map(f, L))
9 print(M)
['HELLO', 'WORLD']
https://colab.research.google.com/drive/1OOQLIsjPx5OK7NW94nh4IEv_W61pl2JS Page 2 of 11
Python-DataStructure.ipynb - Colab 06/08/24, 3:17 PM
1 L = [1, 0, 6, 4, 0, 9, 7]
2
3 M = list(filter(None, L))
4
5 print(M)
[1, 6, 4, 9, 7]
[0, 0]
27
28
https://colab.research.google.com/drive/1OOQLIsjPx5OK7NW94nh4IEv_W61pl2JS Page 3 of 11
Python-DataStructure.ipynb - Colab 06/08/24, 3:17 PM
1512
1 L = [1,6,4,9,7]
2 M = list(map(lambda x: 2*x, L))
3 M
1 L = ["Hello", "WorlD"]
2 M = list(map(lambda x: x.upper(), L))
3 M
['HELLO', 'WORLD']
1 L1=[1,2,3]
2 L2=[4,5,6]
3 L3=[7,8,9]
4 M = list(map(lambda x,y,z: x+y+z, L1,L2,L3))
5 M
1 L = [1,6,4,9,7]
2 M = list(map(lambda x: x%2==0, L))
3 M
1 L = [1,6,4,9,7,0]
2 M = list(filter(lambda x: x%2==0, L))
3 M
[6, 4, 0]
https://colab.research.google.com/drive/1OOQLIsjPx5OK7NW94nh4IEv_W61pl2JS Page 4 of 11
Python-DataStructure.ipynb - Colab 06/08/24, 3:17 PM
[1, 9, 7]
27
1512
1 n=3
2 for i in range(2,n):
3 if n%i==0:
4 print(False)
5 else:
6 print(True)
True
1 def isPrime(n):
2 if n == 2:
3 return True
4 for i in range(2,n):
5 if n%i==0:
6 return False
7 else:
8 return True
9
10 P=[x for x in range(2,100) if isPrime(x)]
11 print(P)
[2, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41
https://colab.research.google.com/drive/1OOQLIsjPx5OK7NW94nh4IEv_W61pl2JS Page 5 of 11
Python-DataStructure.ipynb - Colab 06/08/24, 3:17 PM
1 def isPrime(n):
2 if n == 2:
3 return True
4 for i in range(2,n):
5 if n%i==0:
6 return False
7 else:
8 return True
9
10 P=[x for x in range(2,100) if isPrime(x) if not x%10 == 9]
11 print(P)
[2, 3, 5, 7, 11, 13, 15, 17, 21, 23, 25, 27, 31, 33, 35, 37, 41, 43, 45, 47, 5
1 def isPrime(n):
2 if n == 2:
3 return True
4 for i in range(2,n):
5 if n%i==0:
6 return False
7 else:
8 return True
9
10 P=[x for x in range(2,100) if isPrime(x) if x%10 != 9]
11 print(P)
[2, 3, 5, 7, 11, 13, 15, 17, 21, 23, 25, 27, 31, 33, 35, 37, 41, 43, 45, 47, 5
1 for j in range(0,4):
2 print(j,end='')
0123
https://colab.research.google.com/drive/1OOQLIsjPx5OK7NW94nh4IEv_W61pl2JS Page 6 of 11
Python-DataStructure.ipynb - Colab 06/08/24, 3:17 PM
1 m,n = map(lambda x: int(x), input("Enter the order of the matrix: ").split(' '))
2
3 print("Enter the matrix elements rowwise:")
4 matrix = [list(map(lambda x: int(x),input().split(' '))) for row in range(m)]
5 print("Here is the matrix of order {}x{}:".format(m,n))
6
7 for i in range(m):
8 for j in range(n):
9 print("{:5}".format(matrix[i][j]),end=' ')
10 print()
https://colab.research.google.com/drive/1OOQLIsjPx5OK7NW94nh4IEv_W61pl2JS Page 7 of 11
Python-DataStructure.ipynb - Colab 06/08/24, 3:17 PM
1 m,n = map(lambda x: int(x), input("Enter the order of the matrix: ").split(' '))
2 print("Enter the matrix elements rowwise:")
3 matrix = [list(map(lambda x: int(x),input().split(' '))) for row in range(m)]
4 transpose = [[None] * m for i in range(n)]
5
6 for i in range(m):
7 for j in range(n):
8 transpose[j][i] = matrix[i][j]
9
10 for i in range(n):
11 for j in range(m):
12 print("{} ".format(transpose[i][j]), end='')
13 print()
https://colab.research.google.com/drive/1OOQLIsjPx5OK7NW94nh4IEv_W61pl2JS Page 8 of 11
Python-DataStructure.ipynb - Colab 06/08/24, 3:17 PM
https://colab.research.google.com/drive/1OOQLIsjPx5OK7NW94nh4IEv_W61pl2JS Page 9 of 11
Python-DataStructure.ipynb - Colab 06/08/24, 3:17 PM
1 # Matrix Multiplication
2
3
https://colab.research.google.com/drive/1OOQLIsjPx5OK7NW94nh4IEv_W61pl2JS Page 10 of 11
Python-DataStructure.ipynb - Colab 06/08/24, 3:17 PM
https://colab.research.google.com/drive/1OOQLIsjPx5OK7NW94nh4IEv_W61pl2JS Page 11 of 11