In this Python tutorial, we will check Python for loop, we will see how to use for loop in python with examples.
Also, we will discuss:
- For Loop in python
- Loop over a string in python
- Python range() function
- Nested loops in python
- Else in for loop
- Python for loop backward
For Loop in python
For loop in Python is used to iterate over a items of any sequence such as list, string, tuples etc.
Example:
chocolate = ['Dairy Milk', 'Kit Kat', 'perk']
for a in chocolate:
print(a)
After writing the above code (for loop in python), Ones you will print ” a ” then the output will appear as a “Dairy Milk Kit Kat perk”.
Here, the loop executes a set of statements for each item present in the list. You can refer to the screenshot for the loop in python.
This is a For Loop in python example.
You may like to read, How to use Pandas drop() function in Python.
Loop over a string in python
In python, we can iterate over a word of string by using for loop and in operator in python.
Example:
my_string = "Python"
for a in my_string:
print(a)
After writing the above code (loop over a string in python), Ones you will print ” a ” then the output will appear as a “ P y t h o n ”.
Here, the loop iterate over all characters in the string ” Python “. You can refer to the below screenshot loop over a string in python.
- String methods in Python with examples
- Remove character from string Python
- Python convert list to string
Python range() function
In python, to loop through a code we use range() function in Python and it returns the sequence of number starting from 0 by default and it will end at a specified number.
Example:
for value in range(4):
print(value)
After writing the above code (python range() function), Ones you will print ” value ” then the output will appear as a “ 0 1 2 3 ”.
Here, we use range() function for a sequence of numbers and the for loop to iterate through numbers. You can refer to the screenshot python range() function
The above in an example of Python range() function.
Nested loops in Python
In python, the nested loop is a loop inside the loop it is used to repeat some action of the items in the outer loop.
Example:
numbers = ["5", "8", "10"]
colors = ["red", "blue", "black"]
for a in numbers:
for z in colors:
print(a,z)
After writing the above code (nested loops in python), Ones you will print ” a, z ” then the output will appear.
Here, the inner loop will execute one-time for each iteration of the outer loop. You can refer to the below screenshot nested loops in python.
This is an example, Nested loops in Python.
Else in for loop in Python
In python, else in for loop is used to specifies a block of code when the loop is finished. Else block appears after the body of the loop when iteration is completed.
Example:
for value in range(5):
print(value)
else:
print("Loop Ends")
After writing the above code (else in for loop), Ones you will print ” value ” then the output will appear ” 0 1 2 3 4 Loop Ends “.
Here, else block will be executed when all iterations are completed, the program exits the loop only after the else block. You can refer to the below screenshot else in for loop.
This is an example on Else in for loop in Python.
Python for loop backward
The backward for-loop iterates through a sequence backward, we will use the reversed() function to iterate over a sequence backward.
Example:
for val in reversed(range(5))
print(val)
After writing the above code (python for loop backward), Ones you will print ” val ” then the output will appear ” 4 3 2 1 0 “. Here, we will use reversed(sequence) to print them in the reverse order.
You can refer to the below screenshot for python for loop backward
In this way, we can use For Loops in python.
You may like the following Python tutorials:
- Python square a number
- What is a Python Dictionary
- Python print without newline
- Python Dictionary Methods + Examples
- 11 Python list methods
- How to create a list in Python
- Create a hello world program in python using Visual Studio Code
- Python input and raw_input function
- Python select from a list + Examples
- Python pass by reference or value with examples
In this tutorial, we learned how to use For Loops in python with examples and also we have discussed:
- For Loop in python
- Loop over a string in python
- Python range() function
- Nested loops in python
- Else in for loop
- Python for loop backward
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.