Python Loops:
The flow of the programs written in any programming language is sequential by default. Sometimes we may need to alter the flow of the program. The execution of a specific code may need to be repeated several numbers of times.
For this purpose, The programming languages provide various types of loops that are capable of repeating some specific code several numbers of times.
Why we use loops in Python?
The looping simplifies the complex problems into the easy ones. It enables us to alter the flow of the program so that instead of writing the same code, again and again, we can repeat the same code for a finite number of times.
For example,
If we need to print the first 10 natural numbers then, instead of using the print statement 10 times, we can print inside a loop that runs up to 10 iterations.
Advantages of Loops:
The flow of the programs written in any programming language is sequential by default. Sometimes we may need to alter the flow of the program. The execution of a specific code may need to be repeated several numbers of times.
For this purpose, The programming languages provide various types of loops that are capable of repeating some specific code several numbers of times.
Why we use loops in Python?
The looping simplifies the complex problems into the easy ones. It enables us to alter the flow of the program so that instead of writing the same code, again and again, we can repeat the same code for a finite number of times.
For example,
If we need to print the first 10 natural numbers then, instead of using the print statement 10 times, we can print inside a loop that runs up to 10 iterations.
Advantages of Loops:
- It provides code reusability.
- Using loops, we do not need to write the same code again and again.
- Using loops, we can traverse over the elements of data structures (array or linked lists).
Python For Loop:
The for loop in python is used to iterate the statements or a part of the program several times. It is frequently used to traverse the data structures like list, tuple, or dictionary.
The syntax of for loop in python is given below:
for iterating_var in sequence:
statement(s)
Program to print the table of the given number:
list =[1,2,3,4,5,6,7,8,9,10]
n = 5
for I in list:
c = n*i
print(c)
Output:
5
10
15
20
25
30
35
40
45
50
For loop Using range() function:
The range() function is used to generate the sequence of the numbers. If we pass the range(10), it will generate the numbers from 0 to 9. The syntax of the range() function is given below.
Syntax:
range(start,stop,step size)
- The start represents the beginning of the iteration.
- The stop represents that the loop will iterate till stop-1. The range(1,5) will generate numbers 1 to 4 iterations. It is optional.
- The step size is used to skip the specific numbers from the iteration. It is optional to use. By default, the step size is 1. It is optional.
Nested for loop in Python:
Python allows us to nest any number of for loops inside a for a loop. The inner loop is executed n number of times for every iteration of the outer loop. The syntax is given below.
Syntax:
for iterating_var1 in sequence: #outer loop
for iterating_var2 in sequence: #inner loop
#block of statements
#other statements
Example:
#User input for number of rows
rows = int(input("Enter the rows:"))
#Outer loop will print number of rows
for i in range(0,rows+1);
#Inner loop will print number of Astrisk
for j in range(i):
print("*",end = ")
print()
Output:
Enter the rows:5
*
**
***
****
*****
Using else statement with for loop:
Unlike other languages like C, C++, or JAVA, Python allows us to use the else statement with the loop which can be executed only when all the iterations are exhausted. Here, we must notice that if the loop contains any of the break statements then the else statement will not be executed.
Example:
for i in range(0,5):
print(i)
else:
print("for loop completely exhausted, since there is no break.")
Output:
0
1
2
3
4
for loop completely exhausted, since there is no break.
Python While Loop:
The Python While loop allows a part of the code to be executed until the given condition returns false. It is also known as a pre-tested loop.
It can be viewed as a repeating if statement. When we don't know the number of iterations then the while loop is most effective to use.
The syntax is given below.
while expression:
statements
Example:
i=1
#The while loop will iterate until the condition becomes false.
while(i<=10):
print(i)
i=i+1
Output:
1
2
3
4
5
6
7
8
9
10
Infinite while loop:
If the condition is given in the while loop never becomes false, then the while loop will never terminate, and it turns into the infinite while loop.
Any non-zero value in the while loop indicates an always-true condition, whereas zero indicates the always-false condition. This type of approach is useful if we want our program to run continuously in the loop without any disturbance.
example:
while(1):
print("Hi! TechieMania Welcomes You")
Output:
Hi! TechieMania Welcomes You
Hi! TechieMania Welcomes You
Using else with While Loop:
Python allows us to use the else statement with the while loop also. The else block is executed when the condition given in the while statement becomes false. Like for loop, if the while loop is broken using statement, then the else block will not be executed, and the statement present after else block will be executed. The else statement is optional to use with the while loop.
Consider the following example:
i=1
while(i<=5);
print(i)
i=i+1
if(i==3);
break
else:
print("Thee while loop exhausted")
Output:
1
2
In the above code, when the break statement encountered, then while loop stopped its execution and the else statement.
Hope this helps you. Thank you.
~Jinal Menpara
Great work 👌
ReplyDeletenice work
ReplyDeleteGreat job
ReplyDelete