3.2. Syntax Assignment

  • Four types of assignments: Scalar, Unpacking, Chained, Chained Unpacking Assignment

  • a = 1 - Assignment

  • a, b = 1, 2 - Unpacking assignment

  • a = b = 1 - Chained assignment

  • a, b = c, d = 1, 2 - Chained unpacking assignment

  • Number of objects at both sides must be the same

  • Both left and right expression side brackets are optional

Assignment:

>>> name = 'Alice'

Unpacking assignment:

>>> firstname, lastname = 'Alice', 'Apricot'

Chained assignment:

>>> name1 = name2 = 'Alice'

Chained unpacking assignment:

>>> firstname1, lastname1 = firstname2, lastname2 = 'Alice', 'Apricot'

3.2.1. Chained Assignment

  • identifier1 = identifier2 = object

  • name1 = name2 = 'Alice'

  • name1 = name2 = name3 = name4 = 'Alice'

>>> name1 = name2 = 'Alice'
>>>
>>>
>>> name1
'Alice'
>>>
>>> name2
'Alice'
>>> name1 = name2 = name3 = name4 = 'Alice'
>>>
>>>
>>> name1
'Alice'
>>>
>>> name2
'Alice'
>>>
>>> name3
'Alice'
>>>
>>> name4
'Alice'

3.2.2. Chained Unpacking Assignment

  • iterable[identifier] = iterable[identifier] = iterable[object]

  • firstname1, lastname1 = firstname2, lastname2 = 'Alice', 'Apricot'

>>> firstname1, lastname1 = firstname2, lastname2 = 'Alice', 'Apricot'
>>>
>>>
>>> firstname1
'Alice'
>>>
>>> lastname1
'Apricot'
>>>
>>> firstname2
'Alice'
>>>
>>> lastname2
'Apricot'

3.2.3. Brackets

  • firstname, lastname = 'Alice', 'Apricot'

  • firstname, lastname = ('Alice', 'Apricot')

  • firstname, lastname = ['Alice', 'Apricot']

  • (firstname, lastname) = 'Alice', 'Apricot'

  • (firstname, lastname) = ('Alice', 'Apricot')

  • (firstname, lastname) = ['Alice', 'Apricot']

  • [firstname, lastname] = 'Alice', 'Apricot'

  • [firstname, lastname] = ('Alice', 'Apricot')

  • [firstname, lastname] = ['Alice', 'Apricot']

Brackets does not define tuple, commas do:

>>> firstname, lastname = 'Alice', 'Apricot'

Identifier brackets are optional:

>>> firstname, lastname = 'Alice', 'Apricot'
>>> firstname, lastname = ('Alice', 'Apricot')
>>> firstname, lastname = ['Alice', 'Apricot']

But they can be tuples:

>>> (firstname, lastname) = 'Alice', 'Apricot'
>>> (firstname, lastname) = ('Alice', 'Apricot')
>>> (firstname, lastname) = ['Alice', 'Apricot']

Or lists:

>>> [firstname, lastname] = 'Alice', 'Apricot'
>>> [firstname, lastname] = ('Alice', 'Apricot')
>>> [firstname, lastname] = ['Alice', 'Apricot']

3.2.4. Nested

  • firstname, lastname, (email_work, email_school) = 'Alice', 'Apricot', ('alice@example.com', 'alice@example.edu')

>>> firstname, lastname, (email_work, email_school) = 'Alice', 'Apricot', ('alice@example.com', 'alice@example.edu')
>>>
>>>
>>> firstname
'Alice'
>>>
>>> lastname
'Apricot'
>>>
>>> email_work
'alice@example.com'
>>>
>>> email_school
'alice@example.edu'

3.2.5. Recap

  • Four types of assignments: Scalar, Unpacking, Chained, Chained Unpacking Assignment

  • a = 1 - Assignment

  • a, b = 1, 2 - Unpacking assignment

  • a = b = 1 - Chained assignment

  • a, b = c, d = 1, 2 - Chained unpacking assignment

  • Number of objects at both sides must be the same

  • Both left and right expression side brackets are optional

Assignment:

>>> name = 'Alice'

Unpacking assignment:

>>> firstname, lastname = 'Alice', 'Apricot'

Chained assignment:

>>> name1 = name2 = 'Alice'

Chained unpacking assignment:

>>> firstname1, lastname1 = firstname2, lastname2 = 'Alice', 'Apricot'

Unpacking nested:

>>> a, (b, c) = 1, (2, 3)

3.2.6. Assignments

# %% About
# - Name: Unpack Assignment List
# - Difficulty: easy
# - Lines: 1
# - Minutes: 1

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% English
# 1. Separate firstname, lastname and age from variable `DATA`
# 2. Define variable `firstname` with firstname
# 3. Define variable `lastname` with lastname
# 4. Define variable `age` with age
# 5. Use unpack assignment
# 6. Run doctests - all must succeed

# %% Polish
# 1. Odseparuj imię, nazwisko i wiek ze zmiennej `DATA`
# 2. Zdefiniuj zmienną `firstname` z imieniem
# 3. Zdefiniuj zmienną `lastname` z nazwiskiem
# 4. Zdefiniuj zmienną `age` z wiekiem
# 5. Użyj przypisania z rozpakowaniem (unpack assignment)
# 6. Uruchom doctesty - wszystkie muszą się powieść

# %% Expected
# >>> firstname
# 'Alice'
#
# >>> lastname
# 'Apricot'
#
# >>> age
# 30

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0

>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'

>>> assert firstname is not Ellipsis, \
'Variable `firstname` has an invalid value; assign result of your program to it.'

>>> assert lastname is not Ellipsis, \
'Variable `lastname` has an invalid value; assign result of your program to it.'

>>> assert age is not Ellipsis, \
'Variable `age` has an invalid value; assign result of your program to it.'

>>> assert type(firstname) is str, \
'Variable `firstname` has an invalid type; expected: `str`.'

>>> assert type(lastname) is str, \
'Variable `lastname` has an invalid type; expected: `str`.'

>>> assert type(age) is int, \
'Variable `age` has an invalid type; expected: `int`.'

>>> result = open(__file__).read()
>>> assert 'firstname, lastname, age' + ' = ' + 'DATA' in result, \
'Use unpack assignment'

>>> firstname
'Alice'

>>> lastname
'Apricot'

>>> age
30
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`

# %% Imports

# %% Types
firstname: str
lastname: str
age: int

# %% Data
DATA = ['Alice', 'Apricot', 30]

# %% Result

# %% About
# - Name: Unpack Assignment List
# - Difficulty: easy
# - Lines: 1
# - Minutes: 1

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% English
# 1. Separate firstname, lastname and age from variable `DATA`
# 2. Define variable `firstname` with firstname
# 3. Define variable `lastname` with lastname
# 4. Define variable `age` with age
# 5. Use unpack assignment
# 6. Run doctests - all must succeed

# %% Polish
# 1. Odseparuj imię, nazwisko i wiek ze zmiennej `DATA`
# 2. Zdefiniuj zmienną `firstname` z imieniem
# 3. Zdefiniuj zmienną `lastname` z nazwiskiem
# 4. Zdefiniuj zmienną `age` z wiekiem
# 5. Użyj przypisania z rozpakowaniem (unpack assignment)
# 6. Uruchom doctesty - wszystkie muszą się powieść

# %% Expected
# >>> firstname
# 'Alice'
#
# >>> lastname
# 'Apricot'
#
# >>> age
# '30'

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0

>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'

>>> assert firstname is not Ellipsis, \
'Variable `firstname` has an invalid value; assign result of your program to it.'

>>> assert lastname is not Ellipsis, \
'Variable `lastname` has an invalid value; assign result of your program to it.'

>>> assert age is not Ellipsis, \
'Variable `age` has an invalid value; assign result of your program to it.'

>>> assert type(firstname) is str, \
'Variable `firstname` has an invalid type; expected: `str`.'

>>> assert type(lastname) is str, \
'Variable `lastname` has an invalid type; expected: `str`.'

>>> assert type(age) is str, \
'Variable `age` has an invalid type; expected: `str`.'

>>> result = open(__file__).read()
>>> assert 'firstname, lastname, age' + ' = ' + 'DATA' in result, \
'Use unpack assignment'

>>> firstname
'Alice'

>>> lastname
'Apricot'

>>> age
'30'
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`

# %% Imports

# %% Types
firstname: str
lastname: str
age: str

# %% Data
DATA = 'Alice,Apricot,30'

# %% Result