Python Solution: How ternary operators used in python?

The Ternary operator is the operator that is used to show the conditional statements. This consists of the true or false values with a statement that has to be evaluated for it.

Ternary operator in python is like a plain English. For instances, display a value if a less than b else display b value. See example below.

# Program to demonstrate conditional operator 
x, y = 49, 69
# Assign value of x in minValue if x < y else Assign y 
minValue = x if x < y else y
# Python program with nested ternary operator 
x, y = 10, 20
if x != y: 
    if x > y: 
        print("x is greater than y") 
        print("y is greater than x") 
    print("Both x and y are equal")

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.