Advance Logical Operator in Python [duplicate] - python

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.

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

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.

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.

How to condense if/else into one line in Python? [duplicate]

This question already has answers here:
Does Python have a ternary conditional operator?
(31 answers)
Closed last month.
How might I compress an if/else statement to one line in Python?
An example of Python's way of doing "ternary" expressions:
i = 5 if a > 7 else 0
translates into
if a > 7:
i = 5
else:
i = 0
This actually comes in handy when using list comprehensions, or sometimes in return statements, otherwise I'm not sure it helps that much in creating readable code.
The readability issue was discussed at length in this recent SO question better way than using if-else statement in python.
It also contains various other clever (and somewhat obfuscated) ways to accomplish the same task. It's worth a read just based on those posts.
Python's if can be used as a ternary operator:
>>> 'true' if True else 'false'
'true'
>>> 'true' if False else 'false'
'false'
Only for using as a value:
x = 3 if a==2 else 0
or
return 3 if a==2 else 0
There is the conditional expression:
a if cond else b
but this is an expression, not a statement.
In if statements, the if (or elif or else) can be written on the same line as the body of the block if the block is just one like:
if something: somefunc()
else: otherfunc()
but this is discouraged as a matter of formatting-style.

Categories