
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to use nested loops in Python?
In programming, the term nesting is used when a construct is used inside another construct of same type. Hence nested loop refers to use of a loop inside a loop. In such a case inner loop takes all iterations for each iteration of outer loop.
In Python the body of loop is group of statements with an increased indent level. In case of nested loop, indent level of inner loop id more than outer loop. Following simple example of nested loop shows the feature −
Example
for x in range(4): for y in range(4): print (x,y)
Output
This produces following output
0 0 0 1 0 2 0 3 1 0 1 1 1 2 1 3 2 0 2 1 2 2 2 3 3 0 3 1 3 2 3 3
- Related Articles
- How to use else statement with Loops in Python?
- How to use single statement suite with Loops in Python?
- How to use nested if statement in Python?
- How to use multiple for and while loops together in Python?
- Java Nested Loops with Examples
- Demonstrate nested loops with return statements in JavaScript?
- Golang Program that Removes Duplicates Using Nested Loops
- What is the best way to break from nested loops in JavaScript?
- How to use nested while loop in JavaScript?
- How to use nested array in android listview?
- How to use nested for loop in JavaScript?
- How to make loops run faster using Python?
- How to prevent loops going into infinite mode in Python?
- How to create nested Python dictionary?
- How can we use nested transactions in MySQL?

Advertisements