Python Learning – Day 5: Loops (for, while) with Real-Life Examples

Python Learning – Day 5: Loops (for, while) with Real-Life Examples

Introduction

Welcome to Day 5 of my Python learning journey.
In this lesson, I learned about loops in Python. Loops are used to repeat tasks automatically, which saves time and reduces manual work.

In real life, we repeat tasks daily (like practicing, calculating bills, sending reminders). In programming, loops help us do the same thing efficiently.


📚 Topics Covered in Day 5

In this lesson, I learned:

  • What is a loop in Python
  • for loop and how range() works
  • while loop and condition-based repetition
  • How to use loops with real-life examples
  • Practice programs like even numbers, multiplication table, and sum calculation

🔁 What Is a Loop?

A loop is used to execute a block of code multiple times.

Python mainly provides two types of loops:

  • for loop (counting-based repetition)
  • while loop (condition-based repetition)

for Loop Example (Print 1 to 10)

for number in range(1, 11):
    print(number)

Output:

1
2
3
...
10

range(1, 11) means numbers from 1 to 10 (11 is not included).


while Loop Example (Print 1 to 10)

i = 1
while i <= 10:
    print(i)
    i += 1

The while loop runs as long as the condition is true.


✅ Real-Life Example: Print Even Numbers (1 to 10)

Using for loop:

for number in range(1, 11):
    if number % 2 == 0:
        print(number)

Using while loop:

i = 1
while i <= 10:
    if i % 2 == 0:
        print(i)
    i += 1

The % operator gives the remainder. If a number is divisible by 2, it is even.


🧮 Program 1: Multiplication Table (User Input)

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

for i in range(1, 11):
    result = number * i
    print(number, "x", i, "=", result)

This program prints the multiplication table from 1 to 10.


➕ Program 2: Sum of 1 to N (User Input)

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

if n <= 0:
    print("Please enter a positive number.")
else:
    total = 0
    for i in range(1, n + 1):
        total += i
    print("Sum of 1 to", n, "is:", total)

This program calculates the total sum from 1 to N using a loop.


🌍 Why Loops Are Important?

Loops are used in real-world applications like:

  • Repeating tasks automatically
  • Processing large data lists
  • Running programs until a condition is met (e.g., login systems)
  • Creating patterns and tables
  • Automation and scripts

✅ What I Learned Today

  • How to repeat tasks using loops
  • The difference between for and while
  • Using loops with conditions (even/odd)
  • Building real-life mini projects with loops

🔜 Next Lesson (Day 6)

In Day 6, I will learn about:

  • Lists in Python
  • Storing multiple values in one variable
  • Looping through lists with real-life examples

📢 Final Note

This series is beginner-friendly and public so anyone can learn Python step by step.

Thank you for reading 🚀

Leave a Reply

Your email address will not be published. Required fields are marked *