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
a = (1, 2) # tuple of two integers
b = 1, 2 # alternative way to define a tuple
a[0] = 3 # error: modifying an immutable tuple after its creation
a = (1, 2) # tuple of two integers
b = (1, "one") # tuple of an integer and a string
c = (a, b) # tuple containing two tuples
print(b[1]) # prints the second element of b
print(c[0]) # prints the first element of c
# Example: unpacking
t = (1, 2, 3) # tuple of three integers
(a, b, c) = t # copy first element of t to a, second to b, etc.
a, b, c = t # alternative syntax to accomplish the same thing
print(a)
print(b)
print(c)
# Example: nested tuples
t = (1, (2, 3)) # tuple containing an integer and a tuple
# copy the first element of t to a, second to (b, c)
(a, (b, c)) = t
a, (b, c) = t
print(a)
print(b)
print(c)
# Example: erroneous unpacking
a, b, c = t # error: not enough values to unpack
Enter to Rename, Shift+Enter to Preview