We can run a single statement or set of statements repeatedly using loop command. In Python, we can run a loop by three ways. The basic functionality of the three loop commands is the same, but differ from each other in syntax and the amount of time required for checking the condition.
The following loop commands are available in Python programming language.
Sr. No. |
Name of the loop |
Loop Type and Description |
1 |
While loop |
It repeats the statement, while given
condition is true. It tests the condition before executing the loop body. |
2 |
For loop |
This type of loop executes the code block
multiple times and abbreviates the codes that manages the loop variable. |
3 |
Nested loops |
In nested loop, we can iterate a loop
inside another loop. |
Sr. No |
Name of the control statement |
Descriptions |
1 |
Break statement |
This command terminates the loop’s execution and transfers the
program’s control to the statement next to the loop. |
2 |
Continue statement |
This command skips the current iteration of the loop. The
statements following the continue statement are not executed, once the Python
interpreter reaches the continue statement. |
3 |
Pass statement |
Pass statement is used when a statement is syntactically necessary
but no code is to be executed. |
for value in sequence: {code block} |
---|
#Python program to show how the for loop works #very first create a sequence, like here i create a tuple of numbers numbers=[2, 8, 6, 7, 6, 4, 9, 8, 13, 25] #now create a variable to store the square of the numbers square=0 #now create an empty list of squares of the given numbers squares=[] #finally create a for loop in this way for value in numbers: square=value**2 squares.append(square) print("The list of square is", squares) |
---|
The list of square is [4, 64, 36, 49, 36, 16, 81, 64, 169, 625] |
---|
for value in sequence: #executes the statements until the sequence is exhausted else: #executes the statement when for loop is completed. |
---|
#very first create a sequence list1=(2,4,5,6,3,7,8,9) #now start for loop for value in list1: if value%2==0: print(value) #use else statement else: print("These are even numbers present in the list") |
---|
2 4 6 8 These are even numbers present in the list |
---|
#use of range function print(range(15)) print(list(range(15))) |
---|
range(0, 15) [0, 1, 2, 3, 4, 5, 6, 7. 8. 9, 10, 11, 12, 13, 14] |
---|
#use of range function print(list(range(5, 15))) print(list(range(9, 15, 3))) |
---|
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14) [9, 12] |
---|
#Python program to iterate over a sequence with the help of indexing list2=("Python", "Loops", "Sequence", "Condition", "Range") #iterating over list2 using range() function for iterator in range(len(list2)): print(list2[iterator].upper()) |
---|
PYTHON LOOPS SEQUENCE CONDITION RANGE |
---|
While Loop: While loops are used in Python to iterate untill a specified condition is met. However, the statement in the program that follows the while loop is executed once the condition changes to false.
While<condition>: {code block} |
---|
#Program to define while loop num = 1 #initiating the while loop while num<15: num=num*2 print(num) |
---|
2 4 8 16 |
---|
#Program to define while loop str = 0 #initiating the while loop while str<10: str=str+2 print("Python while loop") |
---|
"Python while loop" "Python while loop" "Python while loop" "Python while loop" "Python while loop" |
---|
#Program to define while loop num = 1 #initiating the while loop while num<30: num=num*2 print(num) #when condition becomes false, else statement starts. else: print("Terminated because 32 exceeds 30, so condition is false now") |
---|
2 4 8 16 32 Terminated because 32 exceeds 30, so condition is false now |
---|
Example1:
#Program to define continue control statement in loop. #initiating the loop for string in "Python loop": if string=="P" or string=="t" or string=="o": continue print('Current letter:',string) |
---|
Current letter: y Current letter: h Current letter: n
|
---|
#Program to define continue control statement in loop. #initiating the loop list1=(4, 7, 9, 8, 3, 4) for value in list1: if value%2==0 : continue print('odd number:',value) |
---|
odd number: 7 odd number: 9 odd number: 3 |
---|
2. Break Statement: It stops the execution of the loop, when the break statement is reached.
#Program to define break control statement in loop. #initiating the loop for string in "Python loop": if string=="l": break print('Current letter:',string) |
---|
Current letter: P Current letter: y Current letter: t Current letter: h
|
---|
#Program to define continue control statement in loop. #initiating the loop list1=(4, 2, 12, 8, 6, 3, 14) for value in list1: if value%2==1: break print('even number:',value) |
---|
even number: 4 even number: 2 even number: 12 even number: 8 even number: 6 |
---|
#Program to define pass control statement in loop. for string in "Python loop": pass print('last letter:',string) |
---|
last letter: p |
---|