Operators

Operators

What are operators?

In programming, operators are special symbols or keywords that perform operations on one or more operands. An operand is a value or a variable on which an operation is performed.

Types of Operators

Here are some common types of operators:

  1. Arithmetic Operators:

    • Perform basic arithmetic operations like addition, subtraction, multiplication, division, etc.

    • Example: +, -, *, /, % (modulo), ** (exponentiation).

        a = 10
        b = 3
      
        # Arithmetic operations
        sum_result = a + b
        difference_result = a - b
        product_result = a * b
        division_result = a / b
        remainder_result = a % b
        exponentiation_result = a ** b
      
  2. Comparison Operators:

    • Compare two values and return a Boolean result (True or False).

    • Example: == (equal), != (not equal), <, >, <=, >=.

        x = 5
        y = 8
      
        # Comparison operations
        is_equal = x == y
        not_equal = x != y
        greater_than = x > y
        less_than_or_equal = x <= y
      
  3. Logical Operators:

    • Combine Boolean values and return a Boolean result.

    • Example: and, or, not.

        p = True
        q = False
      
        # Logical operations
        logical_and = p and q
        logical_or = p or q
        logical_not = not p
      
  4. Assignment Operators:

    • Assign values to variables.

    • Example: =, +=, -=, *=, /=, %=, **=.

        x = 10
      
        # Assignment operations
        x += 5  # Equivalent to x = x + 5
      
  5. Bitwise Operators:

    • Perform operations on binary representations of integers.

    • Example: & (bitwise AND), | (bitwise OR), ^ (bitwise XOR), << (left shift), >> (right shift).

        a = 5
        b = 3
      
        # Bitwise operations
        bitwise_and = a & b
        bitwise_or = a | b
        bitwise_xor = a ^ b
        left_shift = a << 1
        right_shift = a >> 1
      
  6. Identity Operators: They are used to compare the memory locations of two objects. They check if two variables refer to the same object in memory. There are two identity operators in Python:

    1. is:

      • The is operator returns True if both operands refer to the same object in memory; otherwise, it returns False.

          x = [1, 2, 3]
          y = [1, 2, 3]
          z = x
        
          print(x is y)  # False, different objects
          print(x is z)  # True, same object as z
        
    2. is not:

      • The is not operator returns True if both operands do not refer to the same object in memory; otherwise, it returns False.

          a = "hello"
          b = "world"
          c = a
        
          print(a is not b)  # True, different objects
          print(a is not c)  # False, same object as c
        
  7. Membership operators: Membership operators are used to test whether a value or variable is a member of a sequence, such as a string, list, tuple, or set. There are two membership operators in Python:

    1. in:

      • The in operator returns True if the specified value is found in the sequence; otherwise, it returns False.

          numbers = [1, 2, 3, 4, 5]
        
          print(3 in numbers)  # True
          print(6 in numbers)  # False
        
    2. not in:

      • The not in operator returns True if the specified value is not found in the sequence; otherwise, it returns False.

          fruits = ["apple", "banana", "orange"]
        
          print("banana" not in fruits)  # False
          print("grape" not in fruits)   # True
        

Precedence of operators

Operator precedence refers to the order in which operations are performed in an expression. When an expression contains multiple operators, precedence determines which operations are performed first.

If operators have the same precedence, the associativity comes into play, determining the order of evaluation.

In Python, and many other programming languages, operators have different levels of precedence. Operators with higher precedence are evaluated before those with lower precedence. For example, multiplication has a higher precedence than addition, so in the expression 2 + 3 * 4, the multiplication (*) is performed first.

Here are some common operators and their general precedence (from highest to lowest):

  1. Parentheses:

    • ()
  2. Exponentiation:

    • **
  3. Unary Plus and Minus (Sign):

    • +x, -x
  4. Multiplication, Division, and Remainder:

    • *, /, %
  5. Addition and Subtraction:

    • +, -
  6. Bitwise Shifts:

    • <<, >>
  7. Bitwise AND:

    • &
  8. Bitwise XOR:

    • ^
  9. Bitwise OR:

    • |
  10. Comparison Operators:

    • <, <=, >, >=, ==, !=
  11. Logical NOT:

    • not
  12. Logical AND:

    • and
  13. Logical OR:

    • or
  14. Conditional Expression (Ternary Operator):

    • x if condition else y

Operator precedence can be overridden by using parentheses. Expressions inside parentheses are evaluated first. For example, (2 + 3) * 4 ensures that the addition is performed before the multiplication.

Understanding operator precedence is crucial for writing correct and predictable expressions in your code. It helps you avoid unexpected behavior and ensures that expressions are evaluated in the intended order.

What's next?

In the next article in this blog series, we will look into conditional handling in Python.

Stay tuned.