Python for Loop – How to Use the for Loop in Python


How to Use the for Loop Statement in Python

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

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

Python for Loop Syntax

The syntax of the for loop statement is as follows:

for i in <collection>
loop body

Here, 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, let's say you want to loop through a block of code ten times, then you 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, excluding 10.

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 an example of how to use the for loop with the range() function:

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

Output:

0
1
2
3
4
5
6
7
8
9

Example 2: Loop from a Specified Starting Value

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

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

Output:

4
5
6
7
8
9

Example 3: Using the Increment Value

The following code illustrates the for loop, which starts 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 of Strings

The following code loops through a list of strings and print each item in the list:

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

Output:

Apple
Banana
Cherry
Grape
Orange

Example 5: Loop through a Dictionary

The following code loops 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)

Output:

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 loops through a tuple and print each item in the tuple:

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

Output:

Apple
Banana
Cherry
Mango
Grape
Orange

Example 7: Loop through a Set of Characters

The following code illustrates an example of using the for loop statement 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, you've learned how to use the for loop statement in Python. This tutorial also provided examples of how to loop through a list, dictionary, tuple, and string. And you've also learned how to use the range() function with the for loop statement. In Python, besides the for loop statement, a while loop statement is also a control flow statement that allows you to execute a block of code repeatedly until a condition met.

Leave a Reply

Your email address will not be published. Required fields are marked *