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.
Related
This question already has answers here:
Evaluating a mathematical expression in a string
(14 answers)
Closed 1 year ago.
I am trying to take a piece of user input: 5+5 or 5*5 but my code receives it as a string. I want to convert it to an expression from which an answer can be deduced. However, I am not sure how to do this.
For that there's a standard function called as eval.
Examples:
>>> eval("5*5")
25
>>> eval("5+5")
10
>>> eval("2+3*4-4")
10
It will take a string and then calculate the output as per the BODMAS Rule.
Simply use "eval", but unsafe.
This question already has answers here:
Understanding slicing
(38 answers)
Closed 2 years ago.
If a filter's group delay is N, the filtered signal using this filter is sig_filtered, what does sig_filtered[N::] mean in python?
I saw other usages of this python operator "::", e.g. A[::3] in another post (What is :: (double colon) in Python when subscripting sequences?), where A is a list. Can somebody give out a summary on how to use this python operator "::"?
sig_filtered[N::] is the same as sig_filtered[N:] and the same as sig_filtered[N::1], or sig_filtered[N:len(sig_filtered):1], or sig_filtered[N:len(sig_filtered)].
There are three values which define a slice: start, stop and step, e.g. data[start:stop:step]
You can omit start and will
default to 0.
You can omit stop and it will default to the full
length.
You can omit step and it will default to 1.
These behave the same way as the arguments to the range function
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:
How to check all items of a list in python are equal to a?
(2 answers)
Closed 6 years ago.
I have an array consisting of booleans like: [False, True, True, False,...] and so on. I wonder if it is possible to "and" combine them all in a fast way in python. I do not want to bound to a certain kind of variable like in this question: check all items if they are equal to a . This is only for boolean, like in the question described. The answers from this previous question do not refer to booleans. So sth like:
ray = [True,True,True]
for i in range(1,len(ray)):
t = ray[i] and ray[i-1]
But in a fancy python one-line style.
You are looking for the all() function.
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')