Python Operators Explained: Complete Beginner's Guide (Chapter 3)
Vishal Yadav | Course Instructor
Imagine building a calculator, checking user login credentials, comparing product prices, or determining whether a value exists in a list. None of these tasks would be possible without operators. Operators are the building blocks that allow Python programs to perform calculations, make decisions, and manipulate data efficiently.
In Python, operators are special symbols or keywords used to perform operations on variables and values. Whether you're adding numbers, comparing values, or checking conditions, operators play a critical role in programming.
In this chapter, you will learn about Arithmetic Operators, Comparison Operators, Logical Operators, Assignment Operators, Membership Operators, and Identity Operators through practical examples and real-world scenarios.
Key Takeaway: Operators allow programs to perform calculations, compare values, evaluate conditions, and manipulate data efficiently.
What Are Operators in Python?
Operators are symbols or keywords that perform specific operations on operands (values or variables).
For example:
a = 10
b = 5
print(a + b)
Output:
15
Here:
- a and b are operands.
- + is the operator.
Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations.
Arithmetic Operators Table
- + Addition
- - Subtraction
- * Multiplication
- / Division
- // Floor Division
- % Modulus
- ** Exponentiation
Addition (+)
a = 10
b = 20
print(a + b)
Output:
30
Subtraction (-)
a = 20
b = 5
print(a - b)
Output:
15
Multiplication (*)
a = 5
b = 4
print(a * b)
Output:
20
Division (/)
a = 20
b = 4
print(a / b)
Output:
5.0
Floor Division (//)
Returns only the whole-number quotient.
print(17 // 3)
Output:
5
Modulus (%)
Returns the remainder.
print(17 % 3)
Output:
2
Exponentiation (**)
Raises a number to a power.
print(2 ** 3)
Output:
8
Practical Example: Calculate Area of a Rectangle
length = 10
width = 5
area = length * width
print("Area:", area)
Output:
Area: 50
Comparison Operators
Comparison operators compare two values and return either True or False.
Comparison Operators Table
- == Equal to
- != Not equal to
- > Greater than
- < Less than
- >= Greater than or equal to
- <= Less than or equal to
Equal To (==)
print(10 == 10)
Output:
True
Not Equal To (!=)
print(10 != 5)
Output:
True
Greater Than (>)
print(20 > 10)
Output:
True
Less Than (<)
print(5 < 10)
Output:
True
Greater Than or Equal To (>=)
print(18 >= 18)
Output:
True
Less Than or Equal To (<=)
print(15 <= 20)
Output:
True
Practical Example: Age Verification
age = 20
print(age >= 18)
Output:
True
Comparison operators are widely used in conditions, loops, and decision-making logic.
Logical Operators
Logical operators combine multiple conditions and return a Boolean result.
Logical Operators Table
- and
- or
- not
AND Operator
Returns True only if all conditions are True.
age = 25
has_id = True
print(age >= 18 and has_id)
Output:
True
OR Operator
Returns True if at least one condition is True.
is_admin = False
is_manager = True
print(is_admin or is_manager)
Output:
True
NOT Operator
Reverses a Boolean value.
is_logged_in = True
print(not is_logged_in)
Output:
False
Practical Example: Website Access
username_valid = True
password_valid = True
print(username_valid and password_valid)
Output:
True
Assignment Operators
Assignment operators are used to assign values to variables and update them efficiently.
Assignment Operators Table
- = Assign
- += Add and assign
- -= Subtract and assign
- *= Multiply and assign
- /= Divide and assign
- //= Floor divide and assign
- %= Modulus and assign
- **= Exponent and assign
Basic Assignment
score = 100
Add and Assign (+=)
score = 100
score += 50
print(score)
Output:
150
Subtract and Assign (-=)
score = 100
score -= 20
print(score)
Output:
80
Multiply and Assign (*=)
value = 10
value *= 3
print(value)
Output:
30
Practical Example: Shopping Cart
cart_total = 1000
cart_total += 250
print(cart_total)
Output:
1250
Membership Operators
Membership operators check whether a value exists within a sequence such as a string, list, tuple, or set.
Membership Operators Table
- in
- not in
Using IN
fruits = ["Apple
Vishal Yadav
A specialist dedicated to publishing high-quality, readable insights on technology, leadership, and digital growth.