HOME HTML EDITOR C JAVA PHP python

Python Operators

Operators in Python are special symbols or keywords used to perform operations on variables and values. Common categories include arithmetic, assignment, comparison, logical, and membership operators.

Arithmetic and Assignment Operators

Arithmetic operators perform math such as addition, subtraction, multiplication, division, floor division (//), modulo (%), and exponentiation (**). Compound assignment operators like += update a variable in place:

a = 10
b = 3

print(a + b)   # 13 (addition)
print(a / b)   # 3.3333333333333335 (float division)
print(a // b)  # 3 (floor division)
print(a % b)   # 1 (remainder)
print(a ** b)  # 1000 (10 to the power 3)

a += 5         # same as a = a + 5 -> 15
print(a)

Comparison and Logical Operators

Comparison operators (==, !=, >, <, >=, <=) evaluate conditions to a boolean, while and, or, and not combine or invert boolean expressions:

age = 22
has_id = True

print(age >= 18 and has_id)     # True
print(age < 18 or has_id)      # True
print(not has_id)              # False

Membership Operators (in and not in)

  • in: Returns True if a value or substring exists inside a sequence or collection.
  • not in: Returns True if a value or substring is not found in the sequence or collection.
languages = ["Python", "Java", "C"]

print("Python" in languages)   # True
print("PHP" not in languages)    # True
Tip: Use parentheses () in complex expressions when you want to make evaluation order explicit and easy to read.