Data

Operations

Python

 

Arithmetic Operators

Python 2.7
# assume a=5, b=2 for each line.

val= a + b    # val -> 7
val= a - b    # val -> 3
val= a * b    # val -> 10
val= a / b    # val -> 2 (python2)
val= a // b   # val -> 2
val= a % b    # val -> 1
val= a ** b   # val -> 25

+     sums left and right
-     subtracts right from left
*     multiplies left and right
/     divides left and right
//    divides left and right and round it down
%     reminder of the left divided by right
**    left raised to the power of right

Division / operator has different behavior in Python2 and Python3 when it comes to handling decimal points:

# Python2
print (5/2)     # 2
print (-5/2)    # -3
print (5.0/2)   # 2.5
print (-5.0/2)  # -2.5
# Python3
print (5/2)     # 2.5 
print (-5/2)    # -2.5 
print (5.0/2)   # 2.5 
print (-5.0/2)  # -2.5 
 

Comparison Operators

Python 2.7
# assume a=5, b=2 for each line.

val= (a == b)     # val -> False
val= (a != b)     # val -> True
val= (a > b)      # val -> True
val= (a >= b)     # val -> True
val= (a < b)      # val -> False
val= (a <= b)     # val -> False

==    is left and right equal
!=    is left and right different
>     is left bigger than right
>=    is left bigger or equal than right
<     is left smaller than right
<=    is left smaller or equal than right

 

Logical Operators

Python 2.7
# assume yes=True, no=False for each line.

val= (no and no)       # val -> False
val= (no and yes)      # val -> False
val= (yes and no)      # val -> False
val= (yes and yes)     # val -> True

val= (no or no)        # val -> False
val= (no or yes)       # val -> True
val= (yes or no)       # val -> True
val= (yes or yes)      # val -> True

val= (not yes)         # val -> False
val= (not no)          # val -> True

and   logical and of left and right
or    logical or of left and right
not   logical not of its right operand

 

Bitwise Operators

Python 2.7
a=5       # 0000 0101
b=12      # 0000 1100

val= (a & b)  # val -> 4  (0000 0100)
val= (a | b)  # val -> 13 (0000 1101)
val= (a ^ b)  # val -> 9  (0000 1001)
val= (~b)     # val ->-13 (1111 0011)
val= (b<<2)   # val -> 48 (0011 0000)
val= (b>>2)   # val -> 3  (0000 0011)

&    bitwise and of left and right
|    bitwise or of left and right
^    bitwise xor or left and right
~    bitwise one complement of right operand
<<   bitwise left shift of left in amount of right
>>   bitwise signed right shift of left in amount of right

 

Assignment Operators

Python 2.7
# assume val=5, a=2 for each line.

val = a      # val -> 2
val += a     # val -> 7
val -= a     # val -> 3
val *= a     # val -> 10
val /= a     # val -> 2 (python2)
val //= a    # val -> 2
val %= a     # val -> 1
val **= a    # val -> 25
val <<= a    # val -> 20
val >>= a    # val -> 1
val &= a     # val -> 0
val |= a     # val -> 7
val ^= a     # val -> 7

=    assigns left to right
+=   assigns left to sum of left and right
-=   assigns left to subtract of right from left
*=   assigns left to multiplication of left and right
/=   assigns left to division of left to right
//=  assigns left to round-down division of left to right
%=   assigns left to reminder of left divided to right
**=  assigns left to raised of left to the power of right
<<=  assigns left to rightShift of left to value right
>>=  assigns left to leftShift of left to value right
&=   assigns left to bitwise and left and right
|=   assigns left to bitwise or left and right
^=   assigns left to bitwise xor left and right

Just like / operator, the operator /= has different behavior in Python2 and Python3 when it comes to handling decimal points.