Python Operators

Python operators are symbols that perform operations on variables and values. They are classified into several categories based on their functionality. Here’s an overview of the main types of operators in Python:

Arithmetic Operators

Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, division, etc.

  • Addition (+): Adds two operands.
  • Subtraction (-): Subtracts right operand from left operand.
  • Multiplication (*): Multiplies two operands.
  • Division (/): Divides left operand by right operand (always results in a float).
  • Floor Division (//): Divides left operand by right operand and returns the floor value (integer division).
  • Modulus (%): Returns the remainder when left operand is divided by right operand.
  • Exponentiation (**): Raises left operand to the power of right operand.

Comparison Operators

Comparison operators are used to compare values. They return Boolean values (True or False).

  • Equal to (==): Checks if two operands are equal.
  • Not equal to (!=): Checks if two operands are not equal.
  • Greater than (>): Checks if left operand is greater than right operand.
  • Less than (<): Checks if left operand is less than right operand.
  • Greater than or equal to (>=): Checks if left operand is greater than or equal to right operand.
  • Less than or equal to (<=): Checks if left operand is less than or equal to right operand.

Logical Operators

Logical operators are used to combine conditional statements.

  • Logical AND (and): Returns True if both operands are true.
  • Logical OR (or): Returns True if either of the operands is true.
  • Logical NOT (not): Returns True if operand is false (complements the operand).

Assignment Operators

Assignment operators are used to assign values to variables.

  • Assignment (=): Assigns right operand value to left operand.
  • Add and Assign (+=): Adds right operand to left operand and assigns the result to left operand.
  • Subtract and Assign (-=): Subtracts right operand from left operand and assigns the result to left operand.
  • Multiply and Assign (*=): Multiplies right operand with left operand and assigns the result to left operand.
  • Divide and Assign (/=): Divides left operand by right operand and assigns the result to left operand.
  • Modulus and Assign (%=): Computes modulus of left operand with right operand and assigns the result to left operand.
  • Exponentiation and Assign (**=): Raises left operand to the power of right operand and assigns the result to left operand.
  • Floor Division and Assign (//=): Performs floor division on left operand with right operand and assigns the result to left operand.

Bitwise Operators

Bitwise operators perform operations on bits of operands.

  • Bitwise AND (&): Performs AND operation on each bit position.
  • Bitwise OR (|): Performs OR operation on each bit position.
  • Bitwise XOR (^): Performs XOR (exclusive OR) operation on each bit position.
  • Bitwise NOT (~): Inverts all the bits.
  • Left Shift (<<): Shifts bits of left operand to the left by number of positions specified by right operand.
  • Right Shift (>>): Shifts bits of left operand to the right by number of positions specified by right operand.

Membership Operators

Membership operators test for membership in a sequence (lists, tuples, strings).

  • in: Returns True if a sequence with the specified value is present in the object.
  • not in: Returns True if a sequence with the specified value is not present in the object.

Identity Operators

Identity operators compare the memory locations of two objects.

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

Example Usage:

# Arithmetic Operators
a = 10
b = 3
print(a + b)  # Output: 13
print(a - b)  # Output: 7
print(a * b)  # Output: 30
print(a / b)  # Output: 3.3333333333333335
print(a // b) # Output: 3
print(a % b)  # Output: 1
print(a ** b) # Output: 1000

# Comparison Operators
print(a > b)   # Output: True
print(a != b)  # Output: True
print(a <= b)  # Output: False

# Logical Operators
x = True
y = False
print(x and y) # Output: False
print(x or y)  # Output: True
print(not x)   # Output: False

# Assignment Operators
c = 5
c += 2  # Equivalent to: c = c + 2
print(c)  # Output: 7

# Bitwise Operators
d = 10  # Binary: 1010
e = 4   # Binary: 0100
print(d & e)   # Output: 0 (Binary: 0000)
print(d | e)   # Output: 14 (Binary: 1110)
print(d ^ e)   # Output: 14 (Binary: 1110)

# Membership Operators
list1 = [1, 2, 3, 4]
print(2 in list1)    # Output: True
print(5 not in list1)  # Output: True

# Identity Operators
f = 10
g = 10
print(f is g)    # Output: True
print(f is not g)  # Output: False

Understanding and utilizing these operators effectively is essential for writing efficient and expressive Python code. Each operator serves a specific purpose and can greatly enhance your ability to manipulate data and control program flow in Python.

Leave a Reply

Your email address will not be published. Required fields are marked *