0% found this document useful (0 votes)
3 views6 pages

Python Assignment2.1

The document provides an overview of using loops in Python, including for loops, while loops, and nested loops, to efficiently perform repetitive tasks. It includes multiple examples demonstrating how to implement these loops for various applications such as printing numbers, calculating sums, and generating patterns. The conclusion emphasizes the benefits of loops in simplifying code and enhancing readability.
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 views6 pages

Python Assignment2.1

The document provides an overview of using loops in Python, including for loops, while loops, and nested loops, to efficiently perform repetitive tasks. It includes multiple examples demonstrating how to implement these loops for various applications such as printing numbers, calculating sums, and generating patterns. The conclusion emphasizes the benefits of loops in simplifying code and enhancing readability.
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

Topic: Write Programs Using Loops (for, while, and Nested

Loops`) to Solve Repetitive Tasks


Assignment Title:
Implementation of Loops (for, while, and Nested loops`) in Python to Perform
Repetitive Tasks

Objective:
To understand how loops work in Python and use them to execute repetitive tasks efficiently
using for, while, and nested loops.

1. Introduction
Loops in Python are used to repeat a block of code multiple times without writing it again.
They help reduce redundancy and make the program efficient.

Python supports two main types of loops:

• for loop — used to iterate over a sequence like list, tuple, string, or range.
• while loop — executes a block as long as a given condition is true.
• Nested loop — a loop inside another loop, used for working with patterns, matrices,
etc.

2. Real-Life Example Programs


Example 1: Print Numbers from 1 to 10 using for Loop
for i in range(1, 11):
print(i)

Output:

1
2
3
4
5
6
7
8
9
10

Example 2: Sum of First N Natural Numbers using while Loop


n = int(input("Enter N: "))
sum = 0
i = 1

while i <= n:
sum += i
i += 1

print("Sum =", sum)

Output:

Enter N: 5
Sum = 15

Example 3: Print Multiplication Table


num = int(input("Enter a number: "))

for i in range(1, 11):


print(num, "x", i, "=", num * i)

Output:

Enter a number: 7
7 x 1 = 7
...
7 x 10 = 70

Example 4: Countdown Timer using while Loop


count = 5
while count > 0:
print("Countdown:", count)
count -= 1
print("Time’s up!")

Output:

Countdown: 5
Countdown: 4
Countdown: 3
Countdown: 2
Countdown: 1
Time’s up!

Example 5: Display Even Numbers between 1 and 20


for i in range(1, 21):
if i % 2 == 0:
print(i)

Output:
2
4
6
8
...
20

Example 6: Calculate Factorial using while Loop


n = int(input("Enter a number: "))
fact = 1
i = 1

while i <= n:
fact *= i
i += 1

print("Factorial of", n, "is", fact)

Output:

Enter a number: 5
Factorial of 5 is 120

Example 7: Print Characters in a String


word = input("Enter a word: ")

for char in word:


print(char)

Output:

Enter a word: Python


P
y
t
h
o
n

Example 8: Nested Loop – Print a Square Pattern


for i in range(5):
for j in range(5):
print("*", end=" ")
print()

Output:

* * * * *
* * * * *
* * * * *
* * * * *
* * * * *

Example 9: Nested Loop – Right Angle Triangle Pattern


for i in range(1, 6):
for j in range(i):
print("*", end=" ")
print()

Output:

*
* *
* * *
* * * *
* * * * *

Example 10: Reverse a Number using while Loop


num = int(input("Enter a number: "))
rev = 0

while num > 0:


digit = num % 10
rev = rev * 10 + digit
num //= 10

print("Reversed number:", rev)

Output:

Enter a number: 1234


Reversed number: 4321

Example 11: Nested Loop – Multiplication Table (1 to 5)


for i in range(1, 6):
for j in range(1, 6):
print(f"{i*j:3}", end=" ")
print()

Output:

1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25

Example 12: Print Sum of Digits


num = int(input("Enter a number: "))
sum = 0

while num > 0:


sum += num % 10
num //= 10

print("Sum of digits:", sum)

Output:

Enter a number: 123


Sum of digits: 6

Example 13: Print Fibonacci Sequence


n = int(input("Enter how many terms: "))
a, b = 0, 1

for i in range(n):
print(a, end=" ")
a, b = b, a + b

Output:

Enter how many terms: 6


0 1 1 2 3 5

Example 14: Nested Loop – Number Pattern


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

Output:

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Example 15: Sum of Even and Odd Numbers Separately


even_sum = 0
odd_sum = 0

for i in range(1, 11):


if i % 2 == 0:
even_sum += i
else:
odd_sum += i
print("Sum of even numbers:", even_sum)
print("Sum of odd numbers:", odd_sum)

Output:

Sum of even numbers: 30


Sum of odd numbers: 25

3. Conclusion
From this assignment, we learned how to:

• Use for loops to iterate over sequences.


• Use while loops for condition-based repetition.
• Use nested loops for patterns and tabular problems.
Loops simplify repetitive tasks and make code concise, efficient, and readable.

You might also like