Python3 provides the
for loop in order to iterate over a list. The list may contain different types of data like characters, strings, integers, objects,s, etc. The for loop is very popular to process items in a list of sequential types like tuple etc.
Python3 for Loop Syntax
The Python3 for loop has the following syntax.
for ITEM in SEQUENCE: STATEMENT
- ITEM is used to set items in every step of the for loop.
- SEQUENCE is the list of items that are iterated.
- STATEMENT is executed in every step and generally, the ITEM is used in the STATEMENT.
Python3 for Loop
In the following example, we use the for loop to iterate over a list and print these list items.
names = ["ismail","ali","ahmet"] for name in names: print(name)
ismail ali ahmet
Python3 for Loop with Index
By default, the Python3 for loop iterates over items in a sequential way. There is no native way to use index numbers for iteration. In the following example, we get the item count using the
len() method and then iterate over it using the for loop using as the index.
names = ["ismail","ali","ahmet"] for index in range(len(names)): print(names[index])
Break Python3 for Loop
The Python3 for loop can be broken or stopped at some point by using the
names = ["ismail","ali","ahmet"] for name in names: if name = "ali": break print(name)
Pass Python3 for Loop
Also, Python3 for loop can pass an iteration and jump next step easily by using the
names = ["ismail","ali","ahmet"] for name in names: if name = "ali": pass print(name)