DocsHub
Basics

Operators

Learn how to perform calculations, compare values, and combine conditions in Python.

Operators

An operator is a symbol that performs an operation on one or more values. You already know some from math — +, -, *. Python has several categories of operators, each with a different purpose.


Arithmetic operators

These do math. They work on numbers (int and float).

OperatorNameExampleResult
+Addition10 + 313
-Subtraction10 - 37
*Multiplication10 * 330
/Division10 / 33.3333...
//Floor division10 // 33
%Modulus10 % 31
**Exponentiation2 ** 8256
print(10 + 3)    # 13
print(10 - 3)    # 7
print(10 * 3)    # 30
print(10 / 3)    # 3.3333333333333335
print(10 // 3)   # 3  — divides and drops the decimal
print(10 % 3)    # 1  — remainder after division
print(2 ** 8)    # 256

Division always returns a float in Python, even if the result is a whole number:

print(10 / 2)    # 5.0  — not 5
print(10 // 2)   # 5    — use // if you want an integer

Modulus (%) gives you the remainder. It is more useful than it looks — a classic use is checking if a number is even or odd:

print(10 % 2)   # 0 — no remainder, so 10 is even
print(7 % 2)    # 1 — remainder of 1, so 7 is odd

+ also works on strings — it joins them together:

first = "Hello"
second = "World"
print(first + " " + second)   # Hello World

Comparison operators

These compare two values and always return a bool — either True or False.

OperatorMeaningExampleResult
==Equal to5 == 5True
!=Not equal to5 != 3True
>Greater than5 > 3True
<Less than5 < 3False
>=Greater than or equal5 >= 5True
<=Less than or equal3 <= 5True
age = 20

print(age == 20)   # True
print(age != 18)   # True
print(age > 18)    # True
print(age < 18)    # False
print(age >= 20)   # True
print(age <= 19)   # False

Do not confuse = and ==. A single = assigns a value to a variable. A double == compares two values. Using = when you mean == is one of the most common beginner mistakes.


Logical operators

These combine multiple conditions together.

OperatorMeaningExampleResult
andBoth must be TrueTrue and FalseFalse
orAt least one must be TrueTrue or FalseTrue
notFlips True to Falsenot TrueFalse
age = 25
has_id = True

# and — both conditions must be True
print(age >= 18 and has_id)    # True

# or — at least one condition must be True
print(age < 18 or has_id)      # True

# not — reverses the result
print(not has_id)              # False

A real example — checking if someone can enter:

age = 20
has_ticket = True

if age >= 18 and has_ticket:
    print("Welcome in!")
else:
    print("Sorry, you cannot enter.")
# Welcome in!

Assignment operators

These assign a value to a variable. You already know =. The others are shortcuts that combine assignment with an operation.

OperatorMeaningExampleSame as
=Assignx = 5x = 5
+=Add and assignx += 3x = x + 3
-=Subtract and assignx -= 3x = x - 3
*=Multiply and assignx *= 3x = x * 3
/=Divide and assignx /= 3x = x / 3
//=Floor divide and assignx //= 3x = x // 3
%=Modulus and assignx %= 3x = x % 3
**=Exponent and assignx **= 3x = x ** 3
score = 10

score += 5    # score is now 15
score *= 2    # score is now 30
score -= 10   # score is now 20

print(score)  # 20

Identity operators

These check if two variables point to the exact same object in memory — not just the same value.

OperatorMeaning
isSame object
is notNot the same object
x = None

print(x is None)       # True
print(x is not None)   # False

Use is only for comparing with None, True, or False. For everything else, use ==. Two variables can have the same value but be different objects — is would return False in that case.


Membership operators

These check if a value exists inside a sequence like a string, list, or tuple.

OperatorMeaning
inValue is in the sequence
not inValue is not in the sequence
name = "Ali Ahmed"

print("Ali" in name)        # True
print("Khan" not in name)   # True

numbers = [1, 2, 3, 4, 5]
print(3 in numbers)         # True
print(9 in numbers)         # False

Operator precedence

When multiple operators appear in one expression, Python follows a specific order — just like BODMAS in math. Higher in the list means evaluated first:

  1. ** — exponentiation
  2. *, /, //, % — multiplication and division
  3. +, - — addition and subtraction
  4. ==, !=, >, <, >=, <= — comparisons
  5. not — logical not
  6. and — logical and
  7. or — logical or
print(2 + 3 * 4)      # 14  — not 20, multiplication first
print((2 + 3) * 4)    # 20  — parentheses override the order
print(2 ** 3 + 1)     # 9   — exponent first, then + 1

When in doubt, use parentheses. They make your intent clear and prevent bugs from unexpected precedence.


Summary

CategoryOperators
Arithmetic+ - * / // % **
Comparison== != > < >= <=
Logicaland or not
Assignment= += -= *= /= and more
Identityis is not
Membershipin not in

On this page