9.5. Idiom All
Return True if all elements of the iterable are true
If the iterable is empty, return True
Built-in
9.5.1. Problem
>>> data = [True, False, True]
>>>
>>> result = True
>>> for x in data:
... if x is False:
... result = False
>>>
>>> print(result)
False
9.5.2. Solution
>>> data = [True, False, True]
>>> result = all(data)
>>>
>>> print(result)
False
9.5.3. Implementation
>>> def all(iterable):
... for element in iterable:
... if not element:
... return False
... return True
9.5.4. Performance 1
Date: 2025-02-12
Python: 3.13.2
IPython: 8.32.0
System: macOS 15.3
Computer: MacBook M3 Max
CPU: 16 cores (12 performance and 4 efficiency) / 3nm
RAM: 128 GB RAM LPDDR5
IRIS = [
('sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'species'),
(5.8, 2.7, 5.1, 1.9, 'virginica'),
(5.1, 3.5, 1.4, 0.2, 'setosa'),
(5.7, 2.8, 4.1, 1.3, 'versicolor'),
(6.3, 2.9, 5.6, 1.8, 'virginica'),
(6.4, 3.2, 4.5, 1.5, 'versicolor'),
(4.7, 3.2, 1.3, 0.2, 'setosa'),
(7.0, 3.2, 4.7, 1.4, 'versicolor'),
(7.6, 3.0, 6.6, 2.1, 'virginica'),
(4.6, 3.1, 1.5, 0.2, 'setosa'),
]
header, *rows = IRIS
# %%timeit -n 1000 -r 1000
answers = []
for *values, species in rows:
for value in values:
answers.append(value >= 1.0)
result = all(answers)
# 1.21 μs ± 127 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
# 1.22 μs ± 115 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
# 1.22 μs ± 119 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
# 1.22 μs ± 124 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
# 1.22 μs ± 124 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
IRIS = [
('sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'species'),
(5.8, 2.7, 5.1, 1.9, 'virginica'),
(5.1, 3.5, 1.4, 0.2, 'setosa'),
(5.7, 2.8, 4.1, 1.3, 'versicolor'),
(6.3, 2.9, 5.6, 1.8, 'virginica'),
(6.4, 3.2, 4.5, 1.5, 'versicolor'),
(4.7, 3.2, 1.3, 0.2, 'setosa'),
(7.0, 3.2, 4.7, 1.4, 'versicolor'),
(7.6, 3.0, 6.6, 2.1, 'virginica'),
(4.6, 3.1, 1.5, 0.2, 'setosa'),
]
header, *rows = IRIS
# %%timeit -n 1000 -r 1000
result = True
for *values, species in rows:
for value in values:
if value < 1.0:
result = False
# 1.18 μs ± 102 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
# 1.18 μs ± 112 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
# 1.18 μs ± 114 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
# 1.18 μs ± 123 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
# 1.18 μs ± 91.4 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
IRIS = [
('sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'species'),
(5.8, 2.7, 5.1, 1.9, 'virginica'),
(5.1, 3.5, 1.4, 0.2, 'setosa'),
(5.7, 2.8, 4.1, 1.3, 'versicolor'),
(6.3, 2.9, 5.6, 1.8, 'virginica'),
(6.4, 3.2, 4.5, 1.5, 'versicolor'),
(4.7, 3.2, 1.3, 0.2, 'setosa'),
(7.0, 3.2, 4.7, 1.4, 'versicolor'),
(7.6, 3.0, 6.6, 2.1, 'virginica'),
(4.6, 3.1, 1.5, 0.2, 'setosa'),
]
header, *rows = IRIS
# %%timeit -n 1000 -r 1000
result = True
for *values, species in rows:
for value in values:
if value < 1.0:
result = False
break
# 1.20 μs ± 40.3 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
# 1.20 μs ± 46.4 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
# 1.20 μs ± 58.4 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
# 1.21 μs ± 47.5 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
# 1.21 μs ± 57.5 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
IRIS = [
('sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'species'),
(5.8, 2.7, 5.1, 1.9, 'virginica'),
(5.1, 3.5, 1.4, 0.2, 'setosa'),
(5.7, 2.8, 4.1, 1.3, 'versicolor'),
(6.3, 2.9, 5.6, 1.8, 'virginica'),
(6.4, 3.2, 4.5, 1.5, 'versicolor'),
(4.7, 3.2, 1.3, 0.2, 'setosa'),
(7.0, 3.2, 4.7, 1.4, 'versicolor'),
(7.6, 3.0, 6.6, 2.1, 'virginica'),
(4.6, 3.1, 1.5, 0.2, 'setosa'),
]
header, *rows = IRIS
# %%timeit -n 1000 -r 1000
result = True
for *values, species in rows:
for value in values:
if value < 1.0:
result = False
break
if result is False:
break
# 307 ns ± 54.1 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
# 308 ns ± 46.6 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
# 309 ns ± 43.6 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
# 309 ns ± 55.9 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
# 310 ns ± 54.9 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
IRIS = [
('sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'species'),
(5.8, 2.7, 5.1, 1.9, 'virginica'),
(5.1, 3.5, 1.4, 0.2, 'setosa'),
(5.7, 2.8, 4.1, 1.3, 'versicolor'),
(6.3, 2.9, 5.6, 1.8, 'virginica'),
(6.4, 3.2, 4.5, 1.5, 'versicolor'),
(4.7, 3.2, 1.3, 0.2, 'setosa'),
(7.0, 3.2, 4.7, 1.4, 'versicolor'),
(7.6, 3.0, 6.6, 2.1, 'virginica'),
(4.6, 3.1, 1.5, 0.2, 'setosa'),
]
header, *rows = IRIS
# %%timeit -n 1000 -r 1000
answers = []
for *values, species in rows:
answer = all(value>=1.0 for value in values)
answers.append(answer)
result = all(answers)
# 2.08 μs ± 145 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
# 2.08 μs ± 150 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
# 2.08 μs ± 159 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
# 2.09 μs ± 157 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
# 2.09 μs ± 161 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
IRIS = [
('sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'species'),
(5.8, 2.7, 5.1, 1.9, 'virginica'),
(5.1, 3.5, 1.4, 0.2, 'setosa'),
(5.7, 2.8, 4.1, 1.3, 'versicolor'),
(6.3, 2.9, 5.6, 1.8, 'virginica'),
(6.4, 3.2, 4.5, 1.5, 'versicolor'),
(4.7, 3.2, 1.3, 0.2, 'setosa'),
(7.0, 3.2, 4.7, 1.4, 'versicolor'),
(7.6, 3.0, 6.6, 2.1, 'virginica'),
(4.6, 3.1, 1.5, 0.2, 'setosa'),
]
header, *rows = IRIS
# %%timeit -n 1000 -r 1000
answers = []
for *values, species in rows:
answer = all(value>=1.0 for value in values)
answers.append(answer)
if answer is False:
break
result = all(answers)
# 530 ns ± 69.5 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
# 532 ns ± 70.9 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
# 532 ns ± 72.3 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
# 533 ns ± 39.2 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
# 534 ns ± 78.8 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
IRIS = [
('sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'species'),
(5.8, 2.7, 5.1, 1.9, 'virginica'),
(5.1, 3.5, 1.4, 0.2, 'setosa'),
(5.7, 2.8, 4.1, 1.3, 'versicolor'),
(6.3, 2.9, 5.6, 1.8, 'virginica'),
(6.4, 3.2, 4.5, 1.5, 'versicolor'),
(4.7, 3.2, 1.3, 0.2, 'setosa'),
(7.0, 3.2, 4.7, 1.4, 'versicolor'),
(7.6, 3.0, 6.6, 2.1, 'virginica'),
(4.6, 3.1, 1.5, 0.2, 'setosa'),
]
header, *rows = IRIS
# %%timeit -n 1000 -r 1000
result = all(value >= 1.0
for *values, species in rows
for value in values)
# 408 ns ± 69.1 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
# 409 ns ± 69.4 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
# 409 ns ± 72.3 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
# 410 ns ± 61.3 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
# 410 ns ± 72.2 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
IRIS = [
('sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'species'),
(5.8, 2.7, 5.1, 1.9, 'virginica'),
(5.1, 3.5, 1.4, 0.2, 'setosa'),
(5.7, 2.8, 4.1, 1.3, 'versicolor'),
(6.3, 2.9, 5.6, 1.8, 'virginica'),
(6.4, 3.2, 4.5, 1.5, 'versicolor'),
(4.7, 3.2, 1.3, 0.2, 'setosa'),
(7.0, 3.2, 4.7, 1.4, 'versicolor'),
(7.6, 3.0, 6.6, 2.1, 'virginica'),
(4.6, 3.1, 1.5, 0.2, 'setosa'),
]
header, *rows = IRIS
# %%timeit -n 1000 -r 1000
result = all(value >= 1.0 for *values, species in rows for value in values)
# 392 ns ± 36.3 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
# 392 ns ± 66.3 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
# 392 ns ± 67.8 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
# 393 ns ± 63.8 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
# 393 ns ± 65.4 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
IRIS = [
('sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'species'),
(5.8, 2.7, 5.1, 1.9, 'virginica'),
(5.1, 3.5, 1.4, 0.2, 'setosa'),
(5.7, 2.8, 4.1, 1.3, 'versicolor'),
(6.3, 2.9, 5.6, 1.8, 'virginica'),
(6.4, 3.2, 4.5, 1.5, 'versicolor'),
(4.7, 3.2, 1.3, 0.2, 'setosa'),
(7.0, 3.2, 4.7, 1.4, 'versicolor'),
(7.6, 3.0, 6.6, 2.1, 'virginica'),
(4.6, 3.1, 1.5, 0.2, 'setosa'),
]
header, *rows = IRIS
# %%timeit -n 1000 -r 1000
result = all(x>=1.0 for *X,y in rows for x in X)
# 393 ns ± 67.8 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
# 394 ns ± 51.1 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
# 394 ns ± 52.3 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
# 394 ns ± 61.8 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
# 394 ns ± 71.9 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
9.5.5. Performance 2
Date: 2024-12-04
Python: 3.13.0
IPython: 8.30.0
System: macOS 15.1.1
Computer: MacBook M3 Max
CPU: 16 cores (12 performance and 4 efficiency) / 3nm
RAM: 128 GB RAM LPDDR5
Setup:
>>> DATA = [
... ('sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'species'),
... (5.8, 2.7, 5.1, 1.9, 'virginica'),
... (5.1, 3.5, 1.4, 0.2, 'setosa'),
... (5.7, 2.8, 4.1, 1.3, 'versicolor'),
... (6.3, 2.9, 5.6, 1.8, 'virginica'),
... (6.4, 3.2, 4.5, 1.5, 'versicolor'),
... (4.7, 3.2, 1.3, 0.2, 'setosa'),
... (7.0, 3.2, 4.7, 1.4, 'versicolor'),
... ]
>>> # doctest: +SKIP
... %%timeit -n 1000 -r 1000
... result = []
... for row in DATA[1:]:
... for value in row:
... if isinstance(value, float):
... result.append(value >= 1.0)
... result = all(result)
...
964 ns ± 85.9 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
925 ns ± 91.7 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
919 ns ± 52.7 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
941 ns ± 86.1 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
936 ns ± 77.8 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
>>> # doctest: +SKIP
... %%timeit -n 1000 -r 1000
... result = True
... for row in DATA[1:]:
... for value in row:
... if isinstance(value, float):
... if not value >= 1.0:
... result = False
... break
... if not result:
... break
...
310 ns ± 47.3 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
306 ns ± 44.6 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
302 ns ± 33.5 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
307 ns ± 51.7 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
308 ns ± 57.2 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
>>> # doctest: +SKIP
... %%timeit -n 1000 -r 1000
... result = all(value >= 1.0
... for row in DATA[1:]
... for value in row
... if isinstance(value, float))
...
386 ns ± 40.4 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
388 ns ± 58.6 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
387 ns ± 52.3 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
388 ns ± 52.6 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
385 ns ± 57.4 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
>>> # doctest: +SKIP
... %%timeit -n 1000 -r 1000
... result = all(value >= 1.0 for row in DATA[1:] for value in row if isinstance(value, float))
...
387 ns ± 63.4 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
389 ns ± 61 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
387 ns ± 55.2 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
384 ns ± 61 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
387 ns ± 64.3 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
>>> # doctest: +SKIP
... %%timeit -n 1000 -r 1000
... result = all(y >= 1.0 for x in DATA[1:] for y in x if isinstance(y, float))
...
387 ns ± 63.2 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
387 ns ± 65.5 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
388 ns ± 63.9 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
386 ns ± 58.7 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
385 ns ± 62.5 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
>>> # doctest: +SKIP
... %%timeit -n 1000 -r 1000
... result = all(x >= 1.0 for X in DATA[1:] for x in X if isinstance(x, float))
...
389 ns ± 60.7 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
388 ns ± 64.7 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
387 ns ± 60.7 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
386 ns ± 63.8 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
387 ns ± 65.4 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
9.5.6. Use Case - 1
>>> USERS = [
... {'firstname': 'Alice', 'lastname': 'Apricot', 'age': 30},
... {'firstname': 'Bob', 'lastname': 'Banana', 'age': 31},
... {'firstname': 'Carol', 'lastname': 'Corn', 'age': 32},
... {'firstname': 'Dave', 'lastname': 'Durian', 'age': 33},
... {'firstname': 'Eve', 'lastname': 'Elderberry', 'age': 34},
... {'firstname': 'Mallory', 'lastname': 'Melon', 'age': 15},
... ]
>>>
>>> if all(user['age']>=18 for user in USERS):
... print('All users are adult')
... else:
... print('We have at least one child')
...
We have at least one child
9.5.7. Use Case - 2
>>> USERS = [
... {'firstname': 'Alice', 'lastname': 'Apricot', 'is_staff': True, 'is_admin': False},
... {'firstname': 'Bob', 'lastname': 'Banana', 'is_staff': True, 'is_admin': False},
... {'firstname': 'Carol', 'lastname': 'Corn', 'is_staff': True, 'is_admin': False},
... {'firstname': 'Dave', 'lastname': 'Durian', 'is_staff': True, 'is_admin': False},
... {'firstname': 'Eve', 'lastname': 'Elderberry', 'is_staff': True, 'is_admin': True},
... {'firstname': 'Mallory', 'lastname': 'Melon', 'is_staff': False, 'is_admin': False},
... ]
>>>
>>> if all(user['is_staff'] for user in USERS):
... print('All users are staff')
... else:
... print('We have at least one non-staff')
...
We have at least one non-staff
9.5.8. Assignments
# %% About
# - Name: Idiom All IsAdmin
# - Difficulty: easy
# - Lines: 1
# - 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 `result: bool` with the result of checking
# if all users has admin role
# 2. User is admin if it's `uid` is less than 1000
# 3. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj `result: bool` z wynikiem sprawdzenia
# czy wszyscy użytkownicy mają rolę admin
# 2. Użytkownik jest adminem, jeżeli jego `uid` jest mniejszy niż 1000
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'
>>> assert type(result) is bool, \
'Variable `result` has invalid type, should be bool'
>>> result
False
"""
# %% 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: bool
# %% Data
class User:
def __init__(self, username, uid):
self.username = username
self.uid = uid
def is_admin(self):
return self.uid < 1000
USERS = [
User('alice', uid=1000),
User('bob', uid=1001),
User('carol', uid=1002),
User('dave', uid=1003),
User('eve', uid=1004),
User('mallory', uid=1005),
User('root', uid=0),
]
# %% Result
result = ...
# %% About
# - Name: Idiom All A
# - Difficulty: easy
# - Lines: 4
# - 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. Define `result: bool` with the result of checking
# if all numeric values are greater or equal to 1.0
# 2. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj `result: bool` z wynikiem sprawdzenia
# czy wszystkie wartości numeryczne są większe lub równe 1.0
# 2. Uruchom doctesty - wszystkie muszą się powieść
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'
>>> assert type(result) is bool, \
'Variable `result` has invalid type, should be bool'
>>> result
False
"""
# %% 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: bool
# %% Data
DATA = [
('sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'species'),
(5.8, 2.7, 5.1, 1.9, 'virginica'),
(5.1, 3.5, 1.4, 0.2, 'setosa'),
(5.7, 2.8, 4.1, 1.3, 'versicolor'),
(6.3, 2.9, 5.6, 1.8, 'virginica'),
(6.4, 3.2, 4.5, 1.5, 'versicolor'),
(4.7, 3.2, 1.3, 0.2, 'setosa'),
(7.0, 3.2, 4.7, 1.4, 'versicolor'),
(7.6, 3.0, 6.6, 2.1, 'virginica'),
(4.9, 3.0, 1.4, 0.2, 'setosa'),
(4.9, 2.5, 4.5, 1.7, 'virginica'),
]
header, *rows = DATA
# %% Result
result = ...
# %% About
# - Name: Idiom All Impl
# - 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. Write own implementation of a built-in `all()` function
# 2. Define function `myall` with
# parameter `iterable: list[bool]`
# return `bool`
# 3. Don't validate arguments and assume, that user will
# always pass valid type of arguments
# 4. Do not use built-in function `all()`
# 5. Run doctests - all must succeed
# %% Polish
# 1. Zaimplementuj własne rozwiązanie wbudowanej funkcji `all()`
# 2. Zdefiniuj funkcję `myall` z parametrami:
# parametr `iterable: list[bool]`
# return `bool`
# 3. Nie waliduj argumentów i przyjmij, że użytkownik:
# zawsze poda argumenty poprawnych typów
# 4. Nie używaj wbudowanej funkcji `all()`
# 5. Uruchom doctesty - wszystkie muszą się powieść
# %% Example
# myall([]) == True
# myall([True]) == True
# myall([False]) == False
# myall([True, False, True]) == False
# myall([True, True, True]) == True
# myall([False, False, False]) == False
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> from inspect import isfunction
>>> assert isfunction(myall)
>>> myall()
Traceback (most recent call last):
TypeError: myall() missing 1 required positional argument: 'iterable'
>>> myall([])
True
>>> myall([True])
True
>>> myall([False])
False
>>> myall([True, False, True])
False
>>> myall([True, True, True])
True
>>> myall([False, False, False])
False
"""
# %% 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
from typing import Callable
myall: Callable[[list[bool]], bool]
# %% Data
# %% Result
def myall(iterable):
...