Loops in Python
The word "loop" refers to a shape produced by a curve that bends round. Means that, running in the loop, runs continuously. In the same way, loop in Python is used to execute a block of code, multiple times.
Types of Loops in Python
There are 3 types of loop available in Python, including nested loop. Here are the list of all these three loops:
Note - Nested loop is of both type, that is it may be nested for, or nested while or with combination of both. Whatever the programmer need. Both type of nested loop is described in separate tutorials of the loop.
Python Loop Examples
Since both for and while loop is described in detail, in separate tutorial. Therefore, this article only includes some basics of these loops. For detail, you can refer to the separate tutorial on it. Also the next tutorial is of about for loop. For now, let's take an example, illustrates loops in Python:
for i in range(5): print(i)
The output produced by above loop program in Python, is shown in the snapshot given below:
As the above program uses for loop, therefore let's create the same program, using while loop:
i = 0 while i<5: print(i) i = i+1
This program, produces exactly same output as of previous program. As you can see, the main task of both loops in Python, is to execute a block of code, multiple or required number of times. That is, in first program, the following code (single statement):
gets executed 5 times. Whereas, in second program, the following block of code (multiple statements):
print(i) i = i+1
also gets executed 5 times. That is, the program flow continuously goes into the body of while loop, with new value of i (loop variable) each time, until the condition i<5 evaluates to be False.
Nested Loop in Python
Here is an example program, demonstrates the concept and use of nested loop in Python:
for i in range(5): for j in range(3): print(i, "\t", j) print()
The output produced by above program is:
The above program can also be written like, to produce more understandable output:
for i in range(5): for j in range(3): print("i =", i, "\t", "j =", j) print()
Here is the output produced by above program, this time:
Note - The range() function generates a sequence of numbers. If only one parameter is provided to it, then it will generates a sequence of number starting with 0 to one less than the parameter's value. For example, range(5) returns/gives 0, 1, 2, 3, 4.
Therefore, if you replace the range() with its returned sequence of numbers, the above program becomes:
for i in 0, 1, 2, 3, 4: for j in 0, 1, 2: print(i, "\t", j) print()
The program still produces same output. You can check it out with yourself.
Therefore, instead of wasting the time in writing these sequences, we need range(). Therefore, most of the time, we use this function to iterate for loop in Python. We can also iterate string, list etc. Iterating these objects are described in its separate tutorial.
The same program of nested loop can also be created using while loop as shown in the program given below:
i = 0 while i<5: j = 0 while j<3: print(i, "\t", j) j = j+1 print() i = i+1
produces exactly same output as of previous program.
- Print Multiplication Table
- Print Prime Numbers
- Find Factorial of a Number
- Pattern Programs
- Add Two Matrices
- Matrix Multiplication Program
« Previous Tutorial Next Tutorial »
Follow/Like Us on Facebook
Subscribe Us on YouTube