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

Nested loop

The document explains nested for loops in Python, highlighting their syntax and providing examples of their use in generating multiplication tables and various patterns. It includes multiple pattern programs demonstrating how to create shapes and sequences using nested loops. Additionally, it features output examples for each program to illustrate the results of the code.

Uploaded by

tester1623096
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)
3 views

Nested loop

The document explains nested for loops in Python, highlighting their syntax and providing examples of their use in generating multiplication tables and various patterns. It includes multiple pattern programs demonstrating how to create shapes and sequences using nested loops. Additionally, it features output examples for each program to illustrate the results of the code.

Uploaded by

tester1623096
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/ 11

Topic: Nested For Loops in Python

In Python, a for loop is a control structure that allows you to


iterate over a sequence (like a list or a range). A nested for loop
is a loop inside another loop. It's a powerful programming
concept that allows you to perform repetitive tasks with more
complex patterns.
Syntax of Nested For Loop:
for variable1 in sequence1:
# Outer loop code
for variable2 in sequence2:
# Inner loop code
# Rest of the outer loop code
Multiplication table (single loop):
n=int(input("enter n"))
for i in range(1, 11):
print(i, "*",n, "=", i * n)
O/P:
enter n 5
1*5=5
2 * 5 = 10
3 * 5 = 15
4 * 5 = 20
5 * 5 = 25
6 * 5 = 30
7 * 5 = 35
8 * 5 = 40
9 * 5 = 45
10 * 5 = 50
Multiplication table (nested loop):
for i in range(1, 6):
for j in range(1, 11):
print(i, "*", j, "=", i * j)
O/P:
1*1=1
1*2=2
1*3=3
1*4=4
1*5=5
1*6=6
1*7=7
1*8=8
1*9=9
1 * 10 = 10
2*1=2
2*2=4
2*3=6
2*4=8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
2 * 10 = 20
3*1=3
3*2=6
3*3=9
3 * 4 = 12
3 * 5 = 15
3 * 6 = 18
3 * 7 = 21
3 * 8 = 24
3 * 9 = 27
3 * 10 = 30
4*1=4
4*2=8
4 * 3 = 12
4 * 4 = 16
4 * 5 = 20
4 * 6 = 24
4 * 7 = 28
4 * 8 = 32
4 * 9 = 36
4 * 10 = 40
5*1=5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
PATTERN PROGRAMS
1. Simple square Pattern:1
for i in range(4):
for j in range(4):
print("*", end=" ")
print()
O/P:
****
****
****
****
2. Pattern:2-star
for i in range(1,6):
for j in range(1,i+1):
print('*',end=' ')
print()
O/P:
*
**
***
****
*****
3. Pattern:3
for i in range(1,6):
for j in range(i+1,7):
print('*',end='')
print()
O/P:
*****
****
***
**
*
4. Square Pattern 4:-using number
n=5
for i in range(1, n + 1):
for j in range(1, n + 1):
print(i, end=" ")
print()
O/P:
11111
22222
33333
44444
55555
5. Pattern 5: 1,22,333,4444
for i in range(1,6):
for j in range(i):
print(i,end=' ')
print()
O/P:
1
22
333
4444
55555

6. Number pattern 6 :1,12,123,1234


for i in range(1, 5):
for j in range(1, i + 1):
print(j, end=" ")
print()
O/P:
1
12
123
1234
7. Number Pattern 7: 1234,123,12,1
for i in range(5,1,-1):
for j in range(1,i):
print(j,end=" ")
print()
O/P:
1234
123
12
1

8. Character pattern :A,AB,ABC,ABCD,ABCDE..,


n=5
for i in range(n) :
t = 65
for j in range(i + 1) :
print(chr(t), end = ' ')
t += 1
print()

(alternate code)
for i in range(65,70,1):
for j in range(65,i+1,1):
print(chr(j),end=' ')
print()

o/p:
A
AB
ABC
ABCD
ABCDE
9. Number Pattern 8 : 5,54,543,5432,..
for i in range(5,0,-1):
for j in range(5,i-1,-1):
print(j,end='')
print()
O/P:
5
54
543
5432
54321

10. Number Pattern 9: (54321,5432,..)


for i in range(1,6):
for j in range(5,i-1,-1):
print(j,end='')
print()

O/P:
54321
5432
543
54
5
11. Number Pattern 10 : 1,13,135,1357..
for i in range(1,6):
for j in range(1,i+1):
print(j*2-1,end=' ')
print()

O/P:
1
13
135
1357
13579

12. Number Pattern 11 : 1,135,1357,13579..


for i in range(1,6):
for j in range(1,i+i):
print(j*2-1,end=' ')
print()
O/P:
1
135
13579
1 3 5 7 9 11 13
1 3 5 7 9 11 13 15 17
1.Guess the output:
n=1
for a in range(5):
print(n)
n=n*10+1

o/p:
1
11
111
1111
11111

2.Guess the output:


for i in range(4,0,-1):
for j in range(4,4-i,-1):
print(j,end=' ')
print()

o/p:
4321
432
43
4
3.Guess the output:
for i in range(1,5):
for j in range(5-i):
print(i,end=" ")
print()

o/p:
1111
222
33
4

You might also like