1Python does not have a trailing if statement.
2There are two kinds of if in Python:
3
41. if statement:
5
6 if condition: statement
7 if condition:
8 block
9
102. if expression (introduced in Python 2.5)
11
12 expression_if_true if condition else expression_if_false
13
14And note, that both print a and b = a are statements. Only the a part is an expression. So if you write
15 print a if b else 0
16
17it means
18 print (a if b else 0)
19
20and similarly when you write
21 x = a if b else 0
22
23it means
24 x = (a if b else 0)
25
26Now what would it print/assign if there was no else clause? The print/assignment is still there.
27And note, that if you don't want it to be there, you can always write the regular if statement on a single line, though it's less readable and there is really no reason to avoid the two-line variant.
1# Cigar Party problem in https://codingbat.com/prob/p195669
2def cigar_party(cigars, is_weekend):
3 result = False
4 result = True if (is_weekend and cigars >= 40) or (cigars >= 40 and cigars <= 60) else False
5 return result