This question already has answers here:
Does Python have a ternary conditional operator?
(31 answers)
Closed 7 months ago.
After working through some toy examples, I could see that it's possible to emulate the ternary operator of "c" condition?value_if_true:value_if_false in Python using condition and value_if_true or value_if_false.
I would like to know if it works in all cases and if it is better or worse than using value_if_true if condition else value_if_false.
It's strictly worse: the conditional expression was added to the language specifically to avoid the problem with using ... and ... or ... incorrectly.
# 0 is falsy
>>> True and 0 or 5
5
>>> 0 if True else 5
0
PEP-308 references "FAQ 4.16" for other workarounds to the problems with and/or, though I can no longer track down what FAQ is being referred to, but it was eventually decided that a dedicated conditional expression was preferable.
For example, one could write
(True and [0] or [3])[0]
to ensure that the true-result is truthy, so that the false-result isn't returned. The false-result has to be adjusted as well so that regardless of what result is produced, it can be indexed to finally provide the intended result.
Related
This question already has answers here:
What is Truthy and Falsy? How is it different from True and False?
(8 answers)
if statement without a condition
(3 answers)
Closed 2 years ago.
Apologies if this is a straightforward thing, I’m just not having luck getting answers online (and if this isn’t a good place to post).
I’ve been trying to improve my Python, and I’ve been trying to make sense of some code for a Neural Network/Natural Language Processing package. I came across this:
if args.encoder_layers_to_keep:
args.encoder_layers = len(args.encoder_layers_to_keep.split(","))
I haven’t come across an if statement with an expression like this. There’s no comparison between variables or anything. My only guess is that it returns a true or false value and works with that but I’m really not sure.
For reference, here’s the full script - https://github.com/pytorch/fairseq/blob/master/fairseq/models/transformer.py
I’d appreciate any help with this.
Thanks,
Justin
Empty sequences (e.g., lists, tuples, strings) evaluate to False. Non-empty sequences evaluate to True.
args.encoder_layers_to_keep seems to be a string variable. An empty string "" evaluates to False, and a non-empty string evaluates to True.
You can prove this to yourself by using the builtin function bool to convert to a boolean. Try bool("") and bool("foobar").
This is suggested in the Python style guide (PEP8):
For sequences, (strings, lists, tuples), use the fact that empty sequences are false:
if not seq:
if seq:
# Wrong:
if len(seq):
if not len(seq):
This question already has answers here:
Python. Will TWO condition checked, If `ONE == True`? [duplicate]
(3 answers)
Closed 5 years ago.
I have a multi-part boolean expression in a piece of python code, part of which involves a call to a random number generator and evaluating an expoenential of a sum of a 2d array. Since this is buried deep in nested loops I want to avoid checking that last part if at all possible, since it's computationally expensive.
if self.B == 0 or (np.sign(self.B) == -sign) or (np.random.rand() < np.exp(-2*sign*self.B*np.sum(cluster))):
do stuff
If either of the first two expression are true, will the random number generator still be called? Or is it guaranteed to evaluate those parts in order and stop once it finds one that is true?
I can always make it explicit by breaking it up, but it seems like something I should probably know anyway.
In if A or B, B is only evaluated if A is false.
This concept is called short circuiting, and you can read a little about it here.
The idea is that you go from left to right until a result is determined. Once that's the case, you stop.
This question already has answers here:
and / or operators return value [duplicate]
(4 answers)
Closed 6 years ago.
I didn't see this question already, and I'm curious to find out what's going on here. Please forgive me if it's been asked and answered already.
While taking a course on Udacity, the instructor quickly mentioned a line he used in the Python code that looked like this:
n = n and int(n)
He said that it's basically equivalent to:
if n:
n = int(n)
The statement works just as he described, but I'm curious about how it works, because I've never seen this kind of construction in Python before. Can anyone explain what it is about using and in this casting assignment that makes it work this way?
Also, having never seen it before, I would have no idea that this statement is functioning the way it is, which makes me think that it may not be the most Pythonic way to write this code. Is this a widely known and accepted shortcut, or should I generally stick with the explicit if statement when I need to do something like this?
Edit: Ok, initial question is a duplicate, and has been clearly answered elsewhere, and now here as well. Thanks to all for those answers. One final question I have: which way should I prefer in my code (aka which is more Pythonic)?
condition1 and condition2.
condition2 is executed only if the condition1 passes. If n resolves to a True in Boolean context then n is converted to int by the statement int(n) and assigned to n
In context of Boolean operations the following are considered as false
- False, None, numeric zero of all types, and empty strings and containers (including strings, tuples, lists, dictionaries, sets and
frozensets)
From doc
This is caused by python's lazy evaluation (or more accurately, short-circuit boolean expressions), see:
https://docs.python.org/2/reference/expressions.html#boolean-operations :
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.
Also see the answer to:
Does Python evaluate if's conditions lazily?
This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 7 years ago.
I have problems to set up correctly my if statement.
This is my code:
def task_13():
Main_meal=['Meat','Cheese','Fish']
addons=['Potatoes','Rice','Salad']
my_meal=[(x+y) for x in Main_meal for y in addons if (x+y)!= 'FishRice' and 'CheeseRice']
print(my_meal)
My question is why Python filter out the 'CheeseRice' when is it stated there but only filter out the 'FishRice' option.
This is my output:
['MeatPotatoes', 'MeatRice', 'MeatSalad', 'CheesePotatoes', 'CheeseRice', 'CheeseSalad', 'FishPotatoes', 'FishSalad']
Thank you for your advice.
Here's the official reference on Python operator precedence, note that and is lower precedence than !=, so the != is evaluated first. Also and is a simple operator that takes the booleans on either side and returns a boolean representing their logical AND, it doesn't do what you tried to make it do.
Instead of
if (x+y)!= 'FishRice' and 'CheeseRice'
you need:
if (x+y)!= 'FishRice' and (x+y) != 'CheeseRice'
or alternatively
if (x+y) not in ('FishRice', 'CheeseRice')
This question already has answers here:
Why do 'and' & 'or' return operands in Python?
(4 answers)
Closed 8 years ago.
In Python I am very used to using the logical operators and and or and was under the impression that they'd simply get evaluated with the left and right values and replaced with True or False at runtime.
I recently discovered that this is not the case. Consider:
>>> False or 10
10
I see the use case. Here if you have a function that returns a value or False on failure the above allows you to have a default fallback value. But with and:
>>> True and re.match("s", "s")
<_sre.SRE_Match object at 0x19f9098>
This puzzles me... It seems that the last operand of an and evaluation where both operands evaluate to True is always returned which makes sense in that it's the last one to run but I don't see why this is preferable to just having a bool.
Why does python do this?
Is there a use case for this that I'm not seeing?
In situations where you know you want a bool is it a good idea to convert these values? eg:
>>> bool(True and re.match("s", "s"))
True
In Python doc's words:
This is sometimes useful, e.g., if s is a string that should be
replaced by a default value
Boolean operations doc.