diff --git a/python-nested-loops/README.md b/python-nested-loops/README.md new file mode 100644 index 0000000000..cf86edd192 --- /dev/null +++ b/python-nested-loops/README.md @@ -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/). diff --git a/python-nested-loops/clock_analogy.py b/python-nested-loops/clock_analogy.py new file mode 100644 index 0000000000..13bbba9473 --- /dev/null +++ b/python-nested-loops/clock_analogy.py @@ -0,0 +1,3 @@ +for hour in range(0, 3): + for minute in range(0, 60): + print(f"{hour:02d}:{minute:02d}") diff --git a/python-nested-loops/duplicate_check1.py b/python-nested-loops/duplicate_check1.py new file mode 100644 index 0000000000..33c851b4ed --- /dev/null +++ b/python-nested-loops/duplicate_check1.py @@ -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.") diff --git a/python-nested-loops/duplicate_check2.py b/python-nested-loops/duplicate_check2.py new file mode 100644 index 0000000000..3408fb314e --- /dev/null +++ b/python-nested-loops/duplicate_check2.py @@ -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) diff --git a/python-nested-loops/duplicate_check3.py b/python-nested-loops/duplicate_check3.py new file mode 100644 index 0000000000..12cc0bb01d --- /dev/null +++ b/python-nested-loops/duplicate_check3.py @@ -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.") diff --git a/python-nested-loops/multilist_sum.py b/python-nested-loops/multilist_sum.py new file mode 100644 index 0000000000..c3a9908ff1 --- /dev/null +++ b/python-nested-loops/multilist_sum.py @@ -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}") diff --git a/python-nested-loops/multiplication_table.py b/python-nested-loops/multiplication_table.py new file mode 100644 index 0000000000..261a245555 --- /dev/null +++ b/python-nested-loops/multiplication_table.py @@ -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() diff --git a/python-nested-loops/nested_while.py b/python-nested-loops/nested_while.py new file mode 100644 index 0000000000..d15b8ff8d2 --- /dev/null +++ b/python-nested-loops/nested_while.py @@ -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) diff --git a/python-nested-loops/pancake1.py b/python-nested-loops/pancake1.py new file mode 100644 index 0000000000..0ac05ed943 --- /dev/null +++ b/python-nested-loops/pancake1.py @@ -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) diff --git a/python-nested-loops/pancake2.py b/python-nested-loops/pancake2.py new file mode 100644 index 0000000000..5e6572c8a3 --- /dev/null +++ b/python-nested-loops/pancake2.py @@ -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) diff --git a/python-nested-loops/pattern.py b/python-nested-loops/pattern.py new file mode 100644 index 0000000000..1d5b00f6c8 --- /dev/null +++ b/python-nested-loops/pattern.py @@ -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) diff --git a/python-nested-loops/readability_challenge.py b/python-nested-loops/readability_challenge.py new file mode 100644 index 0000000000..14d74e0f7d --- /dev/null +++ b/python-nested-loops/readability_challenge.py @@ -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..." + ) diff --git a/python-nested-loops/sandwich1.py b/python-nested-loops/sandwich1.py new file mode 100644 index 0000000000..37503fbb5e --- /dev/null +++ b/python-nested-loops/sandwich1.py @@ -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 diff --git a/python-nested-loops/sandwich2.py b/python-nested-loops/sandwich2.py new file mode 100644 index 0000000000..95b0cdbc61 --- /dev/null +++ b/python-nested-loops/sandwich2.py @@ -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.") diff --git a/python-nested-loops/space_ball.py b/python-nested-loops/space_ball.py new file mode 100644 index 0000000000..35d8689cd8 --- /dev/null +++ b/python-nested-loops/space_ball.py @@ -0,0 +1,5 @@ +players = ["Bonnie", "Mike", "Raj", "Adah"] + +for player1 in players: + for player2 in players: + print(f"{player1} vs {player2}") diff --git a/python-nested-loops/space_ball2.py b/python-nested-loops/space_ball2.py new file mode 100644 index 0000000000..ae4d7deca5 --- /dev/null +++ b/python-nested-loops/space_ball2.py @@ -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}") diff --git a/python-nested-loops/variable_scoping.py b/python-nested-loops/variable_scoping.py new file mode 100644 index 0000000000..98c5521a86 --- /dev/null +++ b/python-nested-loops/variable_scoping.py @@ -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']}") diff --git a/python-nested-loops/variable_scoping_fix.py b/python-nested-loops/variable_scoping_fix.py new file mode 100644 index 0000000000..14b937e600 --- /dev/null +++ b/python-nested-loops/variable_scoping_fix.py @@ -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']}")