0% found this document useful (0 votes)
2 views2 pages

Nested Loops

This document is a tutorial on nested loops in Python, explaining their definition, usage, and syntax. It provides examples of nested loops using both 'for' and 'while' constructs, as well as an interview question related to printing prime numbers. The tutorial is part of a larger Python series available on YouTube.

Uploaded by

ss2023040
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Nested Loops

This document is a tutorial on nested loops in Python, explaining their definition, usage, and syntax. It provides examples of nested loops using both 'for' and 'while' constructs, as well as an interview question related to printing prime numbers. The tutorial is part of a larger Python series available on YouTube.

Uploaded by

ss2023040
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

PYTHON TUTORIAL FOR BEGINNERS

Source: www.youtube.com/@RishabhMishraOfficial

Chapter - 15

Nested Loops in Python


• Nested Loops Definition
• Nested Loops Examples
• Nested Loops Interview Ques

Nested Loops in Python


Loop inside another loop is nested loop. This means that for every single time the outer
loop runs, the inner loop runs all of its iterations.

Why Use Nested Loops?


• Handling Multi-Dimensional Data: Such as matrices, grids, or lists of lists.
• Complex Iterations: Operations depend on multiple variables or dimensions.
• Pattern Generation: Creating patterns, such as in graphics or games.

Nested Loop Syntax

Syntax:

Outer_loop:
inner_loop:
# Code block to execute - inner loop
# Code block to execute - outer loop

P y t h o n N o t e s b y R i s h a b h M i s h ra
Nested Loop Example
Example: Print numbers from 1 to 3, for 3 times using for-for
nested loop

for i in range(3): # Outer for loop (runs 3 times)


for j in range(1,4):
print(j)
print()

Example: Print numbers from 1 to 3, for 3 times using while-


for nested loop

i = 1
while i < 4: # Outer while loop (runs 3 times)
for j in range(1, 4):
print(j)

print()
i += 1

Nested Loop Interview Question


Example: Print prime numbers from 2 to 10

for num in range(2, 10):


for i in range(2, num):
if num % i == 0:
break
else:
print(num)

Python Tutorial Playlist: Click Here


https://www.youtube.com/playlist?list=PLdOKnrf8EcP384Ilxra4UlK9BDJGwawg9

P y t h o n N o t e s b y R i s h a b h M i s h ra

You might also like