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')
Related
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.
This question already has answers here:
Bitwise operation and usage
(17 answers)
Closed 7 months ago.
I have tried to find out about these operations in Python:
x|3
x^3
x>>=3
x<<=3
I couldn't find anything. Please can anyone tell me what are these operations are called, so that I can search more about them.
See: https://docs.python.org/3/reference/datamodel.html#emulating-numeric-types
Python has methods known as "magic" or "dunder" (i.e. double-under) methods like __add__() that define what happens when operators like + are used.
This question already has an answer here:
Python: How to use re.search() in a compound if statement (is it even possible?)
(1 answer)
Closed 2 years ago.
While parsing lines I am using regex. I assign the re.search to a variable and then perform a conditional check on it.
x=re.search(....)
if x:
Is there a way to do the search and if condition in the same line?
Thanks!
if (re.search(...)):
Doesn't this option work?
If you need to work with x further, use :=
This question already has answers here:
Does Python support short-circuiting?
(3 answers)
Closed 5 years ago.
I have a question about a logical expression of the following sort:
for i in range (k): #k is large
if (a==b and test(c)==b): #test() takes some time to calculate
do something
Now I want to know, how the logical expression is processed. Are the two simple expressions calculated first and then combined via and? Or is a==b calculated, and in case it is False, test(c)==b neglected?
Thanks.
The a==b will be calculated first, and if it's true then the second expression will be evaluated. This is known as 'short-circuiting', see the docs.
This question already has answers here:
Check if a value exists in an array in Cython
(2 answers)
Closed 6 years ago.
In Python we could use if x in list: so I was wondering if there's a similar command in C, so that we don't have to go through the whole thing using a for.
How can you know whether a value is contained in an array without cycling through it? This is exactly what Python does under the hood. No, there's no magical way to instantly know this.