Loops in Python

  • December 5, 2022
  • python
JOIN Clause

There may be times when you need to execute a block of code several times. A loop statement allows us to repeat a statement or group of statements. To handle looping requirements, the Python programming language provides the following types of loops.

for loop:

Sequential traversal is accomplished with for loops. This loop repeats a code block and shortens the code that manages the loop variable.

Syntax:

for value in sequence:  
     #code block 

where value is used to hold the value of every item present in the sequence before the iteration begins until this particular iteration is completed. Loop iterates until the final item of the sequence are reached.

For instance,

n = 10
for i in range(0, n):
    print(i)

Output:

0
1
2
3
4
5
6
7
8
9

Example 2:

List=[1,2,4,8,16,32,64,128]
for i in range(0, len(List)):
	print (List[i])

Output:

1
2
4
8
16
32
64
128

Example 3:

List=[[2, 4, 6], [3, 6, 9], [ 4, 8, 12], [5, 10, 15], [6,12,18]]
for i in List:
	for k in i:
		print ("Element of list within a list - ", k)

Output:

Element of list within a list -  2
Element of list within a list -  4
Element of list within a list -  6
Element of list within a list -  3
Element of list within a list -  6
Element of list within a list -  9
Element of list within a list -  4
Element of list within a list -  8
Element of list within a list -  12
Element of list within a list -  5
Element of list within a list -  10
Element of list within a list -  15
Element of list within a list -  6
Element of list within a list -  12
Element of list within a list -  18
while loop:

In Python, while loops are used to iterate until a condition is met. The statement in the program that follows the while loop, on the other hand, is executed once the condition changes to false.

Syntax:

while condition:
     # execute these statements
else:
     # execute these statements

Example:

count = 0  
  
# Iterating through the while loop  
while (count < 10):      
    count = count + 2  
    print(count) # Executed until condition is met
else:  
    print("while condition false")

Output:

2
4
6
8
10
while condition false

Single statement while Block:

As shown below, the loop can be declared in a single statement. This is similar to the if-else block where the code can be written in a single line.

count = 0  
while (count < 6): print("Hello") 
Loop Control Statements:

The execution of loop control statements differs from the normal sequence. All automatic objects created in that scope are destroyed when execution leaves that scope.

Python provides the following control statements:

StatementDescription
Break This command ends the loop’s execution and moves control to the statement following the loop.
ContinueThis command skips the current loop iteration. Once the Python interpreter reaches the continue statement, the statements following it are not executed.
PassThe pass statement is used when a statement is required but no code is to be executed.

Example of continue:

# Prints all letters except 'a' and 't'
for letter in 'RabeccaFatima':
	if letter == 'a' or letter == 't':
		continue
	print ('Current Letter :', letter)
	var = 10

Output:

Current Letter : R
Current Letter : b
Current Letter : e
Current Letter : c
Current Letter : c
Current Letter : F
Current Letter : i
Current Letter : m

Example of break:

# Prints all letters except 'c'
for letter in 'RabeccaFatima':
	if letter == 'c':
		break
	print ('Current Letter :', letter)

Output:

Current Letter : R
Current Letter : a
Current Letter : b
Current Letter : e

Example of pass:

for letter in 'RabeccaFatima':
	pass
print ('Last Letter :', letter)

Output:

Last Letter : a

Note: also read about Python – if, elif, else Conditions

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Leave a Reply

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