-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathList.py
More file actions
151 lines (109 loc) · 2.96 KB
/
List.py
File metadata and controls
151 lines (109 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#list is a collection of items in a particular order
## mutable
# ordered
# indexed by position
# allows duplicates
#integer list
num = [1,2,3,4,5]
#indexing in list
print(num[0]) #outputs 1
print(num[1]) #outputs 2
print(num[-1]) #outputs 5
#slicing in list
print(num[1:4]) #outputs [2,3,4]
print(num[1:4:2])#outputs [2,4]
print(num[1:4:1])#outputs [2,3,4]
print(num[0:5:1])#outputs [1,2,3,4,5]
print(num[0:5:2])#outputs [1,3,5]
print(num[0:5:3])#outputs [1,4]
#appending new elements in list
num.append(6)#adds 6 at the end of list
num.append(8)#adds 8 at the end of list
num.append(7)#adds 7 at the end of list
print(num) #outputs [1,2,3,4,5,6,8,7]
#popping elements from list
num.pop(0)#removes 1 from the list
num.pop(1)#removes 3 from the list
num.pop(3)#removes 6 from the list
num.pop()#removes last element from the list
print(num)
#looping and conditionals,len()
num=[1,2,3,4,5]
for n in num:#looping through the list
print(n)#outputs 1 2 3 4 5
#creating lists of lists
matrix =[
[1,2,3],
[4,5,6],
[7,8,9]
]
print(matrix) #outputs whole matrix
print(matrix[0]) #outputs [1,2,3]
print(matrix[0][0]) #outputs 1
print(matrix[-1][1]) #outputs 8
#sum of all elements of all elements in a matrix: assignment
matrix =[
[1,2,3],
[4,5,6],
[7,8,9]
]
total=0
for i in range(0,3):
for j in range(0,3):
total+=matrix[i][j]
print(f"Sum of all elements of given matrix is: {total}")
#print matrix in matrix format (assignment)
#membership operator in list
num=[1,2,3,4,5]
print(2 in num)
print(6 not in num)
#note : lists are mutable but tuples are not immutable
#list methods
#insert
colors=['red','green','blue']
colors.insert(1,'yellow')
colors.insert(0,'purple')
print(colors)
#note: append is more efficient than insert because it need to shift all items after it to insert which consumes more memory but append just add elements at the end
#remove
colors.remove('purple')
print(colors)
#sort
print(sorted(colors)) #asscending order
print(sorted(colors,reverse=True)) #descending order
#extend
more_colors=['black','white']
colors.extend(more_colors)
print(colors)
#list comprehension
#traditional way
sq=[]
for i in range(1,6):
sq.append(i**2)
print(sq) #outputs [1,4,9,16,25]
#list comprehensive way
sq=[i**2 for i in range(1,6)]
print (sq) #outputs [1,4,9,16,25]
#can add condition as well:
sq=[i**2 for i in range(1,6) if i%2!=0]
print (sq) #outputs [1,9,25]
# create the filtering the even numbers
num=[1,2,3,4,5,6,7,8,9,10]
even_num=[n for n in num if n%2==0] #n in num extracts the elements while i in range gives the indexing
print(even_num)
#assignment: output [None,2,None,4,None,6,None,8] using list comprehension
result = [None if i % 2 == 0 else i+1 for i in range(8)]
print(result)
sq=[]
sq=[i for i in range(8) if i%2==0] #print even numbers upto 8 by list comprehension.
print(sq)
#matrix
matrix=[
[1,2,3],
[4,5,6],
[7,8,9]
]
for row in matrix:
for col in row:
print(col, end=" ")
print("\n")