Python for loop

In Python, a for loop is a control flow statement that allows you to iterate over a sequence of elements, such as a list, tuple, string, or range of numbers. The syntax for a for loop in Python is as follows:

for variable in sequence:
    # code to execute on each iteration

The variable is a new variable that is created on each iteration of the loop, and it takes on the value of the next element in the sequence. The code inside the loop, which is indented, is executed for each element in the sequence.

Here’s an example of a for loop that iterates over a list of strings and prints each string:

fruits = ['apple', 'banana', 'orange']
for fruit in fruits:
    print(fruit)

This will output:

apple
banana
orange

You can also use the range function to generate a sequence of numbers to iterate over. For example:

for i in range(5):
    print(i)

This will output:

0
1
2
3
4

In this example, the range(5) function generates a sequence of numbers from 0 to 4, and the loop iterates over each of these numbers, printing them out.In this example, the range(5) function generates a sequence of numbers from 0 to 4, and the loop iterates over each of these numbers, printing them out.

Iterating by Using Index of Sequence:

You can also iterate over a sequence using its index by combining the range() and len() functions. This can be useful if you need to access both the index and value of each element in the sequence.

Here’s an example of how to iterate over a list using its index:

fruits = ['apple', 'banana', 'orange']
for i in range(len(fruits)):
    print(i, fruits[i])

In this example, the range(len(fruits)) function generates a sequence of numbers from 0 to the length of the fruits list (which is 3). On each iteration of the loop, the i variable takes on the next value in this sequence, and fruits[i] accesses the corresponding element in the fruits list. The output of this code would be:

0 apple
1 banana
2 orange

Note that while iterating over a sequence using its index is possible in Python, it’s generally considered less Pythonic than iterating directly over the sequence using a for loop, since it’s more verbose and can be error-prone.

Using else Statement with for Loop:

In Python, you can use an else statement with a for loop to execute a block of code after the loop has finished iterating over the entire sequence. This can be useful if you want to perform some final action after processing all the elements in the sequence.

The syntax for using else with a for loop in Python is as follows:

for variable in sequence:
    # code to execute on each iteration
else:
    # code to execute after loop has finished iterating

The code inside the else block is executed once the loop has finished iterating over the entire sequence. Note that the else block will not be executed if the loop is exited prematurely using a break statement.

Here’s an example of using else with a for loop to search for an element in a list:

fruits = ['apple', 'banana', 'orange']
searched_fruit = 'banana'

for fruit in fruits:
    if fruit == searched_fruit:
        print(fruit, "found!")
        break
else:
    print(searched_fruit, "not found")

In this example, the loop iterates over each element in the fruits list. If the current element is equal to the searched_fruit, the loop prints a message indicating that the fruit was found, and exits the loop using the break statement. If the loop completes without finding the fruit, the else block is executed and a message is printed indicating that the fruit was not found.

If the searched_fruit in the above example was set to "mango", the output would be:

mango not found

Note that the else block is executed because the loop completes without finding the searched fruit.

Nested Loops:

In Python, it’s possible to use nested loops to iterate over multiple sequences at the same time. Nested loops consist of a loop within another loop, and are useful for situations where you need to perform a task for each combination of elements in two or more sequences.

The syntax for using nested loops in Python is as follows:

for variable1 in sequence1:
    # code to execute on each iteration of the outer loop
    for variable2 in sequence2:
        # code to execute on each iteration of the inner loop

In this example, the inner loop is nested inside the outer loop. On each iteration of the outer loop, the inner loop iterates over all the elements in sequence2. The code inside the inner loop is executed for each combination of variable1 and variable2.

Here’s an example of using nested loops to print all possible combinations of elements in two lists:

list1 = ['a', 'b', 'c']
list2 = [1, 2, 3]

for i in list1:
    for j in list2:
        print(i, j)

In this example, the outer loop iterates over each element in list1, and the inner loop iterates over each element in list2. The code inside the inner loop prints the current values of i and j. The output of this code would be:

a 1
a 2
a 3
b 1
b 2
b 3
c 1
c 2
c 3

Note that you can nest loops to any level of depth depending on the number of sequences you need to iterate over. However, be careful not to create too many nested loops, as this can lead to slow code and make it difficult to read and understand.