If you are familiar with programming languages like C, C++, or Java you might have heard about loops, while loops, etc. Loops are basically used to repeatedly execute the set of statements. Suppose you want to print the elements from 1 to 100, then initializing the variable 100 times is a very complicated task. So here looping of statements saves time and complex code.
Python supports two types of loops:
This example will illustrate how to write a while loop python program that executes a set of statements repeatedly if the condition is true.
x = 0 while(x <= 5): print(x) x = x + 1
This while loop python program first checks the condition x <= 5 and as the condition is true, print statement will be executed and the value of x increments by 1, then in the second iteration as condition 1<=5 is satisfied, again print will be executed and this happens for 5 times using while loop.
A for loop in python is a way of going through a list of items and running the same code on each item in the list. The syntax for a for loop in python looks like this:
for val in sequence: set of statements in loop body
Here, val
is the variable that takes the value of the item inside the sequence on each iteration.
Loop continues until we reach the last item in the sequence. The body of for loop is separated from the rest of the code using indentation.
fruits = ["apple", "banana", "cherry"] for x in fruits: print(x)
apple banana cherry
The range() function is being provided to execute the set of statements using for loop. The range() function returns a sequence of numbers, starting from 0 by default, increments by 1 (by default), and ends at a specified number.
The following example will illustrate how to use Python for loop range() function
x = 0 for x in range(1,10): print(x)
break statement in python is used to break the sequence of statements. The following example will illustrate the concept of break statements.
x = 0 for x in range(1,10): if(x==5): break print(x)
In the given program, when the value of x becomes 5, then using break statement we are telling the program to stop the loop and execute statements after for loop.
continue statement in Python is usually used in the situation when you want to skip the remaining code in the loop and continue iteration.
companies = ["ABC" , "PQR" , "XYZ"] for x in companies: if(x == "PQR"): continue print(x)
In the given program, when the value of x becomes "PQR", then using continue statement we are telling the program not to execute the next statement in the loop instead execute the next iteration. That's why "PQR" is skipped as you notice in the output after executing it.
Using enumerate function you can get the index of elements along with values.With the for loop, we can execute a set of statements, once for each item in a list, tuple, set etc. The following example will illustrate the use of enumerate function in python as follows
In the given python program, check whether the value of x is 5 or not, and since x != 5 else condition will be executed. So the output will be 3
companies = ["ABC" , "PQR" , "XYZ"] for i,x in enumerate(companies): print(i,x)
Unlock learning opportunities with our expert-led coding courses.
Real-time doubt solving in a live online coding class is also available in the course
that will help you to stay connected with an experts while learning.