50% users failed to get the best score. It's your turn to test now.
Questions curated by the expert mentors at codzify.com
3 min 44 sec read
If you are familiar with programming languages like C, C++ or Java you might have heard about for 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 statement saves the time and complex code.
Python supports two types of loops:
1) for loop
2) while loop
Python while loop program executes set of statements repeatedly if condition is true. Following example will illustrate how to use python while loop,
x = 0 while(x <= 5): print(x) x = x + 1
This 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.
Python for loop is used to execute a set of statements repeatedly if the condition is true. Unlike other programming languages, for loop the syntax is different in python.
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, and increments by 1 (by default), and ends at a specified number.
The following example will illustrate how to use Python for loop
x = 0 for x in range(1,10): print(x)
Python break statement 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.
Python continue statement 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)
Easy to use online data structure compiler where you can execute the programs in your favourite programming language.
(C, C++, Python)