Fixed Top Ad (Local Preview)800x90 • Slot 6608427872

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 ✅ Bitwise Operators

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 Arithmetic Operators in Python
OperatorDescriptionExample
+Addition: Calculates sum of two numbers5 + 3 ➜ 8
-Subtraction: Calculates difference of two numbers5 - 3 ➜ 2
*Multiplication: Calculates product of two numbers5 * 3 ➜ 15
/Division: Calculates quotient of two numbers5 / 3 ➜ 1.667
//Floor Division: Calculates whole number part after division5 // 3 ➜ 1
%Modulus: Calculates remainder after division5 % 3 ➜ 2
**Exponent: Calculates power of a number5 ** 3 ➜ 125
Arithmetic Operators in Python
    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 mult = num_1 * num_2 print(mult) # ➜ 30 division = num_1 / num_2 print(division) # ➜ 3.333 floor_div = num_1 // num_2 print(floor_div) # ➜ 3 remainder = num_1 % num_2 print(remainder) # ➜ 1 power = num_1 ** num_2 print(power) # ➜ 1000
Python Arithmetic Operators
Python Arithmetic Operators
    2.4 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: n1 = 10 result = n1 + 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
    1. Combine an arithmetic operation with assignment. It updates the variable with the result of the operation. Example: x += 5 is equivalent to x = x + 5.
Combined Assignment Operators
OperatorExampleLonger Form
+=x += 5x = x + 5
-=x -= 5x = x - 5
*=x *= 5x = x * 5
/=x /= 5x = x / 5
//=x //= 5x = x // 5
%=x %= 5x = x % 5
**=x **= 5x = x ** 5
Combined Assignment Operators

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 Python Comparison Operators
OperatorDescriptionExample
==Equal to: Checks if value of left is exactly same as that of right5 == 5 ➜ True
!=Not equal to: Checks if value of left is not exactly same as that of right5 != 3 ➜ True
<Less than: Checks if value of left is less than value of right3 < 5 ➜ True
>Greater than: Checks if value of left is greater than value of right5 > 3 ➜ True
<=Less than or equal to: Checks if value of left is less than or equal to value of right3 <= 5 ➜ True
>=Greater than or equal to: Checks if value of left is greater than or equal to value of right5 >= 3 ➜ True
Python Comparison Operators
    4.3 Examples
    1. n1 = 5 n2 = 3is_same = (n1 == n2) print(is_same) # ➜ False is_different = (n1 != n2) print(is_different) # ➜ True is_less = (n1 < n2) print(is_less) # ➜ False is_more = (n1 > n2) print(is_more) # ➜ True is_less_equal = (n1 <= n2) print(is_less_equal) # ➜ False is_more_equal = (n1 >= n2) print(is_more_equal) # ➜ True
Python Comparison Operators
Python Comparison Operators
    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. For example, a person is eligible to vote only if they meet the minimum age requirement and are a citizen of the country. Logical operators check these conditions and return a Boolean value: 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)
Truth Table for Logical AND Operator
Condition 1Condition 2Condition 1 and Condition 2
TrueTrueTrue
TrueFalseFalse
FalseTrueFalse
FalseFalseFalse
Truth Table for Logical AND Operator
    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)
Truth Table for Logical OR Operator
Condition 1Condition 2Condition 1 or Condition 2
TrueTrueTrue
TrueFalseTrue
FalseTrueTrue
FalseFalseFalse
Truth Table for Logical OR Operator
    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
Truth Table for Logical NOT Operator
Conditionnot Condition
TrueFalse
FalseTrue
Truth Table for 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"]result = "apple" in my_list print(result) # ➜ True result = "cat" not in my_list print(result) # ➜ False
    Sample Exercise:
    1. You have a guest list as:guests = ["Sita", "Ram", "Hari"]. Check if sita is invited.guests = ["Sita", "Ram", "Hari"]is_invited = "Sita" in guests print(is_invited) # ➜ True
Ad PlaceholderSlot: 7421026683

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
Ad PlaceholderSlot: 5413242224