Older versions of Python would only allow a single simple statement after for ...:
if ...:
or similar block introductory statements.
I see that one can have multiple simple statements on the same line as any of these. However, there are various combinations that don't work. For example we can:
for i in range(3): print "Here's i:"; print i
... but, on the other hand, we can't:
for i in range(3): if i % 2: print "That's odd!"
We can:
x=10
while x > 0: print x; x-=1
... but we can't:
x=10; while x > 0: print x; x-=1
... and so on.
In any event all of these are considered to be extremely NON-pythonic. If you write code like this then experience Pythonistas will probably take a dim view of your skills.
It's marginally acceptable to combine multiple statements on a line in some cases. For example:
x=0; y=1
... or even:
if some_condition(): break
... for simple break
continue
and even return
statements or assigments.
In particular if one needs to use a series of elif
one might use something like:
if keystroke == 'q': break
elif keystroke == 'c': action='continue'
elif keystroke == 'd': action='delete'
# ...
else: action='ask again'
... then you might not irk your colleagues too much. (However, chains of elif
like that scream to be refactored into a dispatch table ... a dictionary that might look more like:
dispatch = {
'q': foo.break,
'c': foo.continue,
'd': foo.delete
}
# ...
while True:
key = SomeGetKey()
dispatch.get(key, foo.try_again)()