← Back to Python

All Topics

Advertisement

Learn/Python/Python Fundamentals

Loops in Python

Topic: Basics

Advertisement

Introduction

Loops allow you to execute a block of code repeatedly.

For Loop

# Loop through a range
for i in range(5):
    print(i)  # 0, 1, 2, 3, 4

# Loop through a list
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print(fruit)

While Loop

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

Loop Control

# Break - exit loop
for i in range(10):
    if i == 5:
        break
    print(i)

# Continue - skip iteration
for i in range(5):
    if i == 2:
        continue
    print(i)

Practice Problems

  1. Print multiplication table (1-10)
  2. Find factorial using loop
  3. Count even and odd numbers in a list
  4. Search for element in list
  5. Pattern printing (triangles, pyramids)

Advertisement

Advertisement

Need More Practice?

Get personalized Python help from ChatWhole's AI-powered platform.

Get Expert Help →