423,851 Members | 2,762 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,851 IT Pros & Developers. It's quick & easy.

Ternary operator in Python?

P: 5
Explain the ternary operator in Python?
Oct 26 '18 #1
Share this Question
Share on Google+
1 Reply


P: 5
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
2

>>> print("Hi") if a<b else print("Bye")
Hi
Oct 26 '18 #2

Post your reply

Sign in to post your reply or Sign up for a free account.