Ternary Operator

Share: Facebook | Twitter | Whatsapp | Linkedin Visits: 598


Python Ternary Operator


#ScriptsKart

# Python ternary operator - how to use it to make your code more concise.

# The ternary operator evaluates the condition. If the result is True,
# it returns the value_if_true. Otherwise, it returns the value_if_false

# The following syntax is called a ternary operator in Python:

# value_if_true if condition else value_if_false

age = input('Enter your age:')

ticket_price = 20 if int(age) >= 18 else 5

print(f"The ticket price is {ticket_price}")

#ScriptsKart