Basic Operators

Operator library has methods for all the operators that take 1 or 2 arguments (depending upon the operator) and returns the response.

Arithmetic Operators

+ (add), - (subtract), * (multiplication), / (float division), // (int division), % (modulus), ** (power)

Comparison Operators

> (greater than), < (less than), == (is equal to), >= (greater than or equal to), <= (less than or equal to)

Logical Operators

and, or, not

Bitwise Operators

& (bitwise and), | (bitwise or), ~ (bitwise not), ^ (bitwise xor), >> (bitwise right shift), << (bitwise left shift)

Assignment Operators

=, +=, -=, *=, /=, %=, //=, **=, &=, |=, ^=, >>=, <<=

Identity Operators

is, is not

Membership Operators

in, not in
  • Operator library contains methods for operators including:
    • Arithmetic: operator.add(a, b), operator.sub(a, b), operator.mul(a, b), operator.truediv(a, b), operator.floordiv(a, b), operator.mod(a, b), operator.pow(a, b)
    • Comparison: operator.lt(a, b), operator.gt(a, b), operator.le(a, b), operator.ge(a, b), operator.eq(a, b), operator.ne(a, b)
    • Bitwise: operator.and_(a,b), operator.or_(a,b), operator.xor(a,b, operator.invert(a)

Precedence and Associativity of Operators

Following table lists all operators in the order of decreasing precedence.
Operator Associativity
() left-to-right
** right-to-left
*, /, % left-to-right
+, - left-to-right
<<, >> left-to-right
< <=, >, >= left-to-right
==, != left-to-right
is, is not, in, not in left-to-right
& left-to-right
^ left-to-right
| left-to-right
not right-to-left
and left-to-right
or left-to-right
=, +=, -=, *=, /=, %=, &=, ^=, |=, <<=, >>= right-to-left

Ternary Operator

  • Python doesn't contain any special ternary operators. We can use inline if-else to achieve this.
Syntax: [on_true] if [expression] else [on_false]

    a, b = 10, 20
    min = a if a < b else b
        

Operator Overloading


    ''' using + '''
    # when 2 numbers, add
    print(1 + 2)                       # 3
    # when two strings, concat
    print("abc"+"def")                 # abcdef

    '''using * '''
    # when 2 numbers, multiply
    print(3 * 4)                       # 12
    # when a string, repeat the string
    print("ab"*3)                      # ababab
        

Special functions for operator overloading

Operator Method
Binary Operators
+ __add__(self, other)
- __sub__(self, other)
* __mul__(self, other)
/ __truediv__(self, other)
// __floordiv__(self, other
% __mod__(self, other)
** __pow__(self, other)
>> __rshift__(self, other)
<< __lshift__(self, other)
& __and__(self, other)
| __or__(self, other)
^ __xor__(self, other)
Comparison Operator
< __lt__(self, other)
> __gt__(self, other)
<e __le__(self, other)
>= __ge__(self, other)
== __eq__(self, other)
!= __ne__(self, other)
Assignment Operators
+= __iadd__(self, other)
-= __isub__(self, other)
*= __imul__(self, other)
/= __idiv__(self, other)
//= __ifloordiv__(self, other)
%= __imod__(self, other)
**= __ipow__(self, other)
>>= __irshift__(self, other)
<<= __ilshift__(self, other)
&= __iand__(self, other)
|= __ior__(self, other)
^= __ixor__(self, other)
Unary Operators
- __neg__(self, other)
+ __pos__(self, other)
~ __invert__(self, other)

Custom operator overloading example


    class A:
        def __init__(self, a):
            self.a = a
        def __gt__(self, other):        # overloading ">"
            if(self.a>other.a):
                return True
            else:
                return False
        # adding two objects
        def __add__(self, other):       # overloading "+"
            return self.a + other.a
    ob1 = A(2)
    ob2 = A(3)
    print(">" if ob1 > ob2 else "<")    # <
    print(ob1+ob2)                      # 5
        

any vs all

Condition any all
All Truthy values True True
All Falsy values False False
One or more Truthy values, but not all. Others are False True False
One or more Falsy values, but not all. Others are True True False
Empty iterable False True

    print (any([False, False, False, False]))  # False
    print (any([False, False, False, True]))   # True
    print (all([True, True, True, True]))      # True
    print (all([True, True, True, False]))     # False