Python IF Statement with multiple OR [duplicate] - python

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.

Related

Short-circuiting with helper function in print() [duplicate]

This question already has answers here:
How do "and" and "or" act with non-boolean values?
(8 answers)
Closed 5 months ago.
Can someone please explain why the following code comes out as "geeks" in the console?
def check():
return "geeks"
print(0 or check() or 1)
I'm assuming that Python recognizes the boolean operator, thus treating 0 == False?
So does that mean every time boolean operators are used, the arguments are treated as True/False values?
The reason that "geeks" will be printed is that or is defined as follows:
The Python or operator returns the first object that evaluates to true
or the last object in the expression, regardless of its truth value.
When check() returns the string "geeks", that value evaluates to True because it is a non-empty string. That value is then the first term in the or expression that evaluates to True, so that value is the result of the entire expression, and so is what gets printed.

Is there a way to find out which condition (out of many by using or) in "if" is evaluated to true because of which it entered inside the if? [duplicate]

This question already has answers here:
Python - How to find which condition is true in if statement?
(6 answers)
Closed 8 months ago.
I have an if statement, let's say:
if cond1 or cond2 or cond3:
do_something()
Can I find out which among these three conditions (cond1,cond2 and cond3) was true because of which it entered inside and executed do_something()?
P.S: I'm looking for answers which doesn't suggest me to use another 2 if's below my if to find out which was true.
You could do it in a for loop:
for i, j in enumerate((cond1, cond2, cond3)):
if j:
do_something()
print(i) #Do whatever you want to do with the 1st condition that was true
break

Weird output in Python program [duplicate]

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

Python - check if array is empty [duplicate]

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

Why is is-operator not functioning like ==? [duplicate]

This question already has answers here:
Why does comparing strings using either '==' or 'is' sometimes produce a different result?
(15 answers)
Closed 9 years ago.
If I understand correctly, the is operator can take the place of ==.
Why when I write
if inpty == "exit":
return
does the function exit, but when I write
if inpty is "exit":
return
the function does not?
inpty is the value of the input.
is compares identity, whereas == compares equality.
In other words, a is b is the same as id(a) == id(b).
because in this case, the is operator is testing identity, not the value.

Categories