if...else on the same line?

Terry Reedy tjreedy at udel.edu
Sat Sep 21 13:49:05 EDT 2002


"David Garamond" <davegaramond at icqmail.com> wrote in message
news:mailman.1032622182.9002.python-list at python.org...
> i'm sorry for this terrorizingly basic question, but is it possible
at
> all to write an if ... else statement on a single line in python?

If you mean something like

if x == 1: y=3; else z=4

then no.  Keywords that introduce blocks (suites) must be first word
on line:  I believe this includes try, except, finally, for, while,
if, elif, else, def, and class.

A statement using a conditional expression is something else:
y = (x==1) and 3 or 4 #for example

Terry J. Reedy





More information about the Python-list mailing list