Python:Operators

From wiki
Revision as of 16:56, 25 June 2020 by Hdridder (talk | contribs)
Jump to navigation Jump to search

Most operators work as you would expect.

Less obvious assignment

<int> * <string>
Repeat <string> <int> times. Negative <int>s return an empty string.
a / b
Divide to floating point. In python2 the result datatype is determined by the arguments datatype i.e. both arguments int => result is int.
a // b
Divide to integer (truncated, not rounded)
a % b
Modulo
a ** b
Power
a << b
bit-shift left
a >> b
bit-shift right
undef(a)
Remove variable a (Note: variables are pointers to objects, the object may not be removed)

Self assign

a += b
Add b to a, works with other operators and on strings too.

Compare

==, >, <, <=, >=, !=
Just as you would expect

Boolean

and, &
Logical and
or, |
Logical or
^
Logical Xor (exclusive or)
not
Negate the result of the next expression