- Break from the inner loop (if there's nothing else after it)
- Put the outer loop's body in a function and return from the function
- Raise an exception and catch it at the outer level
- Set a flag, break from the inner loop and test it at an outer level.
- Refactor the code so you no longer have to do this.
I would go with 5 every time.
Answer from Duncan on Stack OverflowGeeksforGeeks
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.
Top answer 1 of 5
10
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.
2 of 5
5
Break it down by how the Python interpreter would look at it. The user enters a number. Let's say 3. The first for loop resolves to "for i in range(3)". So for the first loop, i = 0. The second loop then resolves to "for j in range(0, 3)." Because it uses the variable i from the parent loop. That prints "* "three times, because it takes three loops to satisfy range(0, 3) When it completes that, it goes back to the first loop and increments i by 1. The next time the nested loop runs, it's now "for j in range(1, 3)" which will only loop two times. Because the user input is range(3), the first loop will only execute three times before exiting.
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
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
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
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
Videos
09:29
Nested for Loop in Python - YouTube
Nested loops in Python are easy
16:43
Python Nested Loops are Easy | A Must-Have Skill for Data Engineers ...
07:22
Nested Loops Explained (Python From Zero To One Part 16) - YouTube
04:47
Python nested loops โฟ - YouTube
Nested For loop | Nesting of For loops | How to use Nested For ...
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.
Top answer 1 of 8
84
- Break from the inner loop (if there's nothing else after it)
- Put the outer loop's body in a function and return from the function
- Raise an exception and catch it at the outer level
- Set a flag, break from the inner loop and test it at an outer level.
- Refactor the code so you no longer have to do this.
I would go with 5 every time.
2 of 8
31
Here's a bunch of hacky ways to do it:
Create a local function
for a in b: def doWork(): for c in d: for e in f: if somecondition: return # <continue the for a in b loop?> doWork()A better option would be to move doWork somewhere else and pass its state as arguments.
Use an exception
class StopLookingForThings(Exception): pass for a in b: try: for c in d: for e in f: if somecondition: raise StopLookingForThings() except StopLookingForThings: pass
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.
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.
Top answer 1 of 11
60
If you're frequently iterating over a Cartesian product like in your example, you might want to investigate Python 2.6's itertools.product -- or write your own if you're in an earlier Python.
from itertools import product
for y, x in product(range(3), repeat=2):
do_something()
for y1, x1 in product(range(3), repeat=2):
do_something_else()
2 of 11
15
This is fairly common when looping over multidimensional spaces. My solution is:
xy_grid = [(x, y) for x in range(3) for y in range(3)]
for x, y in xy_grid:
# do something
for x1, y1 in xy_grid:
# do something else
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.