Python Operators

1. Operators

    1.1 Real World Scenario
    1. 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
Operator Analogy
Operator Analogy
    1.2 Definition
    1. Operators are special symbols or keywords used to perform operations on variables and values.
    1.3 Example
    1. num_1 = 5 num_2 = 3total = num_1 + num_2 print(total) # ➜ 8 Here, + is the operator that adds the values of num_1 and num_2.
    1.4 Types of Operators
    1. 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.

2. Arithmetic Operators

    2.1 What are Arithmetic Operators?
    1. Operators that are used to perform mathematical operations on numeric data. They are similar to calculator buttons.
    2.2 Common Arithmetic Operators
    1. + ➜ 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
Python Arithmetic Operators
Python Arithmetic Operators
    2.3 Examples
    1. num_1 = 10 num_2 = 3addition = num_1 + num_2 print(addition) # ➜ 13 subtraction = num_1 - num_2 print(subtraction) # ➜ 7 multiplication = num_1 * num_2 print(multiplication) # ➜ 30 division = num_1 / num_2 print(division) # ➜ 3.3333333333333335 floor_division = num_1 // num_2 print(floor_division) # ➜ 3 remainder = num_1 % num_2 print(remainder) # ➜ 1 power = num_1 ** num_2 print(power) # ➜ 1000
    2.4 Important Notes
    1. 📌 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 ^.
    2.5 Order of Operations
    1. 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 = 10 result = a + 3 * (4 - 1) print(result) # ➜ 19

3. Assignment Operators

    3.1 What are Assignment Operators?
    1. 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.
    3.2 Simple Assignment
    1. Assign a value to a variable. Variable on the left side of the operator gets the value on the right side. Example: x = 5 This means the variable x now holds the value 5.
    3.3 Multiple Assignment
    1. Assign the same value to multiple variables in a single statement. Example: x = y = z = 10 This means x, y, and z all hold the value 10.
    3.4 Positional Assignment
    1. Assign multiple values to multiple variables in a single statement. Example: a, b, c = 1, 2, 3 This means a holds 1, b holds 2, and c holds 3.
    3.5 Combined Assignment Operators
    1. These operators perform an operation and assignment in one step. Examples: x += 5 # Equivalent to x = x + 5 x -= 5 # Equivalent to x = x - 5 x *= 5 # Equivalent to x = x * 5 x /= 5 # Equivalent to x = x / 5 x //= 5 # Equivalent to x = x // 5 x %= 5 # Equivalent to x = x % 5 x **= 5 # Equivalent to x = x ** 5

4. Comparison Operators

    4.1 What are Comparison Operators?
    1. Operators that are used to compare two values and return a boolean result (True or False). ⚖️ Comparison Operators help us check relationships between values.
    4.2 Common Comparison Operators
    1. == ➜ 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
Python Comparison Operators
Python Comparison Operators
    4.3 Examples
    1. a = 5 b = 3result = (a == b) print(result) # ➜ False result = (a != b) print(result) # ➜ True result = (a < b) print(result) # ➜ False result = (a > b) print(result) # ➜ True result = (a <= b) print(result) # ➜ False result = (a >= b) print(result) # ➜ True
    4.4 Real-Life Example
    1. Imagine you are checking if you are eligible to vote. The minimum age to vote is 18. age = 20 is_eligible = (age >= 18) print(is_eligible) # ➜ True If age was 16, then is_eligible would be False.

5. Logical Operators

    5.1 What are Logical Operators?
    1. 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.
    5.2 Common Logical Operators
    1. and ➜ Returns True if both conditions are True or ➜ Returns True if at least one condition is True not ➜ Reverses the result
    5.3 and Operator
    1. age = 20 has_citizenship = Truecan_vote = age >= 18 and has_citizenship == True print(can_vote) # ➜ True Here, both conditions must be True.
Logical AND Operator
Logical AND Operator (Like Multiplication)
    5.4 or Operator
    1. is_saturday = True is_holiday = Falsecan_rest = is_saturday or is_holiday print(can_rest) # ➜ True Here, at least one condition must be True.
Logical OR Operator
Logical OR Operator (Like Addition)
    5.5 not Operator
    1. is_logged_in = Falseshould_login = not is_logged_in print(should_login) # ➜ True Here, the result is reversed.
Logical NOT Operator
Logical NOT Operator

6. Membership Operators

    6.1 What are Membership Operators?
    1. 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.
    6.2 Common Membership Operators
    1. in (🔎✅) ➜ Returns True if the value is found in the sequence not in (🔎🚫) ➜ Returns True if the value is not found in the sequence
    6.3 Example with String
    1. my_string = "Hello World"result = "H" in my_string print(result) # ➜ True result = "z" not in my_string print(result) # ➜ True
    6.4 Example with List
    1. my_list = ["apple", "cat", "orange"]result = "apple" in my_list print(result) # ➜ True result = "cat" not in my_list print(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_list print(is_sita_invited) # ➜ True

Practice QuestionsNot started

  1. Arithmetic Operators

    Question 1 of 7

      Create two variables num_1 as 7, num_2 as 3.5 and:
      1. Display their data types
      2. Display their sum
      3. Display their difference
      4. Display their product
      5. Display their division
      6. Display their floor division
      7. Display remainder when num_1 is divided by num_2
      8. Display num_1 raised to the power of 2
      9. Convert both num_1 and num_2 into string and display sum again
  2. Arithmetic Expressions

    Question 2 of 7

      Assign num_3 as 10 and num_4 as 3. Calculate and print the result of:
      1. num_3 + num_4 * 2
      2. (num_3 + num_4) * 2
      3. (num_3 - num_4) * 2
      4. num_3 ** num_4
      5. num_3 // num_4
      6. num_3 % num_4
  3. Assignment Operators

    Question 3 of 7

      Assign mark_1 as 78, mark_2 as 85, and mark_3 as 92.
      1. Calculate total marks
      2. Calculate average marks
      3. Print both total and average
  4. Combined Assignment Operators

    Question 4 of 7

      Assign weight_1, weight_2, weight_3 and weight_4 as 70
      1. Add 5 to weight_1 and reassign it
      2. Subtract 10 from weight_2 and reassign it
      3. Multiply weight_3 by 1.1 and reassign it
      4. Divide weight_4 by 2 and reassign it
      5. Print all updated values
  5. Comparison & Logical Operators

    Question 5 of 7

      Assign your age to variable my_age and:
      1. Check if your age is equal to 16
      2. Check if your age is not 70
      3. Check if you are a child (age less than 13)
      4. Check if you are an adult (age 20 or above)
      5. Check if you are eligible to vote (18+ can vote)
      6. Check if you are a teenager (age between 13 and 19)
      7. Check if you are in dependent age group (below 5 and over 65 considered passive)
      8. Check if you are eligible for driving license (age between 18 and 65)
  6. Membership Operators

    Question 6 of 7

      Assign variable my_name as your name and check if:
      1. Check if letter "a" is in your name
      2. Check if letter "a" is not in your name
  7. Membership with Collections

    Question 7 of 7

      Create a list of your favorite movies: movie_collection = ["Avatar", "Titanic", "Interstellar"]
      1. Check if movie "Avatar" is in your collection
      2. Check if movie "Inception" is not in your collection
    • Repeat the same exercise by creating movie_collection as a tuple
    • Repeat the same exercise by creating movie_collection as a set