Table of Contents
In this tutorial, I will take you through 12 Best Python For loop examples. Python is an interpreted, high-level, general-purpose programming language. Created by Guido van Rossum and first released in 1991. Python has multiple loops which you can use in your source code. In that one of the loop example is for loop which find its many uses while writing code in Python language.
I will go through the different usages of for loop in this tutorial using wide range of examples. It will give you a clear understanding in identifying the usage of python for loop in different use cases.
Python For Loop Examples
Also Read: How to Install Python3 on RedHat/CentOS 7
For loop in python is used to execute a block of statements or code several times until the given condition becomes false. When we know the number of times we need to execute a piece of code , then we use for loop in that cases.
Syntax
for var in sequence: Block of code
Here var will take the value from the sequence and execute it until all the values in the sequence are done.
Example 1
You can iterate over the numeric and string values together as shown in below example.
for i in (1,2,3,'cyberithub'):
print(i)
Output
[root@localhost ~]# python example.py 1 2 3 cyberithub
Example 2
You can use range()
function with step 4 to iterate over the values from 1 to 10 as shown in below example.
for i in range(1,10,4):
print(i)
Output
[root@localhost ~]# python example.py 1 5 9
Example 3
You can iterate over a string and get every single character as shown in below example.
value='cyberithub'
for i in value:
print(i)
Output
[root@localhost ~]# python example.py c y b e r i t h u b
Example 4
You can also use len()
function to iterate over a string and print every character as shown in below python for loop example.
value='cyberithub'
for i in range(len(value)):
print(value[i])
Output
[root@localhost ~]# python example.py c y b e r i t h u b
Example 5
You can print character from an array as shown in below example.
value=['a','b']
for x in value:
print(x)
Output
[root@localhost ~]# python example.py a b
Example 6
Here you can store string in an array and print the value using python for loop as shown below.
value=["this","is","from","cyberithub"]
for x in value:
print(x)
Output
[root@localhost ~]# python example.py this is from cyberithub
Example 7
You can store numerical value in an array and print using python for loop as shown below.
value=[1,2,3,4]
for x in value:
print(x)
Output
[root@localhost ~]# python example.py 1 2 3 4
Example 8
You can also use len()
function to iterate and print all the values as shown in below python for loop example.
value=[1,2,3,4]
for i in range(len(value)):
print(value[i])
Output
[root@localhost ~]# python example.py 1 2 3 4
Example 9
You can also use len()
function to iterate over an array and print character values as shown in below example.
value=['a','b','c','d']
for i in range(len(value)):
print(value[i])
Output
[root@localhost ~]# python example.py a b c d
Example 10
Using len()
function, you can iterate over an array and print string values as shown in below example.
value=['this','is','from','cyberithub']
for i in range(len(value)):
print(value[i])
Output
[root@localhost ~]# python example.py this is from cyberithub
Example 11
Let’s say you want to go in reverse order and print all the values between 10 and 1 using step 1. You can use reversed()
function to print all the values as shown in below python for loop example.
for i in reversed(range(1,10,1)):
print(i)
Output
[root@localhost ~]# python example.py 9 8 7 6 5 4 3 2 1
Example 12
You can use reversed()
function to print the values the decreasing order from 10 to -5 using step of 3 as shown in below example.
for i in reversed(range(-5,10,3)):
print(i)
Output
[root@localhost ~]# python example.py 7 4 1 -2 -5
Also Read: Create Ansible Role from Scratch