11.1. While About

  • Iterate over sequences (iterables)

  • Repeat if until result is False

Example:

>>> while True:
...     print('hello')
hello
hello
hello
...

11.1.1. Problem

>>> data = ['Alice', 'Bob', 'Carol']
>>>
>>> i = 0
>>> until = len(data)
>>>
>>> if i < until:
...     print(f'Hello {data[i]}')
...     i += 1
Hello Alice
>>>
>>> if i < until:
...     print(f'Hello {data[i]}')
...     i += 1
Hello Bob
>>>
>>> if i < until:
...     print(f'Hello {data[i]}')
...     i += 1
Hello Carol

11.1.2. Solution

>>> data = ['Alice', 'Bob', 'Carol']
>>>
>>> i = 0              # start
>>> until = len(data)  # end
>>>
>>> while i < until:   # in progress
...     print(f'Hello {data[i]}')
...     i += 1
...
Hello Alice
Hello Bob
Hello Carol

11.1.3. Syntax

  • Continue execution when argument is True

  • Stops if argument is False

Generic syntax:

while <condition>:
    <do something>

Example:

>>>
... while True:
...     print('hello world')

11.1.4. Convention

  • The longer the loop scope, the longer the variable name should be

  • Avoid one letters if scope is longer than one line

  • Use i for loop counter (it is traditional name in almost all languages)

11.1.5. Assignments

# %% About
# - Name: While About Range
# - Difficulty: easy
# - Lines: 5
# - 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. Generate `result: list` with numbers from 0 to 5 (without 5)
# 2. Use `while`
# 3. Run doctests - all must succeed

# %% Polish
# 1. Wygeneruj `result: list` z liczbami od 0 do 5 (bez 5)
# 2. Użyj `while`
# 3. Uruchom doctesty - wszystkie muszą się powieść

# %% Example
# [0, 1, 2, 3, 4]

# %% Hints
# - `Stop` or `Ctrl+C` kills infinite loop
# - `+=` - increment add operator
# - `<` - less than operator
# - `while`
# - `list.append()`

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

>>> assert type(result) is list
>>> assert all(type(x) is int for x in result)

>>> result
[0, 1, 2, 3, 4]
"""

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

# %% Imports

# %% Types
result: list[int]

# %% Data

# %% Result
result = ...

# %% About
# - Name: While About Count
# - Difficulty: easy
# - Lines: 10
# - Minutes: 8

# %% 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. Count occurrences of each color in `DATA`
# 2. Użyj `while`
# 3. Do not use `list.count()`
# 4. Run doctests - all must succeed

# %% Polish
# 1. Zlicz wystąpienia każdego z kolorów w `DATA`
# 2. Użyj `while`
# 3. Nie używaj `list.count()`
# 4. Uruchom doctesty - wszystkie muszą się powieść

# %% Example
# red = 3
# green = 2
# blue = 2

# %% Hints
# - `Stop` or `Ctrl+C` kills infinite loop
# - `+=` - increment add operator
# - `<` - less than operator
# - `len()`
# - `while`
# - `if`

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

>>> assert red is not Ellipsis, \
'Assign your result to variable `red`'
>>> assert green is not Ellipsis, \
'Assign your result to variable `green`'
>>> assert blue is not Ellipsis, \
'Assign your result to variable `blue`'

>>> assert type(red) is int, \
'Variable `red` has invalid type, should be list'
>>> assert type(green) is int, \
'Variable `green` has invalid type, should be list'
>>> assert type(blue) is int, \
'Variable `blue` has invalid type, should be list'

>>> red
3
>>> green
2
>>> blue
2
"""

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

# %% Imports

# %% Types
red: int
green: int
blue: int

# %% Data
DATA = ['red', 'green', 'blue', 'red', 'green', 'red', 'blue']

# %% Result
red = 0
green = 0
blue = 0

# %% About
# - Name: While About To Float
# - Difficulty: easy
# - Lines: 5
# - Minutes: 5

# %% 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. Generate `result: list` with numbers from `DATA` converted to `float`
# 2. Use `while` and `float()`
# 3. Run doctests - all must succeed

# %% Polish
# 1. Wygeneruj `result: list` z liczbami z `DATA` przekonwertowanymi do `float`
# 2. Użyj `while` i `float()`
# 3. Uruchom doctesty - wszystkie muszą się powieść

# %% Example
# [1.0, 2.0, 3.0]

# %% Hints
# - `Stop` or `Ctrl+C` kills infinite loop
# - `+=` - increment add operator
# - `<` - less than operator
# - `len()`
# - `while`
# - `list[index]`
# - `float()`
# - `list.append()`

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

>>> type(result)
<class 'list'>

>>> assert all(type(x) is float for x in result)

>>> result
[1.0, 2.0, 3.0]
"""

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

# %% Imports

# %% Types
result: list[float]

# %% Data
DATA = (1, 2, 3)

# %% Result
result = ...

# %% About
# - Name: While About To Str
# - Difficulty: easy
# - Lines: 4
# - Minutes: 5

# %% 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 `result: str` with letters from `DATA` joined together
# 2. Use `while`
# 3. Do not use `str.join()`
# 4. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj `result: str` z literami z `DATA` złączonymi razem
# 2. Użyj `while`
# 3. Nie używaj `str.join()`
# 5. Uruchom doctesty - wszystkie muszą się powieść

# %% Example
# 'hello'

# %% Hints
# - `Stop` or `Ctrl+C` kills infinite loop
# - `+=` - increment add operator
# - `<` - less than operator
# - `while`
# - `len()`
# - `list[index]`

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

>>> type(result)
<class 'str'>
>>> result
'hello'
"""

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

# %% Imports

# %% Types
result: str

# %% Data
DATA = ['h', 'e', 'l', 'l', 'o']

# %% Result
result = ...

# %% About
# - Name: While About Remove PL Chars
# - Difficulty: medium
# - Lines: 5
# - Minutes: 5

# %% 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. Use `while` to iterate over `DATA`
# 2. If letter is in `PL` then use conversion value as letter
# 3. Add letter to `result`
# 4. Run doctests - all must succeed

# %% Polish
# 1. Użyj `while` do iteracji po `DATA`
# 2. Jeżeli litera jest w `PL` to użyj skonwertowanej wartości jako litera
# 3. Dodaj literę do `result`
# 4. Uruchom doctesty - wszystkie muszą się powieść

# %% Example
# 'zazolc gesla jazn'

# %% Hints
# - `Stop` or `Ctrl+C` kills infinite loop
# - `+=` - increment add operator
# - `<` - less than operator
# - `while`
# - `len()`
# - `list[index]`
# - `dict.get(key, default)`

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

>>> type(result)
<class 'str'>
>>> result
'zazolc gesla jazn'
"""

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

# %% Imports

# %% Types
result: str

# %% Data
PL = {
    'ą': 'a',
    'ć': 'c',
    'ę': 'e',
    'ł': 'l',
    'ń': 'n',
    'ó': 'o',
    'ś': 's',
    'ż': 'z',
    'ź': 'z',
}

DATA = 'zażółć gęślą jaźń'

# %% Result
result = ...