How can you add and multiple to True or False? [duplicate] - python

This question already has answers here:
Is False == 0 and True == 1 an implementation detail or is it guaranteed by the language?
(3 answers)
Closed 2 years ago.
I started learning python from a site, and it gave me a question that was kind of weird, I had to add and multiply a number to True and False. I've never done that before, so could someone explain what that means? (Can you replace True with 1 and False with 0?)
This is the question:
What is the value of result at the end of the following code?
x = 20
y = 5
result = (x + True) / (4 - y * False)
A)
False
B)
-21
C)
5
D)
5.25

The answer is 5.25 because in Python, True is equal to 1 while False is equal to 0.

Related

What is the Order of evaluation without ()? [duplicate]

This question already has answers here:
BODMAS - python
(4 answers)
How do order of operations go on Python?
(5 answers)
Closed last year.
I don't get why this function returns 2.0.
1+1/2*2
For order of operations, I thought it would be multiplication first, then division, left to right, so 1+1/4 to 1+.25 or maybe even return 2/4 =0.5...
I am really confused how it outputs 2.0.
Thanks in advance! Currently studying for an exam.
As for the multiplication and division, the order is left to right. So here is the division first.
1 + 1 / 2 * 2 = 1 + 0.5 * 2 = 1 + 1 = 2
For the next problem, I think if a == 'A' or 'B' is 2 expressions, a == 'A' and 'B'. I've learned a little about C++, and the 'B' refers 66 in the ASCII, which is not 0, so the expression 'B' is always true. Maybe in python it will be the same.
So the code should be if a == 'A' or a == 'B':
See the stackoverflow post: How do order of operations go on Python?
According to that your math, 1+1/2*2, is as follows:
/ 1 \
ans = 1 + ( --- ) X 2
\ 2 /
= 1 + (0.5 X 2)
= 1 + 1
= 2
Multiplication and division are in the same group and are performed left to right (aggregating the answers from the link I referenced).
Enjoy the ASCII art ¯\_(ツ)_/¯

Evaluation of python expression [duplicate]

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'

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