1value_when_true if condition else value_when_false
2Better Example: (thanks Mr. Burns)
3
4'Yes' if fruit == 'Apple' else 'No'
5
6Now with assignment and contrast with if syntax
7
8fruit = 'Apple'
9isApple = True if fruit == 'Apple' else False
10vs
11
12fruit = 'Apple'
13isApple = False
14if fruit == 'Apple' : isApple = True
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