New

Experience Smart HR with Horilla Mobile App

Google Play Store Google Play Store
Home / Blogs

Overview of Loops in Python

Python
·

May 22, 2025

overview-of-loops-in-python

Programming often involves performing repeated tasks — whether it’s processing items in a list, calculating results step by step, or running actions until a certain condition is met. Loops help us avoid repeating code manually by allowing certain instructions to run multiple times in a clean and efficient way.

In Python, loops are an essential concept that can help you simplify complex problems, automate tasks, and improve the structure of your code. Whether you’re new to Python or refreshing your basics, learning loops will take your coding game to the next level.

In this article, we’ll explore:

  • What loops are and why they matter
  • The two types of loops in Python: for and while
  • Useful loop control tools (break, continue, else)
  • A step-by-step example with code and output

What Are Loops?

A loop is a control structure that enables a block of code to be repeated over and over again under some conditions. Rather than repeating the same instructions yourself, a loop enables the program to do it for you, saving time and preventing mistakes.

Python supports two main types of loops:

  1. for loop – repeats over a sequence (like a list or string).
  2. while loop – loops as long as some condition is true

The for Loop

The for loop is ideal when you want to go through a collection of items or numbers one by one.

Basic Syntax:

for item in collection:
    # your actions with item

Example 1: Loop over a list

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

Expected Output:

Python
Java
C++

Example 2: Use with range()

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

Expected Output:

1
2
3
4
5

The While Loop

The while loop loops as long as some condition is true

. It’s helpful when you don’t know precisely how many times something has to repeat.

Basic Syntax:

while condition:
    # repeat this block

Example 3: Count up with a while loop

count = 1
while count <= 3:
    print("Count is:", count)
    count += 1

Expected Output:

Count is: 1
Count is: 2
Count is: 3

Loop Control Statements

Python gives you tools to manage how loops run:

  1. break : Exit the loop early
for n in range(5):
    if n == 3:
        break
    print(n)

Output:

0
1
2

continue : Skip to the next loop cycle

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

Output:

0
1
3
4

else  Runs after a loop finishes normally

for i in range(3):
    print("Step", i)
else:
    print("Done!")

Output:

Step 0
Step 1
Step 2
Done!

Practice Problem: Sum of Even Numbers from 1 to 100

Solution:

total = 0 
for num in range(1, 101):
    if num % 2 == 0:
        total += num 

print("Sum of even numbers from 1 to 100 is ", total)

Output:

Sum of even numbers from 1 to 100 is 2550

Explanation

  • We initialize a variable total to keep track of the sum.
  • The loop goes through numbers from 1 to 100.
  • Inside the loop, we check if a number is even by using num % 2 == 0.
  • If it’s even, we add it to total.
  • Once all numbers are checked, the final result is printed.

Conclusion

Loops are one of the most powerful tools in programming — they make your code more dynamic, readable, and adaptable. Instead of repeating code manually, loops help automate repetition based on data or conditions, which is essential in building efficient, real-world Python applications.

Whether you’re:

  • Creating reports from lists,
  • Repeating steps in a game,
  • Automating tasks like sending emails, or
  • Processing user input until it’s valid —

Loops are there to help.

By mastering both for and while loops, and understanding when to use break, continue, and else, you unlock a new level of control over how your programs behave.

Horilla Editorial Team Author

Horilla Editorial Team is a group of experienced writers and editors who are passionate about HR software. We have a deep understanding of the HR landscape and are committed to providing our readers with the most up-to-date and informative content. We have written extensively on a variety of HR software topics, including applicant tracking systems, performance management software, and payroll software etc. We are always looking for new ways to share our knowledge with the HR community. If you have a question about HR software, please don't hesitate to contact us.