Evaluation of python expression [duplicate] - python

This question already has answers here:
Python boolean expression and or
(4 answers)
Closed 2 years ago.
I came across the below expression in python to evaluate if a number is odd or even. While it looks elegant I was surprised to see it work. I guess for the input 3:
x%2 equals 1 which evaluates to True and non-empty strings (odd) evaulate to True. So we have True and True or <don't have to check because it doesn't matter if True or False as the overall statement will be True>. But why is the output in that case not True but odd - what concept do I miss?
>>> (lambda x:
... (x % 2 and 'odd' or 'even'))(3)
'odd'

The and and or operators are not limited to True and False; they return one of their operands.
>>> 1 and "odd"
'odd'
>>> 0 or "even"
'even'

Related

What does the ^ character do in logical operation [duplicate]

This question already has answers here:
How do you get the logical xor of two variables in Python?
(28 answers)
What does the caret (^) operator do?
(5 answers)
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
I'm practicing code on codingbat and came across this logic question:
Given a number n, return True if n is in the range 1..10, inclusive. Unless outside_mode is True, in which case return True if the number is less or equal to 1, or greater or equal to 10.
The solution is:
if n == 1 or n == 10:
return True
return (n in range(1,11)) ^ outside_mode
If one googles "python ^ symbol" or "what does the ^ symbol do python" Google returns answers regarding the "#" symbol and the modulo symbol. It is not easy to find what this symbol does. This question should therefor be reopened to provide better chances of an early-stage programmer finding these answers
It's bit-wise XOR. In Python, True == 1, and False == 0, so you can use bitwise operators to get the equivalent boolean operation.
For a single bit, ^ is the same as !=, so this is essentially the same as
return (n in range(1, 11) != outside_mode
^ is the bitwise exclusive or (or XOR)
>>> True ^ True
False
>>> True ^ False
True
>>> False ^ True
True
>>> False ^ False
False

Precedence of `in` and `==` in Python [duplicate]

This question already has answers here:
python operator precedence of in and comparison [duplicate]
(3 answers)
Closed 2 years ago.
Suppose I have the following line of code:
print("valley" in "hillside" == False)
Since the precedence of in and == is equivalent in Python, I expected the operations to be performed from left to right, producing True as the output.
However, in actuality, when I run this line of code, I get False.
I have noticed that adding brackets around "valley" in "hillside" results in True as the output but I don't seem to understand why it's necessary in the first place...
Both in and == are comparison operators, so the parser treats
print("valley" in "hillside" == False)
the same as
print("valley" in "hillside" and "hillside" == False)
See the section on Comparisons in the Python language reference for more details, in particular this note:
Comparisons can be chained arbitrarily, e.g., x < y <= z is equivalent to x < y and y <= z, except that y is evaluated only once (but in both cases z is not evaluated at all when x < y is found to be false).

How to find specific digit in integer in Python? [duplicate]

This question already has answers here:
Check if a digit is present in a list of numbers
(3 answers)
Closed 4 years ago.
I need to write function which can check if there is a specific digit in some entered number.
def number(m):
while (m>0):
n=m%10
m = m/100
if n==2:
return True
return False
some_number = 223
number(some_number)
For example I'm searching for number 2. But with this code it returns True only if number 2 is on last place.
Thanks.
You should divide by 10 instead of 100 in your code.
Also as Tilman B. aka Nerdyyy mention, you can just convert the integer to str, and search using in opearator:
def number(m):
return '2' in str(m)
You are close. Why do steps of 100? Do instead steps of 10 using floor division , otherwise you'll miss some algorisms and your loop will be waaay deeper than it should (for example, for the number 123, your loop as of now would check 12.3, 1.23, 0.123, 0.0123, 0.00123...... until it is so small that its computationally 0 - You don't want that, because you'd just be adding more and more zeros to your m and a 2 would never show up anyway).
def number(m):
while (m>0):
n = m%10
m = m//10
if n==2:
return True
return False
Checking
>> print(number(1))
False
>> print(number(2))
True
>> print(number(13))
False
>> print(number(12))
True
>> print(number(21))
True
>> print(number(11))
False
>> print(number(121))
True

What does x in range(...) == y mean in Python 3? [duplicate]

This question already has answers here:
What does it mean that Python comparison operators chain/group left to right?
(2 answers)
Why does the expression 0 < 0 == 0 return False in Python?
(9 answers)
Where in the python docs does it allow the `in` operator to be chained?
(1 answer)
Closed 5 years ago.
I just stumbled upon the following line in Python 3.
1 in range(2) == True
I was expecting this to be True since 1 in range(2) is True and True == True is True.
But this outputs False. So it does not mean the same as (1 in range(2)) == True. Furthermore it does not mean the same as 1 in (range(2) == True) which raises an error.
Despite years of experience in Python, I am taken off guard. What is going on?
This is due to the fact that both operators are comparison operators, so it is being interpreted as operator chaining:
https://docs.python.org/3.6/reference/expressions.html#comparisons
Comparisons can be chained arbitrarily, e.g., x < y <= z is equivalent
to x < y and y <= z, except that y is evaluated only once (but in both
cases z is not evaluated at all when x < y is found to be false).
So it is equivalent to:
>>> (1 in range(2)) and (range(2) == True)
False

1 == 0 in (0,1) is False; why? [duplicate]

This question already has an answer here:
Python's in (__contains__) operator returns a bool whose value is neither True nor False
(1 answer)
Closed 7 years ago.
How does Python parse the first of the following three expressions? (I expected it to be the same as the second, since == and in have the same precedence.)
>>> 1 == 0 in (0,1), (1==0) in (0,1), 1 == (0 in (0,1))
(False, True, True)
See the documentation of comparison operators: they are chained rather than grouped. So 1 == 0 in (0,1) is equivalent to (1==0) and (0 in (0,1)), which is obviously false.

Categories