Topic 3: Basic Operators

1. Introduction

Operators are the backbone of any programming language, allowing you to perform operations on variables and values. Python boasts a wide range of operators, from basic mathematical ones to more complex logical and bitwise operations.

2. Arithmetic Operators

Used for performing mathematical operations.

  • Addition (+): Adds two numbers.

    python
    x = 5 + 3 # x is 8
  • Subtraction (-): Subtracts the right operand from the left.

    python
    x = 5 - 3 # x is 2
  • Multiplication (*): Multiplies two numbers.

    python
    x = 5 * 3 # x is 15
  • Division (/): Divides the left operand by the right operand.

    python
    x = 8 / 2 # x is 4.0 (result is float)
  • Modulus (%): Returns the remainder when the left operand is divided by the right operand.

    python
    x = 10 % 3 # x is 1
  • Exponentiation (**): Raises the left operand to the power of the right operand.

    python
    x = 2 ** 3 # x is 8
  • Floor Division (//): Divides and returns the integer value of the quotient. It dumps the digits after the decimal.

    python
    x = 9 // 2 # x is 4

3. Comparison Operators

Used for comparing values.

  • Equal (==): Checks if the value of two operands is equal.

    python
    (5 == 5) # True
  • Not Equal (!=): Checks if the value of two operands is unequal.

    python
    (5 != 3) # True
  • Greater Than (>): Checks if the left operand is greater than the right operand.

    python
    (5 > 3) # True
  • Less Than (<): Checks if the left operand is less than the right operand.

    python
    (3 < 5) # True
  • Greater Than or Equal to (>=): Checks if the left operand is greater than or equal to the right operand.

    python
    (5 >= 5) # True
  • Less Than or Equal to (<=): Checks if the left operand is less than or equal to the right operand.

    python
    (4 <= 5) # True

4. Logical Operators

Used to combine conditional statements.

  • and: Returns True if both statements are true.

    python
    (5 > 3 and 7 > 4) # True
  • or: Returns True if one of the statements is true.

    python
    (5 < 3 or 7 > 4) # True
  • not: Reverse the logical state of the operand.

    python
    not(5 > 3) # False

5. Assignment Operators

Used to assign values to variables.

  • =, +=, -=, *=, /=, %=, //=, **=: For example, x += 3 is the same as x = x + 3.

6. Membership Operators

Used to test whether a value or variable is in a sequence.

  • in: Returns True if a value is found in a sequence.

    python
    x = [1, 2, 3, 4] 3 in x # True
  • not in: Returns True if a value is not found in a sequence.

    python
    y = "Hello" "z" not in y # True

7. Identity Operators

Used to compare the objects, not if they are equal, but if they are the same object, with the same memory location.

  • is: Returns True if both variables are the same object.

    python
    x = [1, 2, 3] y = x x is y # True
  • is not: Returns True if both variables are not the same object.

    python
    a = [1, 2, 3] b = [1, 2, 3] a is not b # True

8. Conclusion

Understanding the usage and nuances of operators in Python is fundamental for building logical operations and algorithms. These tools, while simple on their own, are powerful when combined, allowing developers to design sophisticated and efficient solutions to complex problems.


By grasping the essentials of Python operators, developers can effectively manipulate data, construct logical sequences, and build efficient Python programs.