TutorialAce

Python | Java | MySQL | Excel

Python Tutorial

Python For Loop with Examples

How to Use the Python For Loop

In this tutorial, we'll learn the Python for loop. A for loop is a control flow statement, used to execute a block of code repeatedly. We often use the for loop when the number of iterations is known.

This tutorial also gives you some examples illustrate how to use a for loop to iterate over a list, dictionary, tuple, and string.

Python For Loop Syntax

for i in <collection>
loop body

Note that the collection can be a list, tuple or dictionary.

When you know the number of times to loop through a block of code, you can use the range() function as follows:

for i in range(n)
loop body

Here, the parameter n is the number of times that you want to loop through a block of code. For example, if you want to loop through a block of code 10 times, then you simply specify the value of the n parameter as follows:

range(10)

Note that the range() function returns a sequence of numbers, starting from 0 up to, but not including a specified number. For example, range(10) returns a sequence of numbers, starting from 0 to 9. Note that 10 is excluded from the sequence.

However, if you want to start a loop from a specified starting value, you can use the following syntax:

range(start, stop)

Here, the parameter start is the starting integer value and stop is an integer number that range() function generates up to, but not including this number.

For example, range(7,10) returns a sequence of numbers from 7 to 9, but not including 10.

By default, the range() function defaults to increment the sequence by 1. However, you can specify the increment value as follows:

range(start, stop, step)

Where the third parameter step is the increment value. For example, range(2, 10, 2) returns a sequence of numbers as follows:

2, 4, 6, 8

Examples

Example 1: Using the Range() Function

The following code illustrates how to use a for loop with the range() function:

for i in range(10):
print (i);

Result:

0
1
2
3
4
5
6
7
8
9

Example 2: Loop from a Specified Starting Value

The following code illustrates a for loop runs, starting at position 4 to 9, and print each value of i:

for i in range(4,10):
print (i);

Result:

4
5
6
7
8
9

Example 3: Using the Increment Value

The following code illustrates a for loop, starting from 4 to 9, but the increment value is 2:

for i in range(4,10,2):
print (i)

Result:

4
6
8

Example 4: Loop through a List

The following code illustrates how to loop through a list and print each item in the list:

fruits = ["Apple", "Banana", "Cherry", "Grape", "Orange"]
for fruit in fruits:
print (fruit)

Result:

Apple
Banana
Cherry
Grape
Orange

Example 5: Loop through a Dictionary

The following code illustrates how to loop through a dictionary and print each key and value in the dictionary:

employees = {
"1001" : "Employee 1",
"1002" : "Employee 2",
"1003" : "Employee 3"
}
#Loop through keys
for employee_id in employees.keys():
print ("Employee ID: " + employee_id)
#Loop through values
for employee_name in employees.values():
print ("Employee Name: ", employee_name)
#Loop through keys and values
for employee_id, employee_name in employees.items():
print ("Employee ID: " + employee_id + " | Employee Name: " + employee_name)

Result:

Employee ID: 1001
Employee ID: 1002
Employee ID: 1003
Employee Name: Employee 1
Employee Name: Employee 2
Employee Name: Employee 3
Employee ID: 1001 | Employee Name: Employee 1
Employee ID: 1002 | Employee Name: Employee 2
Employee ID: 1003 | Employee Name: Employee 3

Example 6: Loop through a Tuple

The following code illustrates how to loop through a tuple and print each item in the tuple:

fruits = ("Apple", "Banana", "Cherry", "Mango", "Grape", "Orange")
for fruit in fruits:
print (fruit)

Result:

Apple
Banana
Cherry
Mango
Grape
Orange

Example 7: Loop through a Set of Characters

The following code is an example of using a for loop to loop through a string value, and print each character:

strs = "ABCDEF1234";
for str in strs:
print (str, end=' ')

Result:

A B C D E F 1 2 3 4

In this tutorial, we've learned the Python for loop with examples how to loop through a list, dictionary, tuple and string. We've also learned how to use the range() function with a for loop. In Python, besides a for loop statement, a while loop statement is also a control flow statement used to execute a block of code repeatedly until a condition is met.

Leave a Reply