Everything
def foo(i): return 100 if i < 10 else pass return 200 if i < 20 else pass return 1
Why does this not work in python? I believe this code might work the same way:
def foo(i): if i < 10: return 100 elif i < 20: return 200 else: return 1
Thanks!
return 100 if i < 10 else pass
you should read it as return (100 if i < 10 else pass) so pass not a value
return (100 if i < 10 else pass)
pass
In the documentation, you will see that the "ternary operator" should look like this:
conditional_expression ::= or_test ["if" or_test "else" expression] expression ::= conditional_expression | lambda_expr
and pass is statement not an expression
read your code as follows:
return (100 if (i < 10) else pass)
pass is not a value that you can return. The following code will work:
def foo(i): return 100 if i < 10 else (200 if i < 20 else 1)
pass is a null operation, that is, when it is executed, nothing happens. It is useful as a placeholder if the statement is required syntactically, but the code should not be executed. It should not be used as part of any logic.
You should read that you work like
def foo(i): if i < 10: return 100 else: return pass if i < 20: return 200 else: return pass return 1
return 100 if i < 10 else pass not an "if" expression, it is a ternary operator