Operators — Practice Questions
50 questions. Try each one yourself before checking the answer.
Create two variables a = 15 and b = 4. Print the result of a + b.
Using the same a = 15 and b = 4, print the result of a - b.
Using a = 15 and b = 4, print the result of a * b.
Using a = 15 and b = 4, print both a / b (true division) and a // b (floor division), each with a label.
Using a = 15 and b = 4, print the result of a % b (the remainder).
Create base = 2 and exponent = 10. Print base ** exponent.
Create x = 10. Use the += operator to add 5 to it, then print x.
Create a = 5 and b = 8. Print the results of a == b and a != b.
Create a = 7 and b = 3. Print the results of a > b, a < b, a >= b, and a <= b.
Create two booleans, is_sunny = True and is_weekend = False. Print the results of is_sunny and is_weekend, is_sunny or is_weekend, and not is_sunny.
Start with value = 100. Apply -=20, then *=2, then /=4, printing value after each step.
Start with value = 17. Apply //=3, then **=2, then %=5, printing value after each step.
Ask the user for two numbers and print whether the first is greater than the second.
Ask the user for their age and print a boolean is_teen that is True only if the age is between 13 and 19 inclusive, using and.
Create word = "python". Print whether "y" is in the word.
Using the same word = "python", print whether "z" is not in the word.
Create a list allowed_ids = [101, 102, 103]. Print whether 105 is in that list.
Create list1 = [1, 2, 3] and list2 = [1, 2, 3]. Print list1 == list2 and list1 is list2. Why do they differ?
Create value = None. Print value is None and value == None. Which one is the idiomatic way to check for None, and why?
Evaluate 10 + 2 * 3 and print the result. Add a comment explaining the order of operations.
Ask the user for a cart total and print a boolean qualifies_for_free_shipping that is True when the total is over 500.
Ask the user for a password and print whether it meets a minimum length of 8 characters, using len() and >=.
Ask the user for a number and print whether it's even, using %.
Ask the user for a year and print whether it's divisible by 4.
Ask the user for a number and print whether it falls between 10 and 20 (inclusive), using a single chained comparison.
Ask for the user's age and whether they have an ID (yes/no). Print is_eligible, True only if they're 18+ and have an ID.
Ask whether the user is a member (yes/no) and their cart total. Print gets_discount, True if they're a member or their total exceeds 1000.
Create shopping_list = ["milk", "bread", "eggs"]. Ask the user for an item and print whether it's in the list.
The following code is meant to check if a score equals a perfect score, but the comparison doesn't reliably work as intended. Find and fix the bug.
score = int(input("Enter your score: "))
target = 100
print("Perfect score:", score is target)Create is_admin = False and is_owner = True. Print the result of not is_admin and is_owner or False, and explain in a comment how not, and, and or are grouped.
Evaluate 2 + 3 * 4 ** 2 - 1 and print the result. Add a comment breaking down the exact order of operations.
Evaluate 5 + 3 > 2 * 3 and print the result. Explain in a comment why arithmetic operators run before the comparison.
Print 5 > 3 > 1 and (5 > 3) > 1 on separate lines. They give different results — explain why in a comment.
Print 0.1 + 0.2 == 0.3. It's not True — print two working alternatives that correctly check "close enough" equality for floats.
Create a = 256 and b = 256, then print a is b. Create c = 1000 and d = 1000, then print c is d. Explain the difference in a comment.
Create p = True and q = False. Print p and q alongside p & q, and p or q alongside p | q. They give the same results for booleans — explain in a comment what actually differs between them.
Create text = "hello world" and words = ["hello", "world", "python"]. Print whether "hello" is in each of them, showing that in works the same way across different sequence types.
Create message = "Py" and use += to make it "Python". Separately, create border = "-" and use *= to repeat it 10 times. Print both.
Use the walrus operator (:=) to assign 10 to n and print it in the same expression, then print n again on its own.
Create allowed_scores = [90, 95, 100]. Ask the user for a score and print is_valid, True only if the score is in the list and greater than 90.
Build a loan "Eligibility Checker". Ask for age, income, and credit_score. Print three individual boolean checks (age >= 21, income >= 30000, credit_score >= 700) and a combined all_criteria_met result.
Build "Password Strength Flags". Ask for a password and print whether it's at least 8 characters, whether it contains no spaces, and whether both are true (is_strong).
Build a "Shopping Cart Discount Engine". Ask for the cart total and membership status. A discount applies if the total is >= 1000 or the shopper is a member. Print a formatted summary.
Build a "Number Property Reporter". Ask for a number and print whether it's even, positive, within the range 1–100, and a perfect square (compare number ** 0.5 to its int() version).
Build "Access Control Flags". Given a fixed allowed_users list and a fixed correct password, ask for username, password, and whether the account is active, then print each check plus a combined access_granted.
Demonstrate short-circuit evaluation: show that (x != 0) and (10 / x > 1) doesn't crash when x = 0, and that (y > 0) or (10 / 0 > 1) doesn't crash when y = 5. Explain why in a comment.
Demonstrate one scenario where using is instead of == causes a bug, and one scenario where == works but is is the more correct/idiomatic choice.
Ask for a number and print three flags: divisible by 3, divisible by 5, and divisible by both — using % and and.
Given score = 85, print a single boolean is_valid_percentage that is True only if score is an int and falls between 0 and 100 inclusive, combining isinstance(), a chained comparison, and and in one expression.
Build an "Operator Mastery Report". Ask for two numbers a and b, then print every arithmetic operator (+ - * / // % **), every comparison operator (== != > < >= <=), and three logical combinations (a>0 and b>0, a>0 or b>0, not(a==b)) — all clearly labeled in one aligned report.
Still stuck on something?
Book a free 1-on-1 session and we'll work through it together.
Book a Free Session