2. A list is a mutable heterogeneous sequence
Python Tuples
3. A list is a mutable heterogeneous sequence
A tuple is an immutable heterogeneous sequence
Python Tuples
4. A list is a mutable heterogeneous sequence
A tuple is an immutable heterogeneous sequence
I.e., a list that can't be changed aafftteerr ccrreeaattiioonn
Python Tuples
5. A list is a mutable heterogeneous sequence
A tuple is an immutable heterogeneous sequence
I.e., a list that can't be changed aafftteerr ccrreeaattiioonn
Why provide a less general type of collection?
Python Tuples
6. A list is a mutable heterogeneous sequence
A tuple is an immutable heterogeneous sequence
I.e., a list that can't be changed aafftteerr ccrreeaattiioonn
Why provide a less general type of collection?
Full explanation will have to wait for lecture on
sets and dictionaries
Python Tuples
7. A list is a mutable heterogeneous sequence
A tuple is an immutable heterogeneous sequence
I.e., a list that can't be changed aafftteerr ccrreeaattiioonn
Why provide a less general type of collection?
Full explanation will have to wait for lecture on
sets and dictionaries
Useful even before then
Python Tuples
9. Create tuples using () instead of []
Still index using [] (because everything does)
Python Tuples
10. Create tuples using () instead of []
Still index using [] (because everything does)
>>>>>>>>>>>>>>>>>>>>>>>> pprriimmeess == ((22,, 33,, 55,, 77))
>>>>>>>>>>>> print primes[0], primes[-1]
2 7
>>>>>>>>>>>>
Python Tuples
11. Create tuples using () instead of []
Still index using [] (because everything does)
>>>>>>>>>>>>>>>>>>>>>>>> pprriimmeess == ((22,, 33,, 55,, 77))
>>>>>>>>>>>> print primes[0], primes[-1]
2 7
>>>>>>>>>>>> empty_tuple = ()
>>>>>>>>>>>> print len(empty_tuple)
0
>>>>>>>>>>>>
Python Tuples
12. Create tuples using () instead of []
Still index using [] (because everything does)
>>>>>>>>>>>>>>>>>>>>>>>> pprriimmeess == ((22,, 33,, 55,, 77))
>>>>>>>>>>>> print primes[0], primes[-1]
2 7
>>>>>>>>>>>> empty_tuple = ()
>>>>>>>>>>>> print len(empty_tuple)
0
>>>>>>>>>>>>
Must use (val,) for tuple with one element
Python Tuples
13. Create tuples using () instead of []
Still index using [] (because everything does)
>>>>>>>>>>>>>>>>>>>>>>>> pprriimmeess == ((22,, 33,, 55,, 77))
>>>>>>>>>>>> print primes[0], primes[-1]
2 7
>>>>>>>>>>>> empty_tuple = ()
>>>>>>>>>>>> print len(empty_tuple)
0
>>>>>>>>>>>>
Must use (val,) for tuple with one element
Because math says that (5) is just 5
Python Tuples
14. Create tuples using () instead of []
Still index using [] (because everything does)
>>>>>>>>>>>>>>>>>>>>>>>> pprriimmeess == ((22,, 33,, 55,, 77))
>>>>>>>>>>>> print primes[0], primes[-1]
2 7
>>>>>>>>>>>> empty_tuple = ()
>>>>>>>>>>>> print len(empty_tuple)
0
>>>>>>>>>>>>
Must use (val,) for tuple with one element
Because math says that (5) is just 5
One of Python's few syntactic warts…
Python Tuples
16. Don't need parentheses if context is enough
>>>>>>>>>>>> primes = 2, 3, 5, 7
>>>>>>>>>>>> print primes
(2, 3, 5, 7)
>>>>>>>>>>>>
Python Tuples
17. Don't need parentheses if context is enough
>>>>>>>>>>>> primes = 2, 3, 5, 7
>>>>>>>>>>>> print primes
(2, 3, 5, 7)
>>>>>>>>>>>>
Can use on the left of assignment
Python Tuples
18. Don't need parentheses if context is enough
>>>>>>>>>>>> primes = 2, 3, 5, 7
>>>>>>>>>>>> print primes
(2, 3, 5, 7)
>>>>>>>>>>>>
Can use on the left of assignment
>>>>>>>>>>>> left, middle, right = 2, 3, 5
>>>>>>>>>>>>
Python Tuples
19. Don't need parentheses if context is enough
>>>>>>>>>>>> primes = 2, 3, 5, 7
>>>>>>>>>>>> print primes
(2, 3, 5, 7)
>>>>>>>>>>>>
Can use on the left of assignment
>>>>>>>>>>>> left, middle, right = 2, 3, 5
>>>>>>>>>>>> print left
2
>>>>>>>>>>>> print middle
3
>>>>>>>>>>>> print right
5
>>>>>>>>>>>>
Python Tuples
20. Don't need parentheses if context is enough
>>>>>>>>>>>> primes = 2, 3, 5, 7
>>>>>>>>>>>> print primes
(2, 3, 5, 7)
>>>>>>>>>>>>
Can use on the left of assignment
>>>>>>>>>>>> left, middle, right = 2, 3, 5
>>>>>>>>>>>> print left
2
>>>>>>>>>>>> print middle
3
>>>>>>>>>>>> print right
5
>>>>>>>>>>>>
With great power comes
great responsibility…
Python Tuples
26. Sometimes used to return (success, result) pairs
ddddeeeeffff read_if_available(datafile_name):
iiiiffff file_exists(datafile_name):
...
rrrreeeettttuuuurrrrnnnn (TTTTrrrruuuueeee, data_values)
eeeellllsssseeee:
rrrreeeettttuuuurrrrnnnn (FFFFaaaallllsssseeee, [])
Python Tuples
27. Sometimes used to return (success, result) pairs
ddddeeeeffff read_if_available(datafile_name):
iiiiffff file_exists(datafile_name):
...
rrrreeeettttuuuurrrrnnnn (TTTTrrrruuuueeee, data_values)
eeeellllsssseeee:
rrrreeeettttuuuurrrrnnnn (FFFFaaaallllsssseeee, [])
success, data = read_if_available('mydata.dat')
iiiiffff success:
...
Python Tuples
28. Sometimes used to return (success, result) pairs
ddddeeeeffff read_if_available(datafile_name):
iiiiffff file_exists(datafile_name):
...
rrrreeeettttuuuurrrrnnnn (TTTTrrrruuuueeee, data_values)
eeeellllsssseeee:
rrrreeeettttuuuurrrrnnnn (FFFFaaaallllsssseeee, [])
success, data = read_if_available('mydata.dat')
iiiiffff success:
...
We'll meet a better way in the lecture on testing
Python Tuples
30. Provides a quick way to swap variables' values
>>>>>>>>>>>> left, right = 0, 10
>>>>>>>>>>>>
Python Tuples
31. Provides a quick way to swap variables' values
>>>>>>>>>>>> left, right = 0, 10
>>>>>>>>>>>> right, left = left, right
>>>>>>>>>>>>>>>>>>>>>>>>
Python Tuples
32. Provides a quick way to swap variables' values
>>>>>>>>>>>> left, right = 0, 10
>>>>>>>>>>>> right, left = left, right
>>>>>>>>>>>>>>>>>>>>>>>> pprriinntt rriigghhtt
0
>>>>>>>>>>>> print left
10
>>>>>>>>>>>>
Python Tuples
33. Provides a quick way to swap variables' values
>>>>>>>>>>>> left, right = 0, 10
>>>>>>>>>>>> right, left = left, right
>>>>>>>>>>>>>>>>>>>>>>>> pprriinntt rriigghhtt
0
>>>>>>>>>>>> print left
10
>>>>>>>>>>>>
Python creates temporaries if needed
Python Tuples
34. Provides a quick way to swap variables' values
>>>>>>>>>>>> left, right = 0, 10
>>>>>>>>>>>> right, left = left, right
>>>>>>>>>>>>>>>>>>>>>>>> pprriinntt rriigghhtt
0
>>>>>>>>>>>> print left
10
>>>>>>>>>>>>
Python creates temporaries if needed
left
right
0
10
Python Tuples
35. Provides a quick way to swap variables' values
>>>>>>>>>>>> left, right = 0, 10
>>>>>>>>>>>> right, left = left, right
>>>>>>>>>>>>>>>>>>>>>>>> pprriinntt rriigghhtt
0
>>>>>>>>>>>> print left
10
>>>>>>>>>>>>
Python creates temporaries if needed
left
right
_tmp_
0
10
Python Tuples
36. Provides a quick way to swap variables' values
>>>>>>>>>>>> left, right = 0, 10
>>>>>>>>>>>> right, left = left, right
>>>>>>>>>>>>>>>>>>>>>>>> pprriinntt rriigghhtt
0
>>>>>>>>>>>> print left
10
>>>>>>>>>>>>
Python creates temporaries if needed
left
right
_tmp_
0
10
Python Tuples
37. Provides a quick way to swap variables' values
>>>>>>>>>>>> left, right = 0, 10
>>>>>>>>>>>> right, left = left, right
>>>>>>>>>>>>>>>>>>>>>>>> pprriinntt rriigghhtt
0
>>>>>>>>>>>> print left
10
>>>>>>>>>>>>
Python creates temporaries if needed
left
right
_tmp_
0
10
Python Tuples
38. Provides a quick way to swap variables' values
>>>>>>>>>>>> left, right = 0, 10
>>>>>>>>>>>> right, left = left, right
>>>>>>>>>>>>>>>>>>>>>>>> pprriinntt rriigghhtt
0
>>>>>>>>>>>> print left
10
>>>>>>>>>>>>
Python creates temporaries if needed
left
right
_tmp_
0
10
Python Tuples
39. Provides a quick way to swap variables' values
>>>>>>>>>>>> left, right = 0, 10
>>>>>>>>>>>> right, left = left, right
>>>>>>>>>>>>>>>>>>>>>>>> pprriinntt rriigghhtt
0
>>>>>>>>>>>> print left
10
>>>>>>>>>>>>
Python creates temporaries if needed
left
right
0
10
Python Tuples
40. And an easy way to unpack a list
Python Tuples
41. And an easy way to unpack a list
>>>>>>>>>>>> colors = ['yellow', 'magenta', 'lavender']
>>>>>>>>>>>>
Python Tuples
42. And an easy way to unpack a list
>>>>>>>>>>>> colors = ['yellow', 'magenta', 'lavender']
>>>>>>>>>>>> left, middle, right = colors
>>>>>>>>>>>>>>>>>>>>>>>>
Python Tuples
43. And an easy way to unpack a list
>>>>>>>>>>>> colors = ['yellow', 'magenta', 'lavender']
>>>>>>>>>>>> left, middle, right = colors
>>>>>>>>>>>>>>>>>>>>>>>> pprriinntt lleefftt
yellow
>>>>>>>>>>>> print middle
magenta
>>>>>>>>>>>> print right
lavender
>>>>>>>>>>>>
Python Tuples
44. And an easy way to unpack a list
>>>>>>>>>>>> colors = ['yellow', 'magenta', 'lavender']
>>>>>>>>>>>> left, middle, right = colors
>>>>>>>>>>>>>>>>>>>>>>>> pprriinntt lleefftt
yellow
>>>>>>>>>>>> print middle
magenta
>>>>>>>>>>>> print right
lavender
>>>>>>>>>>>>
Number of values must be the same
Python Tuples
46. Often used in loops
>>>>>>>>>>>> pairs = ((1, 10), (2, 20), (3, 30), (4, 40))
>>>>>>>>>>>>
Python Tuples
47. Often used in loops
>>>>>>>>>>>> pairs = ((1, 10), (2, 20), (3, 30), (4, 40))
>>>>>>>>>>>> for p in pairs:
........................ pprriinntt pp[[00]] ++ pp[[11]]
Python Tuples
48. Often used in loops
>>>>>>>>>>>> pairs = ((1, 10), (2, 20), (3, 30), (4, 40))
>>>>>>>>>>>> for p in pairs:
........................ pprriinntt pp[[00]] ++ pp[[11]]
Python Tuples
49. Often used in loops
>>>>>>>>>>>> pairs = ((1, 10), (2, 20), (3, 30), (4, 40))
>>>>>>>>>>>> for (low, high) in pairs:
........................ pprriinntt llooww ++ hhiigghh
Python Tuples
50. Often used in loops
>>>>>>>>>>>> pairs = ((1, 10), (2, 20), (3, 30), (4, 40))
>>>>>>>>>>>> for (low, high) in pairs:
........................ pprriinntt llooww ++ hhiigghh
............
11
22
33
44
>>>>>>>>>>>>
Python Tuples