Python Foundation

Operators in Python

01 · What are Operators? Operators · 1 of 9

What are Operators?

Operators are special symbols used to perform operations on values and variables.

The values on which the operator works are called operands.

Python
a = 10
b = 5
print(a + b)   # + is operator, a and b are operands
Output
15

Types of Operators in Python

  • Arithmetic Operators
  • Comparison (Relational) Operators
  • Logical Operators
  • Assignment Operators
  • Membership Operators
  • Identity Operators
02 · Arithmetic Operators Operators · 2 of 9

Arithmetic Operators

These operators are used to perform mathematical operations.

Python
a = 15
b = 4

print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("Division:", a / b)
print("Floor Division:", a // b)
print("Modulus:", a % b)
print("Exponent:", a ** b)
Output
Addition: 19
Subtraction: 11
Multiplication: 60
Division: 3.75
Floor Division: 3
Modulus: 3
Exponent: 50625

Explanation

  • + → Addition
  • - → Subtraction
  • * → Multiplication
  • / → Division (always returns float)
  • // → Floor Division (gives only whole number)
  • % → Modulus (gives remainder)
  • ** → Exponent (power)
03 · Comparison Operators Operators · 3 of 9

Comparison (Relational) Operators

These operators compare two values and return True or False.

Python
x = 10
y = 20

print(x == y)    # Equal to
print(x != y)    # Not equal to
print(x > y)     # Greater than
print(x < y)     # Less than
print(x >= 10)   # Greater than or equal to
print(x <= 5)    # Less than or equal to
Output
False
True
False
True
True
False

Comparison operators are heavily used in if conditions and loops.

04 · Logical Operators Operators · 4 of 9

Logical Operators

Logical operators are used to combine conditional statements.

Python
a = True
b = False

print(a and b)   # True only if both are True
print(a or b)    # True if at least one is True
print(not a)      # Reverses the result
Output
False
True
False

Practical Example

Python
age = 25
has_id = True

if age >= 18 and has_id:
    print("Entry allowed")
else:
    print("Entry denied")
Output
Entry allowed
05 · Assignment Operators Operators · 5 of 9

Assignment Operators

Assignment operators are used to assign values to variables.

Python
x = 10
print(x)

x += 5      # x = x + 5
print(x)

x -= 3      # x = x - 3
print(x)

x *= 2      # x = x * 2
print(x)

x /= 4      # x = x / 4
print(x)

x //= 2     # x = x // 2
print(x)

x **= 3     # x = x ** 3
print(x)
Output
10
15
12
24
6.0
3.0
27.0
06 · Membership Operators Operators · 6 of 9

Membership Operators

Membership operators check whether a value exists in a sequence (string, list, tuple, set, etc.).

Python
text = "Python"
print("P" in text)
print("z" in text)
print("z" not in text)

numbers = [10, 20, 30, 40]
print(20 in numbers)
print(50 not in numbers)
Output
True
False
True
True
True

in → Returns True if value is present
not in → Returns True if value is not present

07 · Identity Operators Operators · 7 of 9

Identity Operators

Identity operators check whether two variables refer to the same object in memory.

Python
a = [1, 2, 3]
b = a
c = [1, 2, 3]

print(a is b)       # True → same object
print(a is c)       # False → different objects
print(a == c)       # True → same values
print(a is not c)   # True
Output
True
False
True
True

is checks identity (same object), while == checks equality (same value).

08 · Operator Precedence Operators · 8 of 9

Operator Precedence

When multiple operators are used in a single expression, Python follows a specific order (precedence).

Python
print(10 + 5 * 2)        # Multiplication first
print((10 + 5) * 2)       # Parentheses first
print(10 + 5 * 2 ** 3)    # Exponent → Multiply → Add
Output
20
30
50

Common Precedence Order (High to Low)

  1. () → Parentheses
  2. ** → Exponent
  3. *, /, //, % → Multiplication & Division
  4. +, - → Addition & Subtraction
  5. Comparison operators (==, >, < etc.)
  6. Logical operators (not, and, or)
09 · Summary Operators · 9 of 9

Summary – Operators in Python

  • Arithmetic → + - * / // % **
  • Comparison → == != > < >= <=
  • Logical → and or not
  • Assignment → = += -= *= /= etc.
  • Membership → in not in
  • Identity → is is not

Next Topic → Strings in Python

Home Operators in Python

Operators in Python

Learn Arithmetic, Comparison, Logical, Assignment, Membership and Identity operators.

Select any topic from the left sidebar to begin.


Start Learning →