Python Tutorial

Python Flow Control

Python Functions

Python Data Types

Python Date and Time

Python Files

Python String

Python List

Python Dictionary

Python Variable

Python Input/Output

Python Exceptions

Python Advanced

Python Operators

Operators in Python are symbols that enable you to perform specific operations on operands, such as variables and values. Python has several types of operators, including arithmetic, comparison, assignment, logical, bitwise, membership, and identity operators. In this tutorial, we'll briefly discuss each type of operator and provide examples of their usage.

  • Arithmetic Operators Arithmetic operators are used to perform mathematical operations:
  • Addition (+): Adds two numbers
  • Subtraction (-): Subtracts the second number from the first
  • Multiplication (*): Multiplies two numbers
  • Division (/): Divides the first number by the second (returns a float)
  • Floor Division (//): Divides the first number by the second, returning the largest whole number
  • Modulus (%): Returns the remainder of the division
  • Exponentiation (**): Raises the first number to the power of the second

Example:

a = 10
b = 3

print(a + b)  # 13
print(a - b)  # 7
print(a * b)  # 30
print(a / b)  # 3.3333333333333335
print(a // b)  # 3
print(a % b)  # 1
print(a ** b)  # 1000
  • Comparison Operators Comparison operators are used to compare two values and return a boolean result:
  • Equal (==): True if the values are equal, False otherwise
  • Not equal (!=): True if the values are not equal, False otherwise
  • Greater than (>): True if the first value is greater than the second, False otherwise
  • Less than (<): True if the first value is less than the second, False otherwise
  • Greater than or equal to (>=): True if the first value is greater than or equal to the second, False otherwise
  • Less than or equal to (<=): True if the first value is less than or equal to the second, False otherwise

Example:

x = 5
y = 10

print(x == y)  # False
print(x != y)  # True
print(x > y)   # False
print(x < y)   # True
print(x >= y)  # False
print(x <= y)  # True
  • Assignment Operators Assignment operators are used to assign values to variables:
  • Equals (=): Assigns the value on the right to the variable on the left
  • Add and assign (+=): Adds the right value to the left variable and assigns the result
  • Subtract and assign (-=): Subtracts the right value from the left variable and assigns the result
  • Multiply and assign (*=): Multiplies the left variable by the right value and assigns the result
  • Divide and assign (/=): Divides the left variable by the right value and assigns the result
  • Floor divide and assign (//=): Floor divides the left variable by the right value and assigns the result
  • Modulus and assign (%=): Calculates the modulus of the left variable and the right value and assigns the result
  • Exponentiate and assign (**=): Raises the left variable to the power of the right value and assigns the result
  • Bitwise AND and assign (&=): Performs bitwise AND on the left variable and the right value and assigns the result
  • Bitwise OR and assign (|=): Performs bitwise OR on the left variable and the right value and assigns the result
  • Bitwise XOR and assign (^=): Performs bitwise XOR on the left variable and the right value and assigns the result
  1. Arithmetic operators in Python:

    • Description: Arithmetic operators perform basic mathematical operations.
    • Example Code:
      a = 5
      b = 2
      addition = a + b
      subtraction = a - b
      multiplication = a * b
      division = a / b
      modulus = a % b
      print(f"Addition: {addition}, Subtraction: {subtraction}, Multiplication: {multiplication}, Division: {division}, Modulus: {modulus}")
      
  2. Comparison operators in Python:

    • Description: Comparison operators compare values and return True or False.
    • Example Code:
      x = 5
      y = 8
      greater_than = x > y
      equal_to = x == y
      not_equal = x != y
      print(f"Greater Than: {greater_than}, Equal To: {equal_to}, Not Equal: {not_equal}")
      
  3. Logical operators in Python:

    • Description: Logical operators perform logical operations on Boolean values.
    • Example Code:
      is_true = True
      is_false = False
      logical_and = is_true and is_false
      logical_or = is_true or is_false
      logical_not = not is_true
      print(f"Logical AND: {logical_and}, Logical OR: {logical_or}, Logical NOT: {logical_not}")
      
  4. Bitwise operators in Python:

    • Description: Bitwise operators perform operations at the bit level.
    • Example Code:
      x = 5
      y = 3
      bitwise_and = x & y
      bitwise_or = x | y
      bitwise_xor = x ^ y
      print(f"Bitwise AND: {bitwise_and}, Bitwise OR: {bitwise_or}, Bitwise XOR: {bitwise_xor}")
      
  5. Assignment operators in Python:

    • Description: Assignment operators assign values to variables and perform operations in a concise form.
    • Example Code:
      a = 5
      a += 2  # Equivalent to a = a + 2
      print(a)
      
  6. Membership operators in Python:

    • Description: Membership operators test if a value is a member of a sequence.
    • Example Code:
      numbers = [1, 2, 3, 4, 5]
      is_in = 3 in numbers
      is_not_in = 6 not in numbers
      print(f"Is In: {is_in}, Not In: {is_not_in}")
      
  7. Identity operators in Python:

    • Description: Identity operators compare the memory location of two objects.
    • Example Code:
      x = [1, 2, 3]
      y = [1, 2, 3]
      is_same_object = x is y
      is_not_same_object = x is not y
      print(f"Is Same Object: {is_same_object}, Not Same Object: {is_not_same_object}")
      
  8. Operator precedence in Python:

    • Description: Operator precedence determines the order in which operators are evaluated.
    • Example Code:
      result = 5 + 3 * 2  # Multiplication has higher precedence
      print(result)
      
  9. Custom operators and operator overloading in Python:

    • Description: Operator overloading allows custom classes to define their behavior for standard operators.
    • Example Code:
      class Point:
          def __init__(self, x, y):
              self.x = x
              self.y = y
      
          def __add__(self, other):
              return Point(self.x + other.x, self.y + other.y)
      
      p1 = Point(1, 2)
      p2 = Point(3, 4)
      p3 = p1 + p2
      print(f"Result: ({p3.x}, {p3.y})")