|
| 1 | +from typing import Generator |
| 2 | +from functools import reduce |
| 3 | + |
| 4 | + |
| 5 | +def iteration_1() -> None: |
| 6 | + for n in range(1, 11): |
| 7 | + print(n) |
| 8 | + |
| 9 | + |
| 10 | +def iteration_2() -> None: |
| 11 | + numbers = [num for num in range(1, 11)] |
| 12 | + print(numbers, sep="\n") |
| 13 | + |
| 14 | + |
| 15 | +def iterarion_3() -> None: |
| 16 | + counter = 1 |
| 17 | + while counter <= 10: |
| 18 | + print(counter) |
| 19 | + counter += 1 |
| 20 | + |
| 21 | + |
| 22 | +def iteration_4() -> None: |
| 23 | + iter = iter([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) |
| 24 | + print(next(iter)) |
| 25 | + print(next(iter)) |
| 26 | + |
| 27 | + |
| 28 | +def iteraration_5() -> None: |
| 29 | + numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
| 30 | + doubled = list(map(lambda x: x * 2, numbers)) |
| 31 | + print(doubled) |
| 32 | + |
| 33 | + |
| 34 | +def iteration_6() -> None: |
| 35 | + numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
| 36 | + filtered = list(filter(lambda x: x % 2 == 0, numbers)) |
| 37 | + print(filtered) |
| 38 | + |
| 39 | + |
| 40 | +def iteration_7() -> None: |
| 41 | + square_dict = {x: x * 2 for x in range(1, 11)} |
| 42 | + print(square_dict) |
| 43 | + |
| 44 | + |
| 45 | +def iteration_7() -> None: |
| 46 | + numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
| 47 | + result = reduce(lambda x, y: x + y, numbers, 0) |
| 48 | + print(result) |
| 49 | + |
| 50 | + |
| 51 | +def iteration_8() -> None: |
| 52 | + numbers = tuple(num for num in range(1, 11)) |
| 53 | + print(numbers) |
| 54 | + |
| 55 | + |
| 56 | +def iteration_9() -> None: |
| 57 | + numbers = {num for num in range(1, 11)} |
| 58 | + print(numbers) |
| 59 | + |
| 60 | + |
| 61 | +def iteration_10() -> None: |
| 62 | + numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
| 63 | + |
| 64 | + for i, item in enumerate(numbers): |
| 65 | + print(f"elemento {item} en la posición {i}") |
| 66 | + |
| 67 | + |
| 68 | +def iteration_11() -> None: |
| 69 | + numbers = [n for n in range(1, 11)] |
| 70 | + |
| 71 | + for n in range(len(numbers)): |
| 72 | + print(f"elemento {numbers[n]} en la posición {n}") |
| 73 | + |
| 74 | + |
| 75 | +def even_number_generator() -> Generator[int, None, None]: |
| 76 | + num = 0 |
| 77 | + while True: |
| 78 | + yield num |
| 79 | + num += 2 |
| 80 | + |
| 81 | + |
| 82 | +def main() -> None: |
| 83 | + even_numbers = even_number_generator() |
| 84 | + |
| 85 | + for _ in range(11): |
| 86 | + print(next(even_numbers)) |
| 87 | + |
| 88 | + iteration_8() |
| 89 | + iteration_9() |
| 90 | + iteration_10() |
| 91 | + iteration_11() |
| 92 | + |
| 93 | + |
| 94 | +if __name__ == "__main__": |
| 95 | + main() |
0 commit comments