Piton

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.

Cum se afișează suprapunerea OSD în aplicații și jocuri Linux pe ecran complet
Jucarea jocurilor pe ecran complet sau utilizarea aplicațiilor în modul ecran complet fără distragere vă poate elimina din informațiile relevante ale ...
Top 5 cărți de captură a jocului
Cu toții am văzut și ne-au plăcut jocurile de streaming pe YouTube. PewDiePie, Jakesepticye și Markiplier sunt doar câțiva dintre cei mai buni jucător...
Cum să dezvolți un joc pe Linux
Acum un deceniu, nu mulți utilizatori de Linux ar fi prezis că sistemul lor de operare preferat va fi într-o zi o platformă populară de jocuri pentru ...