Python Operators


Operators are special symbols or keywords that perform operations on variables and values.

Python supports various types of operators, including

  1. Arithmetic
  2. Bitwise
  3. Assignment
  4. Comparison
  5. Membership
  6. Identity
  7. logical

Arithmetic Operators :-

These are used to perform mathematical operations like

  1. Addition ( + ) :- Returns addition of two values.
  2. a = 5
    b = 3
    result = a + b
    print(result)  # Output: 8
    
  3. Subtraction ( - ) :- Returns subtraction of two values.
  4. a = 10
    b = 7
    result = a - b
    print(result)  # Output: 3
    
  5. Multiplication ( * ) :- Returns multiplication of two values.
  6. a = 4
    b = 6
    result = a * b
    print(result)  # Output: 24
    
  7. Division ( / ) :- Returns division of two values.
  8. a = 20
    b = 4
    result = a / b
    print(result)  # Output: 5.0
    
  9. Modulo ( % ) :- Returns reminder of numbers.
  10. a = 17
    b = 5
    result = a % b
    print(result)  # Output: 2
    
  11. Floor Division ( // ) :- Returns output of floor division
  12. a = 17
    b = 5
    result = a // b
    print(result)  # Output: 3
    
  13. Exponentiation ( ** ) :- Return value of ab where a and are values
  14. a = 2
    b = 3
    result = a ** b
    print(result)  # Output: 8
    


Bitwise Operators :-

These operators perform bitwise operations on integers. They treat operands as binary numbers and operate bit by bit.

  1. AND ( & ): It returns a new integer where each bit is set to 1 only if the corresponding bits of both operands are 1.
  2. a = 0b0000000000000101  # 16-bit binary representation of 5
    b = 0b0000000000000011  # 16-bit binary representation of 3
    result = a & b        # 0b0000000000000001 in binary, which is 1 in decimal
    print(result)          # Output: 1
    
  3. OR ( | ): It returns a new integer where each bit is set to 1 if at least one of the corresponding bits of the operands is 1.
  4. a = 0b0000000000000101  # 16-bit binary representation of 5
    b = 0b0000000000000011  # 16-bit binary representation of 3
    result = a | b        # 0000000000000111 in binary, which is 7 in decimal
    print(result)          # Output: 7
    
  5. XOR ( ^ ): It returns a new integer where each bit is set to 1 if the corresponding bits of the operands are different.
  6. a = 0b0000000000000101  # 16-bit binary representation of 5
    b = 0b0000000000000011  # 16-bit binary representation of 3
    result = a ^ b        # 0000000000000110 in binary, which is 6 in decimal
    print(result)          # Output: 6
    
  7. NOT ( ~ ): It returns the complement of the integer.
  8. a = 0b0000000000000101  # 16-bit binary representation of 5
    result = ~a           # -0000000000000110 (complement of 0101 in binary), NOT operation inverts each bit
    print(result)          # Output: -6
    
  9. Left Shift << ): Shifts the bits of the first operand to the left by the number of positions specified by the second operand. Zeros are shifted in from the right.
  10. a = 0b0000000000000101  # 16-bit binary representation of 5
    result = a << 1       # 0000000000001010 in binary, which is 10 in decimal
    print(result)          # Output: 10
    
  11. Right Shift >> ): Shifts the bits of the first operand to the right by the number of positions specified by the second operand. Zeros are shifted in from the left.
  12. a = 0b0000000000000101  # 16-bit binary representation of 5
    result = a >> 1       # 0000000000000010 in binary, which is 2 in decimal
    print(result)          # Output: 2
    


Assignment Operators :-

These are used to assign values to variables.

  1. += Addition Assignment Operator :-
  2. # += (Addition Assignment Operator) Example
    x = 5
    x += 3  # Equivalent to x = x + 3
    print("x after addition:", x)  # Output: 8
    
  3. -= Subtraction Assignment Operator :-
  4. # -= (Subtraction Assignment Operator) Example
    x = 10
    x -= 4  # Equivalent to x = x - 4
    print("x after subtraction:", x)  # Output: 6
    
  5. *= Multiplication Assignment Operator :- 
  6. # *= (Multiplication Assignment Operator) Example
    x = 3
    x *= 2  # Equivalent to x = x * 2
    print("x after multiplication:", x)  # Output: 6
    
  7. /= Division Assignment Operator :-
  8. # /= (Division Assignment Operator) Example
    x = 10
    x /= 5  # Equivalent to x = x / 5
    print("x after division:", x)  # Output: 2.0 (result is a float)
    
  9. %= Modulus Assignment Operator :- 
  10. # %= (Modulus Assignment Operator) Example
    x = 17
    x %= 5  # Equivalent to x = x % 5
    print("x after modulus operation:", x)  # Output: 2
    
  11. //= Floor Division Assignment Operator :-
  12. # //= (Floor Division Assignment Operator) Example
    x = 17
    x //= 5  # Equivalent to x = x // 5
    print("x after floor division:", x)  # Output: 3
    
  13. **= Exponentiation Assignment Operator :-
  14. # **= (Exponentiation Assignment Operator) Example
    x = 2
    x **= 3  # Equivalent to x = x ** 3
    print("x after exponentiation:", x)  # Output: 8
    


Comparison Operators :- 

These operators are used to compare values.

  1. Equal to ( == ) : - Checks if two values are equal.
  2. x = 5
    y = 5
    
    if x == y:
        print("x is equal to y")
    else:
        print("x is not equal to y")
    
  3. Not equal to ( != ) :- Checks if two values are not equal.
  4. x = 5
    y = 10
    
    if x != y:
        print("x is not equal to y")
    else:
        print("x is equal to y")
    
  5. Less than ( < ) :- Checks if the left operand is less than the right operand.
  6. x = 5
    y = 10
    
    if x < y:
        print("x is less than y")
    else:
        print("x is not less than y")
    
  7. Greater than ( > ) :- Checks if the left operand is greater than the right operand.
  8. x = 10
    y = 5
    
    if x > y:
        print("x is greater than y")
    else:
        print("x is not greater than y")
    
  9. Less than or equal to ( <= ) :- Checks if the left operand is less than or equal to the right operand.
  10. x = 5
    y = 5
    
    if x <= y:
        print("x is less than or equal to y")
    else:
        print("x is greater than y")
    
  11. Greater than or equal to ( >= ) :- Checks if the left operand is greater than or equal to the right operand.
  12. x = 10
    y = 5
    
    if x >= y:
        print("x is greater than or equal to y")
    else:
        print("x is less than y")
    


Membership Operators :-

These operators are used to test whether a value is a member of a sequence (like a string, list, or tuple).

  1. in Operator :-
  2. # Checking if a character is in a string
    string = "Hello, world!"
    if 'o' in string:
        print("'o' is present in the string.")
    else:
        print("'o' is not present in the string.")
    
  3. not in Operator :-
  4. # Checking if a number is not in a list
    numbers = [1, 2, 3, 4, 5]
    if 6 not in numbers:
        print("6 is not present in the list.")
    else:
        print("6 is present in the list.")
    


Identity Operator :-

These operators are used to compare the memory locations of two objects. 

  1. is Operator :-
  2. # Using the is operator to compare the identity of two objects
    x = [1, 2, 3]
    y = [1, 2, 3]
    
    print(x is y)  # False, because x and y are two different objects in memory
    
  3. is not Operator :-
  4. # Using the is not operator to compare the identity of two objects
    a = "hello"
    b = "world"
    
    print(a is not b)  # True, because a and b are different objects
    


Logical Operators :-

These operators are used to combine conditional statements.

  1. and Operator :-
  2. # Using 'and' operator to check if two conditions are true
    x = 5
    y = 10
    
    if x > 0 and y < 15:
        print("Both conditions are true.")
    else:
        print("At least one condition is false.")
    
  3. or Operator :-
  4. # Using 'or' operator to check if at least one condition is true
    name = "Alice"
    age = 25
    
    if name == "Alice" or age == 30:
        print("At least one condition is true.")
    else:
        print("Both conditions are false.")
    
  5. not Operator :-
  6. # Using 'not' operator to negate a condition
    is_raining = False
    
    if not is_raining:
        print("It's not raining.")
    else:
        print("It's raining.")