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

Practical 3 Iteration Statements

The document provides examples of iteration statements in Python, including for loops and while loops. It includes code snippets demonstrating how to calculate factorials, sum a series, and create various patterns using nested loops. Outputs for each code example are also provided to illustrate the results of the operations.
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 views

Practical 3 Iteration Statements

The document provides examples of iteration statements in Python, including for loops and while loops. It includes code snippets demonstrating how to calculate factorials, sum a series, and create various patterns using nested loops. Outputs for each code example are also provided to illustrate the results of the operations.
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/ 4

Practical 3 – Iteration Statements

Loops in Python

• for loop (and nesting of for loop)


• while loop

Code #1
number = int(input("Enter a number:"))

product = 1

for i in range(1,number+1):

product = product * i

print("Factorial of given number = ",product)

Output #1 :
Enter a number:7

Factorial of given number = 5040

----------------------------------------------------

Enter a number:0

Factorial of given number = 1

Code #2
# Sum of Series 3 7 11 15 19...............

sum = 0

i = 3

while i <= 200:

sum = sum + i

i = i + 4

print("Sum of Natural Numbers from 1 to 10 = ",sum)

Output #2 :
Sum of Natural Numbers from 1 to 10 = 5050

------------------------------------------------
Code #3 -
for x in range(1,4): # 1,2,3

for y in range(1,5): #1,2,3,4

print("x = ",x, "y = ",y)

Output #3 :
x = 1 y = 1

x = 1 y = 2

x = 1 y = 3

x = 1 y = 4

x = 2 y = 1

x = 2 y = 2

x = 2 y = 3

x = 2 y = 4

x = 3 y = 1

x = 3 y = 2

x = 3 y = 3

x = 3 y = 4

Code #4 -
# StarBox

for i in range(1,6): # 1,2,3,4

for j in range(1,i+1): # 1,2,3,4,5

print(i," ", end ="")

print()

print("-----------------------------------")

for i in range(5,0,-1): # 5,4,3,2,1

for j in range(1,i+1): # 1,2,3,4,5

print(i," ", end ="")

print()

Output #4 :
1

2 2

3 3 3

4 4 4 4

5 5 5 5 5
-----------------------------------

5 5 5 5 5

4 4 4 4

3 3 3

2 2

Code #5 -
for i in range(1,11):

for j in range(1,10-i+1):

print(" ",end="")

for k in range(1,i+1):

print("* ",end="")

print()

print("replace * with i (row no.) -----------------------")

for i in range(1,11):

for j in range(1,10-i+1):

print(" ",end="")

for k in range(1,i+1):

print(i," ",end="")

print()

print("replace * with j (column/star no.) -----------------------")

for i in range(1,11):

for j in range(1,10-i+1):

print(" ",end="")
for k in range(1,i+1):

print(k," ",end="")

print()

Output #5 :
*

* *

* * *

* * * *

* * * * *

* * * * * *

* * * * * * *

* * * * * * * *

* * * * * * * * *

* * * * * * * * * *

replace * with i (row no.) -----------------------

2 2

3 3 3

4 4 4 4

5 5 5 5 5

6 6 6 6 6 6

7 7 7 7 7 7 7

8 8 8 8 8 8 8 8

9 9 9 9 9 9 9 9 9

10 10 10 10 10 10 10 10 10 10

replace * with j (column/star no.) -----------------------

1 2

1 2 3

1 2 3 4

1 2 3 4 5

1 2 3 4 5 6

1 2 3 4 5 6 7

1 2 3 4 5 6 7 8

1 2 3 4 5 6 7 8 9

1 2 3 4 5 6 7 8 9 10

You might also like