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 Enum Color
# - Difficulty: easy
# - Lines: 4
# - Minutes: 3

# %% 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(), \
'Color must be an Enum'

>>> assert len(Color) == 3, \
'Color must have 3 elements'

>>> 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 Enum Currency
# - Difficulty: easy
# - Lines: 4
# - Minutes: 3

# %% 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 `Currency`:
# - name: PLN, value: 1.00
# - name: EUR, value: 4.28
# - name: USD, value: 3.70
# 2. Use `Enum`
# 3. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj enum `Currency`:
# - nazwa: PLN, wartość: 1.00
# - nazwa: EUR, wartość: 4.28
# - nazwa: USD, wartość: 3.70
# 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 Currency.mro(), \
'Currency must be an Enum'

>>> assert len(Currency) == 3, \
'Currency must have 3 elements'

>>> assert hasattr(Currency, 'PLN')
>>> assert hasattr(Currency, 'EUR')
>>> assert hasattr(Currency, 'USD')

>>> assert Currency.PLN.value == 1.00
>>> assert Currency.EUR.value == 4.28
>>> assert Currency.USD.value == 3.70
"""

# %% 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
Currency: type[Enum]

# %% Data

# %% Result

# %% About
# - Name: Enum Enum Language
# - Difficulty: easy
# - Lines: 4
# - Minutes: 3

# %% 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 `Language`:
# - name: PL, value: 'polish'
# - name: EN, value: 'english'
# - name: DE, value: 'german'
# 2. Use `Enum`
# 3. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj enum `Language`:
# - nazwa: PL, wartość: 'polish'
# - nazwa: EN, wartość: 'english'
# - nazwa: DE, wartość: 'german'
# 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 Language.mro(), \
'Language must be an Enum'

>>> assert len(Language) == 3, \
'Language must have 3 elements'

>>> assert hasattr(Language, 'PL')
>>> assert hasattr(Language, 'EN')
>>> assert hasattr(Language, 'DE')

>>> assert Language.PL.value == 'polish'

>>> assert Language.EN.value == 'english'

>>> assert Language.DE.value == 'german'
"""

# %% 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
Language: type[Enum]

# %% Data

# %% Result

# %% About
# - Name: Enum Enum HttpStatus
# - Difficulty: easy
# - Lines: 4
# - Minutes: 3

# %% 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 `HttpStatus`:
# - name: OK, value: 200
# - name: NOT_FOUND, value: 404
# - name: BAD_REQUEST, value: 500
# 2. Use `Enum`
# 3. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj enum `HttpStatus`:
# - nazwa: OK, wartość: 200
# - nazwa: NOT_FOUND, wartość: 404
# - nazwa: BAD_REQUEST, wartość: 500
# 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 HttpStatus.mro(), \
'HttpStatus must be an Enum'

>>> assert len(HttpStatus) == 3, \
'HttpStatus must have 3 elements'

>>> assert hasattr(HttpStatus, 'OK')
>>> assert hasattr(HttpStatus, 'NOT_FOUND')
>>> assert hasattr(HttpStatus, 'BAD_REQUEST')

>>> assert HttpStatus.OK.value == 200
>>> assert HttpStatus.NOT_FOUND.value == 404
>>> assert HttpStatus.BAD_REQUEST.value == 500
"""

# %% 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
HttpStatus: type[Enum]

# %% Data

# %% Result