Python Operators

Operators in general are used to perform operations on values and variables in Python. These are standard symbols used for the purpose of logical and arithmetic operations. In this article, we will look into different types of Python operators.

  1. Arithmetic operators: Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication and division.
  2. Operator Description Syntax
    + Addition: adds two operands x + y
    - Subtraction: subtracts two operands x - y
    * Multiplication: multiplies two operands x * y
    / Division (float): divides the first operand by the second x / y
    // Division (floor): divides the first operand by the second x // y
    % Modulus: returns the remainder when first operand is divided by the second x % y
    ** Power : Returns first raised to power second x ** y
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Examples of Arithmetic Operator 
    a = 9
    b = 4
      
    # Addition of numbers 
    add = a +
      
    # Subtraction of numbers 
    sub = a -
      
    # Multiplication of number 
    mul = a *
      
    # Division(float) of number 
    div1 = a /
      
    # Division(floor) of number 
    div2 = a //
      
    # Modulo of both number 
    mod = a %
      
    # Power
    p = a ** b
      
    # print results 
    print(add) 
    print(sub) 
    print(mul) 
    print(div1) 
    print(div2) 
    print(mod)
    print(p)

    chevron_right

    
    

    Output:



    13
    5
    36
    2.25
    2
    1
    6561
    



    Please refer Differences between / and // for some interesting facts about these two operators.

  3. Relational Operators: Relational operators compares the values. It either returns True or False according to the condition.
  4. Operator Description Syntax
    > Greater than: True if left operand is greater than the right x > y
    < Less than: True if left operand is less than the right x < y
    == Equal to: True if both operands are equal x == y
    != Not equal to - True if operands are not equal x != y
    >= Greater than or equal to: True if left operand is greater than or equal to the right x >= y
    <= Less than or equal to: True if left operand is less than or equal to the right x <= y
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Examples of Relational Operators
    a = 13
    b = 33
      
    # a > b is False
    print(a > b)
      
    # a < b is True
    print(a < b)
      
    # a == b is False
    print(a == b)
      
    # a != b is True
    print(a != b)
      
    # a >= b is False
    print(a >= b)
      
    # a <= b is True
    print(a <= b)

    chevron_right

    
    

    Output:

    False
    True
    False
    True
    False
    True
    


  5. Logical operators: Logical operators perform Logical AND, Logical OR and Logical NOT operations.

  6. Operator Description Syntax
    and Logical AND: True if both the operands are true x and y
    or Logical OR: True if either of the operands is true x or y
    not Logical NOT: True if operand is false not x
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Examples of Logical Operator
    a = True
    b = False
      
    # Print a and b is False
    print(a and b)
      
    # Print a or b is True
    print(a or b)
      
    # Print not a is False
    print(not a)

    chevron_right

    
    

    Output:

    False
    True
    False
    




  7. Bitwise operators: Bitwise operators acts on bits and performs bit by bit operation.
  8. Operator Description Syntax
    & Bitwise AND x & y
    | Bitwise OR x | y
    ~ Bitwise NOT ~x
    ^ Bitwise XOR x ^ y
    >> Bitwise right shift x>>
    << Bitwise left shift x<<
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Examples of Bitwise operators
    a = 10
    b = 4
      
    # Print bitwise AND operation  
    print(a & b)
      
    # Print bitwise OR operation
    print(a | b)
      
    # Print bitwise NOT operation 
    print(~a)
      
    # print bitwise XOR operation 
    print(a ^ b)
      
    # print bitwise right shift operation 
    print(a >> 2)
      
    # print bitwise left shift operation 
    print(a << 2)

    chevron_right

    
    

    Output:

    0
    14
    -11
    14
    2
    40
    


  9. Assignment operators: Assignment operators are used to assign values to the variables.

  10. Operator Description Syntax
    = Assign value of right side of expression to left side operand x = y + z
    += Add AND: Add right side operand with left side operand and then assign to left operand a+=b     a=a+b
    -= Subtract AND: Subtract right operand from left operand and then assign to left operand a-=b       a=a-b
    *= Multiply AND: Multiply right operand with left operand and then assign to left operand a*=b       a=a*b
    /= Divide AND: Divide left operand with right operand and then assign to left operand a/=b         a=a/b
    %= Modulus AND: Takes modulus using left and right operands and assign result to left operand a%=b   a=a%b
    //= Divide(floor) AND: Divide left operand with right operand and then assign the value(floor) to left operand a//=b       a=a//b
    **= Exponent AND: Calculate exponent(raise power) value using operands and assign value to left operand a**=b     a=a**b
    &= Performs Bitwise AND on operands and assign value to left operand a&=b     a=a&b
    |= Performs Bitwise OR on operands and assign value to left operand a|=b         a=a|b
    ^= Performs Bitwise xOR on operands and assign value to left operand a^=b       a=a^b
    >>= Performs Bitwise right shift on operands and assign value to left operand a>>=b     a=a>>b
    <<= Performs Bitwise left shift on operands and assign value to left operand a <<= b                    a= a << b


  11. Special operators: There are some special type of operators like-
    • Identity operators-
      is and is not are the identity operators both are used to check if two values are located on the same part of the memory. Two variables that are equal does not imply that they are identical.

      is          True if the operands are identical 
      is not      True if the operands are not identical 
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Examples of Identity operators
    a1 = 3
    b1 = 3
    a2 = 'GeeksforGeeks'
    b2 = 'GeeksforGeeks'
    a3 = [1,2,3]
    b3 = [1,2,3]
      
      
    print(a1 is not b1)
      
      
    print(a2 is b2)
      
    # Output is False, since lists are mutable.
    print(a3 is b3)

    chevron_right

    
    

    Output:

    False
    True
    False
    
    • Membership operators-
      in and not in are the membership operators; used to test whether a value or variable is in a sequence.

      in            True if value is found in the sequence
      not in        True if value is not found in the sequence
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Examples of Membership operator
    x = 'Geeks for Geeks'
    y = {3:'a',4:'b'}
      
      
    print('G' in x)
      
    print('geeks' not in x)
      
    print('Geeks' not in x)
      
    print(3 in y)
      
    print('b' in y)

    chevron_right

    
    

    Output:

    True
    True
    False
    True
    False
  12. Precedence and Associativity of Operators: Operator precedence and associativity as these determine the priorities of the operator.
    • Operator Precedence: This is used in an expression with more than one operator with different precedence to determine which operation to perform first.
      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      # Examples of Operator Precedence
        
      # Precedence of '+' & '*' 
      expr = 10 + 20 * 30
      print(expr) 
        
      # Precedence of 'or' & 'and' 
      name = "Alex"
      age = 0
          
      if name == "Alex" or name == "John" and age >= 2 :  
          print("Hello! Welcome."
      else
          print("Good Bye!!")

      chevron_right

      
      

      Output:

      610
      Hello! Welcome.
      
    • Operator Associativity: If an expression contains two or more operators with the same precedence then Operator Associativity is used to determine. It can either be Left to Right or from Right to Left.
      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      # Examples of Operator Associativity
        
      # Left-right associativity 
      # 100 / 10 * 10 is calculated as  
      # (100 / 10) * 10 and not  
      # as 100 / (10 * 10) 
      print(100 / 10 * 10
          
      # Left-right associativity 
      # 5 - 2 + 3 is calculated as  
      # (5 - 2) + 3 and not  
      # as 5 - (2 + 3) 
      print(5 - 2 + 3
          
      # left-right associativity 
      print(5 - (2 + 3)) 
          
      # right-left associativity 
      # 2 ** 3 ** 2 is calculated as  
      # 2 ** (3 ** 2) and not  
      # as (2 ** 3) ** 2 
      print(2 ** 3 ** 2)

      chevron_right

      
      

      Output:

      100.0
      6
      0
      512
    Operator Description   Associativity
    ( ) Parentheses   left-to-right
    ** Exponent  right-to-left
    *  /  % Multiplication/division/modulus left-to-right
    +  - Addition/subtraction left-to-right
    <<  >> Bitwise shift left, Bitwise shift right left-to-right
    <  <= 
    >  >=
    Relational less than/less than or equal to 
    Relational greater than/greater  than or equal to
    left-to-right
    ==  != Relational is equal to/is not equal to left-to-right

Quiz on Python Operators

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

18


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.