Python for Loop Statements - Tutorialspoint
Python for Loop Statements - Tutorialspoint
It has the ability to iterate over the items of any sequence, such as a list or a string.
Syntax
If a sequence contains an expression list, it is evaluated first. Then, the first item in the sequence is
assigned to the iterating variable iterating_var. Next, the statements block is executed. Each item in
the list is assigned to iterating_var, and the statement(s) block is executed until the entire sequence
is exhausted.
Flow Diagram
Example
Live Demo
#!/usr/bin/python
https://www.tutorialspoint.com/python/python_for_loop.htm 1/3
1/10/2021 Python for Loop Statements - Tutorialspoint
Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : h
Current Letter : o
Current Letter : n
Current fruit : banana
Current fruit : apple
Current fruit : mango
Good bye!
An alternative way of iterating through each item is by index offset into the sequence itself.
Following is a simple example −
Live Demo
#!/usr/bin/python
Here, we took the assistance of the len() built-in function, which provides the total number of
elements in the tuple as well as the range() built-in function to give us the actual sequence to
https://www.tutorialspoint.com/python/python_for_loop.htm 2/3
1/10/2021 Python for Loop Statements - Tutorialspoint
iterate over.
#!/usr/bin/python
10 equals 2 * 5
11 is a prime number
12 equals 2 * 6
13 is a prime number
14 equals 2 * 7
15 equals 3 * 5
16 equals 2 * 8
17 is a prime number
18 equals 2 * 9
19 is a prime number
https://www.tutorialspoint.com/python/python_for_loop.htm 3/3