bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/Python/Foundations
Python•Foundations

Python Variables - Assign Multiple Values

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind Python Variables - Assign Multiple Values?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

x, y, z = "___", "Banana", "Cherry"
3Order

Put the learning moves in the order that makes the concept easiest to apply.

Unpack a Collection
One Value to Multiple Variables
Many Values to Multiple Variables

Many Values to Multiple Variables

Python allows you to assign values to multiple variables in one line:

Example

x, y, z = "Orange", "Banana", "Cherry"
print(x)
print(y)
print(z)

Note

Make sure the number of variables matches the number of values, or else you will get an error.

One Value to Multiple Variables

And you can assign the same value to multiple variables in one line:

Example

x = y = z = "Orange"
print(x)
print(y)
print(z)

Unpack a Collection

If you have a collection of values in a list , tuple etc. Python allows you to extract the values into variables. This is called unpacking .

Example

fruits = ["apple", "banana", "cherry"]

x, y, z = fruits

print(x)

print(y)

print(z)

Learn more about unpacking in our Unpack Tuples Chapter.

Previous

Python - Variable Names

Next

Python - Output Variables