Skip to content

for loop

The for loop is used to iterate over a sequence (like a list, tuple, dictionary, set, or string). It iterates over items in the order they appear in the sequence.

for loop Syntax:

for item in sequence:
print(item)

Examples

Example:

# using a for loop to iterate over a sequence of characters
characters = “Python”
for char in characters:
print(char)

Example:

# Using a for loop to iterate over a list
fruits = ['apple', 'banana', 'cherry']
for fruit in fruits:
print(fruit)

using built-in range() function

A built-in range function returns a sequence of numbers based on the given arguments

range(start, stop, step)

default:

start: defaults to 0

stop: required *

step: defaults to 1

for i in range(6):
print(i)