Ternary conditional operator | Sololearn: Learn to code for FREE!

+2

Ternary conditional operator

Does python have ternary conditional operator?

2/25/2021 6:27:14 AM

Roza Simonyan

5 Answers

New Answer

+7

Yes, it was added in version 2.5. Here you can see syntax: a if condition else b

+2

no 'clasic' short conditional operator, but you could write onelined assignable if..else: value = 42 if condition else 0

+2

https://www.sololearn.com/learning/2500/

+1

Roza Simonyan No. but you can try like this a = 10 b = 20 min = a if a < b else b print (min)

0

It can use python ternary operator in list comprehension my_list = list(range(1,11)) result = ['yes' if a%10==0 else 'No'] The statement inside the list is an example of python ternary operator