From e028ac17f148e2828b7d7c9e8c40f07c1727ed7b Mon Sep 17 00:00:00 2001 From: Leodanis Pozo Ramos Date: Mon, 16 Dec 2024 18:24:41 +0100 Subject: [PATCH 1/3] Sample code for the article on for loops --- python-for-loop/README.md | 3 +++ python-for-loop/async_range.py | 19 ++++++++++++++++++ python-for-loop/break_continue.py | 14 +++++++++++++ python-for-loop/built_in_types.py | 14 +++++++++++++ python-for-loop/colors.py | 4 ++++ python-for-loop/cubes.py | 7 +++++++ python-for-loop/dictionaries.py | 29 +++++++++++++++++++++++++++ python-for-loop/else_clause.py | 10 +++++++++ python-for-loop/exceptions.py | 14 +++++++++++++ python-for-loop/indices.py | 8 ++++++++ python-for-loop/matrix.py | 10 +++++++++ python-for-loop/nemu.py | 6 ++++++ python-for-loop/nested.py | 5 +++++ python-for-loop/parallel_iteration.py | 5 +++++ python-for-loop/repeat.py | 4 ++++ python-for-loop/reversing.py | 4 ++++ python-for-loop/sets.py | 4 ++++ python-for-loop/sorting.py | 17 ++++++++++++++++ python-for-loop/upacking.py | 4 ++++ 19 files changed, 181 insertions(+) create mode 100644 python-for-loop/README.md create mode 100644 python-for-loop/async_range.py create mode 100644 python-for-loop/break_continue.py create mode 100644 python-for-loop/built_in_types.py create mode 100644 python-for-loop/colors.py create mode 100644 python-for-loop/cubes.py create mode 100644 python-for-loop/dictionaries.py create mode 100644 python-for-loop/else_clause.py create mode 100644 python-for-loop/exceptions.py create mode 100644 python-for-loop/indices.py create mode 100644 python-for-loop/matrix.py create mode 100644 python-for-loop/nemu.py create mode 100644 python-for-loop/nested.py create mode 100644 python-for-loop/parallel_iteration.py create mode 100644 python-for-loop/repeat.py create mode 100644 python-for-loop/reversing.py create mode 100644 python-for-loop/sets.py create mode 100644 python-for-loop/sorting.py create mode 100644 python-for-loop/upacking.py diff --git a/python-for-loop/README.md b/python-for-loop/README.md new file mode 100644 index 0000000000..324eb0c9d1 --- /dev/null +++ b/python-for-loop/README.md @@ -0,0 +1,3 @@ +# Python "for" Loops: The Pythonic Way + +This folder provides the code examples for the Real Python tutorial [Python "for" Loops: The Pythonic Way](https://realpython.com/python-for-loop-update/). diff --git a/python-for-loop/async_range.py b/python-for-loop/async_range.py new file mode 100644 index 0000000000..fa59fa8d33 --- /dev/null +++ b/python-for-loop/async_range.py @@ -0,0 +1,19 @@ +import asyncio + + +class AsyncRange: + def __init__(self, start, end): + self.data = range(start, end) + + async def __aiter__(self): + for index in self.data: + await asyncio.sleep(0.5) + yield index + + +async def main(): + async for index in AsyncRange(0, 5): + print(index) + + +asyncio.run(main()) diff --git a/python-for-loop/break_continue.py b/python-for-loop/break_continue.py new file mode 100644 index 0000000000..d02eab0b4e --- /dev/null +++ b/python-for-loop/break_continue.py @@ -0,0 +1,14 @@ +numbers = [1, 3, 5, 7, 9] +target = 5 +for number in numbers: + print(f"Processing {number}...") + if number == target: + print(f"Target found {target}!") + break + +numbers = [1, 2, 3, 4, 5, 6] +for number in numbers: + print(f"{number = }") + if number % 2 != 0: + continue + print(f"{number} is even!") diff --git a/python-for-loop/built_in_types.py b/python-for-loop/built_in_types.py new file mode 100644 index 0000000000..62745b9142 --- /dev/null +++ b/python-for-loop/built_in_types.py @@ -0,0 +1,14 @@ +numbers = [1, 2, 3, 4] +for number in numbers: + print(number) + +person = ("Jane", 25, "Python Dev", "Canada") +for field in person: + print(field) + +text = "abcde" +for character in text: + print(character) + +for index in range(5): + print(index) diff --git a/python-for-loop/colors.py b/python-for-loop/colors.py new file mode 100644 index 0000000000..b8ddfa070c --- /dev/null +++ b/python-for-loop/colors.py @@ -0,0 +1,4 @@ +colors = ["red", "green", "blue", "yellow"] + +for color in colors: + print(color) diff --git a/python-for-loop/cubes.py b/python-for-loop/cubes.py new file mode 100644 index 0000000000..f850db9c94 --- /dev/null +++ b/python-for-loop/cubes.py @@ -0,0 +1,7 @@ +cubes = [] +for number in range(10): + cubes.append(number**3) +print(cubes) + +cubes = [number**3 for number in range(10)] +print(cubes) diff --git a/python-for-loop/dictionaries.py b/python-for-loop/dictionaries.py new file mode 100644 index 0000000000..80c1dc3494 --- /dev/null +++ b/python-for-loop/dictionaries.py @@ -0,0 +1,29 @@ +students = { + "Alice": 89.5, + "Bob": 76.0, + "Charlie": 92.3, + "Diana": 84.7, + "Ethan": 88.9, +} +for student in students: + print(student) + +for student in students: + print(student, "->", students[student]) + +for student in students.keys(): + print(student) + +teams = { + "Colorado": "Rockies", + "Chicago": "White Sox", + "Boston": "Red Sox", + "Minnesota": "Twins", + "Milwaukee": "Brewers", + "Seattle": "Mariners", +} +for team in teams.values(): + print(team) + +for city, team in teams.items(): + print(city, "->", team) diff --git a/python-for-loop/else_clause.py b/python-for-loop/else_clause.py new file mode 100644 index 0000000000..3c1aba24ce --- /dev/null +++ b/python-for-loop/else_clause.py @@ -0,0 +1,10 @@ +numbers = [1, 3, 5, 7, 9] +target = 42 + +for number in numbers: + print(f"Processing {number}...") + if number == target: + print(f"Target found {target}!") + break +else: + print(f"Target no found {target}") diff --git a/python-for-loop/exceptions.py b/python-for-loop/exceptions.py new file mode 100644 index 0000000000..8ac3ccb403 --- /dev/null +++ b/python-for-loop/exceptions.py @@ -0,0 +1,14 @@ +files = ["file1.txt", "file2.txt", "file3.txt"] + +for file in files: + with open(file, "r") as f: + print(f"Contents of {file}:") + print(f.read()) + +for file in files: + try: + with open(file, "r") as f: + print(f"Contents of {file}:") + print(f.read()) + except FileNotFoundError: + print(f"Error: {file} not found. Skipping.") diff --git a/python-for-loop/indices.py b/python-for-loop/indices.py new file mode 100644 index 0000000000..74b4d2a771 --- /dev/null +++ b/python-for-loop/indices.py @@ -0,0 +1,8 @@ +fruits = ["orange", "apple", "mango", "lemon"] + +for index in range(len(fruits)): + fruit = fruits[index] + print(index, fruit) + +for index, fruit in enumerate(fruits): + print(index, fruit) diff --git a/python-for-loop/matrix.py b/python-for-loop/matrix.py new file mode 100644 index 0000000000..80272922d3 --- /dev/null +++ b/python-for-loop/matrix.py @@ -0,0 +1,10 @@ +from itertools import chain + +matrix = [ + [9, 3, 8], + [4, 5, 2], + [6, 4, 3], +] + +for value in chain(*matrix): + print(value**2) diff --git a/python-for-loop/nemu.py b/python-for-loop/nemu.py new file mode 100644 index 0000000000..c34bc1a2f5 --- /dev/null +++ b/python-for-loop/nemu.py @@ -0,0 +1,6 @@ +def display_menu(options): + print("Main Menu:") + for pos, option in enumerate(options, start=1): + print(f"{pos}. {option}") + +display_menu(["Open", "Save", "Settings", "Quit"]) diff --git a/python-for-loop/nested.py b/python-for-loop/nested.py new file mode 100644 index 0000000000..fa31a2551d --- /dev/null +++ b/python-for-loop/nested.py @@ -0,0 +1,5 @@ +for number in range(1, 11): + for product in range(number, number * 11, number): + print(f"{product:>4d}", end="") + print() + diff --git a/python-for-loop/parallel_iteration.py b/python-for-loop/parallel_iteration.py new file mode 100644 index 0000000000..80428e8931 --- /dev/null +++ b/python-for-loop/parallel_iteration.py @@ -0,0 +1,5 @@ +numbers = [1, 2, 3] +letters = ["a", "b", "c"] +for number, letter in zip(numbers, letters): + print(number, "->", letter) + diff --git a/python-for-loop/repeat.py b/python-for-loop/repeat.py new file mode 100644 index 0000000000..499c277c9c --- /dev/null +++ b/python-for-loop/repeat.py @@ -0,0 +1,4 @@ +for _ in range(3): + print("Knock, knock, knock") + print("Penny!") + diff --git a/python-for-loop/reversing.py b/python-for-loop/reversing.py new file mode 100644 index 0000000000..80c3d8e552 --- /dev/null +++ b/python-for-loop/reversing.py @@ -0,0 +1,4 @@ +actions = ["Type text", "Select text", "Cut text", "Paste text"] + +for action in reversed(actions): + print(f"Undo: {action}") diff --git a/python-for-loop/sets.py b/python-for-loop/sets.py new file mode 100644 index 0000000000..ebce488719 --- /dev/null +++ b/python-for-loop/sets.py @@ -0,0 +1,4 @@ +tools = {"Django", "Flask", "pandas", "NumPy"} + +for tool in tools: + print(tool) diff --git a/python-for-loop/sorting.py b/python-for-loop/sorting.py new file mode 100644 index 0000000000..75c61d3769 --- /dev/null +++ b/python-for-loop/sorting.py @@ -0,0 +1,17 @@ +students = { + "Alice": 89.5, + "Bob": 76.0, + "Charlie": 92.3, + "Diana": 84.7, + "Ethan": 88.9, + "Fiona": 95.6, + "George": 73.4, + "Hannah": 81.2, +} + +sorted_students = sorted( + students.items(), key=lambda item: item[1], reverse=True +) + +for name, grade in sorted_students: + print(f"{name}'s average grade: {grade:->{20-len(name)}.1f}") diff --git a/python-for-loop/upacking.py b/python-for-loop/upacking.py new file mode 100644 index 0000000000..4e4daed5b7 --- /dev/null +++ b/python-for-loop/upacking.py @@ -0,0 +1,4 @@ +points = [(1, 4), (3, 6), (7, 3)] + +for x, y in points: + print(f"{x = } and {y = }") From eceb1b4638b6ab550af9b21f4e45fe93231337e6 Mon Sep 17 00:00:00 2001 From: Leodanis Pozo Ramos Date: Mon, 16 Dec 2024 18:29:15 +0100 Subject: [PATCH 2/3] Liter issues --- python-for-loop/nemu.py | 1 + python-for-loop/nested.py | 1 - python-for-loop/parallel_iteration.py | 2 +- python-for-loop/repeat.py | 1 - 4 files changed, 2 insertions(+), 3 deletions(-) diff --git a/python-for-loop/nemu.py b/python-for-loop/nemu.py index c34bc1a2f5..e486cebd5a 100644 --- a/python-for-loop/nemu.py +++ b/python-for-loop/nemu.py @@ -3,4 +3,5 @@ def display_menu(options): for pos, option in enumerate(options, start=1): print(f"{pos}. {option}") + display_menu(["Open", "Save", "Settings", "Quit"]) diff --git a/python-for-loop/nested.py b/python-for-loop/nested.py index fa31a2551d..53cfdd1f9e 100644 --- a/python-for-loop/nested.py +++ b/python-for-loop/nested.py @@ -2,4 +2,3 @@ for product in range(number, number * 11, number): print(f"{product:>4d}", end="") print() - diff --git a/python-for-loop/parallel_iteration.py b/python-for-loop/parallel_iteration.py index 80428e8931..8d0aa1852f 100644 --- a/python-for-loop/parallel_iteration.py +++ b/python-for-loop/parallel_iteration.py @@ -1,5 +1,5 @@ numbers = [1, 2, 3] letters = ["a", "b", "c"] + for number, letter in zip(numbers, letters): print(number, "->", letter) - diff --git a/python-for-loop/repeat.py b/python-for-loop/repeat.py index 499c277c9c..41c643d71d 100644 --- a/python-for-loop/repeat.py +++ b/python-for-loop/repeat.py @@ -1,4 +1,3 @@ for _ in range(3): print("Knock, knock, knock") print("Penny!") - From 310798e4deffc31c2a27c7c23afcb384bc479d4b Mon Sep 17 00:00:00 2001 From: Leodanis Pozo Ramos Date: Fri, 20 Dec 2024 13:20:42 +0100 Subject: [PATCH 3/3] TR updates, first round --- python-for-loop/{nemu.py => menu.py} | 0 python-for-loop/{upacking.py => unpacking.py} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename python-for-loop/{nemu.py => menu.py} (100%) rename python-for-loop/{upacking.py => unpacking.py} (100%) diff --git a/python-for-loop/nemu.py b/python-for-loop/menu.py similarity index 100% rename from python-for-loop/nemu.py rename to python-for-loop/menu.py diff --git a/python-for-loop/upacking.py b/python-for-loop/unpacking.py similarity index 100% rename from python-for-loop/upacking.py rename to python-for-loop/unpacking.py