B2. Python For Loop
In Python, a for
loop is used to iterate over a sequence of elements, and execute the same block of code for each element in the sequence. The basic syntax of a for
loop is as follows:
for variable in sequence:
# code to execute for each element in the sequence
Here, variable
is a variable that takes on the value of each element in the sequence
during each iteration of the loop. The code block inside the loop is executed once for each element in the sequence
.
The sequence
can be any iterable object, such as a list, tuple, string, or dictionary.
Here’s an example of how to use a for
loop in Python:
# Print each element in a list
my_list = ["apple", "banana", "orange"]
for fruit in my_list:
print(fruit)
In this example, we’re using a for
loop to print each element in the my_list
list. The loop iterates through the list and assigns each element to the fruit
variable. The print
function is then called with the fruit
variable as the argument, which prints each element in the list to the console.
The output of this code would be:
apple
banana
orange
Note that the for
loop automatically stops iterating when it reaches the end of the sequence
.
Python loop over a list :
In the below example we check on how to use a for
loop to iterate over a list in Python:
# Iterate over a list and print each element
my_list = [1, 2, 3, 4, 5]
for element in my_list:
print(element)
In this example, we’re creating a list called my_list
with the elements 1, 2, 3, 4, and 5. We then use a for
loop to iterate over each element in the list and print it to the console using the print
function.
The output of this code would be:
1
2
3
4
5
Note that the for
loop automatically stops iterating when it reaches the end of the list. You can perform any operations you like on each element of the list inside the loop, such as adding them together, checking if they meet a certain condition, or appending them to a new list.
Python loop with range() :
In Python, the built-in range()
function can be used in conjunction with a for
loop to repeat a code block a specified number of times. The range()
function generates a sequence of numbers that can be used to control the number of iterations in a for
loop.
The basic syntax of a for
loop with range()
is as follows:
for variable in range(start, stop, step):
# code to execute for each value of the variable
Here, start
is the starting value of the sequence (default is 0), stop
is the exclusive ending value of the sequence, and step
is the increment between values in the sequence (default is 1).
The variable
takes on each value in the sequence during each iteration of the loop, and the code block inside the loop is executed once for each value.
Here’s an example of how to use a for
loop with range()
in Python:
# Print the numbers from 0 to 4
for i in range(5):
print(i)
In this example, we’re using a for
loop with range()
to print the numbers from 0 to 4.
The range(5)
function generates a sequence of numbers from 0 to 4 (inclusive), and the for
loop iterates over each value in the sequence and assigns it to the i
variable.
The print
function is then called with the i
variable as the argument, which prints each number to the console.
The output of this code would be:
0
1
2
3
4
Note that the for
loop automatically stops iterating when it reaches the end of the sequence generated by the range()
function.
Python for loop with else :
In Python, it is possible to use an else
statement in conjunction with a for
loop. The else
block will be executed after the loop has completed, but only if the loop did not encounter a break
statement.
The basic syntax of a for
loop with else
is as follows:
for variable in sequence:
# code to execute for each element in the sequence
else:
# code to execute if the loop completed without encountering a break statement
Here, variable
is a variable that takes on the value of each element in the sequence
during each iteration of the loop. The code block inside the loop is executed once for each element in the sequence
.
The else
block is optional, and is executed after the loop has completed, but only if the loop did not encounter a break
statement.
Here’s an example of how to use a for
loop with else
in Python:
# Check if a number is prime
num = 7
for i in range(2, num):
if num % i == 0:
print("The number is not prime.")
break
else:
print("The number is prime.")
In this example, we’re using a for
loop with range()
to check if the value of num
is prime. The loop iterates over each integer from 2 to num
- 1, and checks if num
is divisible by that integer.
If num
is divisible by any integer, the loop prints “The number is not prime” and exits using a break
statement.
If the loop completes without encountering a break
statement, then num
is prime and the else
block is executed, which prints “The number is prime.” In this case, since num
is 7, which is a prime number, the output will be “The number is prime.”