4.1. Enum About

  • List of finite choices

  • Enumerations

4.1.1. SetUp

>>> from enum import Enum

4.1.2. Syntax

>>> class Color(Enum):
...     RED = 'r'
...     GREEN = 'g'
...     BLUE = 'b'

4.1.3. Get Name, Value

>>> mycolor = Color('g')
>>>
>>> mycolor
<Color.GREEN: 'g'>
>>>
>>> mycolor.name
'GREEN'
>>>
>>> mycolor.value
'g'

4.1.4. Comparison

>>> mycolor = Color('g')
>>>
>>> mycolor is Color.RED
False
>>>
>>> mycolor is Color.GREEN
True

4.1.5. Iteration

>>> for color in Color:
...     print(color)
Color.RED
Color.GREEN
Color.BLUE

4.1.6. Methods

>>> class Color(Enum):
...     RED = 'r'
...     GREEN = 'g'
...     BLUE = 'b'
...
...     @classmethod
...     def get_favourite(cls):
...         return cls.RED
>>> Color.get_favourite()
<Color.RED: 'r'>

4.1.7. Enum vs. Dict

Enum:

>>> class Color(Enum):
...     RED = 'r'
...     GREEN = 'g'
...     BLUE = 'b'
...
>>>
>>> Color.RED
<Color.RED: 'r'>
>>>
>>> Color('r')
<Color.RED: 'r'>

Dict:

>>> color = {
...     'RED': '#FF0000',
...     'GREEN': '#00FF00',
...     'BLUE': '#0000FF',
... }
>>>
>>> color['RED']
'#FF0000'
>>>
>>> color['#FF0000']
Traceback (most recent call last):
KeyError: '#FF0000'
>>>
>>> tmp = {v:k for k,v in color.items()}
>>> tmp['#FF0000']
'RED'

4.1.8. Use Case - 1

  • HTML Colors

>>> class Color(Enum):
...     AQUA = '#00FFFF'
...     BLACK = '#000000'
...     BLUE = '#0000ff'
...     FUCHSIA = '#FF00FF'
...     GRAY = '#808080'
...     GREEN = '#008000'
...     LIME = '#00ff00'
...     MAROON = '#800000'
...     NAVY = '#000080'
...     OLIVE = '#808000'
...     PINK = '#ff1a8c'
...     PURPLE = '#800080'
...     RED = '#ff0000'
...     SILVER = '#C0C0C0'
...     TEAL = '#008080'
...     WHITE = '#ffffff'
...     YELLOW = '#FFFF00'

4.1.9. Assignments

# %% About
# - Name: Enum About Color
# - Difficulty: easy
# - Lines: 4
# - 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. Define enum `Color`:
# - name: RED, value: '#FF0000'
# - name: GREEN, value: '#00FF00'
# - name: BLUE, value: '#0000FF'
# 2. Use `Enum`
# 3. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj enum `Color`:
# - nazwa: RED, wartość: '#FF0000'
# - nazwa: GREEN, wartość: '#00FF00'
# - nazwa: BLUE, wartość: '#0000FF'
# 2. Użyj `Enum`
# 3. Uruchom doctesty - wszystkie muszą się powieść

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> assert Enum in Color.mro(), \
'Class `Color` has an invalid type; expected: `Enum`.'

>>> assert len(Color) == 3, \
'Variable `Color` has an invalid length; expected: `3`.'

>>> assert hasattr(Color, 'RED')
>>> assert hasattr(Color, 'GREEN')
>>> assert hasattr(Color, 'BLUE')

>>> assert Color.RED.value == '#FF0000'

>>> assert Color.GREEN.value == '#00FF00'

>>> assert Color.BLUE.value == '#0000FF'
"""

# %% 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
from enum import Enum

# %% Types
Color: type[Enum]

# %% Data

# %% Result

# %% About
# - Name: Enum About Lookup
# - Difficulty: easy
# - Lines: 2
# - 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. Define variable `result` with an enum object for color `#FF0000`
# 2. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj zmienną `result` z obiektem enum dla koloru `#FF0000`
# 2. Uruchom doctesty - wszystkie muszą się powieść

# %% Example
# >>> result
# <Color.RED: '#FF0000'>

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> assert Enum in Color.mro(), \
'Class `Color` has an invalid type; expected: `Enum`.'

>>> assert len(Color) == 3, \
'Variable `Color` has an invalid length; expected: `3`.'

>>> assert hasattr(Color, 'RED')
>>> assert hasattr(Color, 'GREEN')
>>> assert hasattr(Color, 'BLUE')

>>> assert Color.RED.value == '#FF0000'
>>> assert Color.GREEN.value == '#00FF00'
>>> assert Color.BLUE.value == '#0000FF'

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

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

>>> result
<Color.RED: '#FF0000'>
"""

# %% 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
from enum import Enum

# %% Types
result: list[Color]

# %% Data
class Color(Enum):
    RED = '#FF0000'
    GREEN = '#00FF00'
    BLUE = '#0000FF'


# %% Result
result = ...

# %% About
# - Name: Enum About Is
# - 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. Check if `DATA` is `Color.RED`
# 2. Define variable `result` with the result
# 3. Run doctests - all must succeed

# %% Polish
# 1. Sprawdź czy `DATA` jest `Color.RED`
# 2. Zdefiniuj zmienną `result` z wynikiem
# 3. Uruchom doctesty - wszystkie muszą się powieść

# %% Example
# >>> result
# True

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> assert Enum in Color.mro(), \
'Class `Color` has an invalid type; expected: `Enum`.'

>>> assert len(Color) == 3, \
'Variable `Color` has an invalid length; expected: `3`.'

>>> assert hasattr(Color, 'RED')
>>> assert hasattr(Color, 'GREEN')
>>> assert hasattr(Color, 'BLUE')

>>> assert Color.RED.value == '#FF0000'
>>> assert Color.GREEN.value == '#00FF00'
>>> assert Color.BLUE.value == '#0000FF'

>>> assert isinstance(result, bool), \
'Variable `result` has invalid type, must be a bool'

>>> result
True
"""

# %% 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
from enum import Enum

# %% Types
result: bool

# %% Data
class Color(Enum):
    RED = '#FF0000'
    GREEN = '#00FF00'
    BLUE = '#0000FF'


DATA = Color('#FF0000')

# %% Result
result = ...

# %% About
# - Name: Enum About Name and Value
# - Difficulty: easy
# - Lines: 2
# - 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. Define variable `result_a` with a name of the color in `DATA`
# 2. Define variable `result_b` with a value of the color in `DATA`
# 3. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj zmienną `result_a` z nazwą koloru w `DATA`
# 2. Zdefiniuj zmienną `result_b` z wartością koloru w `DATA`
# 3. Uruchom doctesty - wszystkie muszą się powieść

# %% Example
# >>> result_a
# 'RED'
#
# >>> result_b
# '#FF0000'

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> assert Enum in Color.mro(), \
'Class `Color` has an invalid type; expected: `Enum`.'

>>> assert len(Color) == 3, \
'Variable `Color` has an invalid length; expected: `3`.'

>>> assert hasattr(Color, 'RED')
>>> assert hasattr(Color, 'GREEN')
>>> assert hasattr(Color, 'BLUE')

>>> assert Color.RED.value == '#FF0000'
>>> assert Color.GREEN.value == '#00FF00'
>>> assert Color.BLUE.value == '#0000FF'

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

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

>>> result_a
'RED'

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

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

>>> result_b
'#FF0000'
"""

# %% 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
from enum import Enum

# %% Types
result_a: str
result_b: str

# %% Data
class Color(Enum):
    RED = '#FF0000'
    GREEN = '#00FF00'
    BLUE = '#0000FF'


DATA = Color('#FF0000')

# %% Result
result_a = ...
result_b = ...

# %% About
# - Name: Enum About List
# - Difficulty: easy
# - Lines: 2
# - 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. Define variable `result` with a list of all names in `Color`
# 2. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj zmienną `result` z listą wszystkich nazw w `Color`
# 2. Uruchom doctesty - wszystkie muszą się powieść

# %% Example
# >>> result
# [<Color.RED: '#FF0000'>, <Color.GREEN: '#00FF00'>, <Color.BLUE: '#0000FF'>]

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> assert Enum in Color.mro(), \
'Class `Color` has an invalid type; expected: `Enum`.'

>>> assert len(Color) == 3, \
'Variable `Color` has an invalid length; expected: `3`.'

>>> assert hasattr(Color, 'RED')
>>> assert hasattr(Color, 'GREEN')
>>> assert hasattr(Color, 'BLUE')

>>> assert Color.RED.value == '#FF0000'
>>> assert Color.GREEN.value == '#00FF00'
>>> assert Color.BLUE.value == '#0000FF'

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

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

>>> result
[<Color.RED: '#FF0000'>, <Color.GREEN: '#00FF00'>, <Color.BLUE: '#0000FF'>]
"""

# %% 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
from enum import Enum

# %% Types
result: list[Enum]

# %% Data
class Color(Enum):
    RED = '#FF0000'
    GREEN = '#00FF00'
    BLUE = '#0000FF'


# %% Result
result = ...