Python for loop - Codzify.com

3 min 44 sec read Basic


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)

How was the tutorial? Nice. Right?



Deep Concept videos to crack the highly-piad interviews.

Chekout out our Youtube Channel to get detailed video content on important topics in interviews.



What is your Interview Score?

Test your skillset with the curated questions created by experts around the globe.

Book a free test slot. Now !.

It usually takes 30 mins for an online test and this test will be MCQ based. Get detailed analytics based on your test and recommedations with personalised roadmaps.

Book a test slot. Now!

Have questions about the trial coding class?

Chat with our experts to discuss

Connect on WhatsApp


Recommended tutorials

#online compiler for c     #python for programming    

#dfs and bfs algorithm     #programming with c language

#storageclass in C    #listcomprehension in python