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

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.

Related

Python 'and' operator: what will 'and' return when comparing two numbers? [duplicate]

This question already has answers here:
How do "and" and "or" act with non-boolean values?
(8 answers)
Closed 7 months ago.
print(45 and 2)
What is the mechanism behind the result of 2?
Based on the docs:
https://docs.python.org/3/reference/expressions.html#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."
Which basically means that you will always get 2 long as you keep the 45 (45 != 0). If you try print(0 and 2) you will get 0.
The and operator checks the first operand. If it is truthy, the second operand is returned. Else, the first operand is returned. 45 is a non-zero integer, so it is truthy. Therefore, the second operand (2) is returned. The or operator works in a similar way.
https://realpython.com/python-and-operator/#using-pythons-and-operator-with-common-objects

Why does "x and y" return "y" when "x" and "y" are integers? [duplicate]

This question already has answers here:
Python's Logical Operator AND [duplicate]
(7 answers)
Closed last year.
So I came across the following syntax for a coding-game solution:
.
.
n and 9
and I didn't knew how to interpret it, thus I tried
2 and 3 #3
3 and 2 #2
.
Why does x and y (seems to) equal y i.e how is this calculated/understood?
This is called short-circuiting in boolean expressions if the first is true(non zero considered as true) it goes to 2nd if this was or if the first was false(0 is considered false) it goes to 2nd try it or as or integers hope this clears stuff up
Chain of logical operators returns the first (counting from left-most value) item that lets you know the logical value of the whole statement. So, in case of and, there are 2 options - one of the values is False, at which point you know that whole statement is False and you return that value without checking anything further on the right, or all of them are True and you return the last one, as only then you know that it will be True.

Python IF Statement with multiple OR [duplicate]

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.

What's the name of this operator in Python? [duplicate]

This question already has answers here:
Python list - True/False first two elements? [duplicate]
(2 answers)
Closed 2 years ago.
In a Clash of Code, I've seen this interesting operator thing:
print(["false","true"][i == n])
I haven't seen this before. What's the name of this and what does it do?
It's not exactly an operator but rather the second condition is being used as an index for the ["false", "true"] list.
In case i == n this will be true. Let me remind you that true in python equals to 1
int(True) // = 1
Therefore if i == n, it will be equal to one and will print the element with index one from the list which is "true".
In case i != n, it will be False, equals to 0 which will print the first element from the array which is "false".
This a comparison Operator,
it compares the value or equality of two objects, whereas the Python is operator checks whether two variables point to the same object in memory. In the vast majority of cases, this means you should use the equality operators == and != , except when you're comparing to None.
Output: True or False
Usage: Is used to check whether 2 expressions give the same value.

Advance Logical Operator in Python [duplicate]

This question already has answers here:
Does Python have a ternary conditional operator?
(31 answers)
How do "and" and "or" act with non-boolean values?
(8 answers)
Closed 3 years ago.
I am confused with Logical 'And' and 'or' combination in python
I got some idea from Python's Logical Operator And but I couldn't understand combination of 'AND' and 'OR'
a=3
print(a%2 and 'odd' or 'even')
I understood how even was printed if a is 2 ie.,
a= 2, 2%2 = 0 => False.
Then Note String object is considered as True so 'odd' and 'even' are True.
So False and True(odd) or True(even) will give be False(even) object.
But when I didn't understand how even works. If 1st object is True then the output should be immediate True without checking other condition(or operations), how is it going further and printing 'even'
If you are thinking of trying to emulate JavaScript or other languages ternary operators, you have to do it something like this:
print("even" if a%2 == 0 else "odd")
edit: even though this question has been closed (and I don't believe it should be) I will edit it with the understanding about what your question is actually asking.
In python bool(1) == True and bool(0) == False notice how when you modulo 2 these are the only two possible values you can get.
Now go back to your origional print statement:
print(a%2 and 'odd' or 'even')
Combine the fact that 1 is true and 0 is false with the fact that the first condition of a statement ... and [...] or ... returns if the statement is true and the second returns if it is false.
It is then clear to see that when the number is odd causing the number modulo 2 to be 1 it will return the first condition "odd" and when it is 0 it will cause the second condition to be returned "even".
I hope this explains everything.

Categories