All you need to know about loops in python

Hey everyone, hope you are doing great. Loops are an important programming concept that allows you to execute a block of code repeatedly. Python has two main types of loops: the for loop and the while loop. In this blog post, we will cover everything you need to know about loops in Python, including how to use them, when to use them, and some common pitfalls to avoid.

The for loop

The for loop is used to iterate over a sequence (e.g., a list or tuple) and execute a block of code for each item in the sequence. The basic syntax of a for loop in Python is as follows:

for variable in sequence:
    # code to be executed for each item in sequence

Here, variable is a placeholder variable that represents each item in the sequence, and sequence is the sequence over which you want to iterate. The code inside the loop is indented and will be executed once for each item in the sequence.

Let's look at an example:

fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print(fruit)

Output:

apple
banana
cherry

In this example, we have a list of fruits and we are using a for loop to iterate over the list and print each fruit.

You can also use the range() function with a for loop to iterate a specific number of times:

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

Output:

0
1
2
3
4

Here, the range() function generates a sequence of numbers from 0 to 4, and the for loop iterates over this sequence and prints each number.

The while loop

The while loop is used to execute a block of code repeatedly as long as a certain condition is true. The basic syntax of a while loop in Python is as follows:

while condition:
    # code to be executed as long as condition is true

Here, condition is a boolean expression that is evaluated before each iteration of the loop. If the condition is true, the code inside the loop is executed. This process is repeated until the condition is false.

Let's look at an example:

i = 0
while i < 5:
    print(i)
    i += 1

Output:

0
1
2
3
4

In this example, we are using a while loop to print the numbers 0 to 4. The while loop continues to execute as long as i is less than 5.

Loop control statements

Python provides several loop control statements that allow you to control the flow of a loop. These include:

  • break: Terminates the loop and skips to the next statement after the loop.

  • continue: Skips the current iteration of the loop and moves on to the next iteration.

  • pass: Does nothing and is used as a placeholder when no code is required.

Here's an example that uses the break statement to exit a loop when a certain condition is met:

fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    if fruit == "banana":
        break
    print(fruit)

Output:

apple

In this example, we are using a for loop to iterate over a list of fruits. When the loop encounters the fruit "banana", the break statement is executed, which terminates the loop.

So, this was all you need to know about loops in python. You can practice some of the loops questions on websites like HackerRank to become proficient.Hope you got value out of it. Consider subscribing to the newsletter for more such Python topics

Good Luck :)