- Python Basics
- Python Home
- Python History
- Python Applications
- Python Features
- 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 & 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 & Time
- Python Exception Handling
- Python File Handling
- Python Advanced
- Python Classes & Objects
- Python @classmethod Decorator
- Python @staticmethod Decorator
- Python Class Vs Static Method
- Python @property Decorator
- Python Regular Expressions
- Python Send E-mail
- Python Event Handling
- Python Keywords
- Python All 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
- Python Test
- Python Online Test
- Give Online Test
- All Test List
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:
- for loop
- while loop
- nested loop
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):
print(i)
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.
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 »
Follow/Like Us on Facebook
Subscribe Us on YouTube