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?
Latihan ini adalah bagian dari kursus
Introduction to Functions in Python
Petunjuk latihan
- 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.
Latihan interaktif praktis
Cobalah latihan ini dengan menyelesaikan kode contoh berikut.
# Unpack nums into num1, num2, and num3
# Construct even_nums