Understanding the For Loop in Python

In Python, a for loop is like a helpful friend that helps you do something repeatedly without you having to write the same code over and over again. Let's break it down into simple steps:


  • What is a For Loop?
  • A for loop in Python is used to go through each item in a sequence (like a list, tuple, string, or range) and do something with each item.

  • How to Use a For Loop:
  • Here's a simple example. Let's say you have a list of numbers, and you want to print each number one by one:

    numbers = [1, 2, 3, 4, 5]
    for number in numbers:
        print(number)
    In this loop:

    numbers is our list of numbers.
    number is a variable that represents each item in the list, one at a time.
    print(number) is the action we want to do with each number, which is printing it.

    Output : 1 2 3 4 5


  • Looping Through Different Types of Sequences:
  • You can use a for loop with different types of sequences. For example, if you want to print each character in a string:

    word = "Python"
    for letter in word:
        print(letter)

    Output : p y t h o n


  • Using the Range Function with For Loop:
  • The range() function is handy when you want to loop a specific number of times. For example, if you want to print numbers from 0 to 4:

    for number in range(5):
        print(number)

    Output : 0 1 2 3 4


  • Real-life examples :
  • #1. Question: Write a Python program to print the multiplication table of a given number.

    #Solution:
    # Input the number for which multiplication table is needed
    number = int(input("Enter a number: "))
    # Print multiplication table using a for loop
    print("Multiplication Table of", number)
    for i in range(1, 11):
          print(number, "x", i, "=", number * i)
    #2. Question: Write a Python program to calculate the total expenses for a list of items given their prices.

    #Solution:
    # Define a list of item prices
    prices = [10, 20, 30, 15, 25]
    # Initialize total expenses to zero
    total_expenses = 0
    # Calculate total expenses using a for loop
    for price in prices:
          total_expenses += price
    # Print the total expenses
    print("Total Expenses:", total_expenses)
    #3. Question: Write a Python program to find all prime numbers within a given range.

    #Solution:
    # Input the range from the user
    start = int(input("Enter the start of the range: "))
    end = int(input("Enter the end of the range: "))
    print("Prime numbers between", start, "and", end, "are:")
    # Iterate through each number in the range
    for num in range(start, end + 1):
    # Prime numbers are greater than 1
          if num > 1:
           is_prime = True
    # Check if num is divisible by any number between 2 and its square root
    for i in range(2, int(num ** 0.5) + 1):
         if num % i == 0:
           is_prime = False
          break
    # If the number is prime, print it
    if is_prime:
    print(num)
  • Explanation:
  • The program takes the range (start and end) as input from the user.
    It then iterates through each number in that range using a for loop.
    For each number, it checks if it's greater than 1 (prime numbers must be greater than 1).
    If the number is greater than 1, it checks if it's divisible by any number between 2 and its square root. If it is, the number is not prime.
    If the number is not divisible by any number in that range, it's considered prime and printed out.