Pyton

Python Ternary Operator

Python Ternary Operator
Ternary operators are Python built-in conditional operators that are used to evaluate the defined conditions. It evaluates the condition for being true or false. Like the if-else statement, it is another way of defining conditional statements. The ternary operator is defined in the single line. The if-else statement and ternary operator returns a similar output. The ternary operator was introduced in Python version 2.5. This article explains the Python ternary operator in detail with examples.

Syntax

Let's discuss the syntax of the Python ternary operator first. The syntax is as follows:

[on_true] if [expression or condition] else [on_false]

While using the ternary operator, first we write the name of the variable that value is to be checked in the condition. The on_flase statement is executed when the given condition is not true.

Examples

Now let's see some examples to understand the use of the ternary operator. In the below given example, we are comparing two number values with the ternary operator.

#declaring a num1 variable
num1=5
#declaring a num2 variable
num2=10
#using the ternary operator to check the highest value
result= num1 if num1>num2 else num2
print("The highest value is: ",result)

Output

The same example can also be implemented with the if-else statement. The ternary operator performs the job in a single line; however, the if-else statement uses multiple code lines for the same task. Let's implement the above given example with the if-else statement.

#declaring a num1 variable
num1=5
#declaring a num2 variable
num2=10
#implementing the if else statement
if(num1>num2):
result=num1
else:
result=num2
#printing the highest value
print("The highest value is: ",result)

Output

In the output, it can be seen that we get a similar output to the ternary operator.

The nested ternary operator can also be implemented. Let's implement the nested ternary operator in our Python script.

#declaring a variable num1
num1=5
#declaring a variable num2
num2=10
#implementing nested ternary operator
print ("num1 is greater than num2" if num1>num2 else "num2 is greater than num1"
if num1==num2 else "Both numbers are not equal")

Output

Ways to implement the Python ternary operator

There are different ways to implement the Python ternary operator. We can implement the Python ternary operator using tuples, dictionaries, and lambda functions. Let's implement the ternary operator using a tuple. We have created two number variables num1, and num2, and stored the random in these variables. The random numbers in Python are created using the random() function. The random() function is present in the random module.

import random
num1=random.random()
num2=random.random()
#implementing the ternary operator
print((num1, num2) [num1>num2])

Output

From the output, we cannot determine that either it's num1 or num2 value. Let's implement the above code in the following way:

import random
num1=random.random()
num2=random.random()
print((f"num1:num1", f"num2:num2") [num1>num2])

Output

Alright! Now let's implement the ternary operator using Python dictionary and lambda function.

import random
num1=random.random()
num2=random.random()
#using Python dictionary
print("Using Python dictionary:")
print((True:f"num1:num1",False:f"num2:num2"[num1>num2]))
#using lambda function
print("Using lambda function:")
print((lambda: f"num1:num1", lambda: f"num2:num2")[num1>num2]())

Output

Conclusion

Ternary operators are Python built-in operators that evaluate the given condition. It works like the if-else statement. This article explains the Python ternary operator in detail.

Emulate Mouse clicks by hovering using Clickless Mouse in Windows 10
Using a mouse or keyboard in the wrong posture of excessive usage can result in a lot of health issues, including strain, carpal tunnel syndrome, and ...
Add Mouse gestures to Windows 10 using these free tools
In recent years computers and operating systems have greatly evolved. There was a time when users had to use commands to navigate through file manager...
Control & manage mouse movement between multiple monitors in Windows 10
Dual Display Mouse Manager lets you control & configure mouse movement between multiple monitors, by slowing down its movements near the border. Windo...