Python loops:

In Python, looping structures are used to repeatedly execute a block of code. The primary looping structures in Python are for and while loops.

1. ‘for’ loop:

In Python, the for loop is used to iterate over a sequence (such as a list, tuple, string, or range) or other iterable objects.

The fundamental structure of a for loop is outlined as follows:

for variable in iterable:
#Code to run during each iteration

Here’s an example using a for loop to iterate over a list:

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

In this example:

  • The variable fruit takes on each value in the fruits list in each iteration of the loop.
  • The indented block of code below the for statement is executed for each value of fruit.

You can also use the range() function to generate a sequence of numbers for iteration:

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

In this example, the loop will iterate five times, and the variable i will take on values from 0 to 4.

  • Looping with Indices:

If you need both the value and the index of each element in a sequence, you can use the enumerate() function:

fruits = [“apple”, “orange”, “banana”]
for index, fruit in enumerate(fruits):
print(f”Index: {index}, Fruit: {fruit}”)

This will output:

Index: 0, Fruit: apple
Index: 1, Fruit: orange
Index: 2, Fruit: banana
 
  • Nested for Loops:

You can also have nested for loops to iterate over multiple sequences:

for i in range(3):
for j in range(2):
print(f”({i}, {j})”)

This will produce the following output:

(0, 0)
(0, 1)
(1, 0)
(1, 1)
(2, 0)
(2, 1)

The for loop is a powerful construct in Python, and it is commonly used for iterating over various types of data structures and performing repetitive tasks.

2. ‘while’ loop:

The ‘while’ loop is employed to iteratively execute a set of code while a designated condition remains true. The basic syntax is:

while condition:
#Code to run continuously while the condition remains true

Here’s a simple example:

count = 1
while count <= 5:
print(count)
count += 1
 

In this example:

  • The variable count is initialized to 1.
  • The while loop continues to execute as long as count is less than or equal to 5.
  • In each iteration, the value of count is printed, and then it is incremented by 1.

The output of this code will be:

1
2
3
4
5

It’s important to ensure that the condition in the while loop will eventually become false, or you may end up with an infinite loop. You typically update variables inside the loop to eventually satisfy the exit condition.

You can use loop control statements like break and continue to modify the execution of the while loop. For example:

count = 1
while True:
print(count)
count += 1
if count > 5:
break

In this example, the break statement is used to exit the loop when count becomes greater than 5.

Loop Control Statements:

In Python, loop control statements are used to modify the normal execution of loops. The three main loop control statements are break, continue, and pass.
 

1. ‘break’ Statement:

The break statement is utilized to prematurely exit a loop. Upon encountering a break statement within a loop, the loop is instantly terminated, and the program proceeds to the next statement following the loop.

Example:

for i in range(10):
if i == 5:
break
print(i)
 

In this example, the loop will print numbers from 0 to 4 and then exit when i becomes 5.

2. ‘continue’ Statement:

The continue statement is employed to bypass the remaining code within the loop for the ongoing iteration and move on to the subsequent iteration.

Example:

for i in range(10):
if i == 5:
continue
print(i)
 

In this example, the loop will print numbers from 0 to 9, but it will skip printing 5.

3. ‘pass’ Statement:

The pass statement acts as a placeholder where syntactically some code is required, but no action is desired or necessary. It is often used when a statement is syntactically needed but you want to do nothing.

Example 1:

for i in range(5):
pass # No action needed in this loop
 

Example 2:

if condition:
pass # No action needed in this block
else:
# Code to be executed if condition is false

Example 3:

def my_function():
pass # Function body not implemented yet

class MyClass:
pass # Class body not implemented yet

for i in range(5):
pass # Loop body not implemented yet

In these examples, pass is used to create a valid structure that can be filled in with code later.

The pass statement can be useful when you are in the process of writing code, and you want to create a valid syntactic structure without specifying the actual behavior at that moment.

Scroll to Top