Ternary conditional operator | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Ternary conditional operator

Does python have ternary conditional operator?

4th May 2021, 6:50 PM
Kìrā Çhââñâ
Kìrā Çhââñâ - avatar
2 Answers
+ 2
Yes! Generally, var = [on_true] if [expression] else [on_false] for example: (finding the minimum from two numbers) a, b = 10, 20 min = a if a < b else b src: https://www.geeksforgeeks.org/ternary-operator-in-JUMP_LINK__&&__python__&&__JUMP_LINK/
4th May 2021, 6:59 PM
Rohit Kh
Rohit Kh - avatar
+ 1
yes, it works like so res = True if x > y else False it's just a bit different than other languages
4th May 2021, 7:00 PM
Apollo-Roboto
Apollo-Roboto - avatar