position: fixed; top: auto !important; margin-left: 112px;

Python for loop & while loop

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

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.

For 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)

The break statement

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.

The continue statement

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.

Get Index of elements

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)

Previous Next Article







codzify.com


Largest collection of up-to-date tutorials to learn programming languages. We are focused on easy learning. Massive collection of interview questions one may need for preparation.

Social Profile


Linkedin
Twitter
Facebook

Copyright 2018. All rights reserved.