- Python Basics
- Python Tutorial
- Python Applications
- Python Versions
- Python environment setup
- Python Basic Syntax
- Python end (end=)
- Python sep (sep=)
- Python Comments
- Python Identifiers
- Python Variables
- Python Operators
- Python Ternary Operator
- Python Operator Precedence
- Python Control and Decision
- Python Decision Making
- Python if elif else
- Python Loops
- Python for Loop
- Python while Loop
- Python break Statement
- Python continue Statement
- Python pass Statement
- Python break vs. continue
- Python pass vs. continue
- Python Built-in Types
- Python Data Types
- Python Lists
- Python Tuples
- Python Sets
- Python frozenset
- Python Dictionary
- List vs. Tuple vs. Dict vs. Set
- Python Numbers
- Python Strings
- Python bytes
- Python bytearray
- Python memoryview
- Python Misc Topics
- Python Functions
- Python Variable Scope
- Python Enumeration
- Python import Statement
- Python Modules
- Python operator Module
- Python os Module
- Python Date and Time
- Python Exception Handling
- Python File Handling
- Python Advanced
- Python Classes and Objects
- Python @classmethod Decorator
- Python @staticmethod Decorator
- Python Class vs. Static Method
- Python @property Decorator
- Python Keywords
- Python Keywords
- Python and
- Python or
- Python not
- Python True
- Python False
- Python None
- Python in
- Python is
- Python as
- Python with
- Python yield
- Python return
- Python del
- Python from
- Python lambda
- Python assert
- Python Built-in Functions
- Python All Built-in Functions
- Python print() Function
- Python input() Function
- Python int() Function
- Python len() Function
- Python range() Function
- Python str() Function
- Python ord() Function
- Python chr() Function
- Python read()
- Python write()
- Python open()
- Python Examples
- Python Examples
Loops in Python (with Examples)
The word "loop" refers to a shape produced by a curve that bends around, means that it runs continuously when in the loop. In the same way, a loop in Python is used to execute a block of code multiple times.
Types of Loops in Python
There are three types of loops available in Python, including nested loops. Here is the list of all three loops:
Python Loop Examples
Since both "for" and "while" loops are described in detail in separate tutorials, therefore, this article only includes the basics of these loops. For details, you can refer to the separate tutorial on it. Also, the next tutorial is about "for" loops. For now, let's take an example that illustrates loops in Python:
for i in range(5): print(i)
The output produced by the above loop program in Python is shown in the snapshot given below:
As the above program uses the for loop, let's create the same program using the while loop:
i = 0 while i<5: print(i) i = i+1
This program produces exactly the same output as the previous program. As you can see, the main task of both loops in Python is to execute a block of code multiple times, or the required number of times. That is, in the first program, the following code is a single statement:
print(i)
gets executed five times. Whereas, in the second program, the following block of code contains multiple statements:
print(i)
i = i+1
also gets executed 5 times. That is, the program flow continuously goes into the body of the while loop, with a new value of i (the loop variable) each time, until the condition "i<5" evaluates to false.
Nested Loop in Python
Here is an example program that demonstrates the concept and use of nested loops in Python:
for i in range(5): for j in range(3): print(i, "\t", j) print()
The output produced by the above program is:
The above program can also be written like this to produce a 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 the above program, this time:
Note: The range() function generates a sequence of numbers. If only one parameter is provided to it, then it will generate a sequence of numbers starting with 0 and ending with one less than the parameter's value. For example, range (5) returns or gives 0, 1, 2, 3, 4.
So, if you replace range() with the list of numbers it gives you back, 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 the same output. You can check it out for yourself.
Therefore, instead of wasting time writing these sequences, we need range(). Therefore, most of the time, we use this function to iterate through a loop in Python. We can also iterate over strings, lists, etc. Iterating these objects is described in its own separate tutorial.
The same program of nested loops can also be created using the 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 the same output as the previous program.
More Examples
- Print Multiplication Table
- Print Prime Numbers
- Find Factorial of a Number
- Pattern Programs
- Add Two Matrices
- Matrix Multiplication Program
« Previous Tutorial Next Tutorial »