LearnContact
Lesson 1030 min read

Operators

Master Python's arithmetic, comparison, logical, assignment, identity, and membership operators, along with operator precedence.

Introduction

Operators are the symbols that let you actually do something with your values — add two numbers, compare two scores, check if a condition is true, or see if an item exists in a list. Python groups operators into several families, each suited to a different kind of task.

In this lesson, you will work through every major operator family in Python: arithmetic, comparison, logical, assignment, identity, and membership, and finish with a look at how Python decides which operator runs first when several appear in one expression.

What You Will Learn
  • How to perform arithmetic, including the difference between / and //.
  • How to compare values and combine conditions with and, or, and not.
  • How Python's logical operators short-circuit and return operand values.
  • The difference between is and == for comparing values.
  • How to check membership in a collection using in and not in.
  • How operator precedence determines evaluation order.

What is an Operator?

An operator is a special symbol that performs an operation on one or more values, called operands. In 5 + 3, the + is the operator, and 5 and 3 are the operands.

Arithmetic

Perform math: addition, subtraction, multiplication, division, and more.

Comparison

Compare two values and get back True or False.

Logical

Combine multiple conditions using and, or, and not.

Assignment

Assign or update the value stored in a variable.

Identity

Check whether two variables point to the exact same object in memory.

Membership

Check whether a value exists inside a collection.

Arithmetic Operators

Arithmetic operators perform mathematical calculations on numbers.

OperatorMeaningExampleResult
+Addition5 + 38
-Subtraction5 - 32
*Multiplication5 * 315
/True Division5 / 22.5
//Floor Division5 // 22
%Modulus (remainder)5 % 21
**Exponent (power)5 ** 225
a = 17
b = 4

print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(a // b)
print(a % b)
print(a ** b)
Output
21
13
68
4.25
4
1
83521

True Division vs Floor Division

Python has two distinct division operators, and mixing them up is one of the most common beginner errors.

/ True Division

Always returns a float, even if the numbers divide evenly.

python
print(10 / 2)
# 5.0 — still a float

// Floor Division

Divides and rounds down to the nearest whole number.

python
print(10 // 3)
# 3 — rounded down
print(10 / 2)
print(10 // 2)
print(10 / 3)
print(10 // 3)
Output
5.0
5
3.3333333333333335
3

Comparison Operators

Comparison operators compare two values and always produce a bool result: True or False.

OperatorMeaningExample
==Equal to5 == 5 → True
!=Not equal to5 != 3 → True
>Greater than5 > 3 → True
<Less than5 < 3 → False
>=Greater than or equal to5 >= 5 → True
<=Less than or equal to4 <= 3 → False
score = 82
print(score == 82)
print(score != 100)
print(score >= 90)
Output
True
True
False

Logical Operators

Logical operators combine multiple conditions. Python has three: and, or, and not.

and

True only if both conditions are true.

or

True if at least one condition is true.

not

Flips a condition — True becomes False and vice versa.

age = 20
has_id = True

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

Short-Circuit Evaluation

A subtlety that surprises many beginners: and and or in Python do not always return True or False. They return one of the actual operand values, and they stop evaluating as soon as the overall result is already decided — this is called short-circuiting.

and returns the first falsy value, or the last value
print(0 and 5)
print(3 and 5)
print("" and "hello")
Output
0
5
or returns the first truthy value, or the last value
print(0 or 5)
print(3 or 5)
print("" or "hello")
Output
5
3
hello
Why This Matters

and stops as soon as it finds a falsy value (no need to check further — the result is already false). or stops as soon as it finds a truthy value. This is often used to provide default values, such as name = user_input or "Guest".

Assignment Operators

Assignment operators assign a value to a variable, and the compound forms combine an operation with assignment in one step.

OperatorExampleEquivalent To
=x = 5Assign 5 to x
+=x += 3x = x + 3
-=x -= 3x = x - 3
*=x *= 3x = x * 3
/=x /= 3x = x / 3
//=x //= 3x = x // 3
**=x **= 3x = x ** 3
x = 10
x += 5
print(x)

x -= 3
print(x)

x *= 2
print(x)
Output
15
12
24

Identity Operators

is and is not check whether two variables refer to the exact same object in memory — not just whether their values look equal. This is a different question from ==, which only checks value equality.

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

print(a == b)
print(a is b)
print(a is c)
Output
True
False
True
== vs is

a == b is True because their contents are equal. a is b is False because they are two separate list objects in memory, even though they look the same. c is a is True because c was assigned to point at the exact same object as a.

Membership Operators

in and not in check whether a value exists inside a collection, such as a string, list, or tuple.

fruits = ["apple", "banana", "cherry"]

print("banana" in fruits)
print("mango" in fruits)
print("mango" not in fruits)
print("a" in "banana")
Output
True
False
True
True

Operator Precedence

When an expression contains several operators, Python evaluates them in a specific order, called precedence — similar to the order of operations you learned in math class. Operators higher in the table run before operators lower in the table.

Precedence (high to low)Operators
1. Parentheses()
2. Exponent**
3. Unary sign+x, -x
4. Multiplication/Division*, /, //, %
5. Addition/Subtraction+, -
6. Comparison==, !=, >, <, >=, <=
7. Logical NOTnot
8. Logical ANDand
9. Logical ORor
result = 2 + 3 * 4
print(result)

result2 = (2 + 3) * 4
print(result2)
Output
14
20
When in Doubt, Use Parentheses

You do not need to memorize the entire precedence table. Wrapping expressions in parentheses makes your intent explicit and your code easier to read, even when it is not strictly required.

Common Mistakes

Avoid These Mistakes
  • Using / when you actually need whole-number floor division //.
  • Confusing == (equality) with = (assignment).
  • Using is to compare values instead of ==, especially for numbers or strings.
  • Forgetting that and/or return operand values, not always True or False.
  • Assuming operators run strictly left to right without considering precedence.

Best Practices

  • Use == to compare values; reserve is for checking against None or identical objects.
  • Add parentheses to clarify precedence, even when not strictly necessary.
  • Prefer // when you specifically need a whole-number result from division.
  • Use in / not in instead of manual loops to check membership.
  • Take advantage of short-circuiting for concise default-value patterns.

Frequently Asked Questions

What is the difference between / and //?

/ always performs true division and returns a float. // performs floor division and rounds the result down to the nearest whole number.

Why does 10 // 3 equal 3 and not 3.33?

Floor division discards the remainder and rounds down to the nearest whole number, giving 3.

What is the difference between == and is?

== compares whether two values are equal. is compares whether two variables refer to the exact same object in memory.

Do and and or always return True or False?

No. They return one of the actual operand values based on short-circuit evaluation, which is often True or False, but not always.

Do I need to memorize operator precedence?

Not fully — using parentheses to make your intent explicit is usually clearer and safer than relying on memorized precedence rules.

Key Takeaways

  • Arithmetic operators include + - * / // % **, with / for true division and // for floor division.
  • Comparison operators always produce a bool result.
  • and, or, and not combine conditions and use short-circuit evaluation, returning operand values.
  • Assignment operators like += and -= combine an operation with assignment.
  • is checks object identity, while == checks value equality.
  • in and not in test membership inside collections.
  • Operator precedence determines evaluation order — parentheses make intent explicit.

Summary

Operators are the tools you use to calculate, compare, combine conditions, and test relationships between values. You now know Python's full operator toolkit — arithmetic, comparison, logical, assignment, identity, and membership — along with how precedence determines the order expressions are evaluated.

Next, you will learn how to interact with the user directly through input() and control exactly how your programs display output with print().

Lesson 10 Completed
  • You can perform arithmetic and understand / versus //.
  • You can combine conditions using and, or, and not.
  • You understand the difference between == and is.
  • You are ready to learn about user input and output.
Next Lesson →

User Input & Output