Operators in Python

Operators are a fundamental part of any programming language. They play a vital role in comparison, mathematical, logical and relational operators.

Below is the list of operators with example.

1) Arithmetic Operators

These operators are used for arithmetic calculation purpose.

    #Example for Python Arthmetic Operations.

    #Addition
    a=15
    b=12
    c=a+b

    print(c)

    #Subtraction
    a=10
    b=3
    c=a-b
    print(c)

    #Multiplication
    a=7
    b=3
    c=a*b
    print(c)

    #Division
    a=8
    b=2
    c=a/b
    print(c)

    #Modulus
    a=15
    b=4
    c=a%b
    print(c)

    #Exponential
    a=2
    b=4
    c=a**b # this means 2 to power of 4 (2^4)
    print(c)

    #Floor Division
    a=7
    b=2
    c=a//b #the division ans will be 3.5 but floor will round the value to 3
    print(c)

    #PYTHON OUTPUT
    27
    7
    21
    4.0
    3
    16
    3

2) Assignment Operators

The Assignment operators are used for initializing/assigning value to the variable.

    # (=) Operator
    a=5
    print(a)
    
    # (+=) Operator
    a=2
    a+=4
    print(a)
    
    # (-=) Operator
    a=5
    a-=2
    print(a)
    
    # (/=) Operator
    a=8
    a/=2
    print(a)
    
    # (%=) Operator
    a=15
    a%=3
    print(a)
    
    # (//=) Operator
    a=9
    a//=4
    print(a)
    
    # (**=) Operator
    a=2
    a**=4
    print(a)
    
    # (&=) Operator
    a=2
    a&=2 
    print(a) # this are used the verify if the same variable exists in the memory
    
    # (|=) Operator
    a=8
    a|=10
    print(a)
    
    # (^=) Operator
    x=1
    x^=4
    print(x)
    
    # (>>=) Operator
    a=5
    a>>=10
    
    # (<<=) Operator 
    a=3
    a<<=3
    print(a)

    #PYTHON OUTPUT
    5
    6
    3
    4.0
    0
    2
    16
    2
    10
    5
    24

3) Logical Operator

Logical Operators perform logical/analytical tasks on expressions and also for chaining multiple expressions together in a statement.

    a=5
    b=4
    c=3
    
    # and logical operator
    if a>b and a>c:
      print(a)
    
    # or logical operator
    if b>a or b>c:
      print(b)

    #PYTHON OUTPUT
    5
    4

Comparison Operator

This operator is helpful in comparing the values of expressions in statements.

    a=5
    b=5

    # equalsTo (==) Operator
    if a==b:
    print('a value is equal to b value')


    a=5
    b=4

    # not equalsTo (!=) Operator
    if a!=b:
    print('a value is not equal to be value')

    a=5
    b=4

    # Greater than (>) Operator
    if a>b:
    print('a value is greater the b value')

    a=5
    b=6

    # Lesser than Operator
    if a<b:
    print('a value is lesser than b value')

    a=4
    b=4

    # Greater than equalTo Operator
    if a>=b:
    print('a value is greater than and equal to b value')

    a=5
    b=5

    # Lesser than equalTo Operator
    if a<=b:
    print('a value is lesser than and equal to b value')

    #PYTHON OUTPUT
    a value is equal to b value
    a value is not equal to be value
    a value is greater the b value
    a value is lesser than b value
    a value is greater than and equal to b value
    a value is lesser than and equal to b value

Top Python Post