Operators in Python are special symbols that perform arithmetic or logical computation. The value an operator acts upon is called the operand. These are fundamental building blocks for manipulating variables and values.

Arithmetic Operators

Used for mathematical operations:

  • + Addition: x + y
  • - Subtraction: x - y
  • * Multiplication: x * y
  • / Division: x / y
  • % Modulus (remainder): x % y
  • ** Exponentiation: x ** y
  • // Floor Division: x // y

Comparison Operators

Return true or false when comparing values:

  • == Equal: x == y
  • != Not equal: x != y
  • > Greater than: x > y
  • < Less than: x < y
  • >= Greater than or equal: x >= y
  • <= Less than or equal: x <= y

Logical Operators

Combine conditional statements:

  • and Returns true if both statements are true: x and y
  • or Returns true if one statement is true: x or y
  • not Reverses the result: not x

Assignment Operators

Assign values to variables:

  • = Simple assignment: x = 5
  • += Add and assign: x += 5 (equivalent to x = x + 5)
  • Similar patterns for -=, *=, /=, etc.

Membership Operators

Test if values exist in sequences (strings, lists, tuples, sets, dictionaries):

  • in Value found in sequence: x in y
  • not in Value not found: x not in y

Identity Operators

Compare memory locations of objects:

  • is Same object reference: x is y
  • is not Different object reference: x is not y

Bitwise Operators

Perform bit-by-bit operations:

  • & AND: Sets each bit to 1 if both bits are 1
  • | OR: Sets each bit to 1 if one bit is 1
  • ^ XOR: Sets each bit to 1 if only one bit is 1
  • ~ NOT: Inverts all bits
  • << Left Shift: Shifts bits left
  • >> Right Shift: Shifts bits right

Mastering these operators enables effective data manipulation, decision-making, and complex logical operations in Python programs.