4. Ternary operator

Python supports conditional expressions, which are often called the ternary operator.

A conditional expression keeps a small value choice close to the assignment or return that uses it. It works best for simple two-way decisions; once the branches become large or side-effectful, a normal if statement is usually easier to read.

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'