Skip to content

Commit d59c928

Browse files
authored
Merge pull request #664 from realpython/python-nested-loops-materials
add code for Nested Loops in Python tutorial
2 parents 0bf2eeb + 6f46bc5 commit d59c928

18 files changed

+148
-0
lines changed

python-nested-loops/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Nested Loops in Python
2+
3+
This folder provides the code examples for the Real Python tutorial [Nested Loops in Python](https://realpython.com/nested-loops-python/).
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
for hour in range(0, 3):
2+
for minute in range(0, 60):
3+
print(f"{hour:02d}:{minute:02d}")
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
crew_ids = [1, 2, 8, 4, 5, 6, 7, 8, 9, 10]
2+
for i, first_id in enumerate(crew_ids):
3+
for second_id in crew_ids[i + 1 :]:
4+
if first_id == second_id:
5+
print(f"Clone detected: id {first_id} is a duplicate.")
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
crew_ids = [1, 2, 8, 4, 5, 6, 7, 8, 9, 10]
2+
detected = set()
3+
for id in crew_ids:
4+
if id in detected:
5+
print(f"Clone found: id {id} is a duplicate.")
6+
else:
7+
detected.add(id)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from collections import Counter
2+
3+
crew_ids = [1, 2, 8, 4, 5, 6, 7, 8, 9, 10]
4+
detected = Counter(crew_ids)
5+
for key, value in detected.items():
6+
if value > 1:
7+
print(f"Clone found: id {key} is a duplicate.")
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
resource_donators = [[8, 6, 3], [9, 2, 7], [4, 1, 5]]
2+
total_resources = 0
3+
for planet in resource_donators:
4+
for resource in planet:
5+
total_resources += resource
6+
print(f"Total resources gotten for interstellar travels: {total_resources}")
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
for multiplicant in range(1, 11):
2+
for multiplier in range(1, 4):
3+
expression = f"{multiplicant:>2d} × {multiplier}"
4+
product = multiplicant * multiplier
5+
print(f"{expression} = {product:>2d}", end="\t")
6+
print()
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
while True:
2+
word = input("Enter a word (or type 'exit' to stop): ")
3+
4+
if word == "exit":
5+
break
6+
7+
for letter in word:
8+
print(letter)

python-nested-loops/pancake1.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
pancake_stacks = []
2+
bases = ["plain", "chocolate", "blueberry"]
3+
toppings = ["honey", "whipped cream", "alien syrup"]
4+
5+
for base in bases:
6+
for topping in toppings:
7+
pancake_stacks.append(f"{base.capitalize()} pancake with {topping}")
8+
print(pancake_stacks)

python-nested-loops/pancake2.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
bases = ["plain", "chocolate", "blueberry"]
2+
toppings = ["honey", "whipped cream", "alien syrup"]
3+
4+
pancake_stacks = [
5+
f"{base.capitalize()} pancake with {topping}"
6+
for base in bases
7+
for topping in toppings
8+
]
9+
print(pancake_stacks)

0 commit comments

Comments
 (0)