for Loop in Python

  • Last updated Apr 25, 2024

A for loop in Python is a control structure that allows to execute a block of code repeatedly for a specific number of times or until some conditions are true.

A for loop is commonly used to iterate over items in lists, tuples, dictionaries, sets, string characters, and more.

The basic syntax of a for loop in Python is as follows:

for element in iterable:
    # Code to be executed

Here's an explanation of what each part of the for loop does:

  • element: This is a variable that represents the current element being processed in each iteration of the loop. You can choose any valid variable name here.
  • iterable: This is an object that can be iterated over, such as a list, tuple, string, dictionary, or any other iterable data structure.
  • Code block: The indented code below the for loop is the block of code that gets executed in each iteration. It is executed once for each element in the iterable.

Here's an example of using a for loop to iterate over a list of strings:

fruits = ["grapes", "mango", "apple", "pineapple", "banana"]
for f in fruits:
    print(f)

In this example, the for loop iterates through the strings list, and in each iteration, the value of f changes to the next element in the list. The print(f) statement inside the loop prints each fruit to the console.

The output of the above code is as follows:

grapes
mango
apple
pineapple
banana

You can also use for loops with other data types like numbers, dictionaries, and tuples, and you can combine them with conditional statements (if, elif, else) to control the flow of your program based on the loop's execution.

The break statement

The break statement is used to terminate the loop before the looping through all items is complete.

Here's an example where we iterate over a list of strings and terminate the loop when the current element's value is equal to "apple":

fruits = ["grapes", "mango", "apple", "pineapple", "banana"]
for f in fruits:
    if(f == "apple"):
        print("Value of f is %s. Exiting loop" %(f))
        break
    print(f)
print("End of program")

In this example, we have a list of fruit names in a list called fruits. The for loop iterates through the list. Inside the loop, we use an if statement to check if the current fruit is equal to "apple". If it is, we use the break statement to exit the loop prematurely. This way, the loop will print the fruits until it encounters "apple", and then it will stop executing.

The continue statement

The continue statement is used to skip the current loop and continue with the next loop.

Here's an example where we iterate over a list of strings and skip the loop when the current element's value is equal to "apple":

fruits = ["grapes", "mango", "apple", "pineapple", "banana"]
for f in fruits:
    if(f == "apple"):
        continue
    print(f)
print("End of program")

In this example, we have a list called fruits containing fruit names. The code uses a for loop to iterate through each element in the fruits list and perform certain actions based on the current element's value. Inside the loop, there is an if statement that checks whether the current fruit, represented by the variable f, is equal to the string "apple". If the current value is "apple", the continue statement is executed. This statement immediately skips the rest of the code within the current iteration of the loop and proceeds to the next iteration. As a result, the word "apple" is not printed.

The output of the above code is as follows:

grapes
mango
pineapple
banana
End of program