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 an array of two int's, and I want to check if either is None, so I have this:
print hourArray
if hourArray[0] or hourArray[1] is None:
print "FAILED???"
else:
print "array is full"
And even though the print hourArray shows this right before the if statement
[2040, 2640]
It prints FAILED??? even though neither of the elements in the array is None?
Why is this happening?
The issue is that you are checking if (hourArray[0]) or (hourArray[1] is None) , all non-zero integer values are always true.
You should do -
if hourArray[0] is None or hourArray[1] is None:
Example of non-zero integer values being true -
>>> if 1:
... print('Hello')
...
Hello
Related
This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 12 months ago.
I'm a beginner in Python and I'm trying to write an IF statement with multiple OR conditions in the expression. My problem is that the expression is only "True" for the first OR condition.
def rna_length(mrna):
if (mrna[-3:]) == ("UGA" or "UAA" or "UAG"):
return print("TRUE!")
else:
return print("False")
rna_length('AUGAGGCACCUUCUGCUCCUUAA')
I was expecting True after running the code but the code is printing False.
Kindly help me understand my mistake.
Here is how to do:
if mrna[-3:] in ("UGA", "UAA", "UAG"):
("UGA" or "UAA" or "UAG") will return "UGA" because the operator or return the first non-False value. Since non-empty strings arn't False the first value is returned.
This question already has answers here:
How does all() in python work on empty lists
(5 answers)
Reason for "all" and "any" result on empty lists
(9 answers)
Closed 2 years ago.
Can someone tell why the following result is true.
all(2%p>0 for p in set())
I know that am empty set is false. I cannot understand why the remain after divided by a empty set is greater than zero.
all returns True if it's applied on an empty iterable. set() creates a new empty set, so calling all on any permutation on it, will return True.
This question already has answers here:
Why is "None" printed after my function's output?
(7 answers)
Closed 3 years ago.
I tried to make a program to determine whether a number is odd or not, I get the following output
def is_odd(n):
if n % 2 != 0:
print(True)
else:
print(False)
if I put
print(is_odd(1))
I get:
True
None
Whatever number I choose, I always get
None
at the end.
You have to return something to remove this None . You can also modify the code by returning the True or false and print it in the main
This question already has answers here:
Remove Tuple if it Contains any Empty String Elements
(2 answers)
What is the best way to check if a tuple has any empty/None values in Python?
(5 answers)
Remove a tuple containing nan in list of tuples -- Python
(4 answers)
Closed 4 years ago.
I'm simply trying to determine if my tuple has empty inputs. I'm reading Data from an Excel Sheet and it gives tuples in the form
(3,4,6), (3,4,7)
so basically it typically has 3 parameters but there are some cells that don't include the first or second parameter, so technically it would be
('','',6)
In my code I have set up as:
for i in range(len(self.listOfTuples)):
for j in range(len(self.listOfTuples[i])):
if(self.listOfTuples[i][j][0] == '' or self.listOfTuples[i][j][1] == ''):
print("GOT IT ")
and it's not printing my statement, when I print out the self.listofTuples[i][j][0]. It actually prints out nan, but when I change my if statement with "nan" it still doesn't print out my statement. Is there something i'm missing ?
nan is "not-a-number". You can detect it with math.isnan(self.listOfTuples[i][j][0]).
This question already has answers here:
Is there a difference between "==" and "is"?
(13 answers)
Closed 6 years ago.
Two items may be unequal in many ways. Can python tell what is the reason?
For example: 5 is not 6, int(5) is not float(5), "5" is not "5 ", ...
Edit: I did not ask what kinds of equality test there are, but why and how those are not equal. I think my question is not duplicate.
There are several checks you can do:
# Both variables point to the same object (same memory space)
a is b
# Both variables evaluates to the same value
a == b
# Both variables are of same type
type(a) == type(b)
is checks if the two items are the exact same object. This check identity
== checks if the two objects are equal values
You use is not None to make sure that the object the "real" none and not just false-y.