0

I must be doing something stupid but I just don't see it. I could not get the following simple code to work.

>>> def a_bigger_than_b(a,b):
...   'Yes' if a > b
  File "<stdin>", line 2
    'Yes' if a > b
                 ^
SyntaxError: invalid syntax
1
  • Found This and I agree with him Commented Jun 27, 2014 at 11:42

3 Answers 3

4
def a_bigger_than_b(a,b):
...   return 'Yes' if a > b else 'No!'

Actually your using it as Ternary Operator in such case you need else

i think you must give else this can be well understood by

first

c='Yes' if a > b

what will be the value for a if a is smaller than b this case is ambiguous so else is must

correct syntax

c='Yes' if a > b else 'No!'

A detailed explanation was given by here

*On 9/29/2005, Guido decided to add conditional expressions in the
    form of "X if C else Y".

    The motivating use case was the prevalance of error-prone attempts
    to achieve the same effect using "and" and "or".

    Previous community efforts to add a conditional expression were
    stymied by a lack of consensus on the best syntax.  That issue was
    resolved by simply deferring to a BDFL best judgment call.*
Sign up to request clarification or add additional context in comments.

5 Comments

Very surprised that I can't have a standalone if, thanks for your reply. Just tried that I could not even have an empty else. I really don't understand why it is done this way.
Can I not doing anything if a is smaller than b ? I did not use a variable thou
You can have an if without else but there is no need to have it in a one-liner.
@AskandLearn You should distinguish between the if in the ternary operator and the if as control statement. In the latter case, of course you can omit the else.
@glglgl yes, I did not know ternary operator in python is like that. I know it now and I will remember it.
2

Your syntax resembles me a bit of Perl.

I am not sure what exactly you want

def a_bigger_than_b(a,b):
    'Yes' if a > b

to do; there are several possimble interpretations.

Amongst the ones already mentioned, you could as well want

def a_bigger_than_b(a, b):
    if a > b: return 'Yes'

which does nothing in the <= case and eventually reaches the end of the function, where (implicitly) None is returned.

This is the one-liner syntax for Python, quite equvalent to the do_whatever if foo syntax in Perl.

Be aware that a function must always return a value; if you don't explicitly, None is returned implicitly.

Comments

1

You may change if statement in simpler one:

def a_bigger_than_b(a, b):
    return a > b and 'Yes'

3 Comments

... in which case False is returned if a <= b.
Yup, but may be changed to anything else than False, eg. a > b and 'Yes' or 'Noooo'
In this case yes, but in the general case, a if b else c is leaner than b and a or c. If a is a false value, you get c although b is true. (As said, that doesn't apply here, as 'Yes' is true.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.