Exercises
Tuples
3 tasks. Write the code, press Check, and the page runs it against a real Python interpreter.
Exercise 1Passed
Make a tuple holding only the number 5, and print its type.
Python
one = (5)
print(type(one))It is the comma that makes a tuple.
one = (5,)
print(type(one))Exercise 2Passed
Return both the smallest and largest number from the function, and unpack them.
Python
def min_max(numbers):
# return both
pass
low, high = min_max([3, 1, 4])
print(low, high)Returning two values separated by a comma gives a tuple.
def min_max(numbers):
return min(numbers), max(numbers)
low, high = min_max([3, 1, 4])
print(low, high)Exercise 3Passed
Use a tuple as a dictionary key and look one up.
Python
locations = {}
# map (0, 0) to "origin", then print the lookup
Tuples hash, so they can be keys.
locations = {(0, 0): "origin"}
print(locations[(0, 0)])