Python and Powers Math - python

I've been learning Python but I'm a little confused. Online instructors tell me to use the operator ** as opposed to ^ when I'm trying to raise to a certain number. Example:
print 8^3
Gives an output of 11. But what I'm look for (I'm told) is more akin to: print 8**3 which gives the correct answer of 512. But why?
Can someone explain this to me? Why is it that 8^3 does not equal 512 as it is the correct answer? In what instance would 11 (the result of 8^3)?
I did try to search SO but I'm only seeing information concerning getting a modulus when dividing.

Operator ^ is a bitwise operator, which does bitwise exclusive or.
The power operator is **, like 8**3 which equals to 512.

The symbols represent different operators.
The ^ represents the bitwise exclusive or (XOR).
Each bit of the output is the same as the corresponding bit in x if
that bit in y is 0, and it's the complement of the bit in x if that bit in y is 1.
** represents the power operator. That's just the way that the language is structured.

It's just that ^ does not mean "exponent" in Python. It means "bitwise XOR". See the documentation.

Related

What's the difference between not and ~ in python

Just to check, I couldn't find anything about ~, so I want to make sure it's the same.
Also, can you use ! in python like in C++?
It seems that these two codes give the same output:
bool(~0)
bool(not 0)
This is how it is in Python.
'~' is a bitwise operator
whereas 'not' is a logical opeartor
You should read Python 3.10 documentation at Unary arithmetic and bitwise operations.
The ~ operator does a simple job:
Inverts the bits

Go "&^" operator, what does it mean?

I am struggling to understand what the &^ and &^= operators mean in Go. I cannot find an answer either in the documentation (which states it's a bit clear operator, but it does not help me much) or by trials.
In particular, I want to know if there is an equivalent in Python.
These are the "AND NOT" or "bit clear" operators, "useful" for clearing those bits of the left-hand side operand that are set in right-side operand.
I put the "useful" in quotes since all other languages that derive the bitwise operations from C one does this with bitwise AND & and bitwise NOT ~; thus 5 &^ 2 would be just 5 & ~2 in Python; and a &^= 3 of Go would be a &= ~3 in Python.

How to use not logical operation in python

inputing an logical expression as string and evaluating, i'm getting proper output
str1 = "(1|0)&(1|1&(0|1))"
print eval(str1)
o/p: 1
But the same way if i'm including not operator as ~, the output goes wrong.
str1 = "(~0|~1)&(~1|0)"
print eval(str1)
o/p: -2
Is there any other way of representing not operator here to get proper answer.
These are not logical expressions but bitwise expressions. That is the reason why ~0 == -1. Instead you can look for a parser that parses these expressions the way you want. A quick google search showed up this stackoverflow question.
Sympy seems to implement a similar thing: sympy logic
The logic module for SymPy allows to form and manipulate logic expressions using symbolic and boolean values
&, | and ~ are bitwise operators.
For logic operators use and, or and not.
If your intention is to do logical operations, prefer to use the appropriate boolean values:
True / False
str1 = "(not 0|not 1) and (not 1|0)"
print eval(str1)
In python NOT is not
Ref : https://docs.python.org/2/library/stdtypes.html

In Python, why does a negative number raised to an even power remain negative? [duplicate]

This question already has answers here:
Calculation error with pow operator
(4 answers)
Closed 8 years ago.
In Python
>>> i = 3
>>> -i**4
-81
Why is -i**4 not evaluated as (-i)**4, but as -(i**4)?
I suppose one could argue that raising to a power takes precedence over (implicit) multiplication of i with minus one (i.e. you should read -1*i**4).
But where I learned math, -i**n with n even and i positive, should come out positive.
The ** operator binds more tightly than the - operator does in Python. If you want to override that, you'd use parentheses, e.g. (-i)**4.
https://docs.python.org/2/reference/expressions.html#operator-precedence
https://docs.python.org/3/reference/expressions.html#operator-precedence
You can use the pow() function from math.
import math
i = 3
math.pow(-i,4)
This will yield a positive value.
As stated here: Exponentials in python x.**y vs math.pow(x, y), this option (or just build in pow()) would be ideal if you want to always produce a float.
The power operator (**) has a higher precedence than the unary negation (-) operator. -i**4 is evaluated as -(i**4) - i.e., you take 3 up to the power of four, which is 81, and then negate it, resulting in -81.
You have to do (-i)**4 to get a positive result.
The *'s have higher priority than the '-'.
When in doubt, use parentheses. (or, as Amber suggested, refer to the language documentation)

Could anyone help me with this python singpath program?

First of all, this is not for any class.
I have been working on these 2 programs for a long time and cannot make heads or tails of it. I really want to get past these problems so I can move onto other lessons.
"Create a function that transforms the prefix notation into postfix notation, and postfix notation into prefix notation. The function takes two arguments. The first is a string of an expression without spaces or syntax errors, and the second is another string contains all the operators. Characters not in the second string are regarded as operands. The lengths of all the operators and operands are 1, and all the operators are binary operators."
ex:
>>> fix_trans('ab33c2c11','abc')
'33b211cca'
and Convert to (reverse) Polish notation:
>>> toPolish('(3+5)*(7-2)',D,0)
'*+35-72'
Can you provide any examples of how far you've gotten with this, or what methods haven't worked for you? Also, are you familiar with the shunting-yard algorithm?

Categories