CodeBlanks Logo Icon CodeBlanks

Summary - Variables

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, 2 is multiple assignment.
  • A variable name starts with a letter; snake_case is the convention.
  • id() gives identity, type() gives the type.
  • a, b = b, a performs 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

main.py
# Define x and y

Sample solution

main.py
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

main.py
# Define the value

Sample solution

main.py
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

Question
a, b, c = ____
print(a, b, c)
Answer
a, b, c = 1, 2, 3
print(a, b, c)

Explanation: a, b, c = 1, 2, 3 is a multiple assignment.

All lessons in Beginner