
- 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 else conditional statement with for loop in python?
The else block in a loop (for as well as while) executes after all iterations of loop are completed and before the program flow exits the loop body. The syntax is as follows −
Syntax
while expr==True: #statements to be iterated while expr is true. else: #this statement(s) will be executed afteriterations are over
#this will be executed after the program leaves loop body
example
for x in range(6): print (x) else: print ("else block of loop") print ("loop is over")
Output
The output is as shown below −
0 1 2 3 4 5 else block of loop loop is over
- Related Articles
- Using else conditional statement with for loop in python
- How to use else statement with Loops in Python?
- How to use ‘else if ladder’ conditional statement is C language?
- How to use PowerShell break statement with the For loop?
- How to use continue statement in Python loop?
- How to use if...else statement at the command line in Python?
- How to use for loop in Python?
- How to use PowerShell Break statement with the While Loop?
- How to use for...in statement to loop through an Array in JavaScript?
- How to indent an if...else statement in Python?
- What is the ‘if...else if...else’ statement in Java and how to use it?
- How to use PowerShell break statement in foreach loop?
- How to use ‘for loop’ in Java?
- What is the ‘if else’ statement in Java and how to use it?
- How to use range-based for() loop with std::map?

Advertisements