0% found this document useful (0 votes)
2 views

9 Python Nested Loops

Nested loops in Python consist of an outer loop and one or more inner loops, allowing for repetitive execution of code blocks. Both 'for' and 'while' loops can be nested, enabling the iteration over sequences or the execution of code until a condition is met. Examples are provided for both nested for loops and nested while loops, demonstrating their syntax and functionality.

Uploaded by

rushikeshkonde1
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

9 Python Nested Loops

Nested loops in Python consist of an outer loop and one or more inner loops, allowing for repetitive execution of code blocks. Both 'for' and 'while' loops can be nested, enabling the iteration over sequences or the execution of code until a condition is met. Examples are provided for both nested for loops and nested while loops, demonstrating their syntax and functionality.

Uploaded by

rushikeshkonde1
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Python - Nested Loops

In Python, when you write one or more loops within a loop statement that is known as a nested
loop.

The main loop is considered as outer loop and loop(s) inside the outer loop are known as inner
loops.

The Python programming language allows the use of one loop inside another loop.

A loop is a code block that executes specific instructions repeatedly.

There are two types of loops, namely for and while, using which we can create nested loops.

Python Nested for Loop

The for loop with one or more inner for loops is called nested for loop.

A for loop is used to loop over the items of any sequence, such as a list, tuple or a string and
performs the same action on each item of the sequence.

for iterating_var in sequence:

for iterating_var in sequence:

statements(s)

statements(s)

months = ["jan", "feb", "mar"]

days = ["sun", "mon", "tue"]

for x in months:

for y in days:

print(x, y)

print("Good bye!")
Python Nested while Loop

The while loop having one or more inner while loops are nested while loop.

A while loop is used to repeat a block of code for an unknown number of times until the specified
boolean expression becomes TRUE.

while expression:

while expression:

statement(s)

statement(s)

i=2

while(i < 25):

j=2

while(j <= (i/j)):

if not(i%j): break

j=j+1

if (j > i/j) : print (i, " is prime")

i=i+1

print ("Good bye!")

You might also like