Python For Loops
Python For loop is used for sequential traversal i.e. it is used for iterating over an iterable like String, Tuple, List, Set or Dictionary.
In Python, there is no C style for loop, i.e., for (i=0; i<n; i++). There is “for” loop which is similar to each loop in other languages. Let us learn how to use for in loop for sequential traversals
Note: In Python, for loops only implements the collection-based iteration.
For Loops Syntax
for var in iterable: # statements
Flowchart of for loop
Here the iterable is a collection of objects like lists, tuples. The indented statements inside the for loops are executed once for each item in an iterable. The variable var takes the value of the next item of the iterable each time through the loop.
Examples of For Loops in Python
Example 1: Using For Loops in Python List
Geeks for geeks
Example 2: Using For Loops in Python Dictionary
Dictionary Iteration xyz 123 abc 345
Example 3: Using For Loops in Python String
String Iteration G e e k s
Loop Control Statements
Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed. Python supports the following control statements.
Continue Statement in Python
Python continue Statement returns the control to the beginning of the loop.
Example: Python for Loop with Continue Statement
Current Letter : g Current Letter : k Current Letter : f Current Letter : o Current Letter : r Current Letter : g Current Letter : k
Break Statement in Python
Python break statement brings control out of the loop.
Example: Python For Loop with Break Statement
Current Letter : e
Pass Statement in Python
The pass statement to write empty loops. Pass is also used for empty control statements, functions, and classes.
Example: Python For Loop with Pass Statement
Last Letter : s
range() function in Python
Python range() is a built-in function that is used when a user needs to perform an action a specific number of times. range() in Python(3.x) is just a renamed version of a function called xrange() in Python(2.x).
The range() function is used to generate a sequence of numbers. Depending on how many arguments the user is passing to the function, user can decide where that series of numbers will begin and end as well as how big the difference will be between one number and the next.range() takes mainly three arguments.
- start: integer starting from which the sequence of integers is to be returned
- stop: integer before which the sequence of integers is to be returned.
The range of integers end at stop – 1.
- step: integer value which determines the increment between each integer in the sequence
Example: Python for loop with range function
0 1 2 3 4 5 6 7 8 9 Sum of first 10 numbers : 45
For loop in Python with else
In most programming languages (C/C++, Java, etc), the use of else statements has been restricted with the if conditional statements. But Python also allows us to use the else condition with for loops.
Note: The else block just after for/while is executed only when the loop is NOT terminated by a break statement
1 2 3 No Break
Note: For more information refer to our Python for loop with else tutorial.