Does ternary surgery exist in Python?

Asked

Viewed 40,933 times

24

I often see several programming languages where a ternary operation is almost always identical.

(x % 2 == 0) ? "par" : "impar"

However, when I went to try to do this in Python, it was wrong:

(x % 2 == 0) ? "par" : "impar"
             ^
SyntaxError: invalid syntax

If it doesn’t work that way, then what would a ternary operation look like in Python?

  • 3

    Remembering that this operator is not called ternary but yes conditional. It turns out that it is usually the only ternary operator (i.e., it receives three operands) of the languages in which it appears, so the strong association.

6 answers

38


There is, in Python it is known as Conditional Expression.

<expressao1> if <condicao> else <expressao2>

First, the condition is assessed (rather than expressao1), if the condition is true, expressao1 is evaluated and its value is returned; otherwise, expressao2 is evaluated and its value returned.

Based on your example, the code looks like this:

x = 10
print ("par" if x % 2 == 0 else "impar")

An alternative with boolean operators and and or:

<condicao> and <expressao1> or <expressao2>

However, it does not work in the same way as a Conditional Expression, if the condition is true, expressao1 is evaluated and its value returned; if expressao1 is false, expressao2 is evaluated and its value returned.

Based on your example:

x = 10
print (x % 2 == 0 and "par" or "impar")

According to the PEP 308, Conditional Expressions, the reason the syntax has not been implemented <condicao> ? <expressao1> : <expressao2> used in many languages derived from C is:

(In free translation)

Eric Raymond even implemented this.

The BDFL rejected this for several reasons: the two-point already has many uses in Python (although in fact it would not be ambiguous, because the point of question requires the corresponding two-point); for persons other than use languages derived from C, it is difficult to understand.

Obs: BDFL (Benevolent Dictator For Life): Guido van Rossum, creator of Python.

  • Um... how interesting. "It happened" is before the if and the "if not" after the else.

  • Is that really a ternary or is it a Python sugar syntax?

  • @Wallacemaxters It is more for ternary operator, from a look at the link of the documentation and in the PEP to see the discussion... =)

  • I’ll give you a moral :p. It was hard to choose one. Then I put a reward so as not to upset anyone :p

  • @Thanks wallacemaxters, I edited the answer to clarify some things..

  • 1

    Looks like you’re a Python fan, huh?

Show 1 more comment

10

In Python the if can be used as much as a statement, as an operator in different contexts. As an operator you can do this:

print(1 if True else 2)

In his example:

x = randint(0,9)
print ("par" if x % 2 == 0 else "impar")

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

He takes the first value if the condition is true, if the condition is false he takes what is in the else. Generally speaking the conditional expression (I don’t like the term ternary) is like this:

valorVerdadeito if condicao else valorFalso

7

Yes, in the tender operation you use the if and else normally.

y = "par" if x % 2 == 0 else "impar"

6

The language Python has its own syntax for ternary operation. Which differs from most that use the known syntax (condição ? verdadeiro : false).

His expression with if and else "normal" would be:

if x % 2 == 0:
    print "Par"
else:
    print "Ímpar"

Moving on to Python’s ternary operation, I’d be:

print "Par" if x % 2 == 0 else "Ímpar"

See working on Ideone.

  • @Wallacemaxters, https://docs.python.org/2/reference/expressions.html#conditional-Expressions

  • The doc is even better :p. Pity it’s in English

  • @Wallacemaxters, I put the documentation because it is the most reliable source. In Portuguese has pythonBrasil. http://wiki.python.org.br/SimulandoOperadorTernario

0

An interesting way to do it is also:

('impar', 'par')[x%2==0]

It’s even simpler in my opinion

-4

The function uses a ternary operator to calculate the MDC of two numbers

def mdc(a,b):
  return b if (a == 0) else mdc(b % a, a)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.