How come 2 ^ 3 = 1 in python 3.9 [duplicate] - python

This question already has answers here:
What does the ^ (XOR) operator do? [duplicate]
(6 answers)
Closed 2 years ago.
Shouldn't it be 8?
The same thing goes with 3 ^ 2. I also got 1.
This is confusing...

In Python, ^ is a bitwise XOR operator. I believe what you're looking for is the exponent operator, **. An example would be 2**3 which outputs 8, like I believe you were looking for.

The ^ operator does a bitwise XOR operation. In python to do power calculation use pow() function:
pow(3,2)
Or use **
3**2

Related

When to use the bitwise and operator (&)? [duplicate]

This question already has answers here:
Real world use cases of bitwise operators [closed]
(41 answers)
Bitwise operation and usage
(17 answers)
Closed 12 months ago.
I understand that the bitwise and operator (&) is equivalent to a product of two bit values. When would I use it?
Please also help me understand what num&1 does in the code below:
def func(num):
n = 1 + func((3*num+1) if num&1 else (num>>1))
return n
As the comments mentioned, num&1 is a bitwise AND between num and 1.
Since 1 in binary is ...000000001, the AND will result True iff the least significant bit of num is 1, in other words, if it is odd (here some explanation of binary)

Negative one to the power of two question - i.e. (-1)**2 [duplicate]

This question already has answers here:
Calculation error with pow operator
(4 answers)
Closed 1 year ago.
Can someone explain the difference between these two expressions in Python:
(-1)**2 == 1
-1**2 == -1
Why do the parentheses change the outcome?
The parentheses means the whole value inside is will be raised to the power 2.
(-1)**2 == 1
So -1*-1 is 1
No parentheses means the - will be taken out of the equation and added to the end of the answer.
1) -1**2
2) 1**2
3) 1
4) -1
Python handles this the same way the world does :)

This is regarding Python Exponentiation [duplicate]

This question already has answers here:
Why is exponentiation applied right to left?
(4 answers)
Closed 2 years ago.
print (2**3**2)
Answer is 512.
Why 512 is answer not 64? Because ((2^3)^2) = 64
I want to know the inside math operation of print (2** 3**2)
The order of operations for exponentiation is right-to-left, not-left-to right. So:
2**3**2
is interpretted as:
2**(3**2) = 2**(9) = 512

Python exponential calculation [duplicate]

This question already has answers here:
Why is exponentiation applied right to left?
(4 answers)
Closed 2 years ago.
Am New to python trying out some basic python function. Came across exponential
In python
2 ** 2 ** 3 is 256
But while in mathematics getting as 64.
Use parentheses. This will give the correct answer.
(2 ** 2) ** 3
Use parentheses
x = (2**2)**3
or:
pow(2,2*3)

Difference in divison of -a//b and a//b in python 2.7 [duplicate]

This question already has answers here:
Floor division with negative number
(4 answers)
Closed 6 years ago.
I am currently working on python 2.7.
I observed that if we have : -10//6 the answer is -2 while 10//6 yields 1
Can i know why is there the difference?
// performs the floor operation.
10/6 is 1.666, the floor of which is 1
-10/6 is -1.666, the floor of which is -2

Categories

Resources