Skip to content

Commit 373f6c7

Browse files
authored
Merge pull request #677 from realpython/python-enumerate
Add materials for enumerate tutorial
2 parents 088402c + 249425f commit 373f6c7

File tree

8 files changed

+70
-0
lines changed

8 files changed

+70
-0
lines changed

python-enumerate/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Python enumerate(): Simplify Loops That Need Counters
2+
3+
This folder contains sample code for the Real Python tutorial [Python enumerate(): Simplify Loops That Need Counters](https://realpython.com/python-enumerate/).
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def my_enumerate(iterable, start=0):
2+
n = start
3+
for elem in iterable:
4+
yield n, elem
5+
n += 1
6+
7+
8+
seasons = ["Spring", "Summer", "Fall", "Winter"]
9+
10+
print(my_enumerate(seasons))
11+
print(list(my_enumerate(seasons)))
12+
print(list(my_enumerate(seasons, start=1)))
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from itertools import pairwise
2+
3+
# Advanced Iteration Patterns
4+
5+
cities = ["Graz", "Belgrade", "Warsaw", "Berlin"]
6+
7+
for city, next_city in pairwise(cities):
8+
print(f"{city} -> {next_city}")
9+
10+
# Slicing
11+
12+
game_name = "Stardew Valley"
13+
print(game_name[:4])
14+
15+
# Multi-Sequence Iteration
16+
17+
pets = ["Leo", "Aubrey", "Frieda"]
18+
owners = ["Bartosz", "Sarah Jane", "Philipp"]
19+
20+
for pet, owner in zip(pets, owners):
21+
print(f"{pet} & {owner}")

python-enumerate/line_numbers.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
lines = [
2+
"This is a\tline",
3+
"This line is fine",
4+
"Another line with whitespace ",
5+
]
6+
7+
for lineno, line in enumerate(lines, start=1):
8+
if "\t" in line:
9+
print(f"Line {lineno}: Contains a tab character.")
10+
if line.rstrip() != line:
11+
print(f"Line {lineno}: Contains trailing whitespace.")

python-enumerate/runners_v1.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
runners = ["Lenka", "Martina", "Gugu"]
2+
3+
for position, name in enumerate(runners):
4+
print(position, name)

python-enumerate/runners_v2.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
runners = ["Lenka", "Martina", "Gugu"]
2+
3+
for position, name in enumerate(runners, start=1):
4+
print(position, name)

python-enumerate/secret_message.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
secret_message = "3LAigf7eq 5fhiOnpdDs2 Ra6 nwUalyo.9"
2+
message = ""
3+
4+
for index, char in enumerate(secret_message):
5+
if index % 2:
6+
message += char
7+
8+
print(message)

python-enumerate/tasks.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
tasks_by_priority = ["Pay Rent", "Clean Dishes", "Buy Milk"]
2+
3+
for index, task in enumerate(tasks_by_priority):
4+
if index == 0:
5+
print(f"* {task.upper()}!")
6+
else:
7+
print(f"* {task}")

0 commit comments

Comments
 (0)