Python Ternary operator

Short form of an if …​ else condition is the ternary operator, it is known as a conditional operator as well. It simply allows testing a condition in a single line replacing the multiline if …​ else, so it makes the code compact.


Objectives

  • Ternary operator syntax and examples


Python Ternary operator syntax and examples

Syntax

'True statement' if 'condition check' else 'False statement'

Code

age = 16
print("Hmm, You are adult.") if age >= 18 else print("You are not adult.")

Output

You are not adult.

Process finished with exit code 0


Python Ternary operator video tutorial