Course Content
Python Loops Tutorial
Python Loops Tutorial
Nested for Loop
A nested loop is a loop located within the body of an outer loop. Either the inner or outer loop can take on various forms, including a while
loop or a for
loop. For instance, the outer for
loop can encompass a while
loop, and conversely.
Example: Displaying Cities in a Pattern
We will use a nested loop to print each city multiple times in a structured format, resembling a triangle pattern.
travel_list = ["Monako", "Luxemburg", "Liverpool", "Barcelona", "Munchen"] # Outer loop for controlling rows for i in range(1, len(travel_list) + 1): # Inner loop for controlling columns for j in range(i): print(travel_list[j], end=' ') # Print cities in a row print('') # Move to the next line after each row
Explanation:
- Outer Loop:
the
for
loop withrange(1, len(travel_list) + 1)
determines the number of rows. Each iteration represents a row.i
controls how many cities are printed in the current row. - Inner Loop:
the
for
loop withrange(i)
iterates over the cities to be printed in the current row.j
accesses the city names in thetravel_list
up to the current row index. - Printing:
the
print(travel_list[j], end=' ')
ensures cities are printed on the same row.print('')
moves to the next line after each row.
Swipe to show code editor
Imagine you have multiple trips planned, and each trip consists of several destinations. The trips are represented as a list of lists, where each inner list contains the destinations for one trip. Your task is to:
- Iterate through each trip (outer list).
- Print all destinations in each trip (inner list).
- Display the trip number before listing its destinations.
Expected Output:
Thanks for your feedback!
Nested for Loop
A nested loop is a loop located within the body of an outer loop. Either the inner or outer loop can take on various forms, including a while
loop or a for
loop. For instance, the outer for
loop can encompass a while
loop, and conversely.
Example: Displaying Cities in a Pattern
We will use a nested loop to print each city multiple times in a structured format, resembling a triangle pattern.
travel_list = ["Monako", "Luxemburg", "Liverpool", "Barcelona", "Munchen"] # Outer loop for controlling rows for i in range(1, len(travel_list) + 1): # Inner loop for controlling columns for j in range(i): print(travel_list[j], end=' ') # Print cities in a row print('') # Move to the next line after each row
Explanation:
- Outer Loop:
the
for
loop withrange(1, len(travel_list) + 1)
determines the number of rows. Each iteration represents a row.i
controls how many cities are printed in the current row. - Inner Loop:
the
for
loop withrange(i)
iterates over the cities to be printed in the current row.j
accesses the city names in thetravel_list
up to the current row index. - Printing:
the
print(travel_list[j], end=' ')
ensures cities are printed on the same row.print('')
moves to the next line after each row.
Swipe to show code editor
Imagine you have multiple trips planned, and each trip consists of several destinations. The trips are represented as a list of lists, where each inner list contains the destinations for one trip. Your task is to:
- Iterate through each trip (outer list).
- Print all destinations in each trip (inner list).
- Display the trip number before listing its destinations.
Expected Output:
Thanks for your feedback!