Python Basics

Loops in Python (for, while, and nested loops)

Introduction

“Python loops for beginners are essential to automate tasks and repeat code efficiently. In this post, we’ll explore for loops, while loops, and nested loops with practical examples.

Welcome back, Python learners!

In Post 4, we learned how to make Python decide what to do using if, elif, and else statements.
But what if we want Python to do something again and again — for example:

Print numbers from 1 to 100

Go through every item in a list

Repeat a task until the user wants to stop

That’s where loops come in!


 What Are Loops?

A loop allows you to execute a block of code multiple times without writing it repeatedly.

For example:

print(“Hello”)

print(“Hello”)

print(“Hello”)

This repeats the same code 3 times. Instead, we can use a loop to do it automatically!


Types of Loops in Python

Python has two main types of loops:

 for loop — runs a fixed number of times
 while loop — runs until a condition becomes False

Let’s explore both!


 1. The for Loop

The for loop is used to iterate (loop) over a sequence — like a list, string, or range of numbers.

Basic Syntax:

for variable in sequence:

 Example 1: Using range()

for i inrange(5):

    print(“Hello, Python!”)

 Output:

Hello, Python!

Hello, Python!

Hello, Python!

Hello, Python!

Hello, Python!

 range(5) means: 0, 1, 2, 3, 4 (it stops before 5).


 Example 2: Print numbers from 1 to 10

for i inrange(1, 11):

    print(i)

🧾 Output:

1

2

3

4

5

6

7

8

9

10


 Example 3: Loop through a list

fruits = [“apple”, “banana”, “mango”]

for fruit in fruits:

    print(“I like”, fruit)

🧾 Output:

I like apple

I like banana

I like mango


 Example 4: Loop through a string

for letter in”Python”:

    print(letter)

 Output:

P

y

t

h

o

n


 2. The while Loop

The while loop repeats code as long as a condition is True.

 Syntax:

while condition:

 Example 1: Print numbers 1 to 5

i =

while i <= 5:

    print(i)

    i +=

 Output:

1

2

3

4

5

 Be careful!
If you forget to update the variable (like i += 1), you’ll create an infinite loop that never stops.


 Example 2: Countdown Timer

count =

while count > 0:

    print(count)

    count -=

print(“Time’s up!”)

 Output:

5

4

3

2

1

Time’s up!


 Combining Loops and Conditions

You can use loops with if statements to make powerful logic.

 Example: Print even numbers from 1 to 10

for i inrange(1, 11):

    if i % 2 == 0:

        print(i)

🧾 Output:

2

4

6

8

10


 Breaking and Skipping Loops

Sometimes you may want to:

Stop the loop early → use break

Skip one iteration → use continue

 Example 1: Using break

for i inrange(1, 10):

    if i == 5:

    print(i)

 Output:

1

2

3

4

 The loop stopped when i == 5.


 Example 2: Using continue

for i inrange(1, 10):

    if i == 5:

    print(i)

 Output:

1

2

3

4

6

7

8

9

 The loop skipped printing 5.


 Nested Loops (Loop inside a Loop)

Sometimes, you need one loop inside another loop.

 Example:

for i inrange(1, 4):

    for j inrange(1, 4):

        print(i, j)

 Output:

1 1

1 2

1 3

2 1

2 2

2 3

3 1

3 2

3 3


 Fun Example: Multiplication Table

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

for i inrange(1, 11):

    print(num, “x”, i, “=”, num * i)

 Output Example:

Enter a number: 5

5 x 1 = 5

5 x 2 = 10

5 x 10 = 50


 Bonus: Infinite Loop Example (Be Careful!)

whileTrue:

    name = input(“Enter your name (type ‘exit’ to stop): “)

    if name.lower() == “exit”:

    print(“Hello,”, name)

 Output Example:

Enter your name (type’exit’to stop): John

Hello, John

Enter your name (type’exit’to stop): exit


 Practice Tasks

Try these to master loops

 Print all odd numbers between 1 and 50.
 Write a program that prints the sum of numbers from 1 to 100.
 Ask the user for a number and check if it’s prime or not (using a loop).
 Print this pattern:

*

* *

* * *

* * * *

* * * * *

 Create a menu-driven program using while True that lets users:

Add two numbers

Subtract two numbers

Exit when they choose



 Summary

You’ve learned:

How to use for loops and while loops

The difference between fixed and conditional looping

Using break, continue, and nested loops

Real-world examples like calculators and patterns

Loops make your programs automated and efficient — you’ll use them constantly as a Python developer!

About the author

guestpostlinkingum@gmail.com

Leave a Comment