Ternary Operator in Python

Ternary operators are more commonly known as conditional expressions in Python that evaluate something based on a condition being true or not. It simply allows to test a condition in a single line replacing the multiline if-else making the code compact. Syntax :
[true] if [expression] else [false]
This basically reads as follows: The result will be True if the expression true, otherwise the result is False .
x=20 y=10 res = "x greater" if x>y else "y greater" print(res)

Same as

x=20 y=10 if x>y: res = "x greater" else: res = "y greater" print(res)
Ternary operator allows to quickly test a condition instead of a multiline if statement. Often times it can be immensely helpful and can make your code compact but still maintainable.