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
Related
The docs say that two string literals that are next to each other are concatenated. For example:
>>>print("py" "thon")
python
However, this feature is implemented at compile time instead of runtime like the + and * operators, so this interesting effect occurs:
>>>print(2 * "py" + "thon")
pypython
>>>print(2 * "py" "thon")
pythonpython
I understand why this happens in the language, but I can't think of a reason for it to be that way. Is there a reason, or was it just easier to leave it alone?
Quite frankly, If I were to design python today, I would make
print ("py" "thon")
A syntax error
Same as
print (5 3)
I would guess that the reason for concatenating adjacent strings, is for consistency with bash / perl
echo "py""thon"
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.
I get boolean expression like below :
string = '!True && !(True || False || True)'
I know eval('1+2') returns 3. But when I am executing eval(string) it is throwing me error as in invalid syntax.
Is there any other way I can execute the above expression?
None of !, && and || are valid Python operators; eval() can only handle valid Python expressions.
You'd have to replace those expressions with valid Python versions; presumably ! is not, && is and, and || is or, so you could just replace those with the Python versions:
eval(string.replace('&&', 'and').replace('||', 'or').replace('!', 'not '))
Note the space after not because Python requires this.
The better approach would be to not use the wrong spelling for those operators (they look like Java or JavaScript or C).
If you want to parse boolean logic (in contrast to control flow) take a look at this stackoverflow post. It mentions pyparsing
The pyparsing module is an alternative approach to creating and executing simple grammars
and sympy.
The logic module for SymPy allows to form and manipulate logic expressions using symbolic and boolean value.
There also seems to be a parser implemented with pyparsing.
Of course you could also write a parser yourself.
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
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.