Google News
logo
Python Program to The for loop
In the following example of Python program that demonstrates how to use the `for` loop :
Program :
# Using the for loop to iterate over a list of numbers
numbers = [1, 2, 3, 4, 5]

for number in numbers:
    print(number)

# Using the for loop to iterate over a string
word = "hello"

for letter in word:
    print(letter)
Output :
1
2
3
4
5
h
e
l
l
o
In the above program, we use the `for` loop to iterate over a list of numbers and a string. Inside the loop, we use the `print()` function to print each item in the list and each character in the string.

You can use the `for` loop to iterate over a sequence of items such as a list, string, or tuple. The loop will execute once for each item in the sequence, and the loop variable (in this case, `number` and `letter`) will take on the value of each item in turn.