/  Technology   /  Python   /  How can the ternary Operators be used in Python?
Ternary Operators (i2tutorials.com)

How can the ternary Operators be used in Python?

The Ternary operator legitimate to assign one value to the variable if the condition is true and another value when the condition is not true. It is the operator that is basically known, as the conditional expression in python. It amazingly judges an object, which based on the condition being true or not.

These operators exist in various programming languages that allow to quickly testing the condition. The usage of ternary operators in python can be hugely fruitful and can also help to keep the code compact in an appropriate condition. In python, these operators mainly test the condition in a single line after restoring the multiline. If you want to know the usage of ternary operators, just have a look at this simple method of using the ternary operator in python.

Easy Method To Use Ternary Operators In Python:

Here is a simple example of using ternary operators in python, let’s check out quickly:


[ on_true] if  [expression ] else [ on_false ]

Method of Using:

# Program to demonstrate conditional operator

a,b= 10, 20

# copy value of a in min if a< b else copy b

min= a if a< b else b


Output: 10



Leave a comment