What does the "^" do in python [duplicate] - python

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

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 ¯\_(ツ)_/¯

Why is this method saying the square of 1 is 3? [duplicate]

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

Python: What does 'and not' mean in this code [duplicate]

This question already has answers here:
What is Truthy and Falsy? How is it different from True and False?
(8 answers)
Closed 3 years ago.
This is the entire code. What does 'and not' mean in the code. I understand it to mean that only a number that will equal to 0 when the number modulus 2 is carried out.
That is if 10 is entered by the user, 2,4,6,8 will be sum to get 20.
the_max = int(input("Enter the upper limit:"))
the_sum = 0
extra = 0
for number in range(1,the_max):
if number%2 and not number%3:
the_sum = the_sum + number
else:
extra = extra + 1 # Line 1
print(the_sum) # Line 2
print(extra) # Line 3
It means that the number is not a multiple of 2 but a multiple of 3;
the if statement has two conditions:
number % 2
since the % returns the remainder of a a number divided by 2, it will return 0 on a multiple of 2 and will result in rejection of if condition.
and not number % 3
This means that we need both condition to be good. But with this one the not operand reverses it.
So this time any number % 3 in which number is a multiple of 3 will result in 0 and will be reversed to 1;
You're parsing it wrong. The correct interpretation is
if (number % 2 != 0) and (number % 3 == 0):
This code is taking shortcuts by omitting the explicit comparison to zero, since 0 evaluates to False in a boolean expression like this one, whereas any other integer value evaluates to True.
The second clause, thus, is given a not to flip its polarity. not (number % 3) is equivalent to (number % 3 == 0).

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.

Why does 1+++2 = 3?

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))

Categories