1. Break from the inner loop (if there's nothing else after it)
  2. Put the outer loop's body in a function and return from the function
  3. Raise an exception and catch it at the outer level
  4. Set a flag, break from the inner loop and test it at an outer level.
  5. Refactor the code so you no longer have to do this.

I would go with 5 every time.

Answer from Duncan on Stack Overflow
๐ŸŒ
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.
Published ย  March 13, 2026
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ [python] can someone please explain to me how nested for loops work using this example. i don't understand how it works
r/learnprogramming on Reddit: [Python] Can someone please explain to me how nested for loops work using this example. I don't understand how it works
March 11, 2025 -
userInput = int(input())

for i in range(userInput):
    for j in range(i,userInput):
        print('*', end=' ')
    print()

input is: 3

the output is:

* * * 
* * 
* 

I have been looking at videos and different websites, but i cannot understand it. 
Discussions

How to continue in nested loops in Python - Stack Overflow
How can you continue the parent loop of say two nested loops in Python? for a in b: for c in d: for e in f: if somecondition: More on stackoverflow.com
๐ŸŒ stackoverflow.com
Python Code for nested loops
Does anyone know how to print the following pattern using nested loops? 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 More on discuss.python.org
๐ŸŒ discuss.python.org
3
0
August 31, 2022
In python is there an easier way to write 6 nested for loops? - Stack Overflow
This problem has been getting at me for a while now. Is there an easier way to write nested for loops in python? For example if my code went something like this: for y in range(3): for x in ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Nested loops
Actually, that does not print 1 to 10, it will print 1 to 9. Perhaps adding information to show which loop iteration you're on would make more sense: >>> for i in range(3): ... for k in range(3): ... print(i, k) ... (0, 0) (0, 1) (0, 2) (1, 0) (1, 1) (1, 2) (2, 0) (2, 1) (2, 2) So, we have the i-loop with three values: 0, 1, 2. We start with the i equal to zero. Our next instruction is another loop, the k-loop, with three values: 0, 1, 2. So, we print out i and k for each iteration through the k-loop sequence, with i still zero. After we complete the k-loop, there are no more instructions in the i-loop, so we go to the next value in the i-sequence: 1. And, we do that again and again, until we're done with the i-loop values. More on reddit.com
๐ŸŒ r/learnpython
11
52
February 18, 2019
๐ŸŒ
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.
๐ŸŒ
Mimo
mimo.org โ€บ glossary โ€บ python โ€บ nested-loops
Python Nested Loops: Syntax, Usage, and Examples
Nested loops in Python are loops placed inside other loops, so the inner loop runs fully for every single step of the outer loop.
๐ŸŒ
Medium
medium.com โ€บ @dnaresh2323 โ€บ nested-loops-pattern-printing-in-python-8d3503340f1d
Nested Loops & Pattern Printing in Python | by D Naresh | Medium
August 18, 2025 - Nested loops = loops inside loops. The outer loop controls the rows, the inner loop controls the columns. Useful for working with grids, tables, and pattern printing. ๐Ÿ“– Missed the previous articles?
Find elsewhere
๐ŸŒ
Kansas State University
textbooks.cs.ksu.edu โ€บ intro-python โ€บ 05-loops โ€บ 09-nested-for
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. So, we wonโ€™t go through a full example using Python Tutor.
๐ŸŒ
Real Python
realpython.com โ€บ nested-loops-python
Nested Loops in Python โ€“ Real Python
February 22, 2025 - Browse Topics Guided Learning Paths ... web-scraping ... Nested loops in Python allow you to place one loop inside another, enabling you to perform repeated actions over multiple sequences....
๐ŸŒ
CSE CGI Server
cgi.cse.unsw.edu.au โ€บ ~cs2041 โ€บ 26T1 โ€บ assignments โ€บ ass2 โ€บ index.html
COMP(2041|9044) 26T1 โ€” Assignment 2: Sharpie
April 14, 2026 - The for, in, do, and done keywords are used to start and end for loops. For example: Extra whitespace has been added to the Python code so that the position of each line matches the Shell code ยท You do not need to add extra whitespace to your Python code. In this subset you do not need to handle nested for loops.
๐ŸŒ
CodeSignal
codesignal.com โ€บ learn โ€บ courses โ€บ exploring-iterations-and-loops-in-python โ€บ lessons โ€บ exploring-nested-loops-in-python
Exploring Nested Loops in Python
Nested loops signify the placement of one loop inside another. These loops prove particularly useful when we need to address scenarios involving more than one sequence, or when the number of iterations depends on the data itself.
๐ŸŒ
OpenStax
openstax.org โ€บ books โ€บ introduction-python-programming โ€บ pages โ€บ 5-3-nested-loops
5.3 Nested loops - Introduction to Python Programming | OpenStax
March 13, 2024 - A nested loop has one or more loops within the body of another loop. The two loops are referred to as outer loop and inner loop. The outer loop controls the number of the inner loop's full execution.
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
Python Code for nested loops - Python Help - Discussions on Python.org
August 31, 2022 - Does anyone know how to print the following pattern using nested loops? 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
๐ŸŒ
Code.org
studio.code.org โ€บ courses โ€บ express-2025 โ€บ units โ€บ 1
Unit: Express Course (2025) - Code.org
Learn computer science by trying the lessons below at your own pace! Learn to create computer programs, develop problem-solving skills, and work through fun challenges! Make games and creative projec.
๐ŸŒ
W3Schools
w3schools.com โ€บ c โ€บ c_for_loop_nested.php
C Nested Loops
It is also possible to place a loop inside another loop. This is called a nested loop.
๐ŸŒ
Cbseacademic
cbseacademic.nic.in โ€บ web_material โ€บ CurriculumMain26 โ€บ SrSec โ€บ Computer_Science_SrSec_2025-26.pdf pdf
COMPUTER SCIENCE Subject Code - 083 Class XI (2025-26) 1. Learning Outcomes
Python Programming ยท โ— Input a welcome message and display it. โ— Input two numbers and display the larger / smaller number. โ— Input three numbers and display the largest / smallest number. โ— Generate the following patterns using nested loops: Pattern-1 ยท
๐ŸŒ
Tutedude
tutedude.com โ€บ category โ€บ python
Learn Python: Start Your Programming Journey with Tutedude
You can watch the lessons anytime, learn at your own speed, and complete the course along with assignments/projects as per your schedule. You also get live 1:1 doubt support from industry experts between 7 PM to 10 PM, Monday to Saturday. ... No prior AI experience is needed. Basic Python is helpful but not mandatory โ€” the first phase of the challenge covers everything you need from scratch.
๐ŸŒ
FastAPI
fastapi.tiangolo.com โ€บ async
Concurrency and async / await - FastAPI
Anyway, in any of the cases above, FastAPI will still work asynchronously and be extremely fast. But by following the steps above, it will be able to do some performance optimizations. Modern versions of Python have support for "asynchronous code" using something called "coroutines", with async and await syntax.
๐ŸŒ
Claude Code Docs
code.claude.com โ€บ docs โ€บ en โ€บ agent-sdk โ€บ agent-loop
How the agent loop works - Claude Code Docs
1 day ago - Each interaction with the SDK creates or continues a session. Capture the session ID from ResultMessage.session_id (available in both SDKs) to resume later. The TypeScript SDK also exposes it as a direct field on the init SystemMessage; in Python itโ€™s nested in SystemMessage.data.
๐ŸŒ
Geek Python
geekpython.in โ€บ nested-for-loops-in-python
Understanding Nested for Loops in Python - How Does it Work
March 1, 2024 - In Python, nested for loops are loops that have one or more for loops within them.