Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
First Nested Loop Pattern | 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

First Nested Loop Pattern

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.

Initially, let's attempt to create a basic image using a nested loop.

Employ end = ' ' within the print function to display elements in a row.

Examine the code below:

1234567
# Printing a triangle # Outer for loop for i in range(1, 5): # Inner for loop for j in range(1, i + 1): print('*', end=' ') print('')

How does the code work?

Task

You should reverse the triangle shown in the example.

  1. Configure the range() function for the outer for loop with step = -1.
  2. Configure the range() function for the inner for loop.
  3. Print '*' within the nested loop.

Task

You should reverse the triangle shown in the example.

  1. Configure the range() function for the outer for loop with step = -1.
  2. Configure the range() function for the inner for loop.
  3. Print '*' within the nested loop.

Everything was clear?

Section 3. Chapter 1
toggle bottom row

First Nested Loop Pattern

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.

Initially, let's attempt to create a basic image using a nested loop.

Employ end = ' ' within the print function to display elements in a row.

Examine the code below:

1234567
# Printing a triangle # Outer for loop for i in range(1, 5): # Inner for loop for j in range(1, i + 1): print('*', end=' ') print('')

How does the code work?

Task

You should reverse the triangle shown in the example.

  1. Configure the range() function for the outer for loop with step = -1.
  2. Configure the range() function for the inner for loop.
  3. Print '*' within the nested loop.

Task

You should reverse the triangle shown in the example.

  1. Configure the range() function for the outer for loop with step = -1.
  2. Configure the range() function for the inner for loop.
  3. Print '*' within the nested loop.

Everything was clear?

Section 3. Chapter 1
toggle bottom row

First Nested Loop Pattern

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.

Initially, let's attempt to create a basic image using a nested loop.

Employ end = ' ' within the print function to display elements in a row.

Examine the code below:

1234567
# Printing a triangle # Outer for loop for i in range(1, 5): # Inner for loop for j in range(1, i + 1): print('*', end=' ') print('')

How does the code work?

Task

You should reverse the triangle shown in the example.

  1. Configure the range() function for the outer for loop with step = -1.
  2. Configure the range() function for the inner for loop.
  3. Print '*' within the nested loop.

Task

You should reverse the triangle shown in the example.

  1. Configure the range() function for the outer for loop with step = -1.
  2. Configure the range() function for the inner for loop.
  3. Print '*' within the nested loop.

Everything was clear?

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.

Initially, let's attempt to create a basic image using a nested loop.

Employ end = ' ' within the print function to display elements in a row.

Examine the code below:

1234567
# Printing a triangle # Outer for loop for i in range(1, 5): # Inner for loop for j in range(1, i + 1): print('*', end=' ') print('')

How does the code work?

Task

You should reverse the triangle shown in the example.

  1. Configure the range() function for the outer for loop with step = -1.
  2. Configure the range() function for the inner for loop.
  3. Print '*' within the nested loop.

Section 3. Chapter 1
Switch 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