Loops in Python

Python 10 min min read Updated: Mar 09, 2026 Beginner
Loops in Python
Beginner Topic 7 of 10

Python Loops

Loops are an essential concept in Python programming that allow us to execute a block of code multiple times without writing it repeatedly. Instead of writing the same statement again and again, loops make programs shorter, cleaner, and more efficient.

Loops are commonly used when working with lists, processing large datasets, performing repetitive calculations, or automating tasks.

What are Loops?

A loop is a programming structure that repeatedly executes a block of code until a specified condition is met. Python provides two main types of loops:

  • for loop
  • while loop

Python also provides additional loop control statements such as break, continue, and pass that help manage the flow of loops.

The for Loop

The for loop is used to iterate over a sequence such as a list, tuple, string, or range of numbers.

python for i in range(5): print("Learning Python")

This loop will execute five times and print the message each time.

Using range() Function

The range() function is commonly used with the for loop to generate a sequence of numbers.

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

This loop prints numbers from 1 to 5.

Looping Through a List

The for loop can also iterate through elements in a list.

python fruits = ["apple", "banana", "mango"] for fruit in fruits: print(fruit)

This program prints each fruit in the list one by one.

The while Loop

The while loop repeatedly executes a block of code as long as a given condition remains true.

python count = 1 while count <= 5: print(count) count += 1

This loop prints numbers from 1 to 5 and stops once the condition becomes false.

Infinite Loops

If the condition in a while loop never becomes false, the loop runs indefinitely. This is known as an infinite loop.

python while True: print("This loop runs forever")

Infinite loops should be used carefully because they can cause programs to run endlessly.

Break Statement

The break statement is used to exit a loop immediately when a specific condition is met.

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

The loop stops once the value reaches 5.

Continue Statement

The continue statement skips the current iteration and moves to the next iteration of the loop.

python for i in range(5): if i == 2: continue print(i)

In this example, the number 2 will be skipped.

Pass Statement

The pass statement acts as a placeholder and does nothing. It is useful when a loop is required syntactically but no action is needed yet.

python for i in range(5): pass

Nested Loops

A nested loop is a loop inside another loop. Nested loops are commonly used in situations such as matrix operations or pattern printing.

python for i in range(3): for j in range(2): print(i, j)

This loop runs the inner loop for each iteration of the outer loop.

Real-World Example

Loops are commonly used in real-world applications such as processing student marks or analyzing data.

python marks = [85, 78, 92, 88] total = 0 for mark in marks: total += mark average = total / len(marks) print("Average Marks:", average)

This program calculates the average marks of students using a loop.

Best Practices for Using Loops

  • Avoid unnecessary nested loops because they can slow down the program.
  • Use for loops when working with sequences.
  • Use while loops when the number of iterations is not known beforehand.
  • Always ensure loop conditions eventually become false to avoid infinite loops.

Conclusion

Loops are powerful tools in Python that allow developers to automate repetitive tasks efficiently. By using for loops, while loops, and loop control statements like break and continue, programmers can write cleaner and more efficient code.

In the next tutorial, we will learn about Functions in Python and understand how to organize code into reusable blocks.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators