Loops are used when same task needs to be executed multiple number of times. Example, printing mathematical table of a number, printing a message 10 times, sending multiple requests to an end point, creating a menu based application which continues till the user wants etc.
Python provides following loop constructs:

1. while loop
This loop consists of a condition and a set of statements. Set of statements are executed repeatedly till the condition remains True or the condition evaluates to True. There may be a single condition or multiple conditions separated by and/or operators.  Statements that are inside loop block are called Loop body. All statements that are part of Loop body are indented, that is, they are at least one space more indented than loop condition and are at the same indentation level.

while condition:
   # statement one
   # statement two
   # statement three
   # more statements


count = 0
while count < 5:        # loop till count < 5
   print('Count is ' + str(count))  # print count value
   count = count + 1    # increment count

Condition of loop is count < 5 which will remain True till count is less than 5. Loop body consists of 2 statements which are at same indentation level.  Output of the above loop will be

Count is 0
Count is 1
Count is 2
Count is 3
Count is 4

2. for loop
This loop can also be used to iterate till a condition remains True but its main use lies in its ability to iterate over data structures such as list, tuple or dictionary where it provides access to each item in the data structure and keeps on iterating till all the items are covered. A for loop can be used in the following two scenarios.

Iterate using an index
Many times you want to iterate for a definite number of times using a counter or an index variable. Example, from 1 to 10. Using for loop this can be done as

for index in range(5):
   print(index)      # prints 0 to 4

where index is a user defined variable and range is an in-built method which returns a range of numbers starting from 0 to 1 less than the argument passed to it. Thus, range(5) returns numbers from 0 to 4.

Iterate over a data structure
Suppose there is a list of items and you want to iterate it over that list using a for loop. Code will be

fruits = ['papaya', 'orange', 'mango', 'banana'] # create a list
for fruit in fruits:

Output of above code will be


Looking at both the above usages of for loop, its syntax can be generalized as below:

for <variable name> in <data structure or limit>:
     # statement one
     # statement two
     # other statements

Notice the in keyword and a colon(:) at the end. Also, all the statements of for loop are indented to denote that they belong to same block.


Share your thoughts !!

Close Menu

Never Miss an article !

Get the new post delivered straight into your inbox, enter your email and hit the button

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

codippa will use the information you provide on this form to be in touch with you and to provide updates and marketing.