2.1. Syntax Underscore

  • _ is used to skip values

  • It is a regular variable name, not a special Python syntax

  • By convention it is used for data we don't want to access in future

  • It can be used multiple times in the same statement

>>> user = ('Alice', 'Apricot', 30)
>>> firstname, lastname, age = ('Alice', 'Apricot', 30)

2.1.1. Syntax

  • Underscore (_) is a regular variable name

  • It's not a special Python syntax

  • By convention it is used for data we don't want to access in future

>>> _ = 'Alice'
>>> print(_)
Alice

2.1.2. Single Underscore

>>> firstname, lastname, _ = ('Alice', 'Apricot', 30)

2.1.3. Multiple Underscores

>>> _, _, age = ('Alice', 'Apricot', 30)

2.1.4. For Loop

>>> DATA = [
...     ('firstname', 'lastname', 'age'),
...     ('Alice', 'Apricot', 30),
...     ('Bob', 'Blackthorn', 31),
...     ('Carol', 'Corn', 32),
...     ('Dave', 'Durian', 33),
...     ('Eve', 'Elderberry', 34),
...     ('Mallory', 'Melon', 15),
... ]
>>> for firstname, lastname, age in DATA[1:]:
...     print(firstname, lastname)
...
Alice Apricot
Bob Blackthorn
Carol Corn
Dave Durian
Eve Elderberry
Mallory Melon
>>> for firstname, lastname, _ in DATA[1:]:
...     print(firstname, lastname)
...
Alice Apricot
Bob Blackthorn
Carol Corn
Dave Durian
Eve Elderberry
Mallory Melon
>>> for _, _, age in DATA[1:]:
...     print(age)
...
30
31
32
33
34
15

2.1.5. Recap

  • _ is used to skip values

  • It is a regular variable name, not a special Python syntax

  • By convention it is used for data we don't want to access in future

  • It can be used multiple times in the same statement

2.1.6. Use Case - 1

>>> line = 'alice:x:1000:1000:Alice:/home/alice:/bin/bash'
>>> username, _, uid, _, _, home, _ = line.split(':')
>>>
>>> print(f'{username=}, {uid=}, {home=}')
username='alice', uid='1000', home='/home/alice'

2.1.7. Use Case - 2

  • Skip

>>> a, b, _ = 'red', 'green', 'blue'
>>> a, _, _ = 'red', 'green', 'blue'
>>> a, _, c = 'red', 'green', 'blue'
>>> _, b, _ = 'red', 'green', 'blue'
>>> _, _, c = 'red', 'green', 'blue'

2.1.8. Use Case - 3

>>> _, important, _ = 1, 2, 3
>>>
>>> print(important)
2
>>> _, (important, _) = [1, (2, 3)]
>>>
>>> print(important)
2
>>> _, _, important = (True, [1, 2, 3, 4], 5)
>>>
>>> print(important)
5
>>> _, _,  important = (True, [1, 2, 3, 4], (5, True))
>>>
>>> print(important)
(5, True)
>>>
>>> _, _, (important, _) = (True, [1, 2, 3, 4], (5, True))
>>>
>>> print(important)
5

Python understands this as:

>>> _ = (True, [1, 2, 3, 4], (5, True))
>>>
>>> a,b,c = (object, object, object)
>>> a,b,(c,d) = (object, object, (object,object))

2.1.9. Assignments

# %% About
# - Name: Syntax Underscore One
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2

# %% 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. Unpack 3 variables from tuple `data`
# 2. Assign first to `firstname`, second to `lastname`, third to `_`
# 3. Run doctests - all must succeed

# %% Polish
# 1. Rozpakuj 3 zmienne z krotki `data`
# 2. Pierwszą przypisz do `firstname`, drugą do `lastname`, trzecią do `_`
# 3. Uruchom doctesty - wszystkie muszą się powieść

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

# %% 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 type(firstname) is str, \
'Variable `firstname` has an invalid type; expected: `str`.'

>>> firstname
'Alice'

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

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

>>> lastname
'Apricot'

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

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

>>> _
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
_: int

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

# %% Result

# %% About
# - Name: Syntax Underscore Many
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2

# %% 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. Unpack 3 variables from tuple `data`
# 2. Assign first to `_`, second to `_`, third to `age`
# 3. Run doctests - all must succeed

# %% Polish
# 1. Rozpakuj 3 zmienne z krotki `data`
# 2. Pierwszą przypisz do `_`, drugą do `_`, trzecią do `age`
# 3. Uruchom doctesty - wszystkie muszą się powieść

# %% Expected
# >>> 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 age is not Ellipsis, \
'Variable `age` has an invalid value; assign result of your program to it.'

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

>>> age
30

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

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

>>> _
'Apricot'
"""

# %% 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
_: str
age: int

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

# %% Result