This question already has answers here:
How to test multiple variables for equality against a single value?
(31 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 5 years ago.
beginner question here:
I want to check if certain keys in a dict have a certain value
simplified version of what I tried:
d = {'a':'b', 'b':'b', 'c':'c'}
def b_check():
if d['a'] and d['b'] and d['c'] == 'b':
return True
else:
return False
However, I always get True as the output.
Thanks in advance for the help :)
Related
This question already has answers here:
How to test multiple variables for equality against a single value?
(31 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 9 months ago.
I am trying to call a python function inside of an if when you input a certain letter. Even if the letter is not meant to be noticed by the if statement it calls both functions.
if printWrite == 'p' or 'P':
printNum()
if printWrite == 'w' or 'W':
writeNum()
Always calls both functions.
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 are strings compared?
(7 answers)
Closed 2 years ago.
Why the result of “bag” > “apple” is True in Python?
I tried this code below i don't know why it show this result and how? Please some one explain it.
print("bag" > "apple")
True
I believe this is True because Python compares the first letter of each word. And b is greater than a in Python.
This question already has answers here:
Check if a given key already exists in a dictionary
(16 answers)
Closed 3 years ago.
Please advise what is the correct way to verify if key x is stored in a dictionary dict. and if it is, set a value check to True and otherwise it will be set to False.
check = True
try :
y = d.x
except KeyError as e
check = False
Or is it the right way to do one the following?
check = dict.contains(x)
check = dict.has_key(x)
check = x in dict
check = x.is_elem(dict)
The most common approach seems to be x in d
This question already has answers here:
"is" operator behaves unexpectedly with integers
(11 answers)
Is there a difference between "==" and "is"?
(13 answers)
Closed 3 years ago.
I'm using the pow function and found out I had a bug in my code because == and is were not having the same behavior.
Here goes an example: pow(3, 47159012670, 47159012671) == 1 returns True but pow(3, 47159012670, 47159012671) is 1 returns False.
I'd like to know what is it that I'm not getting.
The difference between is and == is:
a1 is a2 # return True
only when they share the same location or id in memory, that means when id(a1) and id(a2) are the same. I hope this will help more.