- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
How to use for loop in Python?
The for loop in Python is used to iterate over a number of elements or some specific integer range. The elements might be of an array, string or any other iterative object in Python.
The for loop is the most frequently used looping statement. Most of the programming questions we encounter make use of the for loop in its solution.
For in range loop
Python has for an range loop. It takes the two integer values which specify the range within which the variable must iterate. If only one integer parameter is specified, then the specified integer is taken as the end of the range and the starting is 0 by default.
for variable in range( starting value, end value) for variable in range( end value)
Note: The for loop in Python iterates one less than the end value. This means that if the starting and end values are 1 and 5 respectively, then the loop will iterate for 1,2,3,4. The starting value is inclusive but the end value is not inclusive.
for i in range(1,5): print(i,end=" ") print() for i in range(5): print(i,end=" ")
1 2 3 4 0 1 2 3 4
Reverse for loop
The for in range loop can take a third parameter -1, which specifies that the for loop will iterate in reverse order. The loop starts from starting value and iterates in decreasing order to the end value(not inclusive).
for i in range(5,0,-1): print(i,end=" ")
5 4 3 2 1
Specify third parameter in the for loop
The third parameter in the for loop can be modified to use for loop in different ways. The third parameter, in actual, specifies the steps the variable should jump. If not specified, it is 1 by default.
If the third parameter is negative, it specifies that the loop will iterate in reverse order.
The number of steps (jumps) backwards will be specified by this parameter.
If the parameter is positive, the loop will iterate in forward order. The number of steps forward will be specified by this parameter.
Let’s understand with the help of the below example.
for i in range(0,11,2): print(i,end=" ") print() for i in range(10,-1,-2): print(i,end=" ")
0 2 4 6 8 10 10 8 6 4 2 0
For in loop
This loop is used to iterate over a iterable object such as string or array. It cannot be used to iterate over some specific integer range.
for variable in iterable object
This loop iterates over all the elements of the iterable object one by one without taking into account their index. If indexes are needed, use for in range loop.
st="Tutorials" for i in st: print(i,end=" ") print() array=[2,4,6,8,10] for i in array: print(i,end=" ") print() for i in range(len(array)): print(array[i],end=" ")
T u t o r i a l s 2 4 6 8 10 2 4 6 8 10
Note: As clear from the above example, the variable in for in loop holds the elements of the iterable object one by one, whereas the variable in for in range loop holds the indexes of the elements.
- Related Articles
- How to use else conditional statement with for loop in python?
- How to use ‘for loop’ in Java?
- How to use continue statement in Python loop?
- How to use FOR LOOP in MySQL Stored Procedure?
- Python - How to convert this while loop to for loop?
- How to convert a Python for loop to while loop?
- How to write and use for loop in Vue js?
- How to break a for loop in Python?
- How to use for each loop through an array in Java?
- How to use PowerShell break statement with the For loop?
- How to use range-based for() loop with std::map?
- How to create Tkinter buttons in a Python for loop?
- How to use ‘while loop’ in Java?