• Home
  • 2.10 Multiple Assignment

2.10 Multiple Assignment

View Categories

2.10 Multiple Assignment

4 min read

Python allows multiple variables to be assigned values in a single statement. This feature is known as multiple assignment.

Multiple assignment makes programs shorter, cleaner, and easier to read.

Some common uses are:

  • Initializing multiple variables
  • Assigning different values at once
  • Swapping variable values
  • Unpacking collections such as lists and tuples

Syntax 1: Assigning Multiple Values #

variable1, variable2, variable3 = value1, value2, value3

Components #

Component Description
variable1, variable2, ... Variables that receive values.
= Assignment operator.
value1, value2, ... Values assigned to the corresponding variables.

Syntax 2: Assigning the Same Value #

variable1 = variable2 = variable3 = value

Components #

Component Description
variable1 = variable2 = variable3 Variables that receive the same value.
value Value assigned to every variable.

Example 1: Assigning Multiple Values #

main.py

name, age, city = "Alice", 22, "New York"

print(name)
print(age)
print(city)

Output #

Alice
22
New York

Explanation #

  • Three variables are created in a single statement.
  • Each variable receives the corresponding value.
  • The order of variables and values must match.

Example 2: Assigning the Same Value to Multiple Variables #

main.py

x = y = z = 100

print(x)
print(y)
print(z)

Output #

100
100
100

Explanation #

  • The value 100 is assigned to z.
  • The same value is then assigned to y and x.
  • All three variables store the same value.

Example 3: Swapping Two Variables #

main.py

a = 10
b = 20

a, b = b, a

print(a)
print(b)

Output #

20
10

Explanation #

  • Initially, a stores 10 and b stores 20.
  • The statement a, b = b, a swaps the values.
  • Python performs the swap without using a temporary variable.

Example 4: Unpacking a List #

main.py

numbers = [10, 20, 30]

a, b, c = numbers

print(a)
print(b)
print(c)

Output #

10
20
30

Explanation #

  • numbers is a list containing three elements.
  • Each element is assigned to one variable.
  • This process is called unpacking.

Example 5: Unpacking a Tuple #

main.py

student = ("Alice", 21)

name, age = student

print(name)
print(age)

Output #

Alice
21

Explanation #

  • The tuple contains two values.
  • Python assigns each value to the corresponding variable.
  • The number of variables matches the number of tuple elements.

Example 6: Using the Star (*) Operator #

main.py

numbers = [10, 20, 30, 40, 50]

first, *middle, last = numbers

print(first)
print(middle)
print(last)

Output #

10
[20, 30, 40]
50

Explanation #

  • first receives the first element.
  • last receives the last element.
  • *middle collects all remaining elements into a list.
  • The starred variable can capture multiple values.

Example 7: Ignoring Unwanted Values #

main.py

student = ("Alice", 21, "New York")

name, _, city = student

print(name)
print(city)

Output #

Alice
New York

Explanation #

  • The second value is assigned to _.
  • By convention, _ indicates that the value is intentionally ignored.
  • This improves readability when some values are not needed.

Example 8: Multiple Assignment with Expressions #

main.py

a = 10
b = 20

sum_value, product = a + b, a * b

print(sum_value)
print(product)

Output #

30
200

Explanation #

  • Expressions are evaluated first.
  • Their results are assigned to sum_value and product.
  • Multiple assignment works with expressions as well as literal values.

Example 9: Reading Multiple Values #

main.py

a, b = input("Enter two numbers: ").split()

print(a)
print(b)

Sample Input #

Enter two numbers: 10 20

Output #

10
20

Explanation #

  • input() reads an entire line as a string.
  • split() separates the input into individual strings.
  • Each string is assigned to one variable.

Example 10: Reading Multiple Integers #

main.py

a, b = map(int, input("Enter two numbers: ").split())

print(a + b)

Sample Input #

Enter two numbers: 15 25

Output #

40

Explanation #

  • split() divides the input into separate strings.
  • map(int, ...) converts each string into an integer.
  • The converted integers are assigned to a and b.
  • Their sum is then displayed.

Common Mistakes #

1. Mismatch Between Variables and Values #

Incorrect

a, b = 10, 20, 30

Output #

ValueError: too many values to unpack (expected 2)

Reason

The number of variables must match the number of values unless a starred (*) variable is used.

Correct

a, b, c = 10, 20, 30

2. Forgetting the Comma #

Incorrect

a b = 10, 20

Output #

SyntaxError: invalid syntax

Reason

Variables in multiple assignment must be separated by commas.


3. Assuming Swapping Requires a Temporary Variable #

Poor Practice

temp = a
a = b
b = temp

Better Practice

a, b = b, a

Reason

Python supports direct swapping without a temporary variable.


4. Forgetting split() When Reading Multiple Inputs #

Incorrect

a, b = input("Enter two numbers: ")

Sample Input #

10 20

Output #

ValueError: too many values to unpack

Reason

input() returns a single string. Use split() to separate the values.


Best Practices #

  • Use multiple assignment to write cleaner and shorter code.
  • Use direct variable swapping instead of a temporary variable.
  • Ensure the number of variables matches the number of assigned values.
  • Use meaningful variable names.
  • Use _ to ignore values that are not needed.

Key Points to Remember #

  • Multiple assignment allows several variables to be assigned in one statement.
  • Multiple variables can receive different values or the same value.
  • Python supports swapping variables without a temporary variable.
  • Lists and tuples can be unpacked into variables.
  • The * operator can collect multiple values during unpacking.
  • The number of variables and values must match unless a starred variable is used.
  • Multiple assignment makes Python code more concise and readable.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *