Combining colon statements on one line?

Jeff Shannon jeff at
Fri Jul 16 19:44:03 CEST 2004

Dave Blackington wrote:

>Hi, I'm trying to figure out how to combine an 'if'
>and 'else' statement on one line.  Tonight I was
>programming and came up with a very short if/else
>scenario that demanded 4 lines due to the fact that
>python can't read an 'if' & 'else' on the same line. 

You can also do this in only two lines, if that's really important to you.

    if year < 500:    year += 2000
    else:                   year += 1000

Personally, I don't mind running a few extra lines.  I can generally get 
enough visible in my editor that it's not a problem -- functions/methods 
that are more than a screen long are rather rare.  And when I need to 
see two more-widely separated functions at once... well, that's what 
code folding is for.  :)

Jeff Shannon
Credit International

More information about the Python-list mailing list