B.Tech Students: Apply for Live Programming Internship C, C++, Java, Python ,Web page Designing, PHP Python for Loops with Examples | LiveCodeProgramming
Python Installation First Python Program Control Flow if...else nested if else match-case logical-operators for loops while loops break / continue Functions Defining Functions Function Arguments Function return value Lambda Function Recursion Exception Handling Exception Handling Try Except Else and Finally Raise Exception Custom Exceptions File Handling File in python Read File Write File Append File Delete File Exception Handling

Python for Loop

The for loop in Python is used to iterate over a sequence like list, tuple, string, or range. It repeats a block of code for each item.

Python में for loop का उपयोग list, tuple, string या range जैसे sequence पर चलने के लिए होता है। यह हर item के लिए कोड को repeat करता है।

10 Python for Loop Programs with Output and Explanation
  1. Print numbers from 1 to 5
    for i in range(1, 6):
        print(i)
    Output:
    1
    2
    3
    4
    5
    Explanation: Loops through numbers from 1 to 5.
  2. Print even numbers between 1 to 10
    for i in range(2, 11, 2):
        print(i)
    Output:
    2
    4
    6
    8
    10
  3. Print characters in a string
    for ch in "Python":
        print(ch)
    Output:
    P
    y
    t
    h
    o
    n
  4. Loop through a list
    colors = ["red", "green", "blue"]
    for color in colors:
        print(color)
    Output:
    red
    green
    blue
  5. Print table of 5
    for i in range(1, 11):
        print("5 x", i, "=", 5*i)
    Output:
    5 x 1 = 5
    5 x 2 = 10
    5 x 3 = 15
    5 x 4 = 20
    5 x 5 = 25
    5 x 6 = 30
    5 x 7 = 35
    5 x 8 = 40
    5 x 9 = 45
    5 x 10 = 50
  6. Print square of numbers 1 to 5
    for i in range(1, 6):
        print("Square of", i, "is", i*i)
    Output:
    Square of 1 is 1
    Square of 2 is 4
    Square of 3 is 9
    Square of 4 is 16
    Square of 5 is 25
  7. Count vowels in a string
    text = "education"
    vowels = "aeiou"
    count = 0
    for ch in text:
        if ch in vowels:
            count += 1
    print("Vowel count:", count)
    Output:
    Vowel count: 5
  8. Print reverse of a string
    s = "hello"
    for ch in reversed(s):
        print(ch)
    Output:
    o
    l
    l
    e
    h
  9. Print index and value
    items = ["pen", "book", "bottle"]
    for index, value in enumerate(items):
        print(index, ":", value)
    Output:
    0 : pen
    1 : book
    2 : bottle
  10. Nested loop - print 3x3 pattern
    for i in range(1, 4):
        for j in range(1, 4):
            print(i, j)
    Output:
    1 1
    1 2
    1 3
    2 1
    2 2
    2 3
    3 1
    3 2
    3 3