-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGUIDE_list_and_tuple.py
More file actions
217 lines (146 loc) · 5.58 KB
/
GUIDE_list_and_tuple.py
File metadata and controls
217 lines (146 loc) · 5.58 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# author: Asmaa Mirkhan ~ 2019
# LISTS AND TUPLES
# ----------------------------------------------------------
# LISTS
# list declaration and appending
my_list = [0, 15, 12.5, 2, 3, 4]
my_list.append(74) # appends one element
my_list.extend([14, 12]) # appends multiple elements
print('After appending:', my_list)
# Output: [0, 15, 12.5, 2, 3, 4, 74, 14, 12]
# -----------------------------------------------------
# inserting at sepecific position (index, value)
my_list.insert(2, 44) # inserts the 44 value at 2nd position
print('After inserting:', my_list)
# Output: [0, 15, 44, 12.5, 2, 3, 4, 74, 14, 12]
# -----------------------------------------------------
# deleting element(s)
del my_list[0] # deletes element(s)
print('After deleting:', my_list)
# Output: [15, 44, 12.5, 2, 3, 4, 74, 14, 12]
# -----------------------------------------------------
# statistical operations
print('length:', len(my_list), 'min:', min(my_list), 'max:', max(my_list), 'sum:',
sum(my_list), 'count', my_list.count(2), 'index of 2:', my_list.index(2))
# Output: length: 9 min: 2 max: 74 sum: 180.5 count 1 index of 2: 3
# -----------------------------------------------------
# list slicing
print('list[1:3]', my_list[1:3])
print('list[-4:-2]', my_list[-4:-2])
print('list[:3]', my_list[:3])
print('list[4:]', my_list[4:])
# Output
# [44, 12.5] ## start printing from 1st position to 3rd position (3rd not included) in maths [1,3)
# [4, 74] ## start printing from 4th position from the end to 2nd position from the end (3rd not included)
# [15, 44, 12.5] ## start printing from the start to the 3rd position (3rd not included)
# [3, 4, 74, 14, 12] ## start printing from the 4th position to the end
# sample list indexing
# list: 15 11 -45 -14.6 74.45 87 96
# positive indexing [0] [1] [2] [3] [4] [5] [6]
# negative indexing [-7] [-6] [-5] [-4] [-3] [-2] [-1]
# -----------------------------------------------------
# Arithmetic operators on lists
print('+ operation', my_list[2:5] + my_list[3:6])
print('* operations', my_list[:3]*3)
# Output:
# [12.5, 2, 3, 2, 3, 4]
# [15, 44, 12.5, 15, 44, 12.5, 15, 44, 12.5]
# -----------------------------------------------------
# list sorting
my_list.sort()
print('Sorted list', my_list)
# Output: [2, 3, 4, 12, 12.5, 14, 15, 44, 74]
# -----------------------------------------------------
# list comperhansions
my_filtered_list = [i for i in my_list if i > 11]
print('Filtered list', my_filtered_list)
# Output: [12, 12.5, 14, 15, 44, 74]
my_complex_list = [
i*j for i in my_list for j in my_filtered_list if i*j < 50]
print('Complex list', my_complex_list)
# Output: [24, 25.0, 28, 30, 36, 37.5, 42, 45, 48]
square_list = [i**2 for i in range(1, 6)]
print(square_list)
# Output: [1, 4, 9, 16, 25]
# -----------------------------------------------------
# reversing a list
square_list.reverse()
print('Reversed list', square_list)
# Output: [25, 16, 9, 4, 1]
# -----------------------------------------------------
# My list
print('My list',my_list)
# Output: [2, 3, 4, 12, 12.5, 14, 15, 44, 74]
# removing a value
# removes first value that satisfies in the list
my_list.remove(3)
print('After removing',my_list)
# Output: [2, 4, 12, 12.5, 14, 15, 44, 74]
# removing item by pop
# removes the item and returns its value
# NOTE: pop's parameter is an index not a value ==> pop([i])
print('Removing by pop', my_list.pop(2),my_list)
# Output: 12 [2, 4, 12.5, 14, 15, 44, 74]
# clearing a list
my_list.clear()
print(my_list)
# Output: []
# -----------------------------------------------------
# IN operation
print('25? ', 25 in square_list)
# Output: True
# -----------------------------------------------------
# iterating
for val in square_list:
print(val)
# Output: all square_list's values
for i, val in enumerate(square_list):
print(i, 'th value:', val)
# Output: all square_list's indeces and values
for i, val in enumerate(square_list, start=3):
print(i, 'th value (started from index=3):', val)
# Output: square_list's indeces and values starting from 3rd position
# iterating in reversed order
for val in reversed(square_list):
print('value (reversed order): ', val)
# Output: all square_list's indeces and values in reversed order
# ------------------------------------------------------------
# TUPLES
# tuple declaration
some_tuple = 1, 2, 5, 'asmaa', [-88, -55, 13]
sample_tuple = (15, 11.5, 13)
print(some_tuple)
print(sample_tuple)
# Output:
# (1, 2, 5, 'asmaa', [-88, -55, 13])
# (15, 11.5, 13)
# -----------------------------------------------------
# tuple indexing and slicing
print('indexing', some_tuple[1], some_tuple[3]
[2], some_tuple[-1], some_tuple[1:4])
# Output: 2 m [-88, -55, 13] (2, 5, 'asmaa')
# Note: tuples are immutable
# ==> some_tuple[0]=2 #ERROR
some_tuple[-1][2] = 1111 # OK, element is itself a mutable datatype
print('Changed tuple:', some_tuple)
# Output: (1, 2, 5, 'asmaa', [-88, -55, 1111])
# -----------------------------------------------------
# arithmetic operators on tuples
sample_tuple = some_tuple + (47, 48, 49)
print('+ operation', sample_tuple)
sample_tuple = (11, 22, 33)*3
print('* operation', sample_tuple)
# Output
# (1, 2, 5, 'asmaa', [-88, -55, 1111], 47, 48, 49)
# (11, 22, 33, 11, 22, 33, 11, 22, 33)
# -----------------------------------------------------
# deleting a tuple
del sample_tuple
# print(sample_tuple) ERROR
# iterating
for val in some_tuple:
print(val)
# Output: prints all values in the tuple
for i, val in enumerate(some_tuple):
print(i, 'th value: ', val)
# Output: prints all values and indeces in the tuple