Exercises
Multiple Assignment
3 tasks. Write the code, press Check, and the page runs it against a real Python interpreter.
Exercise 1Passed
Swap the values of left and right in a single line, without a temporary variable.
Python
left = "A"
right = "B"
# swap them here
print(left, right)Put both names on the left of the = and both values on the right, in the other order.
left = "A"
right = "B"
left, right = right, left
print(left, right)Exercise 2Passed
Unpack the point tuple into x and y, then print their sum.
Python
point = (4, 9)
# unpack into x and y
print(x + y)Two names on the left, the tuple on the right.
point = (4, 9)
x, y = point
print(x + y)Exercise 3Passed
Use a starred name so first holds 10 and rest holds the other three numbers.
Python
numbers = [10, 20, 30, 40]
# assign first and rest
print(first)
print(rest)Put a * in front of the name that should collect what is left.
numbers = [10, 20, 30, 40]
first, *rest = numbers
print(first)
print(rest)