fbpx

What is Operator in Python | Types of Operator

0
(0)

Introduction

Operators are the symbols carryout the arithmetic or logical computations on the operands.

Types of operators:

There are 7 types of operators. They are

  • Arithmetic operators
  • Comparison operators
  • Logical operators
  • Bitwise operators
  • Assignment operators
  • Identity operators
  • Membership operators

Arithmetic operators

OperatorDescriptionExample
+Add two operands or unary plusx + y+ 2
Subtract right operand from the left or unary minusx – y- 2
*Multiply two operandsx * y
/Divide left operand by the right one (always results into float)x / y
//Floor division. Divide left operand by right operand and round the quotient to nearest lowest integer.x//y
%Divide the left operand by right operand and returns the remainderx %y
Example
1
2
3
4
5
6
7
8
x=16
y=6
print("x+y=",x+y)
print("x-y=",x-y)
print("x*y=",x*y)
print("x/y=",x/y)
print("x//y=",x//y)
print("x%y=",x%y)

 Output:

x+y= 22
x-y= 10
x*y= 96
x/y= 2.6666666666666665
x//y= 2
x%y= 4

    Comparison operators

    Comparison operators compare given two operands or values and return True or False.

    OperatorDescriptionExample
    >Greater than. Returns True if left operand is greater than the rightx > y
    >=Greater than or equal to. Returns True if left operand is greater than or equal to the rightx >= y
    <Less than. Returns True if left operand is less than the rightx < y
    <=Less than or equal to. Returns True if left operand is less than or equal to the rightx <= y
    ==Equal to. Returns True if both operands are equalx == y
    !=Not equal to. Returns True if operands are not equalx != y
    Example
    1
    2
    3
    4
    5
    6
    7
    8
    x=10
    y=15
    print("x>y is:",x>y)
    print("x<y is:",x<y)
    print("x>=y is:",x>=y)
    print("x<=y is:",x<=y)
    print("x==y is:",x==y)
    print("x!=y is:",x!=y)
    
    Output:
    x>y is: False
    x<y is: True
    x>=y is: False
    x<=y is: True
    x==y is: False
    x!=y is: True
    

      Logical operators

      Logical operators are used to club two conditions and to evaluate them. The conditions can be negated using logical not operator.

      OperatorDescriptionExample
      andReturns True if both the conditions are truex and y
      orReturns True if either of the conditions is truex or y
      notReturns True if condition is false (negate the operand)not x

      Example
      1
      2
      3
      print((5>2)and (6>3))
      print((2<5)or (6>3))
      print(not(5==5))
      

      Output:

      True
      True
      False

      Bitwise operators

      Bitwise operators act on binary equivalent of the operands as if they were strings of binary digits. They operate bit by bit.

      For example 2 in binary is 0010, 7 in binary is 0111.

      In the below table let x = 10 (0000 1010 in binary) and y = 4 (0000 0100 in binary)

      OperatorDescriptionExample
      &Bitwise ANDx & y = 0 (0000 0000)
      |Bitwise ORx | y = 14 (0000 1110)
      ~Bitwise NOT~x = -11 (1111 0101) (In 2’s complement format)
      ^Bitwise XORx ^ y = 14 (0000 1110)
      >>Bitwise right shiftx >> 2 = 2 (0000 0010)(Right most two bits dropped and 0 at two left most bits added)
      <<Bitwise left shiftx << 2 = 40 (0010 1000)(Left most two bits dropped and 0 at two right most bits added)

       Example

      1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      x=10
      y=4
      print("x:",x)
      print("y:",y)
      print("x&y:",x&y)
      print("x|y:",x|y)
      print("~x:",~x)
      print("x^y:",x^y)
      print("x>>2:",x>>2)
      print("x<<2:",x<<2)
      

      Output:
      x: 10
      y: 4
      x&y: 0
      x|y: 14
      ~x: -11
      x^y: 14
      x>>2: 2
      x<<2: 40

      Assignment operators

      Assignment operators are used to assign a value to a variable. Simple assignment operator(=)assigns the value to its right to variable to its left.

      Example
      1
      2
      x=10
      print("x:",x)
      



       Output:

      x:10

      Compound assignment operators

      OperatorExampleSame As
      =x = 5x = 5
      +=x += 5x = x + 5
      -=x -= 5x = x – 5
      *=x *= 5x = x * 5
      /=x /= 5x = x / 5
      %=x %= 5x = x % 5
      //=x //= 5x = x // 5
      **=x **= 5x = x ** 5
      &=x &= 5x = x & 5
      |=x |= 5x = x | 5
      ^=x ^= 5x = x ^ 5
      >>=x >>= 5x = x >> 5
      <<=x <<= 5x = x << 5

      Identity operators

      Identity operators “is” and “is not” are used to check if the operands or objects to the left and right of these operators are referring to a value stored in the same memory location and returns True or False.

      OperatorDescriptionExample
      isTrue if the operands are identical (refer to the same object)x is True
      is notTrue if the operands are not identical (do not refer to the same object)x is not True
      Example
      1
      2
      3
      4
      5
      6
      7
      8
      9
      n1=10
      n2=10
      print(n1 is n2)
      s1="Good morning"
      s2="Good morning"
      print(s1 is not s2)
      l1=[1,2,3]	#refer list copy behaviour
      l2=[1,2,3]
      print(l1 is l2)
      

      Output:

      True
      False
      False

      Membership operators

      Membership operators “in” and “not in” are used to check if the given value is present in the sequence of values and returns True or False.

      OperatorDescriptionExample
      inTrue if value/variable is found in the sequence5 in x
      not inTrue if value/variable is not found in the sequence5 not in x

      In dictionary, membership operators will return True if the key is present. Presence of value in a Dictionary cannot be checked using membership operators.

       Example

      1
      2
      3
      4
      a=[1,2,3,4]
      print(5 in a)
      square={1:1,2:4,3:9,4:16}
      print(4 in a)
      

      Output:
      False
      True

      How useful was this post?

      Click on a star to rate it!

      Average rating 0 / 5. Vote count: 0

      No votes so far! Be the first to rate this post.

      As you found this post useful...

      Follow us on social media!

      We are sorry that this post was not useful for you!

      Let us improve this post!

      Tell us how we can improve this post?

      Leave a Reply