Python Programming for Beginners Part 4

Dr. Virendra Kumar Shrivastava
6 min readAug 18, 2021

Repetitive statements / Loops in Python

Hey guys!

In the last week post, we have detailed discussion on the python conditional statements. In this post we are going to talk about repetitive statements or loops in python. The loop helps in repetitive execution of desired statement until specified condition remains True. So, get ready to understand loop.

What is repetitive statement or loop?

Repetitive statement or loop is one of the important concepts in the programming languages. As other languages like C, C++, Java etc supports looping, so the Python programming.

A repetitive statement is notion that allows to execute one or a group of statements at multiple times. Repetitive statement is also called a loop. Loops are used to execute the same code of lines at multiple times. The number of repetitions is controlled by the criteria specified in the loop expression / condition. The loop repeats a certain statement or group of statements until the condition specified in the loop is True. Let’s try to understand the loop with the help of an example. Suppose you went to the supermarket to purchase a pair of shoes. So, you will go to each and every shop until you get your preferred pair of shoes and then you return to your home. That’s how loop works in Python programming, Python will also perform the desired task until the specified condition does not meet.

Type of loop

Python programming supports following type of loop:

For loop

While loop

Nested loop

The else suite

What is for Loop?

The for is a repetitive or looping statement in the python programming. It is used to carry out repetitive task until a specified condition becomes false.

The syntax of for loop is given blow:

for < variable > in sequence:

statement block

For example, your spouse asked you to purchase some grocery items from Big Bazar. She has provided you a list of items that you have to purchase. The list of items is given below:

· Sugar

· Tea

· Bread

· Coffee

· Butter

· Milk

To print list of items, we can use for loop.

Let’s see another example: Print number between 1 to 10 using for loop.

In the above example, we took a variable name, “i”. which stores the value and print function displays your number 1 to 10.

Range() function

The range() is a inbuilt or pre-defined function in python programming language. It is used to iterate over a sequence of numbers. The general syntax of range() is given below:

range( beginning, end, [step] )

We can also use range() function as given below:

It is important to remember about range function that it prints one less value of its last value that means if you write such as range( 1, 5) then, it only prints the number from 1 to 4.

What is While Loop?

The while loop also works the same manner as for loop in the python. The syntax of while Loop is given below:

statement a

while (condition ):
statement block

statement b

For example:

In the above example, first we initialize a variable named as “i“ and stores a value 1 in it. The while loop evaluates an expression / a condition that the loop works continuously until the value of “i” is not greater than 11 and print the all value of “i“, until condition does not become false.

Nested loop

Nested loop is nothing but a loop within a loop that mean you can also execute a loop under another loop.

For example:

What is break Statement and continue Statement?

The break statement is applied to stop the execution of the loop at a specific point. The break statement is used inside while and for loop to come out of the loop. For example:

So, in the above example, we used for loop to run until the value of “i“ is not equal to 50 but after that, we realized that we only need 40 value of “I”. So, we used break command to terminate the program as soon as “i” becomes 40.

Continue Statement

The continue statement is used in a loop to ignore rest of the statements of the loop and transfer control to the beginning of the loop. When continue statement is executed, subsequent statements in the loop are by passed.

For example:

You can observe in the above example that the value “40” is not shown in the output due to use of continue statement.

Infinite Loop

Here, in the above, we forgot to increment values of “i”. Thus while loop condition will remain true and loop will execute infinitely. Because value of “i” will remain 1 forever. To stop program execution, we can press ctrl + C at the system prompt.

The else suite

Python allows to use else statement along with while or for loop. The syntax of else suite is given below:

while loop with else

While(condition):

Statements

else:

statements

for loop with else

for var in sequence:

Statements

else:

statements

Here statements followed by else are called suit.

In the above example, we can see that the else part will always execute regardless of the statements in the loop (while or for) are executed or not. Thus, in the above example, even grocery item found in the list still else part executed and displays grocery item not found. So, to over come this situation, we can use continue statement. For example:

Conclusion

In this post, we have explored repetitive statement or loops like for, while, nested loop, break, continue and infinite loop with example. You can also access Jupyter notebook to perform tutorial shown in this post.

Next up in the series, we will discuss on arrays in the Python.

On winding up notes, feel free to share your comments. Your comments will surely help me to present contents in better way. See you next week.

--

--

Dr. Virendra Kumar Shrivastava

Professor || Alliance College of Engineering and Design || Alliance University || Writer || Big Data Analytics