Google News
logo
Python - Interview Questions
Explain the ternary operator in Python.
Unlike C++, we don’t have ?: in Python, but we have this :
 
[on true] if [expression] else [on false]
 
If the expression is True, the statement under [on true] is executed. Else, that under [on false] is executed.
 
Below is how you would use it :
 
>>> a,b=2,3
>>> min=a if a<b else b
>>> min
Output : 2
 
>>> print("Hi") if a<b else print("Bye")
Output : Hi
Advertisement