From 5d1e0098b301b9649279797d45910d49d6dda8e3 Mon Sep 17 00:00:00 2001 From: Philipp Date: Wed, 18 Jun 2025 19:13:47 +0200 Subject: [PATCH 1/2] Add materials for enumerate tutorial --- python-enumerate/README.md | 3 +++ python-enumerate/custom_enumerate.py | 12 ++++++++++++ python-enumerate/enumerate_alternatives.py | 21 +++++++++++++++++++++ python-enumerate/line_numbers.py | 11 +++++++++++ python-enumerate/runners_v1.py | 4 ++++ python-enumerate/runners_v2.py | 4 ++++ python-enumerate/secret_message.py | 8 ++++++++ python-enumerate/tasks.py | 7 +++++++ 8 files changed, 70 insertions(+) create mode 100644 python-enumerate/README.md create mode 100644 python-enumerate/custom_enumerate.py create mode 100644 python-enumerate/enumerate_alternatives.py create mode 100644 python-enumerate/line_numbers.py create mode 100644 python-enumerate/runners_v1.py create mode 100644 python-enumerate/runners_v2.py create mode 100644 python-enumerate/secret_message.py create mode 100644 python-enumerate/tasks.py diff --git a/python-enumerate/README.md b/python-enumerate/README.md new file mode 100644 index 0000000000..5a1523a169 --- /dev/null +++ b/python-enumerate/README.md @@ -0,0 +1,3 @@ +# Python enumerate(): Simplify Loops That Need Counters + +This folder contains sample code for the Real Python tutorial [Python enumerate(): Simplify Loops That Need Counters](https://realpython.com/python-enumerate/). diff --git a/python-enumerate/custom_enumerate.py b/python-enumerate/custom_enumerate.py new file mode 100644 index 0000000000..a5d27f1127 --- /dev/null +++ b/python-enumerate/custom_enumerate.py @@ -0,0 +1,12 @@ +def my_enumerate(iterable, start=0): + n = start + for elem in iterable: + yield n, elem + n += 1 + + +seasons = ["Spring", "Summer", "Fall", "Winter"] + +print(my_enumerate(seasons)) +print(list(my_enumerate(seasons))) +print(list(my_enumerate(seasons, start=1))) diff --git a/python-enumerate/enumerate_alternatives.py b/python-enumerate/enumerate_alternatives.py new file mode 100644 index 0000000000..977d2fbcf9 --- /dev/null +++ b/python-enumerate/enumerate_alternatives.py @@ -0,0 +1,21 @@ +# Slicing + +game_name = "Stardew Valley" +print(game_name[:4]) + +# Multi-Sequence Iteration + +pets = ["Leo", "Aubrey", "Frieda"] +owners = ["Bartosz", "Sarah Jane", "Philipp"] + +for pet, owner in zip(pets, owners): + print(f"{pet} & {owner}") + +# Advanced Iteration Patterns + +from itertools import pairwise + +cities = ["Graz", "Belgrade", "Warsaw", "Berlin"] + +for city, next_city in pairwise(cities): + print(f"{city} -> {next_city}") diff --git a/python-enumerate/line_numbers.py b/python-enumerate/line_numbers.py new file mode 100644 index 0000000000..281e7a8001 --- /dev/null +++ b/python-enumerate/line_numbers.py @@ -0,0 +1,11 @@ +lines = [ + "This is a\tline", + "This line is fine", + "Another line with whitespace ", +] + +for lineno, line in enumerate(lines, start=1): + if "\t" in line: + print(f"Line {lineno}: Contains a tab character.") + if line.rstrip() != line: + print(f"Line {lineno}: Contains trailing whitespace.") diff --git a/python-enumerate/runners_v1.py b/python-enumerate/runners_v1.py new file mode 100644 index 0000000000..08be57c48c --- /dev/null +++ b/python-enumerate/runners_v1.py @@ -0,0 +1,4 @@ +runners = ["Lenka", "Martina", "Gugu"] + +for position, name in enumerate(runners): + print(position, name) diff --git a/python-enumerate/runners_v2.py b/python-enumerate/runners_v2.py new file mode 100644 index 0000000000..7492a3f50d --- /dev/null +++ b/python-enumerate/runners_v2.py @@ -0,0 +1,4 @@ +runners = ["Lenka", "Martina", "Gugu"] + +for position, name in enumerate(runners, start=1): + print(position, name) diff --git a/python-enumerate/secret_message.py b/python-enumerate/secret_message.py new file mode 100644 index 0000000000..4fbd771a41 --- /dev/null +++ b/python-enumerate/secret_message.py @@ -0,0 +1,8 @@ +secret_message = "3LAigf7eq 5fhiOnpdDs2 Ra6 nwUalyo.9" +message = "" + +for index, char in enumerate(secret_message): + if index % 2: + message += char + +print(message) diff --git a/python-enumerate/tasks.py b/python-enumerate/tasks.py new file mode 100644 index 0000000000..2dcd9fc2e2 --- /dev/null +++ b/python-enumerate/tasks.py @@ -0,0 +1,7 @@ +tasks_by_priority = ["Pay Rent", "Clean Dishes", "Buy Milk"] + +for index, task in enumerate(tasks_by_priority): + if index == 0: + print(f"* {task.upper()}!") + else: + print(f"* {task}") From 249425ff9926d340b289c8abfeefe307eafc6ce9 Mon Sep 17 00:00:00 2001 From: Philipp Date: Wed, 18 Jun 2025 19:15:35 +0200 Subject: [PATCH 2/2] Put itertools import to the top --- python-enumerate/enumerate_alternatives.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/python-enumerate/enumerate_alternatives.py b/python-enumerate/enumerate_alternatives.py index 977d2fbcf9..d797d0260c 100644 --- a/python-enumerate/enumerate_alternatives.py +++ b/python-enumerate/enumerate_alternatives.py @@ -1,3 +1,12 @@ +from itertools import pairwise + +# Advanced Iteration Patterns + +cities = ["Graz", "Belgrade", "Warsaw", "Berlin"] + +for city, next_city in pairwise(cities): + print(f"{city} -> {next_city}") + # Slicing game_name = "Stardew Valley" @@ -10,12 +19,3 @@ for pet, owner in zip(pets, owners): print(f"{pet} & {owner}") - -# Advanced Iteration Patterns - -from itertools import pairwise - -cities = ["Graz", "Belgrade", "Warsaw", "Berlin"] - -for city, next_city in pairwise(cities): - print(f"{city} -> {next_city}")