Summary - Variables
- Python course
- Beginner
- Lesson 12/116
- 5 min
Combine multiple assignment and swapping to write a math helper.
Summary - Variables
In this group you learned assignment, multiple assignment, naming, id/type and swap.
- We assign values with
=. a, b = 1, 2is multiple assignment.- A variable name starts with a letter; snake_case is the convention.
id()gives identity,type()gives the type.a, b = b, aperforms a swap.
Now two tasks combine these concepts.
Practice Variables and swap
Assign x = 10 and y = 20. Swap them with x, y = y, x. Then print print(x, y).
Starter code
# Define x and ySample solution
x = 10
y = 20
x, y = y, x
print(x, y)Expected output
20 10
Practice Print the type check
Assign value = 42. Then print its type with print(type(value)).
Starter code
# Define the valueSample solution
value = 42
print(type(value))Expected output
<class 'int'>
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which is wrong?
- a, b = b, a raises an error Correct answer
- 2age is invalid
- type(5) returns int
- age = 25 is valid
Explanation: a, b = b, a is a valid swap in Python.
2 Complete the multiple assignment
a, b, c = ____
print(a, b, c)
a, b, c = 1, 2, 3
print(a, b, c)
Explanation: a, b, c = 1, 2, 3 is a multiple assignment.