PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, December 10, 2022

[FIXED] What is the use of "else" after "for" loop in Python?

 December 10, 2022     python, syntax     No comments   

Issue

It seems both of the below codes are printing the same, then what is the need of "else" block after "for" loop in python.

Code 1:

for i in range(10):
    print i
else:
    print "after for loop"

Code 2:

for i in range(10):
    print i

print "after for loop"

Thanks in advance.


Solution

From the documentation:

Loop statements may have an else clause; it is executed when the loop terminates through exhaustion of the list (with for) or when the condition becomes false (with while), but not when the loop is terminated by a break statement.

Follow the link for an example how this can be used.



Answered By - Aaron Digulla
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing