Given the following Python function (ignoring its shortcomings):
def adjust_year(year):
return year > 2000 and year - 2000 or year - 1900
If I change it to instead be:
def adjust_year(year):
return year - 2000 if year > 2000 else year - 1900
Will the behavior be equivalent or have I changed it in some subtle way?
They are indeed equivalent, but the conditional expression variation is preferred.
Your expression narrowly avoids the typical and ... or
pitfall where the middle expression evaluates to a falsy value (year >= 2000 and year - 2000 or year - 1900
and year = 2000
will result in 100
).