Python Operators
✕1. Operators
- Imagine you are using a calculator. You enter two numbers: 5 and 3. Then you press different buttons: +, -, *, / to add, subtract, multiply and divide. Each button tells the calculator what action to perform. Similarly, in Python, operators are symbols or keywords that tell the computer what operation to perform on values or variables. 🧮 5 + 3 ➜ Add two numbers 🏷️ x = 5 ➜ Store value in a variable ⚖️ age >= 18 ➜ Compare values
1.1 Real World Scenario

- Operators are special symbols or keywords used to perform operations on variables and values.
num_1 = 5num_2 = 3total = num_1 + num_2print(total)# ➜ 8 Here,+is the operator that adds the values ofnum_1andnum_2.- Python has different types of operators. In this chapter, we will learn: ✅ Arithmetic Operators ✅ Assignment Operators ✅ Comparison Operators ✅ Logical Operators ✅ Membership Operators 📌 Other operators such as Bitwise and Identity operators are not discussed here.
1.2 Definition
1.3 Example
1.4 Types of Operators
2. Arithmetic Operators
- Operators that are used to perform mathematical operations on numeric data. They are similar to calculator buttons.
- + ➜ Addition: Calculates sum of two numbers - ➜ Subtraction: Calculates difference of two numbers * ➜ Multiplication: Calculates product of two numbers / ➜ Division: Calculates quotient of two numbers // ➜ Floor Division: Calculates whole number part after division % ➜ Modulus: Calculates remainder after division ** ➜ Exponent: Calculates power of a number
2.1 What are Arithmetic Operators?
2.2 Common Arithmetic Operators

num_1 = 10num_2 = 3addition = num_1 + num_2print(addition)# ➜ 13subtraction = num_1 - num_2print(subtraction)# ➜ 7multiplication = num_1 * num_2print(multiplication)# ➜ 30division = num_1 / num_2print(division)# ➜ 3.3333333333333335floor_division = num_1 // num_2print(floor_division)# ➜ 3remainder = num_1 % num_2print(remainder)# ➜ 1power = num_1 ** num_2print(power)# ➜ 1000- 📌 Division (/) always returns a float value, even if both numbers are integers.
Example:
print(10 / 2)# ➜ 5.0 📌 Floor division (//) returns the whole number part after division. Example:print(10 // 3)# ➜ 3 📌 % returns the remainder. Example:print(10 % 3)# ➜ 1 📌 ** is used for exponent/power. Example:print(2 ** 3)# ➜ 8 ⚠️ In Python, exponent is written using **, not ^. - Python follows mathematical order of operations:
Brackets → Exponent → Multiplication/Division → Addition/Subtraction
📌 You may know this as BODMAS from school — Python follows the same rule.
Example:
a = 10result = a + 3 * (4 - 1)print(result)# ➜ 19
2.3 Examples
2.4 Important Notes
2.5 Order of Operations
3. Assignment Operators
- Operators that are used to assign values to variables. They can also perform an operation and assign the result back to the variable. 🏷️ Assignment Operators help us store or update values.
- Assign a value to a variable. Variable on the left side of the operator gets the value on the right side.
Example:
x = 5This means the variablexnow holds the value5. - Assign the same value to multiple variables in a single statement.
Example:
x = y = z = 10This meansx,y, andzall hold the value10. - Assign multiple values to multiple variables in a single statement.
Example:
a, b, c = 1, 2, 3This meansaholds1,bholds2, andcholds3. - These operators perform an operation and assignment in one step.
Examples:
x += 5# Equivalent tox = x + 5x -= 5# Equivalent tox = x - 5x *= 5# Equivalent tox = x * 5x /= 5# Equivalent tox = x / 5x //= 5# Equivalent tox = x // 5x %= 5# Equivalent tox = x % 5x **= 5# Equivalent tox = x ** 5
3.1 What are Assignment Operators?
3.2 Simple Assignment
3.3 Multiple Assignment
3.4 Positional Assignment
3.5 Combined Assignment Operators
4. Comparison Operators
- Operators that are used to compare two values and return a boolean result (True or False). ⚖️ Comparison Operators help us check relationships between values.
- == ➜ Equal to: Checks if value of left is exactly same as that of right != ➜ Not equal to: Checks if value of left is not exactly same as that of right < ➜ Less than: Checks if value of left is less than value of right > ➜ Greater than: Checks if value of left is greater than value of right <= ➜ Less than or equal to: Checks if value of left is less than or equal to value of right >= ➜ Greater than or equal to: Checks if value of left is greater than or equal to value of right
4.1 What are Comparison Operators?
4.2 Common Comparison Operators

a = 5b = 3result = (a == b)print(result)# ➜ Falseresult = (a != b)print(result)# ➜ Trueresult = (a < b)print(result)# ➜ Falseresult = (a > b)print(result)# ➜ Trueresult = (a <= b)print(result)# ➜ Falseresult = (a >= b)print(result)# ➜ True- Imagine you are checking if you are eligible to vote. The minimum age to vote is 18.
age = 20is_eligible = (age >= 18)print(is_eligible)# ➜ True Ifagewas16, thenis_eligiblewould beFalse.
4.3 Examples
4.4 Real-Life Example
5. Logical Operators
- Operators that are used to combine multiple conditions. They return a boolean result: True or False. 🧠 Logical Operators help us make decisions using multiple conditions.
- and ➜ Returns True if both conditions are True or ➜ Returns True if at least one condition is True not ➜ Reverses the result
age = 20has_citizenship = Truecan_vote = age >= 18 and has_citizenship == Trueprint(can_vote)# ➜ True Here, both conditions must be True.
5.1 What are Logical Operators?
5.2 Common Logical Operators
5.3 and Operator

is_saturday = Trueis_holiday = Falsecan_rest = is_saturday or is_holidayprint(can_rest)# ➜ True Here, at least one condition must be True.
5.4 or Operator

is_logged_in = Falseshould_login = not is_logged_inprint(should_login)# ➜ True Here, the result is reversed.
5.5 not Operator

6. Membership Operators
- Membership operators are used to check whether a value exists inside a sequence such as: String, List, Tuple, Set, Dictionary and return a boolean result: True or False. 🔍 Membership Operators help us search inside a collection.
- in (🔎✅) ➜ Returns True if the value is found in the sequence not in (🔎🚫) ➜ Returns True if the value is not found in the sequence
my_string = "Hello World"result = "H" in my_stringprint(result)# ➜ Trueresult = "z" not in my_stringprint(result)# ➜ Truemy_list = ["apple", "cat", "orange"]result = "apple" in my_listprint(result)# ➜ Trueresult = "cat" not in my_listprint(result)# ➜ False You have a guest list as:guest_list = ["Sita", "Ram", "Hari"]. Check if sita is invited.guest_list = ["Sita", "Ram", "Hari"]is_sita_invited = "Sita" in guest_listprint(is_sita_invited)# ➜ True
6.1 What are Membership Operators?
6.2 Common Membership Operators
6.3 Example with String
6.4 Example with List
Practice QuestionsNot started
Arithmetic Operators
Question 1 of 7
- Display their data types
- Display their sum
- Display their difference
- Display their product
- Display their division
- Display their floor division
- Display remainder when num_1 is divided by num_2
- Display num_1 raised to the power of 2
- Convert both
num_1andnum_2into string and display sum again
Create two variablesnum_1as7,num_2as3.5and:Arithmetic Expressions
Question 2 of 7
- num_3 + num_4 * 2
- (num_3 + num_4) * 2
- (num_3 - num_4) * 2
- num_3 ** num_4
- num_3 // num_4
- num_3 % num_4
Assignnum_3as 10 andnum_4as 3. Calculate and print the result of:Assignment Operators
Question 3 of 7
- Calculate total marks
- Calculate average marks
- Print both total and average
Assignmark_1as 78,mark_2as 85, andmark_3as 92.Combined Assignment Operators
Question 4 of 7
- Add 5 to weight_1 and reassign it
- Subtract 10 from weight_2 and reassign it
- Multiply weight_3 by 1.1 and reassign it
- Divide weight_4 by 2 and reassign it
- Print all updated values
Assignweight_1,weight_2,weight_3andweight_4as 70Comparison & Logical Operators
Question 5 of 7
- Check if your age is equal to 16
- Check if your age is not 70
- Check if you are a child (
age less than 13) - Check if you are an adult (
age 20 or above) - Check if you are eligible to vote (
18+ can vote) - Check if you are a teenager (
age between 13 and 19) - Check if you are in dependent age group (
below 5 and over 65 considered passive) - Check if you are eligible for driving license (
age between 18 and 65)
Assign your age to variablemy_ageand:Membership Operators
Question 6 of 7
- Check if letter "a" is in your name
- Check if letter "a" is not in your name
Assign variablemy_nameas your name and check if:Membership with Collections
Question 7 of 7
- Check if movie "Avatar" is in your collection
- Check if movie "Inception" is not in your collection
- Repeat the same exercise by creating
movie_collectionas atuple - Repeat the same exercise by creating
movie_collectionas aset
Create a list of your favorite movies:movie_collection = ["Avatar", "Titanic", "Interstellar"]
