This question already has answers here:
What do these operators mean (** , ^ , %, //)? [closed]
(3 answers)
Closed 4 years ago.
Why is ^ not squaring in Python? I know exponentiation is ** instead, but what exactly is ^ and why wasn't that operator used instead?
For example 2^2=0, 3^2=1.
The ^ operator was already used for bitwise xor.
>>> x = 42; format(x, '08b')
'00101010'
>>> y = 137; format(y, '08b')
'10001001'
>>> z = x ^ y; format(z, '08b')
'10100011'
That leaves the old Fortran-style ** operator for exponentiation.
>>> base = 5
>>> exp = 2
>>> base ** exp
25
The "^" symbol in python is a bit-wise exclusive OR (XOR) operator. An OR gate is true if one of the inputs OR another is true. The XOR gate is true if and only if just a single input is true. 00 and 11 are false. 01 and 10 are true. The bit-wise XOR can be used to check how many bits differ.
For example,
2^2 = 10
^10
= 00
= 0
3^2 = 11
^10
= 01
= 1
Related
This question already has answers here:
What does & mean in python [duplicate]
(5 answers)
Closed 11 months ago.
print(2 & 3)
I came across a problem statement in one of my technical assessments, I can't understand the usecase of this '&' operator. Can anyone help me, with how this & operator work in python3
& is a bitwise operator, so this is simply where the binary bits line up between 2 and 3
https://wiki.python.org/moin/BitwiseOperators
>>> bin(2)
'0b10'
>>> bin(3)
'0b11'
>>> int("0b10", base=2) # binary string -> int (base10)
2
Here's an example with some bigger numbers
>>> bin(12)
'0b1100'
>>> bin(10)
'0b1010'
>>> 12&10
8
>>> bin(8)
'0b1000'
>>> bin(~8) # NOTE 8 is signed
'-0b1001'
>>> 8&-8
8
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 ¯\_(ツ)_/¯
I'm learning python and I stumbled upon something I don't understand.
For instance:
x = 50
while x:
print(x)
x >>= 1
Outputs:
50
25
12
6
3
1
So I infer that it divides by two and rounds to the left if it's not an integer or something similar.
But when I change it to x >>= 3 for instance the output is:
50
6
Can someone please explain what >>= does?
If so, what are useful applications of this kind of operator.
>>= is the augmented assignment statement for the >> right-shift operator. For immutable types such as int it is exactly the same thing as:
x = x >> 1
right-shifting the bits in x one step to the right.
You can see what it does if you print the binary representation of x first:
>>> x = 50
>>> format(x, '08b')
'00110010'
>>> x >>= 1
>>> format(x, '08b')
'00011001'
>>> x = 50
>>> x >>= 3
>>> format(x, '08b')
'00000110'
>>> x
6
Each shift to the right is equivalent to a floor division by 2; 3 shifts thus is as if x was divided by 2 to the power 3, then floored to an integer.
The complementary operator is the left-shift << operator, multiplying the left-hand integer by 2; it is a binary power-of-two operator:
>>> x = 6
>>> format(x, '08b')
'00000110'
>>> x <<= 3
>>> x
48
>>> format(x, '08b')
'00110000'
Augmented assignment operators can differ in behaviour when applied to mutable types such as a list object, where the operation can take place in-place. For example, listobj += [1, 2, 3] will alter listobj itself, not create a new list object, as if listobj.extend([1, 2, 3]) was called.
This is augmented assignment with the right shift operator.
x >>= 1 is shorthand for x = x >> 1.
x >>= k divides by 2**k (i.e. 2 raised to the k-th power).
Thus, x >>= 3 is the integer division by eight.
It's the binary right shift operator.
For example,
if you have 0100b, and you do: 0100b >> 2, then as a result you will have the number 0001b (you've shifted the one two positions to the right).
This question already has answers here:
What does the caret (^) operator do?
(5 answers)
Closed 9 years ago.
I'm struggling to find documentation on what the ^ does in python.
EX.
6^1 =
7
6^2 =
4
6^3 =
5
6^4 =
2
6^5 =
3
6^6 =
0
Help?
It is the bitwise exclusive-or operator, often called "xor". For each pair of corresponding bits in the operands, the corresponding bit in the result is 0 if the operand bits are the same, 1 if they are different.
Consider 6^4:
6 = 0b0110
4 = 0b0100
6^4 = 0b0010 = 2
As you can see the least-significant bit (the one on the right, in the "one's" place) is zero in both numbers. Thus the least-significant bit in the answer is zero. The next bit is 1 in the first operand and 0 in the second, so the result is 1.
XOR has some interesting properties:
a^b == b^a # xor is commutative
a^(b^c) == (a^b)^c # xor is associative
(a^b)^b == a # xor is reversible
0^a == a # 0 is the identity value
a^a == 0 # xor yourself and you go away.
You can change the oddness of a value with xor:
prev_even = odd ^ 1 (2 = 3 ^ 1)
next_odd = even ^ 1 (3 = 2 ^ 1)
for more information on XOR , please react the documentation on Python.org at here:
http://docs.python.org/2/library/operator.html
How does Python evaluate the expression 1+++2?
How many ever + I put in between, it is printing 3 as the answer. Please can anyone explain this behavior
And for 1--2 it is printing 3 and for 1---2 it is printing -1
Your expression is the same as:
1+(+(+2))
Any numeric expression can be preceded by - to make it negative, or + to do nothing (the option is present for symmetry). With negative signs:
1-(-(2)) = 1-(-2)
= 1+2
= 3
and
1-(-(-2)) = 1-(2)
= -1
I see you clarified your question to say that you come from a C background. In Python, there are no increment operators like ++ and -- in C, which was probably the source of your confusion. To increment or decrement a variable i or j in Python use this style:
i += 1
j -= 1
The extra +'s are not incrementors (like ++a or a++ in c++). They are just showing that the number is positive.
There is no such ++ operator. There is a unary + operator and a unary - operator though. The unary + operator has no effect on its argument. The unary - operator negates its operator or mulitplies it by -1.
+1
-> 1
++1
-> 1
This is the same as +(+(1))
1+++2
-> 3
Because it's the same as 1 + (+(+(2))
Likewise you can do --1 to mean - (-1) which is +1.
--1
-> 1
For completeness there is no * unary opeartor. So *1 is an error. But there is a **
operator which is power of, it takes 2 arguments.
2**3
-> 8
1+(+(+2)) = 3
1 - (-2) = 3
1 - (-(-2)) = -1
Trying Unary Plus and Unary minus:
The unary - (minus) operator yields the negation of its numeric argument.
The unary + (plus) operator yields its numeric argument unchanged.
>>> +2
2
>>> ++2
2
>>> +++2
2
>>> -2
-2
>>> --2
2
>>> ---2
-2
>>> 1+(++2)
3
I believe it's being parsed as, the first + as a binary operation (add), and the rest as unary operations (make positive).
1 + (+(+2))
Think it as 1 + (+1*(+1*2))). The first + is operator and following plus signs are sign of second operand (= 2).
Just like 1---2 is same as 1 - -(-(2)) or 1- (-1*(-1*(2))