Like a every programming langauge, Python has for loop to iterate a datatype of sequence. It can be array, list, set,dictionary or a string.

Perl for loop

initialization : variable is initialized and sets intial value Condition: perl Condition that evaluates true and false. It is used to test whether loop continue or exit. increment/decrement: updated variable value by add/substract Syntax:

for( initialization; condition;increment/decrement){
    //code statements body

Python loops

Looping through a range of numbers:

  • Print the numbers 0 to 9
for index in range(5):

Looping through a list:

  • Print the elements of a list
numbers = ['one', 'two', 'three']
for number in numbers:

Looping through a string:

  • Print each character in a string
s = 'hello'
for c in s:
Looping through a dictionary:
Copy code
# Print the keys and values of a dictionary
emps = {'john': 1, 'eric': 2, 'mark': 3}
for name, id in emps.items():
    print(f'{name}: {id}')
Looping with a counter:
# Print the numbers 0 to 9, along with their indices
numbers = ['one', 'two', 'three']
for i, number in enumerate(numbers):
    print(f'{i}: {number}')
These are just a few examples of the types of for loops you can use in Python. For loops are a powerful and flexible way to iterate over a sequence of items and perform a task for each item.