-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTuplesDataStructure.py
More file actions
executable file
·180 lines (129 loc) · 3.01 KB
/
TuplesDataStructure.py
File metadata and controls
executable file
·180 lines (129 loc) · 3.01 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 10 16:43:50 2021
@author: maherme
"""
#%%
# You don't need the parenthesis for creating a tuple
a = (10, 20, 30)
b = 10, 20, 30
print(type(a))
print(type(b))
#%%
# Sometimes you will need to specifi the parenthesis:
def print_tuple(t):
for e in t:
print(e)
print_tuple((10, 20, 30))
print_tuple(10, 20, 30) # This will fail
#%%
# We can access to a tuple indexing:
a = 'a', 10, 200
print(a[0])
print(a[1])
#%%
# We can also use slicing or iterate over a tuple
a = 1, 2, 3, 4, 5, 6
print(type(a))
print(a[2:5])
for e in a:
print(e)
#%%
# We can unpack a tuple:
a = 'a', 10, 20
x, y, z = a
print(x)
print(y)
print(z)
#%%
# You can use extender unpacked
a = 1, 2, 3, 4, 5
x, *_, y, z = a
print(x)
print(_)
print(y)
print(z)
#%%
# Tuple is inmutable. But not the objects inside a tuple:
class Point2D:
def __init__(self, x, y):
self.x = x
self.y = y
def __repr__(self):
return f'{self.__class__.__name__}(x={self.x}, y={self.y})'
pt = Point2D(10, 20)
print(pt)
print(id(pt))
pt.x = 100
print(pt)
print(id(pt))
#%%
a = Point2D(x=0, y=0), Point2D(10, 20)
print(a)
print(id(a[0]))
a[0].x = 100
print(a)
print(id(a[0])) # The memory address is the same than before, so is inmutable
#%%
a = 1, 2, 3
print(a)
print(id(a))
a += (4, 5)
print(a)
print(id(a)) # Notice this is a new tuple
a = a + (4, 5)
print(a)
print(id(a)) # As tuple is inmutable, adding objects to the tuple creates a new tuple
#%%
# Notice these tuples are heterogeneous
london = 'London', 'UK', 8_780_000
new_york = 'New York', 'USA', 8_500_000
beijing = 'Beijing', 'China', 21_000_000
# Notice this list is homogeneous
cities = [london, new_york, beijing]
total = 0
for city in cities:
total += city[2]
print(total)
#%%
# A better way to do the code above:
total = sum(city[2] for city in cities)
print(total)
#%%
record = 'DJIA', 2018, 1, 19, 25_987, 26_072, 25_942, 26_072
symbol, year, month, day, open_, high, low, close = record
print(symbol)
print(year)
symbol, *_, close = record
print(symbol, close)
print(_)
#%%
# You can unpack using a loop:
for city, country, population in cities:
print(city, country, population)
#%%
# You can use enumerate:
for index, city in enumerate(cities):
print(index, city)
#%%
# You can use tuple for returning several values from a function.
# This example calculates an aproximation of Pi number:
from random import uniform
from math import sqrt
def random_shot(radius):
random_x = uniform(-radius, radius)
random_y = uniform(-radius, radius)
if sqrt(random_x**2 + random_y**2) <= radius:
is_in_circle = True
else:
is_in_circle = False
return random_x, random_y, is_in_circle
num_attempts = 1_000_000
count_inside = 0
for i in range(num_attempts):
*_, is_in_circle = random_shot(1)
if is_in_circle:
count_inside += 1
print(f'Pi is approximately: {4 * count_inside / num_attempts}')
#%%