Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which version of python added the else clause for for loops?

Which was the first version of python to include the else clause for for loops?

I find that the python docs usually does a good job of documenting when features were added, but I can't seem to find the info on this feature. (It doesn't help that 'for' and 'else' are particularly difficult terms to google for on a programming website)

like image 509
Moe Avatar asked Mar 25 '09 15:03

Moe


People also ask

Does for loop have else?

'else' Clause in 'for' LoopLoop statements may have an else clause. It is executed when the for loop terminates through exhaustion of the iterable — but not when the loop is terminated by a break statement.

Do the loops have ELSE clause in Python?

Python - else in Loop As you have learned before, the else clause is used along with the if statement. Python allows the else keyword to be used with the for and while loops too. The else block appears after the body of the loop. The statements in the else block will be executed after all iterations are completed.

What is ELSE clause in Python?

An else statement contains the block of code that executes if the conditional expression in the if statement resolves to 0 or a FALSE value. The else statement is an optional statement and there could be at most only one else statement following if.

How do you use else while loop in Python?

While loop with elseThe else part is executed if the condition in the while loop evaluates to False . The while loop can be terminated with a break statement. In such cases, the else part is ignored. Hence, a while loop's else part runs if no break occurs and the condition is false.


1 Answers

It's been present since the beginning. To see that, get the source from alt.sources, specifically the message titled "Python 0.9.1 part 17/21". The date is Feb 21, 1991. This post included the grammar definition, which states:

for_stmt: 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] 

You might be able to find the 0.9.0 sources if you try harder than I did, but as the first public release was 0.9.0 on 20 Feb, that would get you back one day. The 0.9.1 release was a minor patch that did not affect this part of the grammar.

(Is that a UTSL reference or what? When was the last time you looked at a shar file? ;)

BTW, I reconstructed the original source and tweaked it a bit to compile under gcc-4.0 on my OS X 10.4 box. Details for those interested few, including python-0.9.1.tar.gz.

The entire development history is available from version control, even after changing version control systems twice. "hg log -p -r 6:7" from the cpython Mercurial archive shows that the "for/else" was committed on Sun Oct 14 12:07:46 1990 +0000, and the previous commit was Sat Oct 13 19:23:40 1990 +0000. for/else has been part of Python since October 1990.

like image 162
Andrew Dalke Avatar answered Oct 13 '22 00:10

Andrew Dalke