🌐
PYnative
pynative.com › home › python › nested loops in python
Python Nested Loops [With Examples] – PYnative
September 2, 2021 - Let’s understand this with examples on how nested for loop work in Python. We use for loop to iterates on the given elements of a sequence or iterable. like for i in list. Here time complexity is O(n) because we are iterating all items from a list.
🌐
OpenStax
openstax.org › books › introduction-python-programming › pages › 5-3-nested-loops
5.3 Nested loops - Introduction to Python Programming | OpenStax
March 13, 2024 - In this example, the outer loop iterates over the time's hour portion between 8 and 9, and the inner loop iterates over the time's minute portion between 0 and 59. hour = 8 minute = 0 while hour <= 9: while minute <= 59: print(hour, ":", minute) ...
Discussions

[Python] Can someone please explain to me how nested for loops work using this example. I don't understand how it works
What does this do? for j in range(i,userInput): print('*', end=' ') If you don't know, try it. Assign values to i and userInput manually and see if you can figure it out. More on reddit.com
🌐 r/learnprogramming
15
1
March 11, 2025
Someone please reccomend a good tutorial to learn nested loops.
Single loop: for each finger on your hand, count each finger Nested loop: for each finger on one hand, count each finger on your other hand Double nested loop: for each finger on your right hand, count each finger on your left hand, and for each finger on your left, count all your toes More on reddit.com
🌐 r/learnpython
15
2
July 13, 2021
People also ask

When should I use nested loops in Python?
You should use nested loops in Python when you need to iterate over items within items, such as rows and columns in a table, elements in a matrix, or characters in a list of strings. These nested loops in python examples are common.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › nested for loop in python
Nested For Loop in Python | Logic, Syntax, and Examples
How do nested loops in Python work?
The outer loop executes first. For each iteration of the outer loop, the inner loop completes all its iterations. This is a crucial aspect of how nested loops in python function.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › nested for loop in python
Nested For Loop in Python | Logic, Syntax, and Examples
How does the continue statement work in nested loops?
The continue statement skips the rest of the current iteration of the innermost loop and proceeds to the next iteration of that inner loop. The outer loop is unaffected. The continue statement skips the rest of the current iteration of the innermost loop and proceeds to the next iteration of that inner loop. The outer loop is unaffected.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › nested for loop in python
Nested For Loop in Python | Logic, Syntax, and Examples
🌐
W3Schools
w3schools.com › python › gloss_python_for_nested.asp
Python Nested Loops
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Training ... A nested loop is a loop inside a loop. ...
🌐
Tutorialspoint
tutorialspoint.com › python › python_nested_loops.htm
Python - Nested Loops
You can put any type of loop inside of any other type of loop. For example, a for loop can be inside a while loop or vice versa. The for loop with one or more inner for loops is called nested for loop.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-nested-loops
Python Nested Loops - GeeksforGeeks
In Python, there are two types of loops: for loop and while loop. Using these loops, we can create nested loops, which means loops inside a loop. For example, a while loop inside a for loop, or a for loop inside another for loop.
Published   March 13, 2026
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › nested for loop in python
Nested For Loop in Python | Logic, Syntax, and Examples
March 31, 2026 - You should use nested loops in Python when you need to iterate over items within items, such as rows and columns in a table, elements in a matrix, or characters in a list of strings.
🌐
EDUCBA
educba.com › home › software development › software development tutorials › python tutorial › python nested loops
Python Nested Loops | Complete Guide To Nested Loops in Python
May 10, 2024 - It would be good to briefly touch ... with Python specifically. If a loop exists inside the body of another loop, it is termed as Nested Loop. This means that we want to execute the inner loop code multiple times.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
Find elsewhere
🌐
Toppr
toppr.com › guides › computer-science › introduction-to-python › conditional-constructs-and-looping › nested-loops
Nested Loops: Python Nested Loops, Nested for Loop Syntax, FAQs
May 21, 2021 - Answer 1: A nested loop refers to a loop within a loop, an inner loop within the body of an outer one. Further, the first pass of the outer loop will trigger the inner loop, which will execute to completion.
🌐
Kansas State University
textbooks.cs.ksu.edu › intro-python › 05-loops › 09-nested-for › embed.html
Nested For Loops :: Introduction to Python
June 27, 2024 - This is a very typical structure for nested for loops - the innermost loop will handle printing one line of data, and then the outer for loop is used to determine the number of lines that will be printed. The process for dealing with nested for loops is nearly identical to nested while loops.
🌐
Software Testing Help
softwaretestinghelp.com › home › python programming for beginners – free python tutorials › python loops – for, while, nested loops with examples
Python Loops - For, While, Nested Loops With Examples
April 1, 2025 - While a while loop is a condition-based loop, that executes a block of statements repeatedly as long as its condition is TRUE. ... Answer: Unfortunately, Python doesn’t support the do-while loop. ... Answer: Python generally supports two types of loops: for loop and while loop. However, a third loop[nested loop] can be generated by nesting two or more of these loops. Looping statements in python are used to execute a block of statements or code repeatedly for several times as specified by the user.
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › nested for loops in python
Nested For Loops in Python - Spark By {Examples}
May 31, 2024 - How to implement nested for loops in Python? You can implement nested for loops using various techniques of Python. Python provides two types of loops
🌐
Scaler
scaler.com › home › topics › python nested loops
Python Nested Loops - Scaler Topics
March 22, 2024 - Nested loop in Python involves the inclusion of one loop within another. This construct allows for iterating over elements in multiple dimensions, such as rows and columns of a matrix.
🌐
GeeksforGeeks
geeksforgeeks.org › loops-in-python
Loops in Python - For, While and Nested Loops - GeeksforGeeks
After the loop ends it prints "Inside Else Block" as the else block executes when the loop completes without a break. Python programming language allows to use one loop inside another loop which is called nested loop. Following section shows ...
Published   March 8, 2025
🌐
Python Basics
pythonbasics.org › home › python basics › nested loops
Nested loops - pythonbasics.org
A loop can contain one or more other loops: you can create a loop inside a loop. This principle is known as nested loops. Nested loops go over two or more loops
🌐
Real Python
realpython.com › nested-loops-python
Nested Loops in Python – Real Python
February 22, 2025 - A good analogy for a nested loop is the hour and minute hands of a clock. The hour hand moves slowly around the clock, completing one full revolution every twelve hours. Meanwhile, the minute hand moves at a much faster rate, completing a revolution every hour. While both hands rotate at different speeds, they work together, each completing their own cycle within the same clock. Here’s how the clock logic looks in Python code:
🌐
DataFlair
data-flair.training › blogs › python-loop
Python Loop - Python For Loop, Nested For Loop - DataFlair
April 28, 2026 - Here, we will study Python For ... and Nested For Loop in Python with their subtypes, syntax, and examples. So, let’s start Python Loop Tutorial. When you want some statements to execute a hundred times, you don’t repeat them 100 times. Think of when you want to print numbers 1 to 99. Or that you want to say Hello to 99 friends. In such a case, you can use loops in ...
🌐
TutorialKart
tutorialkart.com › python › python-nested-for-loop
Nested For Loop in Python
February 14, 2025 - The inner loop (for j in range(i)) runs i times in each iteration of the outer loop, printing *. The print() at the end moves the cursor to a new line after each row. ... We can use nested loops to iterate through a two-dimensional list (a matrix).
🌐
Scientech Easy
scientecheasy.com › home › blog › nested loops in python
Nested Loops in Python - Scientech Easy
January 25, 2026 - The general syntax for using nested for loops in Python is as follows: # Outer for loop for iterating_var in sequence: statement-1 # Outer block statement. # Inner for loop for iterating_var in sequence: statement-2 # Inner block statement. statement-3 # Outer block statement. statement-4 · [blocksy-content-block id=”12371″] Let’s understand the working of nested for loop with the help of an example.
Top answer
1 of 3
1

Solution

Let's break the problem down into steps.

  1. First, we need to get an integer from the input, and make sure that the integer is either 3, 10, or any number in between. That's what 3 to 10 inclusive means.

  2. We then need to ask a user for a single character that will be used to print a box.

  3. After we've gotten the integer, and the character, we need to display a box made up of the character whose width and height are determined by the integer.

For step one, we need to get input from the user. This can be done using the builtin function input(). It takes an optionally prompt as an argument, and returns the input as a string. So let's write that down:

integer = input()

Great. Now we need to convert the input to an integer, so it can be used in the rest of our program. This can be accomplished using the built-in function int(). We'll pass in your string, and it will convert it to an integer:

integer = int(input('Enter a number: '))

Step two is to get a character from the user. That's easy. We'll use input() once again to get a single character from the user. but since the input need to be a character, we shouldn't convert this value:

character = input('Enter a character: ')

Alright. This is what we have so far:

integer = int(input('Enter a number: '))
character = input('Enter a character: ')

The last step is to print a box made of the value of character that has a width of integer and a height of integer. Now all we need to do is to print a row with integer number of characters integer number of times. This is where the nested for loop comes in.

We know each row must be the width of integer. So we need to print character that many times. We can use the range() built-in function to do this. We pass in integer as an argument, and it will generate all of the numbers from 0 to integer:

for _ in range(integer):
    print(character, end="")

The the reason we used the underscore in our for loop instead of an actual readable variable name, is because we don't need to use the values that the for loops would give us. We only care about repeating our code.

The end="" part is telling the print() builtin function not to print a newline each time it is called, because we want all the character to be in one row. The print() before the for loop is to add some space between our prompts and the program output. Let's see what our program outputs so far:

Enter a number: 5
Enter a character: @

@@@@@
>>> 

Great. The last thing to do is to print each row integer number of times. Since we know that executing the for loop above prints one row, we just need to execute it integer number of times to print integer rows. So we'll wrap that for loop inside of another for loop and use range() to execute the nested for loop integer number of times. We'll use print() once again, to add a newline between each row:

print()
for _ in range(integer):
    for _ in range(integer):
        print(character, end="")
    print()

And that's it! Were done. Let's test our program:

Enter a number: 7
Enter a character: #

#######
#######
#######
#######
#######
#######
#######
>>> 

As you can see, we're getting our expected output. Here is the final program:

integer = int(input('Enter a number: '))
character = input('Enter a character: ')

print()
for _ in range(integer):
    for _ in range(integer):
        print(character, end="")
    print()

Error handling

You may also need to implement error handling. This means making your program recover from the user entering invalid input. In this case, the only time invalid input could really be enetered is when we are asking the user for an integer. We need to:

  1. make sure the user enetered a valid number. And
  2. make sure that number is either 3, 10 or any number in between.

To do this, we'll need a loop. This is so we can continue to ask the user for valid input, until they do so. We can use a while True loop. This means we'll keep looping until we break from the loop.

while True:

Now we need to make sure the user entered a valid number. We can use a try/except block. The way a try/except block works is that we put a block of code in the try portation that may raise an error. if no error is raised, we continue in the program normally. If an error is raised however, Python will jump to the except block and the code in there will be executed.

In our case we'll try to convert the users input to an integer. If this fails a ValueError will be raised. So we need to catch that error message and print it to the user:

while True:
    try:
        integer = int(input())
    except ValueError:
        print('Please enter a number')

All that's left is to test if the integer entered is in the correct range. If so, we'll break from our loop and continue with the program. If not, we'll display an error message and try again. Here is how that would look:

while True:
    try:
        integer = int(input('Enter a number: '))
        if integer >= 3 and integer <= 10:
            break
        print('Please enter a number between 3 and 10 inclusive')
    except ValueError:
        print('Please enter a number')

character = input('Enter a character: ')

print()
for _ in range(integer):
    for _ in range(integer):
        print(character, end="")
    print()

Here is how the above would look in the full program:

And here is a demo:

Enter a number: -23
Please enter a number between 3 and 10 inclusive
Enter a number: a
Please enter a number
Enter a number: 11
Please enter a number between 3 and 10 inclusive
Enter a number: 10
Enter a character: #

##########
##########
##########
##########
##########
##########
##########
##########
##########
##########
>>> 

For more resources about error handling in a program, see the question: Asking the user for input until they give a valid response.

2 of 3
0

A nested loop is a loop within a loop.

But how does it work?

The outer loop statement gets executed, which triggers the inner statement. The inner loop then executes till its completed..

So the inner statement will keep on going until it has satisfied the statement you have set it to.

Then, the outer loop triggers the inner loop again...and it will keep on triggering the inner loop forever until the outer loop's statement is satisfied.