Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Nested for Loop | Nested Loops
Python Loops Tutorial
course content

Course Content

Python Loops Tutorial

Python Loops Tutorial

1. The For Loop
2. The while Loop
3. Nested Loops
4. List and Dictionary Comprehensions

book
Nested for Loop

We will use a nested loop to print each city multiple times in a structured format, resembling a triangle pattern.

12345678
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
copy
  1. Outer loop: the for loop with range(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;
  2. Inner loop: the for loop with range(i) iterates over the cities to be printed in the current row. j accesses the city names in the travel_list up to the current row index;
  3. Printing: the print(travel_list[j], end=' ') ensures cities are printed on the same row. print('') moves to the next line after each row.
Task

Swipe to start coding

You are working on a navigation system that processes lists of trips, where each trip includes multiple countries. However, a data processing error has caused all country names to appear in lowercase, making them unreadable by the system.
To fix this issue, you need to extract and format the country names correctly.

  • Extract all country names from trips and store them in countries.
  • Capitalize each country name before adding it to countries using capitalize() method.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 1
toggle bottom row

book
Nested for Loop

We will use a nested loop to print each city multiple times in a structured format, resembling a triangle pattern.

12345678
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
copy
  1. Outer loop: the for loop with range(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;
  2. Inner loop: the for loop with range(i) iterates over the cities to be printed in the current row. j accesses the city names in the travel_list up to the current row index;
  3. Printing: the print(travel_list[j], end=' ') ensures cities are printed on the same row. print('') moves to the next line after each row.
Task

Swipe to start coding

You are working on a navigation system that processes lists of trips, where each trip includes multiple countries. However, a data processing error has caused all country names to appear in lowercase, making them unreadable by the system.
To fix this issue, you need to extract and format the country names correctly.

  • Extract all country names from trips and store them in countries.
  • Capitalize each country name before adding it to countries using capitalize() method.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 1
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt