
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
Advertisements