Python | loops

Python | loops

In Python, loops are used to repeatedly execute a block of code. There are two main types of loops: for loop and while loop.

for loop:

for loop are used to iterate over a sequence of items, such as a list or a range of numbers. Here’s an example of a for loop that iterates over a list of numbers and prints each one:

numbers = [1, 2, 3, 4, 5]
for num in numbers:
    print(num)

This code will output:

1
2
3
4
5

You can also use the range() function to create a range of numbers to iterate over. Here’s an example that uses a for loop to print the numbers from 0 to 9:

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

This code will output:

0
1
2
3
4
5
6
7
8
9

while loop:

while loop are used to repeatedly execute a block of code as long as a certain condition is true. Here’s an example of a while loop that counts down from 5 and stops when the count reaches 0:

count = 5
while count > 0:
    print(count)
    count -= 1

This code will output:

5
4
3
2
1

It is important to note that, if you are using a while loop, you must ensure that the condition will eventually become False otherwise the loop will run indefinitely, this is known as an infinite loop.

Nested loop:

A nested loop is a loop that is inside another loop. This means that a loop can contain another loop, and that inner loop will be executed for each iteration of the outer loop.
Here’s an example of nested loops:

for i in range(3):
    for j in range(2):
        print(i, j)

This code will output:

0 0
0 1
1 0
1 1
2 0
2 1

In this example, the outer loop iterates over the range of 3, and for each iteration of the outer loop, the inner loop iterates over the range of 2. This means that the inner loop will be executed a total of 3*2=6 times, and the output will be the combination of each iteration of the outer and inner loop.
Another example, using nested loops to iterate over a matrix:

matrix = [[100, 200, 300], [400, 500, 600], [700, 800, 900]]
for i in range(len(matrix)):
    for j in range(len(matrix[i])):
        print(matrix[i][j], end=' ')
    print()

This code will output:

100 200 300
400 500 600
700 800 900

In this example, the outer loop iterates over the matrix using the range of the matrix’s length, and the inner loop iterates over the matrix’s sub-lists using the range of the sub-lists’ length. This means that the inner loop will be executed a total of 3*3=9 times, and the output will be the matrix.

In addition to these basic loops, Python also has the break statement, which can be used to exit a loop early, and the continue statement, which can be used to skip the rest of the current iteration and continue with the next one.

In summary, loops in Python are used to repeatedly execute a block of code. The for loop is used to iterate over a sequence of items, such as a list or a range of numbers, and the while loop is used to repeatedly execute a block of code as long as a certain condition is true. The break and continue statements are used to control the flow of a loop.

Leave a Reply

Your email address will not be published. Required fields are marked *

📢 Need further clarification or have any questions? Let's connect!

Connect 1:1 With Me: Schedule Call


If you have any doubts or would like to discuss anything related to this blog, feel free to reach out to me. I'm here to help! You can schedule a call by clicking on the above given link.
I'm looking forward to hearing from you and assisting you with any inquiries you may have. Your understanding and engagement are important to me!

This will close in 20 seconds