Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




PYTHON - OPERATORS


'Operators' are used in Pyton to perform calculations or operations.


Say for example, we have used the addition '+' operator to add two numbers.


Example :


x = 5
y = 6
z = x + y
print("The added value is :",z) 


Output :



  The added value is : 11

In the above example, we have used two operators of Python.


  1. Addition Operator (i.e. '+')

    Used to add two or more values.

    z = x + y
  2. Assignment Operator (i.e. '=')

    Used to assign a value to a variable.

    x = 5
    y = 6
    z = x + y

So far, we have seen only two 'operators'. Now, let us see all the 'operators' available in Python.


List of Operators available in Python


  1. Arithmetic Operators



    Arithmetic Operators are used to perform Arithmetic operations like addition, subtraction, multiplication e.t.c.

    Let us see them below.



    Operator Used For Example
    + Addition x + y
    - Subtraction x - y
    * Multiplication x * y
    / Division x / y
    // Floor Division x // y
    % Modulus x % y
    ** Exponentiation x ** y


    CLICK HERE TO SEE ARITHMETIC OPERATORS IN DETAIL



    1. Addition Operator (+)

      Example :



      x = 5
      y = 6
      z = x + y
      print("The added value is :",z) 


      Output :



        The added value is : 11


    1. Subtraction Operator (-)

      Example :



      x = 8
      y = 2
      z = x - y
      print("The subtracted value is :",z)  


      Output :



        The subtracted value is : 6


    1. Multiplication Operator (*)

      Example :



      x = 5
      y = 6
      z = x * y
      print("The multiplied value is :",z)   


      Output :



        The multiplied value is : 30


    1. Division Operator (/)

      Example :



      x = 8
      y = 2
      z = x / y
      print("The divided value is :",z)  


      Output :



        The divided value is : 4


    1. Floor division Operator (//)

      Floor division Operator is just like the division operator with a mild difference.

      It takes the result and omits the floating point. It just takes the whole number.

      Example :



      x = 9
      y = 2
      z = x // y
      print("The floor divided value is :",z) 


      Output :



        The floor divided value is : 4


      If we look at the above output. It is '4'. But if we divide 9 by 2, the result should be '4.5'.

      And since, we have used 'Floor division Operator (i.e. //)', only '4' is taken out of '4.5' omitting the floating point '.5'.


    1. Modulus Operator (%)

      Modulus Operator calculates the remainder.

      Example :



      x = 9
      y = 2
      z = x % y
      print("The modulus is :",z)  


      Output :



        The modulus is : 1


      The remainder of '9' and '2' is '1'.


    1. Exponentiation Operator (**)

      Example :



      x = 2
      y = 3
      z = x ** y
      print("The exponential is :",z)  


      Output :



        The exponential value is : 8


      In the above code we are calculating (2)3 .

      i.e.

      2*2*2 = 8
  2. Bitwise Operators



    The are a few number system. Among which we usually use the decimal numbers (i.e. 1, 2,3, .... ). And there is also something called 'Binary Numbers'(i.e. '0' and '1').

    Say for example, if we convert the decimal number '5' to Binary, it is '101'.

    '0' and '1' are also called as 'Bit'. And the operator that we are going to use, deals with 'Bits'.

    So, they are called as 'Bitwise Operators'.

    Let us see them below :



    Operator Name Example
    & AND x & y
    | OR x | y
    ~ NOT ~x
    ^ XOR x ^ y
    >> Right Shift x >>
    << Left Shift x <<


    CLICK HERE TO SEE BITWISE OPERATORS IN DETAIL



    1. The Operator '&' or 'AND'

      The '&' Operator checks, if both the bits are '1' then it returns '1' else it returns '0'.

      Let us understand it with the below example.

      Example :



      x = 5
      y = 2
      z = x & y     
      print(z) 


      Output :



         0


      So, in the above code, we have,

      x = 5


      And,

      y = 2


      So, what Python does internally is, converts both the numbers (i.e. '5' and '2') to binary.

      The binary equivalent of '5' is '101'

      And the binary equivalent of '2' is '10'

      Now, if we calculate the '&' of '5' and '2'.

        101
      &  010
        ------
        000


      And we got the output '000' because we could only get a '1' if both are '1'.

      But in this case, '1 & 0' is '0', '0 & 1' is '0' and '1 & 0' is also '0' i.e. '000'.

      And if we convert '000' to decimal we get '0' as output.


    1. The Operator '|' or 'OR'

      The '|' Operator checks, if either one of the bits is '1' then it returns '1' else it returns '0'.

      Let us understand it with the below example.

      Example :



      x = 5
      y = 2
      z = x | y     
      print(z)  


      Output :



        7


      So, in the above code, we have,

        x = 5


      And,

        y = 2


      So, what Python does internally is, converts both the numbers (i.e. '5' and '2') to binary.

      The binary equivalent of '5' is '101'

      And the binary equivalent of '2' is '10'

      Now, if we calculate the '|' of '5' and '2'.

        101
      |   010
        ------
        111


      And we got the output '111' because we can get a '1' if either of the bits are '1'.

      But in this case, '1 | 0' is '1', '0 | 1' is '1' and '1 | 0' is also '1' i.e. '111'.

      And if we convert '111' to decimal, it is '7'.


    1. The Operator '~' or '' NOT

      The '~' Operator or 'not', simply converts '1's to '0's and '0's to '1's.

      Let us understand it with the below example.

      Example :



      x = 5
      z = ~x     
      print(z)   


      Output :



        -6


      If we look at the output, it looks a little weird. Where on earth did '-6' come from.

      Well! Let us not get much deeper into it and understand a simple formula.

      i.e.

      ~x can be written as '-x-1' or '-(x+1)'

      So, if we apply the same formula to the above code, we have,

        x = 5


      And, the binary equivalent of '5' is '101'

      Now, if as per the formula,

      ~x = -(101 + 1)

        101
         1
        ------
        110


      The decimal equivalent of '110' is 6.

      So,

        ~x = -6


      And thus we get the output as '-6'.


    1. The Operator '^' or 'XOR'

      The '^' or 'XOR'/'eXclusive OR' is almost same as the 'OR' Operator. The only difference is, if both are '1' it returns '0'. Else for '1' and '0' or '0' and '1' it returns '1'.

      Let us understand it with the below example.

      Example :



      x = 5
      y = 4
      z = x ^ y     
      print(z) 


      Output :



        1


      So, in the above code, we have,

        x = 5


      And,

        y = 4


      So, what Python does internally is, converts both the numbers (i.e. '5' and '2') to binary.

      The binary equivalent of '5' is '101'

      And the binary equivalent of '2' is '100'

      Now, if we calculate the '|' of '5' and '2'.

      ~x = -(101 + 1)

        101
      ^  100
        ------
        001


      And we got the output '001' because '1 ^ 1' is '0', '0 ^ 0' is '0' and '1 ^ 0' is '1'

      i.e. '001'.

      And if we convert '001' to decimal, it is '1'.

      Just note that '1 ^ 1' is '0' and not '1'.


    1. The Operator '>>' or 'Right Shift'

      The '>>' Operator or 'Right Shift', simply shifts bits to the right by number of positions specified.

      Let us understand it with the below example.

      Example :



      x = 5
      z = x >> 1     
      print(z)


      Output :



        2


      So, in the above code, we have,

      x = 5


      And we try Right Shifting the value of 'x' by 1.

      z = x >> 1


      So, what Python does internally is, converts the number (i.e. '5') to binary.

      The binary equivalent of '5' is '101'

      Now, if we right shift '101' by '1'.

      '101' >> '010'


      And after Right Shifting by '1', the '1' to the extreme right is lost.

      And we got the output '010'.

      And if we convert '010' to decimal, it is '2'.


    1. The Operator '<<' or 'Left Shift'

      The '>>' Operator or 'Left Shift', simply shifts bits to the left by number of positions specified.

      Let us understand it with the below example.

      Example :



      x = 5
      z = x << 2     
      print(z)  


      Output :



        20


      So, in the above code, we have,

      x = 5


      And we try Left Shifting the value of 'x' by 2.

      'z = x << 2


      So, what Python does internally is, converts the number (i.e. '5') to binary.

      The binary equivalent of '5' is '101'

      Now, if we left shift '101' by '2'.

      '00101' >> '10100'


      And after Left Shifting by '2', all the elements shifts 2 places to the right.

      And we got the output '10100'.

      And if we convert '10100' to decimal, it is '20'.
  3. Assignment Operators



    We have already used the Assignment Operator i.e. '='. When we tried to initialise any variable.

    Say for example,



    x = 5
    y = 6
    z = x + y




    Although, there is just one assignment operator i.e. '='. But there are various ways by which we can write it.

    Let us see them below.



    Operator Example Is Same As
    = x = 1 x = 1
    += x += 1 x = x + 1
    -= x -= 1 x = x - 1
    *= x *= 1 x = x * 1
    /= x /= 1 x = x / 1
    %= x %= 1 x = x % 1
    **= x **= 1 x = x ** 1
    //= x //= 1 x = x // 1
    &= x &= 1 x = x & 1
    |= x |= 1 x = x | 1
    ^= x ^= 1 x = x ^ 1
    >>= x >>= 1 x = x >> 1
    <<= x <<= 1 x = x << 1


    CLICK HERE TO SEE ASSIGNMENT OPERATORS IN DETAIL



    1. - The Operator '='

      Example :



      x = 5
      print("The value of x is :",x)


      Output :



         The value of x is : 5


    1. The operator '+='

      Say, we have a statement,

      x = x + 3


      Now, we can shorten the above statement by using the operator '+='.

      x += 3


      Example :



      x = 5
      x += 3
      print("The new value is :",x) 


      Output :



        The new value of x is : 8


    1. The operator '-='

      Say, we have a statement,

      x = x - 3


      Now, we can shorten the above statement by using the operator '-='.

      x -= 3


      Example :



      x = 5
      x -= 3
      print("The new value is :",x)   


      Output :



         The new value of x is : 2


    1. The operator '*='

      Say, we have a statement,

      x = x * 3


      Now, we can shorten the above statement by using the operator '*='.

      x *= 3


      Example :



      x = 5
      x *= 3
      print("The new value is :",x) 


      Output :



        The new value of x is : 15


    1. The operator '/='

      Say, we have a statement,

      x = x / 3


      Now, we can shorten the above statement by using the operator '/='.

      x /= 3


      Example :



      x = 12
      x /= 3
      print("The new value is :",x) 


      Output :



        The new value of x is : 4


    1. The operator '%='

      Say, we have a statement,

      x = x % 3


      Now, we can shorten the above statement by using the operator '%='.

      x %= 3


      Example :



      x = 5
      x %= 3
      print("The new value is :",x)  


      Output :



        The new value of x is : 2


    1. The operator '**='

      Say, we have a statement,

      x = x ** 3


      Now, we can shorten the above statement by using the operator '**='.

      x **= 3


      Example :



      x = 5
      x **= 3
      print("The new value is :",x)  


      Output :



        The new value of x is : 125


    1. The operator '//='

      Say, we have a statement,

      x = x // 3


      Now, we can shorten the above statement by using the operator '//='.

      x //= 3


      Example :



      x = 5
      x //= 3
      print("The new value is :",x)   


      Output :



        The new value of x is : 1


    1. The operator '&='

      Say, we have a statement,

      x = x & 3


      Now, we can shorten the above statement by using the operator '&='.

      x &= 3


      Example :



      x = 12
      x &= 3
      print("The new value is :",x)  


      Output :



         The new value of x is : 1


    1. The operator '|='

      Say, we have a statement,

      x = x | 3


      Now, we can shorten the above statement by using the operator '|='.

      x |= 3


      Example :



      x = 5
      x |= 3
      print("The new value is :",x) 


      Output :



         The new value of x is : 7


    1. The operator '^='

      Say, we have a statement,

      x = x ^ 3


      Now, we can shorten the above statement by using the operator '^='.

      x ^= 3


      Example :



      x = 5
      x ^= 3
      print("The new value is :",x) 


      Output :



        The new value of x is : 6


    1. The operator '>>='

      Say, we have a statement,

      x = x >> 3


      Now, we can shorten the above statement by using the operator '>>='.

      x >>= 3


      Example :



      x = 5
      x >>= 3
      print("The new value is :",x) 


      Output :



        The new value of x is : 0


    1. The operator '<<='

      Say, we have a statement,

      x = x << 3


      Now, we can shorten the above statement by using the operator '<<='.

      x <<= 3


      Example :



      x = 12
      x <<= 3
      print("The new value is :",x) 


      Output :



        The new value of x is : 40
  4. Comparison Operators



    There would be scenarios, where we would have to compare two values.

    Say, we wanted to check, if the first number is equal to the other number or not. Or if one number is greater than or less than the other number and so on.

    Usually, we would be using the Comparison Operators with 'if' statements and 'Loops'. Which we would be learning in later tutorials.

    For now we can compare the values and just print the results. The result would be 'True' if the condition matches. Else it would return 'False'.

    There are various comparison operators we have listed below.



    Operator Name Example
    == Equal To x == y
    != Not Equal x != y
    > Greater Than x > y
    < Less Than x < y
    >= Greater Than or Equal To x >= y
    <= Greater Than or Equal To x <= y


    CLICK HERE TO SEE COMPARISON OPERATORS IN DETAIL



    1. The Operator '=='

      It is used to compare two values and check if they are equal or not.

      Example :



      x = 5
      y = 7
      print(x == y)


      Output :



         False


      Since, the values of 'x' and 'y' are not equal. We got the output as 'False'.

      Let us see the next Example.



      x = 5
      y = 5
      print(x == y)


      Output :



         True


      Now, the values of 'x' and 'y' are equal. We got the output as 'True'.


    1. The Operator '!='

      It is used to compare two values and check if they are not equal. '!' is a sign for not.

      Example :



      x = 5
      y = 7
      print(x != y) 


      Output :



        True


      Since, the values of 'x' and 'y' are not equal. We got the output as 'True'.

      Let us see the next Example.



      x = 5
      y = 5
      print(x != y)


      Output :



         False


      Now, the values of 'x' and 'y' are equal. We got the output as 'False'.


    1. The Operator '>'

      It is used to compare two values and check if the first value is greater than the second or not.

      Example :



      x = 5
      y = 7
      print(x > y) 


      Output :



        False


      Since, the values of 'x' is not greater than 'y'. We got the output as 'False'.

      Let us see the next Example.



      x = 7
      y = 5
      print(x > y)


      Output :



         True


      Now, the values of 'x' is greater than 'y'. We got the output as 'True'.


    1. The Operator '<'

      It is used to compare two values and check if the first value is less than the second or not.

      Example :



      x = 5
      y = 7
      print(x < y) 


      Output :



        True


      Since, the values of 'x' is less than 'y'. We got the output as 'True'.

      Let us see the next Example.



      x = 7
      y = 5
      print(x < y)


      Output :



         False


      Now, the values of 'x' is not less than 'y'. We got the output as 'False'.


    1. The Operator '>='

      It is used to compare two values and check if the first value is greater or equal to the second or not.

      Example :



      x = 5
      y = 7
      print(x >= y) 


      Output :



        False


      Since, the values of 'x' is not greater than or equal 'y'. We got the output as 'False'.

      Let us see the next Example.



      x = 5
      y = 5
      print(x >= y)


      Output :



         True


      Now, the values of 'x' is equal than 'y'. We got the output as 'True'.


    1. The Operator '<='

      It is used to compare two values and check if the first value is less than or equal to the second or not.

      Example :



      x = 5
      y = 7
      print(x <= y) 


      Output :



        True


      Since, the values of 'x' is less than 'y'. We got the output as 'True'.

      Let us see the next Example.



      x = 7
      y = 5
      print(x <= y)


      Output :



         False


      Now, the values of 'x' is not less than 'y'. We got the output as 'False'.
  5. Logical Operators



    There can be scenarios where we need to compare more than two values.

    Say for example,

    There are four numbers and we want to check if the first number is greater than the second. And also need to check if the third number is greater than the fourth number.

    Something like the below one,

    first_number > second_number and third_number > fourth_number


    And if you see the above statement, we need an 'and' statement to compare four values.

    And thus, 'Logical Operators' comes into picture. It can be 'and', 'or', 'not'.

    Let's see them below :



    Operator Description Example
    and It is true when both conditions are True. i < j and k < l
    or It is true when one of the condition is True i < j or k < l
    not As the name suggests, it calculates the inverse not(i < j or k < l)


    CLICK HERE TO SEE LOGICAL OPERATORS IN DETAIL



    1. The Operator 'and'

      It is true when both conditions are True.

      Let us clear with the below example.

      Example :



      i = 5
      j = 7
      k = 8
      l = 9
      print(i < j and k < l)


      Output :



         True


      The above statement checks if 'i < j' and 'k < l'.

      Now, since '5' is less than '7' and also '8' is less than '9'. The print statement returns True.

      print(i < j and k < l)


      Let us see another example.

      Example :



      i = 5
      j = 7
      k = 8
      l = 4
      print(i < j and k < l)


      Output :



         False


      So, the above statement checks if 'i < j' and 'k < l'.

      Now, since '5' is less than '7' but '8' is not less than '4'. The print statement returns False.

      print(i < j and k < l)


      Just remember, for 'and' operator, both the conditions needs to be satisfied.


    1. The Operator 'or'

      It is true when one of the condition is True.

      Let us clear with the below example.

      Example :



      i = 5
      j = 7
      k = 8
      l = 4
      print(i < j or k < l)


      Output :



         True


      So, the above statement checks if 'i < j' or 'k < l'.

      Now, since '5' is less than '7' and although '8' is not less than '4'. The print statement returns True.

      print(i < j and k < l)


      Just remember, for 'or' operator, if only one condition works then the entire condition becomes True.


    1. The Operator 'not'

      It is true when a condition is False. As the name suggests, it calculates the inverse.

      Let us clear with the below example.

      Example :



      i = 5
      j = 7
      k = 8
      l = 4
      print(not(i < j or k < l))


      Output :



         False


      So, the above statement checks if 'i < j' or 'k < l'.

      Now, since '5' is less than '7' and although '8' is not less than '4'. The condition is True. And since we are using 'not' operator, False is printed.

      print(not(i < j or k < l))
  6. Membership Operators



    Say we have 100 Names in a list and you want to check, if a particular name is present in the list or not.

    Well! Membership Operators comes handy in such case.

    There are two Membership Operators in Python. i.e. 'in' and 'not in'.

    Let us see them below :



    Operator Description Example
    in Used to search a particular data from a collection of data 5 in x
    not in 'not in' operator is just the opposite of 'in' operator 5 not in x


    CLICK HERE TO SEE MEMBERSHIP OPERATORS IN DETAIL



    1. The Operator 'in'

      As said 'in' operator is used to search a particular data from a collection of data.

      Example :



      x = ["John", "Keith", "Manohar", "Salim"]
      print("Manohar" in x)


      Output :



         True


      As we can see the output, it is 'True'. Because the 'in' operator searches for the String 'Manohar',

      print("Manohar" in x)


      From the list

      x = ["John", "Keith", "Manohar", "Salim"]


      And since, 'Manohar' is present in the list, True is printed.


    1. The Operator 'not in'

      'not in' operator is just the opposite of 'in' operator.

      Example :



      x = ["John", "Keith", "Manohar", "Salim"]
      print("Manohar" not in x)


      Output :



         False


      As we can see the output, it is 'False'. Since we are asking 'Is Manohar not present in the list?'.

      And since, 'Manohar' is present in the list, False is printed.
  7. Identity Operators



    Just remember, all the values we store in Python are actually stored in a Memory location.

    Now, if you want to check, if two values are stored in the same Memory location, Identity Operators are used.

    There are just two Identity Operators, 'is' and 'is not'.



    Operator Description Example
    is Returns True, if both the Variables belongs to the same Memory location x is y
    is not Just the opposite of 'is' operator x is not y


    CLICK HERE TO SEE IDENTITY OPERATORS IN DETAIL



    1. The Operator 'is'

      It returns True, if both the Variables belongs to the same Memory location.

      Example :



      x = [5,2]
      y = [5,2]
      z = y     
      print(x is y)
      print(y is z)


      Output :



         False
        True


      So, we have created two lists,

      x = [5,2]


      And

      y = [5,2]


      With the same values.

      And the assign the variable 'y' to a new variable 'z'.

      z = y


      Now, when we compare 'x' and 'y',

      print(x is y)


      We get the output as False. And that is because, the 'is' operator checks if 'x' and 'y' belongs to the same memory location or not.

      And in this case, although they contain the same value. But they are in different Memory locations.

      But in the next statement,

      print(y is z)


      The output is True because we have initialised 'y' to 'z'. And both 'y' and 'z' belongs to the same Memory location.


    1. The Operator 'is not'

      The Operator 'is not' is just the opposite of 'is' operator.

      Example :



      x = [5,2]
      y = [5,2]
      z = y     
      print(x is not y)
      print(y is not z)


      Output :



         True
        False