Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions python-nested-loops/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Nested Loops in Python

This folder provides the code examples for the Real Python tutorial [Nested Loops in Python](https://realpython.com/nested-loops-python/).
3 changes: 3 additions & 0 deletions python-nested-loops/clock_analogy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
for hour in range(0, 3):
for minute in range(0, 60):
print(f"{hour:02d}:{minute:02d}")
5 changes: 5 additions & 0 deletions python-nested-loops/duplicate_check1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
crew_ids = [1, 2, 8, 4, 5, 6, 7, 8, 9, 10]
for i, first_id in enumerate(crew_ids):
for second_id in crew_ids[i + 1 :]:
if first_id == second_id:
print(f"Clone detected: id {first_id} is a duplicate.")
7 changes: 7 additions & 0 deletions python-nested-loops/duplicate_check2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
crew_ids = [1, 2, 8, 4, 5, 6, 7, 8, 9, 10]
detected = set()
for id in crew_ids:
if id in detected:
print(f"Clone found: id {id} is a duplicate.")
else:
detected.add(id)
7 changes: 7 additions & 0 deletions python-nested-loops/duplicate_check3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from collections import Counter

crew_ids = [1, 2, 8, 4, 5, 6, 7, 8, 9, 10]
detected = Counter(crew_ids)
for key, value in detected.items():
if value > 1:
print(f"Clone found: id {key} is a duplicate.")
6 changes: 6 additions & 0 deletions python-nested-loops/multilist_sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
resource_donators = [[8, 6, 3], [9, 2, 7], [4, 1, 5]]
total_resources = 0
for planet in resource_donators:
for resource in planet:
total_resources += resource
print(f"Total resources gotten for interstellar travels: {total_resources}")
6 changes: 6 additions & 0 deletions python-nested-loops/multiplication_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
for multiplicant in range(1, 11):
for multiplier in range(1, 4):
expression = f"{multiplicant:>2d} × {multiplier}"
product = multiplicant * multiplier
print(f"{expression} = {product:>2d}", end="\t")
print()
8 changes: 8 additions & 0 deletions python-nested-loops/nested_while.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
while True:
word = input("Enter a word (or type 'exit' to stop): ")

if word == "exit":
break

for letter in word:
print(letter)
8 changes: 8 additions & 0 deletions python-nested-loops/pancake1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
pancake_stacks = []
bases = ["plain", "chocolate", "blueberry"]
toppings = ["honey", "whipped cream", "alien syrup"]

for base in bases:
for topping in toppings:
pancake_stacks.append(f"{base.capitalize()} pancake with {topping}")
print(pancake_stacks)
9 changes: 9 additions & 0 deletions python-nested-loops/pancake2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
bases = ["plain", "chocolate", "blueberry"]
toppings = ["honey", "whipped cream", "alien syrup"]

pancake_stacks = [
f"{base.capitalize()} pancake with {topping}"
for base in bases
for topping in toppings
]
print(pancake_stacks)
9 changes: 9 additions & 0 deletions python-nested-loops/pattern.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
height = 6
sail_patterns = "*#-x+o"
for row in range(height):
pattern = ""
spacing = " " * (height - row)
for symbol in sail_patterns:
pattern += symbol * row + spacing

print(pattern)
11 changes: 11 additions & 0 deletions python-nested-loops/readability_challenge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
living_quarters = 3
sections = 2
floors = 3

for floor in range(floors):
for section in range(sections):
for living_quarter in range(living_quarters):
print(
f"Scanning quarter {living_quarter} in section {section} "
f"on floor {floor} for the intruder..."
)
16 changes: 16 additions & 0 deletions python-nested-loops/sandwich1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
blt_sandwich = [
["bread", "lettuce", "tomato", "bacon"],
["bread", "bacon", "lettuce", "tomato"],
["bacon", "bacon", "tomato", "lettuce"],
]
target = "bacon"
found = False
for layer in blt_sandwich:
for ingredient in layer:
if ingredient == target:
print(f"Found the crispy {target}!")
found = True
break
if found:
print("Enjoying the crunch and worth it.")
break
19 changes: 19 additions & 0 deletions python-nested-loops/sandwich2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
blt_sandwich = [
["bread", "lettuce", "tomato", "bacon"],
["bread", "bacon", "lettuce", "tomato"],
["bacon", "bacon", "tomato", "lettuce"],
]
target = "bacon"
found = False
for layer in blt_sandwich:
for ingredient in layer:
if ingredient != target:
print("This is not bacon. Skipping...")
continue
print(f"Found the crispy {target}!")
found = True
break

if found:
break
print("Enjoying the crunch and worth it.")
5 changes: 5 additions & 0 deletions python-nested-loops/space_ball.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
players = ["Bonnie", "Mike", "Raj", "Adah"]

for player1 in players:
for player2 in players:
print(f"{player1} vs {player2}")
6 changes: 6 additions & 0 deletions python-nested-loops/space_ball2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
players = ["Bonnie", "Mike", "Raj", "Adah"]

for player1 in players:
for player2 in players:
if player1 != player2:
print(f"{player1} vs {player2}")
10 changes: 10 additions & 0 deletions python-nested-loops/variable_scoping.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
employees = [("Dorothy", "DevOps"), ("Abdel", "HR"), ("Nataliya", "DevOps")]
departments = [
{"name": "DevOps", "city": "Berlin"},
{"name": "HR", "city": "Abuja"},
]

for name, department in employees:
for department in departments:
if department["name"] == department:
print(f"{name} works in {department['city']}")
10 changes: 10 additions & 0 deletions python-nested-loops/variable_scoping_fix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
employees = [("Dorothy", "DevOps"), ("Abdel", "HR"), ("Nataliya", "DevOps")]
departments = [
{"name": "DevOps", "city": "Berlin"},
{"name": "HR", "city": "Abuja"},
]

for name, department in employees:
for dept in departments:
if dept["name"] == department:
print(f"{name} works in {dept['city']}")