Python
Operators
Arithmetic Operators
● An arithmetic expression consists of operands and
operators combined in a manner that is already familiar
to you from learning algebra.
Precedence rules
● Exponentiation
● Unary negation
● Multiplication, both types of division, and remainder are
evaluated
● Addition and subtraction
Exceptions:
Operations of equal precedence are left associative,
left to right
Exponentiation and assignment operations are right
associative right to left.
Use parentheses to change the order of evaluation
Some Examples:
Semantic
Error
Comparison Operator
Compares two values
Logical Operator
used to combine conditional statements
Assignment Operator
Assignment Operator combines the effect of arithmetic
and assignment operator
Operator Description Example
= Assigned values from right side x=12
operands to left variable
+= added and assign back the result to x+=2
x=x+2
left operand
-= subtracted and assign back the x-=2
x=x-2
result to left operand
*= multiplied and assign back the x*=2
x=x*2
result to left operand
/= divided and assign back the x/=2
x=x/2
result to left operand
%= taken modulus and assign the x%=2
x=x%2
result to left operand
//= floor division and assign value to x//=2
x=x//2
the left operand
Identity Operator
Identity operators are used to compare the
objects, not if they are equal, but if they are
actually the same object, with the same memory
location:
Membership Operators
Membership operators are used to test if a
sequence is presented in an object
Bitwise Operators
Bitwise operators are used to compare (binary)
numbers
Example:
a = 0011 1100
b = 0000 1101
-----------------
a&b = 0000 1100
a|b = 0011 1101
a^b = 0011 0001
~a = 1100 0011
Precedence
parentheses,
exp,
unary,
arithmetic * / // % + -,
bitwise << >> & ^ | ,
comparison,
logical ~ & ^