A brief introduction to tuples
Alongside learning about functions, you've also learned about tuples! Here, you will practice what you've learned about tuples: how to construct, unpack, and access tuple elements. Recall how Hugo unpacked the tuple even_nums in the video:
a, b, c = even_nums
A three-element tuple named nums has been preloaded for this exercise. Before completing the script, perform the following:
- Print out the value of
numsin the IPython shell. Note the elements in the tuple. - In the IPython shell, try to change the first element of
numsto the value 2 by doing an assignment:nums[0] = 2. What happens?
This exercise is part of the course
Introduction to Functions in Python
Exercise instructions
- Unpack
numsto the variablesnum1,num2, andnum3. - Construct a new tuple,
even_numscomposed of the same elements innums, but with the 1st element replaced with the value, 2.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Unpack nums into num1, num2, and num3
# Construct even_nums