This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Python Ternary Operator
Does Python have an equivalent of the ternary operator?:
( x < 5 ? 1 : 0 )
Or must I express the same thing with an if-else pair?
You can use a conditional expression:
1 if x < 5 else 0
In code written for very old versions of Python, you may also see:
x < 5 and 1 or 0
However, the conditional expression form is preferred for Python 2.5 and later.
Python has:
1 if x < 5 else 0
or the old style:
x < 5 and 1 or 0
Related
This question already has answers here:
Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?
(8 answers)
How to test multiple variables for equality against a single value?
(31 answers)
Comparison operators and 'is' - operator precedence in python?
(2 answers)
Check if all values in list are greater than a certain number
(9 answers)
Closed 6 months ago.
This is a small element of a larger script. Through trial and error I have found that this is the offending line:
>>> print('lower than all') if (3 < (2 and 9 and 9)) is True else print('false')
lower than all
This is clearly incorrect. However changing to this gives the right answer:
>>> print('lower than all') if 3 < (2 and 9 and 9) is True else print('false')
false
I have simplified the example, in reality all the numbers are variables being checked. I really don't understand the difference here!
Again why does this work correctly?!
>>> print('lower than all') if 3 < 2 and 9 and 9 is True else print('false')
false
If you see this chunk, it returns 9
>>> 2 and 9 and 9
9
To understand this, take a look at how the and operation between integers works. From the official doc:
The expression x and y first evaluates x; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned.
In this case, 2 is true, 9 is true and the final 9 is also true. Here, the last 9 is y and that's the value being returned.
Try out 2 and 1 and you'll see 1 as the result.
Due to this, your statement
print('lower than all') if (3 < (2 and 9 and 9)) is True else print('false')
Actually becomes
print('lower than all') if (3 < 9) is True else print('false')
And prints 'lower than all'
This question already has answers here:
Does Python have a ternary conditional operator?
(31 answers)
Closed 5 years ago.
Suppose that I have the following Python Code:
def fun5(a, b, c):
return a <= b if b <= c else False
fun5(2, 4, 6)
The output of fun5 is True. How is this code being evaluated by Python?
I was expecting a SyntaxError because of the lack of indentations and Python requires indentation.
What you're looking at is called a conditional expression/ternary operator, and it is perfectly valid syntax.
It's equivalent to:
if b <= c:
return a<=b
else:
return False
This question already has answers here:
Syntax error on print with Python 3 [duplicate]
(3 answers)
Closed 5 years ago.
# Example for Algorithm Case Study
def naïve(a, b):
x = a
y = b
z = 0
while x > 0:
z = z + y
x = x - 1
return z
print naïve(4,5)
The output should be 20. Because of syntax error in print statement, I am not getting the answer.
print in Python 3 is a function, meaning you need to call it with parenthesis:
print(naïve(4,5))
This question already has answers here:
Check if all values in list are greater than a certain number
(9 answers)
Closed 6 years ago.
I am doing this following:
if ycoords[0] > 0 and ycoords[1] > 0 and ycoords[2] > 0:
# do stuff
Can you shorten this code by doing something like:
if (ycoords[0] and ycoords[1] and ycoords[2]) > 0:
# do stuff
Yes, you could use all:
if all(x > 0 for x in ycoords):
or ycoords[:3] if ycoords has more than 3 elements.
No, you can however simply use min to simplify the test syntactically:
if min(ycoords[0],ycoords[1],ycoords[2]) > 0:
# do stuff
and given that ycoords exactly has three elements, even shorter:
if min(*ycoords) > 0:
#do stuff
you can here, as #Tagc says, omit the asterisk (*):
if min(ycoords) > 0:
#do stuff
but this will result in some overhead.
Another option is to use an all:
if all(x > 0 for x in [ycoords[0],ycoords[1],ycoords[2]]):
# do stuff
or again, if ycoords contains only these three elements:
if all(x > 0 for x in ycoords):
# do stuff
Something that is not intuitive is that and:
"neither and nor or restrict the value and type they return to False and True, but
rather return the last evaluated argument"
Just open a python terminal and do:
>> 4 and 3
3
Why is this important to take in account?
You might think that:
(ycoords[0] and ycoords[1] and ycoords[2]) > 0
is equivalent to:
ycoords[0] > 0 and ycoords[1] > 0 and ycoords[2] > 0
or that is equivalent to:
(bool(ycoords[0]) and bool(ycoords[1]) and bool(ycoords[2])) > 0
but it's instead equivalent to:
ycoords[2] > 0
So, be careful because the interpreter is not doing what you think is doing.
This question already has answers here:
Does Python have a ternary conditional operator?
(31 answers)
Closed 7 years ago.
In java, I usually use
boolean x = a? b : c;
instead
if(a)
boolean x = b;
else
boolean x = c;
Is there any possibility for doing that in python?
You can do:
x = b if a else c
which means the same thing.