logical-operators

Task for invalid number in Python

Task for invalid number in Python Question: I need to write a program where numbers 100 to 200 and 0 are declared as valid. Everything else is invalid. When you put a invalid number in, it should print invalid but if the number is valid the nothing should print nothing in the terminal. I wrote …

Total answers: 3

Type check of user input and exclude negative numbers with exceptions

Type check of user input and exclude negative numbers with exceptions Question: I have the following method, which I use to take inputs from the user and handle it via exceptions until they fullfill the criteria. def enter_data(self, message: str, typ: type): while True: try: v = typ(input(message)) if isinstance(v, int) or isinstance(v, float) and …

Total answers: 1

Numpy 'logical and' producing different type list

Numpy 'logical and' producing different type list Question: I am doing a logical operation on a data frame column data using NumPy. I am checking if a row value is less than two compared to the preceding and successful row. I am getting output but different than what I expected ad = pd.DataFrame({‘a’:[1,3,4,6,7,5,9,10]}) op=np.array(np.logical_and.reduce([ad[‘a’]. diff(x).abs().le(2) …

Total answers: 2

Why does "and" operator on two values give the last value?

Why does "and" operator on two values give the last value? Question: I’ve been tinkering a little with Python operators and came across something I’m not sure about. If I perform a bitwise operation (&, |) on 2 integers I will get unsurprisingly their bitwise value. Meaning: >>> a = 11 >>> b = 3 …

Total answers: 4

How Do I use logical operators

How Do I use logical operators Question: I have tried everything and if you don’t choose “ST” it constantly loops round the while loop. I am not sure what to do, and it would be super helpful if anyone could tell me. I added the code at the top for some context; I only need …

Total answers: 3

Performing a logical or operation of multiple lists in Python

Performing a logical or operation of multiple lists in Python Question: I have a list containing some number of sublists. list1=[ [True,False,True,False], [False,False,True,False],[True,False,True,True], …..] I want to perform a logical or of these sublists in Python but the number of sublists is unknown. For two lists I can do numpy.logical_or but how can I do …

Total answers: 2

Python safe dictionary key access

Python safe dictionary key access Question: I am looking for a convenient, safe python dictionary key access approach. Here are 3 ways came to my mind. data = {‘color’: ‘yellow’} # approach one color_1 = None if ‘color’ in data: color_1 = data[‘color’] # approach two color_2 = data[‘color’] if ‘color’ in data else None …

Total answers: 2

How do "and" and "or" act with non-boolean values?

How do "and" and "or" act with non-boolean values? Question: I’m trying to learn python and came across some code that is nice and short but doesn’t totally make sense the context was: def fn(*args): return len(args) and max(args)-min(args) I get what it’s doing, but why does python do this – ie return the value …

Total answers: 8

Logical vs. bitwise operator AND

Logical vs. bitwise operator AND Question: I don’t understand the difference between & and and, even if I read some other questions about it. My code is: f=1 x=1 f==1 & x==1 Out[60]: True f==1 and x==1 Out[61]: True f=1 x=2 f==1 and x==2 Out[64]: True f==1 & x==2 Out[65]: False Why is it the …

Total answers: 4