4. Ternary operator

There is no official ternary operator in Python, but we may use the if/else statement as follows to mimic the ternary operator.

4.1. Don’t do this

1is_male = True
2
3if is_male:
4    gender = 'male'
5else:
6    gender = 'female'

4.2. Do this

1is_male = True
2
3gender = 'male' if is_male else 'female'