for
KeywordPython 2.0+Beginner
Starts a loop that iterates over a sequence or iterable
Quick Info
- Documentation
- Official Docs
- Python Version
- 2.0+
Learn by Difficulty
Quick Example
python
fruits = ["apple", "banana", "cherry"] for fruit in fruits: print(fruit) # Iterate over a range for i in range(5): print(i, end=" ") print() # Iterate over a string for char in "Python": print(char, end="-") print()
for iterates over any iterable: lists, ranges, strings, dicts, files, and more.
Try in PlaygroundTags
languagesyntaxcorecontrol-flowloopiteration
Related Items
while
Keyword
Starts a loop that repeats as long as its condition is true
break
Keyword
Exits the nearest enclosing for or while loop immediately
continue
Keyword
Skips the rest of the current loop iteration and moves to the next
range()
Built-in Function
Generates an immutable sequence of integers
enumerate()
Built-in Function
Adds a counter to an iterable, yielding (index, value) pairs
in
Keyword
Membership test operator; also used in for loops to iterate over items