i = 3
x = "even" if i % 2 == 0 else "odd"
print(x)odd
if statements are not expressions
if/elif/else behaviour anywhere we can use an expressionfail() is never called, because the middle expression False is the one controlling the branchif syntax mimics that for filteringif to control what values are included in a list comprehensionand returns the first falsely value or last truthyor returns the first truthy or last falseyThis always evaluates to [1]
Avoid using boolean expressions
Can instead use a regular if, else statement
elif blocksdivisible by three
odd
Avoid if they have to split over multiple lines, e.g.
Here the expression is the same length as if we had just written it using a normal if...else construct
Cell In[11], line 3 if x and z := x > y: ^ SyntaxError: cannot use assignment expressions with expression
Cell In[13], line 1 w = dict(other_value = y := 1) ^ SyntaxError: invalid syntax
if statement where you might want an expressiontrue result if test expression else false resultif conditions and helper functions