Python - Ternary Operator

python logo pixel art

Python has a ternary operator that simplifies the 'if/else' statement.


(true_value) if condition else (false_value)

is_member = True
price = '$2.00' if is_member else '$8.50'
x = input('Enter a number: ')
x = float(x) if '.' in x else int(x)
print(type(x))
import os

def _main():
    print('The main process')

def _fork():
    print('The fork process')

_fork() if os.fork() == 0 else _main()