for-loop

Visit each item in an iterable series For-loops in Python correspond to what are called for-each loops in many other programming languages

Syntax

for item in iterable:
    ...body...

Video

Iterate through list

>>> fruits=["Apple","Mango","Banana"]
>>> for fruit in fruits:
...     print(fruit)
...
Apple
Mango
Banana

Iterate through dictionary

>>> students={"tom":70,"ram":60,"dora":30} # Student marks     
>>> for name in students:                       
...     print(name,students[name])              
...                                             
ram 60                                          
tom 70                                          
dora 30                                         

Python Home