Boolean operator precedence [duplicate] - python

This question already has answers here:
Python operators precedence
(2 answers)
Closed 2 years ago.
I have question about Python operator precedence.
print(not (8 < 4) or (10 == 5 * 2) and not (5 > 3))
The above code prints 'True'. But I think the result is 'False', with the following steps:
>>print(not F or T and not T)
>>print(T or T and F)
>>print(T and F)
>>print(F)
So I don't know why the result is 'True'
Maybe I'm missing the small detail.

and has higher precedence than or.
>>print(not F or T and not T)
>>print(T or T and F)
>>print(T or (T and F)) # Evaluated like this.
>>print(T or F)
>>print(T)

Adding the following (unnecessary) brackets will explain how it's getting evaluated:
print((not (8 < 4)) or ((10 == 5 * 2) and not (5 > 3)))
Since (not (8 < 4)) is evaluated to True (the other side of the or doesn't matter) this is the returned result

Related

C# mod gives wrong answer not as python mod (%) [duplicate]

This question already has answers here:
modulo of a number - python vs c#
(3 answers)
Mod of negative number is melting my brain
(15 answers)
Closed 7 months ago.
I'm trying to get % result of ( -1%26 )
the result should be 25
in python it's right
but C# gives -1 as result
why and how to get 25 in C# not -1
You can use a different kind of modulus function, like this:
int Modulus(int a, int b) {
return ((a % b) + b) % b;
}
Console.WriteLine(Modulus(-1, 26)); // prints 25

How can an expression be different from however paarenthesized? [duplicate]

This question already has answers here:
Why does the expression 0 < 0 == 0 return False in Python?
(9 answers)
python operator precedence of in and comparison [duplicate]
(3 answers)
Closed 5 years ago.
Consider this statement:
> False == False in [False]
True
However:
> (False == False) in [False]
False
> False == (False in [False])
False
It is incredible. What's the reason and the interpretation?
dis.dis to the rescue and a bit of my interpretation.
Anyway, it looks like this. Consider three expression X, Y, Z and two operators O1, O2. Then
X O1 Y O2 Z
is equivalent to
(X O1 Y) and (Y O2 Z)
as can be seen by
a < b < c
example. I find this behaviour very counterintuitive.
And the docs:
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).
Note that in is a comparison operator.

5-bit signed binary to int [duplicate]

This question already has answers here:
Two's Complement in Python
(20 answers)
Closed 6 years ago.
My code has to read 5-bit signed binary values. Let's say it reads a 11111, which would be two's complement -1. But int("0b11111", 2) returns 31 instead.
How can I parse the correct value?
Here's a possible solution testing all 5-length binary numbers of your future emulator:
import itertools
def two_complement(value, length):
if (value & (1 << (length - 1))) != 0:
value = value - (1 << length)
return value
opcodes_emulator = ["".join(seq) for seq in itertools.product("01", repeat=5)]
for my_string in opcodes_emulator:
print my_string, two_complement(int(my_string, 2), len(my_string))

why does print 2++3 print 5 in python? [duplicate]

This question already has answers here:
Why does 1+++2 = 3?
(6 answers)
Closed 7 years ago.
I am new to python and I read that there is no ++ operator in Python but I am not able to understand the below code.
>>>print (2++3)
>>>5
+ and - act as unary as well as binary operators. So,
a ++ b is same as a + (+b)
a -+ b is same as a - (+b)
a -- b is same as a - (-b)
a +- b is same as a + (-b)
As can be seen below
>>> 2++3
5
>>> 2-+3
-1
>>> 2--3
5
>>> 2+-3
-1
This is not the ++ operator. Your code is interpreted as follows:
2 + (+3)
Now since +3 is 3, the final result is 5. For fun, try the following:
2++++++++++++++3
2+++++++++++++++++++++-8
See Expressions for details.

C++ syntax when declaring an integer [duplicate]

This question already has answers here:
Does Python have a ternary conditional operator?
(31 answers)
Closed 8 years ago.
I'm having a difficult time learning python syntax. I've been doing some algorithms for a merge sort and I've run into a bit of an issue.
def arrMerge(a):
for i in range(1,len(a), *2):
for j in range(0,len(a)-1,2*i):
end2 = (2*i < len(a) -j) ? 2*i : len(a) -j
this block in python any ideas on how I should go about executing it?
I assume that you are asking what is the Python equivalent syntax to C++ ternary operator. In Python you would use a conditional expression that has the syntax value if condition else other_value.
So your assignment would become:
end2 = 2 * i if 2 * i < len(a) - j else len(a) - j
It is usually better to use a plain if though:
if 2 * i < len(a) - j:
end2 = 2 * i
else:
end2 = len(a) - j

Categories

Resources