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
Related
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'
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:
This question already has answers here:
How to test the membership of multiple values in a list
(12 answers)
Closed 3 years ago.
I have an if statement which always returns as true even if it is not.
I have a global list :
NUMBERS_LIST = []
Numbers get added to it via an API call, that works fine.
When it does the following:
def func():
if 8 or 9 in NUMBER_LIST:
return true
elif 1 or 2 in NUMBER_LIST:
return true
But for some reason, it always returns true on the first if statement, even if NUMBER_LIST = [1]
I debugged my program and can see that NUMBER_LIST does contain 1, it's type is int.
I tried doing int(8), converting both types to str but that did not fix my issue.
When I debugged and step through the program, it doesn't really tell me much, I am using Pycharm.
or does not distribute. What you have written is not equivalent to
if 8 in NUMBER_LIST or 9 in NUMBER_LIST:
which is what you want, but to
if 8 or (9 in NUMBER_LIST):
Since 8 is a truthy value, the containment operation is never evaluated.
This question already has answers here:
What is Truthy and Falsy? How is it different from True and False?
(8 answers)
Closed 3 years ago.
In C/C++, true is standardized as 1, and false as 0. Although not a good practice,
if variable:
#do something
kind of decision making seems ok in python and it seems makes decision based on whether variable is None or is zero - if it is a number. Followings are reasonable.
a = 123123
if a:
print "if condition is true" # this prints
a = "a string"
if a:
print "if condition is true" # this prints
a = None
if a:
print "if condition is true" # this does not print
However, I wonder why it evaluates to false with an empty list, which is neither None, nor zero. What is the exact implementation of python if statement?
a = [] # a is not None
if a:
print "true" # this does not print
An if statement requires a bool. That means the the expression following the if is analyzed is a boolean context. Said differently it is converted to bool.
If a bool context, the following items evaluate to False (non exhaustive list):
None
numeric 0
empty list
empty string
empty dict
Non null numerics and non empty iterables eveluate to True
Lists are empty, so they're False, that's how python reacts, to know whether something is False so doesn't go trough, do:
>>> bool([])
False
>>>
So it is False.
This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 6 years ago.
Why does the following evaluates to False in Python?
6==(5 or 6)
False
'b'==('a' or 'b')
False
The first expression evaluates (5 or 6) first, which evaluates to 5 because 5 is truthy. 5 is NOT equal to 6 so it returns False.
The second expression evaluates ('a' or 'b') first, which evaluates to 'a' for the same reason as above. 'a' is NOT equal to 'b' so it returns False.
A good example to explain this would be to try to put a falsey value as the first part of the or expression like 6 == ([ ] or 6) or 6 == (None or 6). Both of these will return true because the or statement will evaluate to the truthy value (in each case it is 6).
There are a couple of ways to create the statement I think you want. The first would be to use in like 6 in (6,5). The second way would be to expand your boolean expression to read like this (6 == 5) or (6 == 6). Both of these will return True.
The condition within parentheses is evaluated first. So
6==(5 or 6)
evaluates to
6==(5) # since 5 != 0 (True), the second operand is not evaluated
which is False. Same goes for the second one. ('a' != None)
Try:
>>>6 == (6 or 5)
True
What's going on? Try:
>>5 or 6
5
I'm not totally sure why "5 or 6" evaluates to 5, but I would just try writing that expression differently.
But there is a caveat:
>> 5 == (0 or 5)
True
Because 0 is not truth-worthy, hence the bracket result in 5.