Back to: Python Programming
Python For Loop is a control flow statement that allows you to iterate over a sequence of elements. The loop body is executed once for each element in the sequence until the end of the sequence is reached. In this tutorial, we will discuss the basic syntax of the for loop and demonstrate how to use it with various data structures.
Basic Syntax
The basic syntax of the for loop in Python is as follows:
for variable in iterable:
# do something with variable
Here, iterable
is a sequence of elements, and variable
is a new variable that takes on each value in the sequence. The loop body is executed once for each value of variable
.
Iterating Over a List
You can use the for loop to iterate over a list of elements. For example:
fruits = ['apple', 'banana', 'cherry']
for fruit in fruits:
print(fruit)
This code will iterate over each element in the fruits
list and print it to the console.
Iterating Over a Dictionary
You can also use the for loop to iterate over a dictionary. In this case, the loop variable takes on each key in the dictionary, and you can use the key to access the corresponding value. For example:
my_dict = {'name': 'John', 'age': 30}
for key, value in my_dict.items():
print(key, value)
This code will iterate over each key-value pair in the my_dict
dictionary and print both the key and the value to the console.
Using Range() Function
The range()
function can be used with the for loop to generate a sequence of integers. The function takes three arguments: start
, stop
, and step
. For example:
for i in range(1, 6, 2):
print(i)
This code will generate the sequence [1, 3, 5]
and print each value to the console.
Loop Control Statements
You can use the break
and continue
statements to control the flow of the for loop. The break
statement allows you to exit the loop when a condition is met, and the continue
statement allows you to skip the current iteration and continue with the next one. For example:
for i in range(1, 6):
if i == 3:
break
print(i)
This code will print the values [1, 2]
to the console and then exit the loop when the value 3
is encountered.
Python For Loop Summary Table:
Concept | Syntax | Example | Explanation |
---|---|---|---|
Basic For Loop | for variable in iterable: | for i in range(5): print(i) | Executes the loop body for each element in the iterable object |
Iterating Over a List | for element in list: | fruits = ['apple', 'banana', 'cherry'] for fruit in fruits: print(fruit) | Executes the loop body for each element in the list |
Iterating Over a Dictionary | for key, value in dict.items(): | my_dict = {'name': 'John', 'age': 30} for key, value in my_dict.items(): print(key, value) | Executes the loop body for each key-value pair in the dictionary |
Using Range() Function | for i in range(start, stop, step): | for i in range(1, 6, 2): print(i) | Executes the loop body for each integer in the range |
Loop Control Statements | break and continue | for i in range(1, 6): if i == 3: break print(i) | Breaks the loop when the condition is met or skips the current iteration and continues with the next one |
This chart summarizes the basic concepts of Python For Loop and provides examples for each concept. It can be used as a quick reference guide for beginners who are learning Python.
Code example on Python for loop:
# Example of a Python for loop with range, break, and continue
# Loop through numbers 1 to 10 using range
for i in range(1, 11):
# Skip printing even numbers using continue
if i % 2 == 0:
continue
# Stop the loop if the number is greater than 5 using break
if i > 5:
break
# Print the current number
print(i)
In this example, we use the range()
function to loop through the numbers 1 to 10. We then use continue
to skip over even numbers and break
to stop the loop once we reach a number greater than 5.
Within the loop, we first check if the current number is even using the modulus operator (%
) and the number 2. If the result is 0 (i.e., the number is even), we use continue
to skip over the current iteration of the loop and move on to the next one.
Next, we check if the current number is greater than 5. If it is, we use break
to exit the loop immediately.
Finally, if the current number passes both of these checks, we print it out using the print()
function.
When we run this code, we should see the odd numbers 1, 3, and 5 printed out on separate lines, since the even numbers are skipped over using continue
, and the loop stops once we reach the number 6 (which is greater than 5) using break
.
Follow us on social media
Follow Author