Operators
Operators are special symbols and keywords that help perform an operation or computation on one or more values or variables.
The values or variables that the operator works on are called operands.
Operators are special symbols and keywords that help perform an operation or computation on one or more values or variables.
The values or variables that the operator works on are called operands.
Comparison operators evaluate two values, variables, or expressions, and always return a boolean result (True or False)
These help form conditional statements allowing your programs to make decisions based on - which we will learn later
Since you haven't learned if statements yet, you use the clever boolean math trick: True equals 1 and False equals 0
Write a Python program that does the following:
Ask for Input:
Ask the user for the first number (decimal number/float).
Ask the user for the second number (decimal number/float).
Calculate:
Use comparison operators and boolean math multiplication to isolate the biggest number:
Print the Summary:
Use an f-string to display both numbers and the final maximum result.
Use a dashed line ("-" * 25) to make your output look neat.
Example of What Your Program Should Do:
--- Maximum Number Finder ---
Enter first number: 45.5
Enter second number: 92.0
-------------------------
Number 1: 45.5
Number 2: 92.0
The biggest number is: 92.0
-------------------------
Write a Python program that does the following:
Ask for Input:
Ask the user for the first number (decimal number/float).
Ask the user for the second number (decimal number/float).
Calculate:
Use comparison operators and boolean math multiplication to isolate the smallest number: smallest_number = (num1 * (num1 <= num2)) + (num2 * (num2 < num1))
Hint: If num1 is smaller, the first check becomes 1 and the second becomes 0, making the equation keep only num1.
Print the Summary:
Use an f-string to display both numbers and the final minimum result.
Use a dashed line ("-" * 25) to make your output look neat.
--- Minimum Number Finder ---
Enter first number: 45.5
Enter second number: 92.0
-------------------------
Number 1: 45.5
Number 2: 92.0
The smallest number is: 45.5
-------------------------
Write a Python program that does the following:
Ask for Input:
Ask the user for the first number (decimal number/float).
Ask the user for the second number (decimal number/float).
Ask the user for the third number (decimal number/float).
Calculate:
Create boolean checks to see which number is the biggest:
is_num1_max = (num1 >= num2) and (num1 >= num3)
is_num2_max = (num2 > num1) and (num2 >= num3)
is_num3_max = (num3 > num1) and (num3 > num2)
Multiply each number by its boolean check and add them together to find the winner: biggest_number = (num1 * is_num1_max) + (num2 * is_num2_max) + (num3 * is_num3_max)
Hint: Only one of those boolean checks will be True (1), while the others will be False (0), so only the biggest number survives the addition!
Print the Summary:
Use an f-string to display all three numbers and the final maximum result.
Use a dashed line ("-" * 25) to make your output look neat.
--- Triple Max Number Finder ---
Enter first number: 15.5
Enter second number: 42.0
Enter third number: 30.0
-------------------------
The numbers entered are: 15.5, 42.0, 30.0
The biggest number is: 42.0
-------------------------
Imagine you want to build a quick grade tracker for a student taking three tests. The passing mark is 50.
Write a Python program that does the following:
Ask for Input:
Ask the user for the score in Math (whole number/integer).
Ask the user for the score in Science (whole number/integer).
Ask the user for the score in English (whole number/integer).
Calculate:
Check if each score meets or exceeds the passing mark of 50:
passed1 = math_score >= 50
passed2 = science_score >= 50
passed3 = english_score >= 50
Add the boolean results together to find the total passed count: total_passed = passed1 + passed2 + passed3
Hint: Python treats True as 1 and False as 0, so adding them gives you an exact count!
Print the Summary:
Use an f-string to display individual pass/fail results and the total count.
Use a dashed line ("-" * 25) to make your output look neat.
--- Pass Fail Checker ---
Enter score for Math: 45
Enter score for Science: 82
Enter score for English: 60
-------------------------
Passed Math?: False
Passed Science?: True
Passed English?: True
Total subjects passed: 2 out of 3
-------------------------
Scenario
Imagine you are building a checkout system for an online store. The store offers a 10% discount, but only if the customer's shopping cart total is greater than or equal to $100.00.
Write a Python program that does the following:
Ask for Input:
Ask the user for the customer name (text).
Ask the user for the shopping cart total in dollars (decimal number/float).
Calculate:
Check if the cart total meets or exceeds the $100.00 threshold: qualifies_for_discount = cart_total >= 100.0
Calculate the discount amount by multiplying the 10% discount value by the boolean check (1 or 0): discount_applied = (cart_total * 0.10) * qualifies_for_discount
Subtract the discount from the cart total to find the final price: final_price = cart_total - discount_applied
Print the Summary:
Use an f-string to display the qualification status, discount amount, and final total.
Use a dashed line ("-" * 25) to make your output look neat.
--- Smart Store Checkout ---
Enter customer name: Alice
Enter shopping cart total ($): 120.0
-------------------------
Customer: Alice
Qualifies for 10% Discount?: True
Discount Amount: $12.0
Final Total to Pay: $108.0
-------------------------
Write a Python program that asks the user to enter an integer. The program must determine whether the entered number is even and display the result (True or False) directly on the screen.
Enter an integer: 8
True
You are building a simple digital wallet program.
The program should:
Ask for the starting balance.
Ask for a deposit and add it to the balance.
Ask for an expense.
Check whether the user can afford that expense using a comparison operator.
Display the remaining balance.
Enter your starting balance: ₹1000
Enter deposit amount: ₹500
Enter expense: ₹800
Can you spend ₹800? True
Remaining balance will be ₹700