# Ternary operator in Python?

Welcome to my blog, in this blog we will learn what is ternary operator how to use it in python.

1. Ternary Operator in Python

What is a ternary operator: The ternary operator is a conditional expression that means this is a comparison operator and results come on a true or false condition and it is the shortest way to writing an if-else statement. It is a condition in a single line replacing the multiline if-else code.

syntax : condition ? value_if_true : value_if_false

condition: A boolean expression evaluates true or false
value_if_true: a value to be assigned if the expression is evaluated to true.
value_if_false: A value to be assigned if the expression is evaluated to false.

How to use ternary operator in python here are some examples of Python ternary operator if-else.

Brief description of examples we have to take two variables a and b. The value of a is 10 and b is 20. find the minimum number using a ternary operator with one line of code. ( min = a if a < b else b ) . if a less than b then print a otherwise print b and second examples are the same as first and the third example is check number is even or odd.

```1)
a, b = 10, 20
min = a if a < b else b
print(min)
Output: 10

2)
num1, num2 = 5, 10
min = num2 if num1 > num2 else num1
output : 5

3)
def even_or_odd(num):
return 'even' if num%2==0 else 'odd'
even_or_odd(2)
# 'even'
even_or_odd(3)
# 'odd'```

The nested ternary operator in python case 1: we call a min() function with parameter 5 and 10 and check 5 is greater than 10 now the condition is false to go to nested condition and check num1 less than num2 now the condition is true print num1. it is a python ternary operator with multiple conditions.

```def min(num1, num2):
return num2 if num1>num2 else (num1 if num1<num2 else 'equal')
min(5,10)
# 5
min(10,5)
# 5
min(5,5)
# 'equal'```

## ternary operator in python using lambda:

```a, b = 10, 20
print((lambda: b, lambda: a)[a < b]())
#20

f = lambda x: 1.1*x if x>100 else 1.05*x
print(f(100))
#105.0```

## How to use ternary operator in dictionary python.

syntax : expression :var = dict[“key”] if dict.has_key(“key”) else “something”

a) Ternary operator with membership in python.

In this example, first we take a dict and check with ternary operator key is exist dict if condition is true then print the value of key and if condition is false then print -1

```dic = {'Name': "Martin", 'Age': 30}
key = 'Name'

#check has key
var = dic[key] if key in dic else -1
print(var)

# Martin

# Doesn't have key:
key = 'Test'
var = dic[key] if key in dic else -1
print(var)

# -1```

b) ternary operator with dict.get(key,default):

```dic = {'Name': "Martin", 'Age': 30}
key = 'Name'

# Has key:
var = d.get(key, -1)
print(var)
# Martin

# Doesn't have key:
key = 'Test'
var = d.get(key, -1)
print(var)

# -1```

Ternary operator topic is completed. If you like the article share it with your friends. if you have any question or query related to the article and have any suggestions please leave in the comment box.