Why is my if statement true when it should be false [duplicate] - python

This question already has answers here:
How to find list intersection?
(16 answers)
Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?
(8 answers)
Closed 2 years ago.
My if statement is returning the wrong values:
for i in range (3325 , 3325+1):
FSBRID = []
for j in range(93, 94):
a = ws1.cell(row=i, column=j)
print(a.value)
if 'SOIL' or 'soil' in a.value:
print('wrong')
The returned values:
TISSUE
wrong
Process finished with exit code 0

The construct
if 'SOIL' or 'soil' in a.value:
is parsed as though it were
if ('SOIL') or ('soil' in a.value):
and non-empty strings are true, making it equivalent to
if True or ('soil' in a.value):
One solution is to split it into two “in” operators, the results of which is combined.
if 'SOIL' in a.value or 'soil' in a.value:

Related

why is this conditional statement using comparison operators incorrect? [duplicate]

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'

Understanding the "and" operator - Why is "None and None" not False? [duplicate]

This question already has answers here:
Logical operators in Python [duplicate]
(4 answers)
Closed 2 years ago.
I though I understood Python the "and" operator. But after seeing
assert (None and None) is None
it was apparently that my understanding was not precise. Which was that None and None would be the same as bool(None) and bool(None).
Does anybody have definition of the "and" operator and can explain the logic.
According to the official documentation:
help('and')
[...] 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. [...]
and returns the first value if it is "falsey"*. Otherwise it returns the second one.
For example:
3 and 6 -> 6
0 and 7 -> 0
[] and 'abc' -> []
* a thing is falsey if bool(thing) is False

Why if integer is always True [duplicate]

This question already has answers here:
What is Truthy and Falsy? How is it different from True and False?
(8 answers)
Closed 4 years ago.
I am a bit confused with if/else statement. Why the code always prints True while it should be False.
I have tried with different variables like i =10, i = 'a', i = 25. And it will be False if i=[]
This is my code:
i =1
if i:
print True
else:
print False
In your code you say if I: True. But your not comparing it to anything. You need a comparison operator. Like if i == 1 otherwise the if statement will just be true IF I has a value by default

Python is operator [duplicate]

This question already has answers here:
Understanding the "is" operator [duplicate]
(11 answers)
Two variables in Python have same id, but not lists or tuples
(5 answers)
Closed 5 years ago.
I understand that the "is" operator checks identity between two objects, but what is this?
a=25
b=25
a is b
True
Why is this true if
a = [1,2,3]
b = [1,2,3]
a is b
False
And from this post, https://www.quora.com/Python-is-operator, it says all immutable objects, like integers from the first example, will have the first example's result, True, but tuples are immutable and this happens:
a = (1,2)
b = (1,2)
a is b
False
Can someone explain please?
Thanks.

When checking if an item does not exist in a list, why doesn't this code work - if item in list == False: [duplicate]

This question already has answers here:
python operator precedence of in and comparison [duplicate]
(3 answers)
Closed 9 years ago.
Consider this list:
list = [1,2,3,4,5]
I want to check if the number 9 is not present in this list. There are 2 ways to do this.
Method 1: This method works!
if not 9 in list: print "9 is not present in list"
Method 2: This method does not work.
if 9 in list == False: print "9 is not present in list"
Can someone please explain why method 2 does not work?
This is due to comparison operator chaining. From the documentation:
Comparisons can be chained arbitrarily, e.g., x < y <= z is equivalent to x < y and y <= z, except that y is evaluated only once (but in both cases z is not evaluated at all when x < y is found to be false).
You are assuming that the 9 in list == False expression is executed as (9 in list) == False but that is not the case.
Instead, python evaluates that as (9 in list) and (list == False) instead, and the latter part is never True.
You really want to use the not in operator, and avoid naming your variables list:
if 9 not in lst:
It should be:
if (9 in list) == False: print "9 is not present in list"

Categories

Resources