Python for Loop with Examples

How to Use the for Loop Statement in Python

In this tutorial, you will learn how to use the for loop in Python. The for loop statement is a control flow statement used to iterate through a code block. The for loop statement is often used when the number of iterations is known.

This tutorial provides several examples of how to use the for loop to iterate over lists, dictionaries, tuples, and strings.

Related tutorials:

Python for Loop Syntax

The syntax of the for loop statement is as follows:

for i in <collection>
    loop body

A collection can be a list, a tuple, or a dictionary.

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

for i in range(n)
    loop body

The parameter n specifies how many times you want a code block to loop. For example, if you want a block of code to loop ten times, the value of the n parameter should be specified as follows:

range(10)

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

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

range(start, stop)

The parameter start is the starting integer value, and the parameter stop is the maximum value produced by the range() function, but not including this value.

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

The default for the range() function is to increment the sequence by 1, but it is possible to specify an increment value as follows:

range(start, stop, step)

where the third parameter step is an increment value.
For example, range(2, 10, 2) returns the following sequence of numbers:

2, 4, 6, 8

Examples

Example 1: Using the range() function

The following code shows how to use the for loop and the range() function:

  1. for i in range(10):
  2. print (i);

Output:

0
1
2
3
4
5
6
7
8
9

Example 2. Loop from a specified starting value

The following code shows the execution of a for loop starting at positions 4 through 9 and displays each value of i:

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

Output:

4
5
6
7
8
9

Example 3: Using an incremental value

The following code shows a for loop starting from 4 to 9, but with an incremental value of 2:

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

Output:

4
6
8

Example 4: Looping through a list of strings

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

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

Output:

Apple
Banana
Cherry
Grape
Orange

Example 5: Looping through a dictionary

The following code loops through the dictionary and displays each key and value in the dictionary:

  1. employees = {
  2. "1001" : "Employee 1",
  3. "1002" : "Employee 2",
  4. "1003" : "Employee 3"
  5. }
  6. #Loop through keys
  7. for employee_id in employees.keys():
  8. print ("Employee ID: " + employee_id)
  9. #Loop through values
  10. for employee_name in employees.values():
  11. print ("Employee Name: ", employee_name)
  12. #Loop through keys and values
  13. for employee_id, employee_name in employees.items():
  14. 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: Looping through a tuple

The following code loops through the tuple and displays each item in the tuple:

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

Output:

Apple
Banana
Cherry
Mango
Grape
Orange

Example 7: Loop over a set of characters

The following code shows an example of looping through a string value and displaying each character using a for loop statement:

  1. strs = "ABCDEF1234";
  2. for str in strs:
  3. print (str, end=' ')

Output:

A B C D E F 1 2 3 4

Summary

In this tutorial, you learned how to use the for loop statement in Python. We also showed examples of looping through lists, dictionaries, tuples, and strings and how to use the range() function with the for loop statement. In Python, in addition to the for loop statement, the while loop statement is also a control flow statement that allows you to execute a block of code repeatedly until a condition is met.


See also:
Python return Statement with Examples
Python List Methods and Functions with Examples
Python abs() – Absolute Value in Python
Python Factorial – Find the Factorial of a Number
Python min() – Find the Smallest Value in Python

Leave a Comment