Skip to main content

Python Loops: for Loop and while Loop with Examples

 ✍️Introduction

In programming, we often need to repeat a task multiple times.

For example:

  • Print numbers from 1 to 10
  • Display all items in a list

Python uses loops to repeat a block of code.


In this article, you will learn:

  • What loops are
  • for loop
  • while loop
  • break and continue statements

What are Loops in Python?

Loops are used to execute a block of code repeatedly until a condition is satisfied.

Python mainly has two loops:

1. for loop

2. while loop

1️⃣ Python for Loop

The for loop is used to iterate over a sequence such as:

  • list
  • tuple
  • string
  • range

Syntax:

for variable in sequence:
    statement


Example 1: Print numbers

for i in range(1, 6):
    print(i)

Example 2: Loop through a list

languages = ["Python", "Java", "C"]
for lang in languages:
    print(lang)

2️⃣ Python while Loop

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

Syntax:

while condition:
    statement

Example:

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

🔁 Difference Between for and while Loop

    for Loop                            while Loop

Used when number.       Used when condition
of iterations is known      is unknown 

Works with                             Works with  
sequences                                condition

Simple & clean             Needs manual control 


3️⃣ break Statement

The break statement is used to stop the loop immediately.

Example:

for i in range(1, 10):
    if i == 5:
        break
    print(i)


4️⃣ continue Statement

The continue statement skips the current iteration and continues with the next.

Example:

for i in range(1, 6):
    if i == 3:
        continue
    print(i)

5️⃣ else with Loop

Python allows else with loops.
The else block runs when the loop finishes normally.

Example:

for i in range(3):
    print(i)
else:
    print("Loop completed")

❌ Common Loop Mistakes

  • Infinite loop
  • Forgetting to update variable
  • Wrong indentation

Example of infinite loop:

while True:
    print("Hello")

🎯 Why Loops are Important?

  • Save time & code
  • Used in data processing
  • Important for exams & interviews
  • Core concept of programming

Real-Life Example

password = "python123"
user_input = ""

while user_input != password:
    user_input = input("Enter password: ")

print("Access granted")

Conclusion

Python loops help execute tasks efficiently and repeatedly.
Understanding for and while loops is essential for writing powerful programs.

Practice loops daily to master Python programming.

💬 Let’s Interact!

What will be the output?

for i in range(2, 5):
    print(i)


Comment your answer 👇😊

📌 Related Articles:






Comments

popular

What Is Python Programming? Features, Uses and Career Scope.

  ✍️ Introduction Python is one of the most popular and beginner-friendly programming languages in the world today. It is widely used for web development, data science, artificial intelligence, automation, and software development. Because of its simple syntax and powerful features, Python is an excellent choice for students, beginners, and professionals. In this article, you will learn what Python programming is, its key features, real-life uses, and the career opportunities it offers. 🧠 What Is Python Programming? Python is a high-level, interpreted programming language created to make coding easy and readable. It allows developers to write programs using simple English-like statements, which makes it ideal for beginners. Python supports multiple programming styles such as procedural, object-oriented, and functional programming. ⭐ Features of Python Easy to learn and understand Simple and readable syntax Interpreted language Platform independent Large standard l...

what is Python Date and Time ? Complete Guide with Examples:

  ✍️ Introduction Python provides powerful tools to work with dates and times . The most commonly used module is datetime . The datetime module allows Python programs to create, manipulate, format, and perform calculations with dates and times easily. Date and time handling is used in: Logging systems Attendance & billing systems Data analysis Real-time applications How To Import Date and Time Module : In Python, the datetime module is used to work with dates , times , and date–time combinations . It is part of Python’s standard library, so no installation is required. You can import it in different ways depending on your need. import datetime Get Current Date and Time: This code obtains the current system date and time as a single datetime object using the datetime module. Example: import datetime now = datetime.datetime.now() print (now) Output: 2026-02-13 11:25:53.445827 "Code executed successfully"  Get Only Date: This code retrieves t...

Features of Python Programming Language Explained with Examples

✍️ Introduction Python is one of the most popular programming languages because of its powerful and easy-to-use features. It is designed to make programming simple, readable, and efficient. Python is widely used by beginners as well as professionals due to its flexibility and strong community support. In this article, we will discuss the main features of Python programming language in simple English with easy examples. 🧠 What Are Features of Python? Features of Python are the special characteristics that make it different from other programming languages. These features help programmers write clean, readable, and efficient code with less effort. ⭐ Key Features of Python Programming Language 1️⃣ Easy to Learn and Easy to Use Python has a simple syntax that is close to the English language. This makes it very easy for beginners to learn and understand programming concepts. Example: print("Hello, World!") 2️⃣ Interpreted Language Python is an interpreted language, which me...

Python Operators: Types with Examples

 ✍️Introduction In Python, operators are symbols used to perform operations on variables and values. Operators help us do calculations, comparisons, and logical decisions in programs. In this article, you will learn: • What Python operators are • Types of Python operators • Simple examples for each type