Skip to content

Instantly share code, notes, and snippets.

@smly
Last active December 13, 2015 21:38
Show Gist options
  • Select an option

  • Save smly/4978576 to your computer and use it in GitHub Desktop.

Select an option

Save smly/4978576 to your computer and use it in GitHub Desktop.
for ... else ...
$ python -v
Python 2.7.2

case1:

else: print 1

result1:

  File "a.py", line 1
    else: print 1
       ^
SyntaxError: invalid syntax

case2:

for i in range(10):
  else: print i

result2:

  File "a.py", line 2
    else: print i
       ^
SyntaxError: invalid syntax

case3:

for i in range(10):
else: print 1

result3:

  File "a.py", line 2
    else: print 1
       ^
IndentationError: expected an indented block

case4:

for i in range(10):
  if i < 5: print i
else: print i

result4:

0
1
2
3
4
9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment