Python Programming from First Principles: Learn to Code, Think Computationally, and Solve Real-World Problems
PDFEPUB
BOOK 000 · CHAPTER 2 · Operators in Python

Operators in Python

On this pageArithmetic OperatorsComparison OperatorsLogical OperatorsAssignment OperatorsIdentity OperatorsMembership OperatorsMini Project — Discount Eligibility Checker

Operators are special symbols that tell Python to perform specific actions on values. They are the building blocks of all expressions — combining variables, numbers, and text to produce results.

In this section, you will learn about the main categories of operators in Python: arithmetic, comparison, logical, assignment, identity, and membership.

Arithmetic Operators

Arithmetic operators perform basic mathematical calculations. They are among the most frequently used operators in any program — from scientific analysis to financial applications.

print(10 + 5)   # Addition
print(10 - 5)   # Subtraction
print(10 * 5)   # Multiplication
print(10 / 3)   # Division (returns float)
print(10 // 3)  # Floor division (whole number)
print(10 % 3)   # Modulus (remainder)
print(2 ** 3)   # Exponentiation (power)
Listing 85 — Arithmetic Operators

Output

15
5
50
3.3333333333333335
3
1
8

These operators follow standard mathematical rules, including parentheses () for grouping, so you can write expressions such as (a + b) * c.

Comparison Operators

Comparison operators are used to compare values. They always return a Boolean result — either True or False. These are essential for decision-making in your programs.

Operator Meaning Example Result
== Equal to 5 == 5 True
!= Not equal to 5 != 3 True
> Greater than 7 > 10 False
< Less than 2 < 5 True
>= Greater than or equal to 10 >= 10 True
<= Less than or equal to 4 <= 3 False
Table 4 — Comparison Operators

Example:

x = 10
y = 7
print(x > y)
print(x == 10)
Listing 86 — Comparison Operators

Output

True
True

Logical Operators

Logical operators combine multiple Boolean expressions to form complex conditions. They are especially useful when you need to check several things at once.

Operator Meaning Example Result
and True if both conditions are True (5 > 3) and (8 > 6) True
or True if at least one condition is True (5 > 10) or (8 > 6) True
not Inverts the Boolean value not (5 > 3) False
Table 5 — Logical Operators

Example:

age = 20
has_id = True
print(age >= 18 and has_id)
Listing 87 — Logical Operators

Output

True

This shows how logical operators can combine conditions in real-world decision-making — like verifying if someone can enter an event.

Assignment Operators

Assignment operators store values in variables and can also perform arithmetic in the same step. They make your code shorter and more expressive.

x = 10     # simple assignment
x += 5     # same as x = x + 5
x -= 3     # same as x = x - 3
x *= 2     # same as x = x * 2
x /= 4     # same as x = x / 4
print(x)
Listing 88 — Assignment Operators

Output

6.0

Each operation calculates a result and assigns it back to x, which is useful for totals and incremental changes.

Identity Operators

Identity operators check whether two names refer to the same object. This differs from comparing their values. They are often used when working with objects and references.

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

print(a is b)     # True - both refer to the same list
print(a is c)     # False - same contents, different objects
print(a is not c) # True
Listing 89 — Identity Operators

Output

True
False
True

The keyword is tests for identity (same object), while == tests for equality (same value).

Membership Operators

Membership operators test whether a value is present inside a sequence like a list or a string. They return either True or False.

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

print("banana" in fruits)     # True
print("orange" not in fruits) # True
Listing 90 — Membership Operators

This is useful in all sorts of data-related tasks — for example, checking if a record exists in a dataset.

Mini Project — Discount Eligibility Checker

Write a program that determines if a customer qualifies for a discount based on their age and membership status.

Instructions:

  1. Ask for the customer’s age and whether they are a member (yes or no).
  2. Apply the rule: If the customer is under 18 or a member, they get a discount.
  3. Display the result.

Example Run:

Enter your age: 16
Are you a member? (yes/no): no
You qualify for a discount!
Listing 91 — Mini Project — Discount Eligibility Checker