← Back to Chapters

Python Operators

? Python Operators

? Quick Overview

Operators are special symbols used to perform operations on variables and values. Python has a wide range of built-in operators grouped into several categories such as arithmetic, assignment, comparison, logical, identity, membership, and bitwise operators.

? Key Concepts

  • Arithmetic operators perform basic math operations like addition and division.
  • Assignment operators assign values to variables and can combine assignment with an operation.
  • Comparison operators compare two values and return True or False.
  • Logical operators combine conditions using and, or, and not.
  • Identity operators check if two references point to the same object in memory.
  • Membership operators test if a value is present in a sequence (like a list or string).
  • Bitwise operators work at the bit level (0s and 1s) of integers.

? Syntax & Theory

➕ Arithmetic Operators

  • + – Addition
  • - – Subtraction
  • * – Multiplication
  • / – Division (returns float)
  • % – Modulus (remainder)
  • ** – Exponentiation
  • // – Floor division (truncates decimal part)
? View Arithmetic Example
a = 10
b = 3
print(a + b)   # 13
print(a ** b)  # 1000

? Assignment Operators

  • =, +=, -=, *=, /=, //=, %=, **=
? View Assignment Example
x = 5
x += 3  # same as x = x + 3
print(x)  # 8

⚖️ Comparison Operators

  • == – equal to
  • != – not equal to
  • > – greater than
  • < – less than
  • >= – greater than or equal to
  • <= – less than or equal to
? View Comparison Example
a = 10
b = 5
print(a == b)  # False
print(a > b)   # True

? Logical Operators

  • and – returns True if both conditions are True
  • or – returns True if at least one condition is True
  • not – inverts a boolean value
? View Logical Example
x = 5
print(x > 3 and x < 10)  # True
print(not(x > 3))        # False

? Identity Operators

  • isTrue if both variables refer to the same object
  • is notTrue if they do not refer to the same object
? View Identity Example
a = [1, 2]
b = a
c = [1, 2]
print(a is b)  # True
print(a is c)  # False

? Membership Operators

  • in – checks if a value exists in a sequence
  • not in – checks if a value does not exist in a sequence
? View Membership Example
mylist = [1, 2, 3]
print(2 in mylist)     # True
print(5 not in mylist) # True

? Bitwise Operators

  • & – bitwise AND
  • | – bitwise OR
  • ^ – bitwise XOR
  • ~ – bitwise NOT
  • << – left shift
  • >> – right shift
? View Bitwise Example
a = 5      # 0101
b = 3      # 0011
print(a & b)  # 1
print(a | b)  # 7

? Live Output / Explanation

?️ Understanding the Results

  • a + b with a = 10 and b = 3 gives 13 because it adds two numbers.
  • a ** b calculates 10 raised to the power 3, which is 1000.
  • a == b returns False because 10 is not equal to 5.
  • a is b is True only when both variables refer to the exact same object in memory.
  • 2 in mylist is True when 2 exists in the list [1, 2, 3].

? Tips & Best Practices

  • Use parentheses () to ensure the correct order of operations in complex expressions.
  • Remember that floor division // truncates the decimal part; it does not round.
  • Check types with type() when mixing integers and floats to avoid unexpected behavior.
  • Use == to compare values and is to compare object identity.

? Try It Yourself

  • Write a small calculator program using all arithmetic operators (+, -, *, /, %, **, //).
  • Declare two variables and test whether they point to the same object using is and is not.
  • Create a list of numbers and check if a given number exists using in and not in.
  • Combine multiple conditions using and / or and print whether the overall expression is True or False.