🐍 Python for Everyone

← Back to exercises

Chapter 3 Practice Bank 35-45 min, hands-on

Practice Bank: Operators

A deeper set of practice problems, organized by topic, for extra reps beyond the main exercises — including scenario-based problems written in the same style you'll meet in real interviews. Still uses only what Chapters 1-3 covered: print(), variables, basic types, and this chapter's arithmetic/assignment, relational/logical, bitwise, and is/in operators. No if, loops, or functions yet — conditions are just printed, not used to branch.

How to run these Download starter.py below, then run it from a terminal with python3 starter.py.

Topic 1: Arithmetic & assignment

1 All seven arithmetic operators

Find # TODO 1.1. Given a = 17 and b = 5, print the result of +, -, *, /, //, %, and **.

2 Every compound assignment shortcut

Find # TODO 1.2. Starting from score = 50, apply +=, -=, *=, //=, %=, and **= in turn, printing after each one.

3 Celsius to Fahrenheit

Find # TODO 1.3. Given celsius = 25, convert it with celsius * 9 / 5 + 32 and print the result.

4 Debug the Code

Find # TODO 1.4. This is supposed to print the square of -3 (which is 9), but a precedence gotcha between unary minus and ** makes it print -9 instead. Fix it with parentheses.

5 Area and perimeter

Find # TODO 1.5. Given length = 8 and width = 3, compute and print the area and perimeter of the rectangle.

💼 Scenario

Grocery bill split

Find # TODO 1.A. Split a bill of 1875 cents evenly among 4 friends using // for each person's share and % for the leftover cents.

💼 Interview Prep

Does / ever return an int?

Find # TODO 1.B. Prove that / always returns a float in Python, even when the division comes out exact, by printing 10 / 2.

Topic 2: Relational & logical

1 All six relational operators

Find # TODO 2.1. Given x = 8 and y = 3, print the result of ==, !=, <, >, <=, and >=.

2 Range check

Find # TODO 2.2. Given age = 25, print a single expression with and checking whether it's between 18 and 65 (inclusive).

3 Too hot or too cold

Find # TODO 2.3. Given temperature = 95, print a single expression with or checking whether it's over 90 or under 32.

4 Flip a boolean

Find # TODO 2.4. Given is_raining = False, print not is_raining.

5 Combined eligibility check

Find # TODO 2.5. Given credit_score = 720 and has_income = True, print a single and expression checking both conditions.

💼 Scenario

Login gatekeeper

Find # TODO 2.A. Print whether login is allowed based on is_active_account and correct_password, both set to True.

💼 Interview Prep

Prove short-circuit evaluation

Find # TODO 2.B. Given x = 0, print x != 0 and 10 / x > 1 and explain in a comment why this doesn't crash.

Topic 3: Bitwise

1 AND, OR, XOR

Find # TODO 3.1. Given a = 5 and b = 6, print a & b, a | b, and a ^ b.

2 Shift left and right

Find # TODO 3.2. Using the same a = 5, print a << 1 and a >> 1.

3 Bitwise NOT

Find # TODO 3.3. Print ~a and add a comment explaining what it does to every bit.

4 Combine flags

Find # TODO 3.4. Given read = 4, write = 2, execute = 1, combine read and write with | and print it.

5 Check a flag

Find # TODO 3.5. Using the permissions value from TODO 3.4, check whether the write flag is set with &.

💼 Scenario

Unix file permissions

Find # TODO 3.A. Combine read, write, and execute into one full_permissions value using |, just like Unix rwx permissions.

💼 Interview Prep

Why powers of 2?

Find # TODO 3.B. Explain why bit flags are assigned as powers of 2 instead of sequential numbers, then prove it by combining and checking flags with | and &.

Topic 4: Special: is / in

1 Identity vs. equality

Find # TODO 4.1. Given a = 10 and b = 10, print a is b and a == b.

2 Same object, two names

Find # TODO 4.2. Given name = "Ada" and name_alias = name, print name is name_alias.

3 Substring membership

Find # TODO 4.3. Given word = "python", print whether "th" is inside it using in.

4 Absence check

Find # TODO 4.4. Using the same word, print whether "z" is NOT inside it using not in.

5 PIN digit check

Find # TODO 4.5. Given pin = "4821" and digit = "8", print whether digit is in pin.

💼 Scenario

Access code validator

Find # TODO 4.A. Given allowed_chars = "ABCDEF0123456789" and entered_char = "F", print whether entered_char is in allowed_chars.

💼 Interview Prep

Is "is" safe for numbers?

Find # TODO 4.B. Given x = 300 and y = 300, print x == y and x is y, then explain in a comment why is isn't guaranteed to match == for integers this large.

Checking your work Compare your output against solution.py. Your exact wording doesn't need to match — the goal is that your program runs without errors and does what each TODO asks.
← Exercises Interview Questions →