Files
Learn-Python-Programming-Th…/ch05/squares.map.py
T
adii1823 ef37ce0c4e ch05
2021-10-28 17:38:16 +05:30

16 lines
395 B
Python

# squares.map.py
# This is not a valid Python module - Don't run it.
# If you code like this you are not a Python dev! ;)
>>> squares = []
>>> for n in range(10):
... squares.append(n ** 2)
...
>>> squares
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
# This is better, one line, nice and readable
>>> squares = map(lambda n: n**2, range(10))
>>> list(squares)
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]