</>
Vizly

Python Operators & String Formatting

July 3, 20263 min
PythonFundamentalsPracticeBeginner

Chapter 2 of Python Fundamentals. 14 practice drills on operators, short-circuit logic, the walrus operator, and f-strings. Predict the output before you run anything.

The rules, again

Same game as chapter 1: read the question, commit to a prediction, run it, and only then open the answer. Wrong prediction means the drill goes on your redo list for three days from now.

This chapter is where Python quietly stops behaving like other languages. Chained comparisons do not exist in C or JavaScript. and returns an operand, not a bool. & and and are different animals. And f-strings pack a small formatting language behind that colon.

Learn the concepts first

Rusty on the basics? Work through Basic Operators and String Formatting on learnpython.org first, then come back and drill.


One idea before you start

Half of this chapter's traps come from a single mechanism: Python evaluates logical operators lazily and hands back the operand itself.

Definition

When Python evaluates x and y, it stops at x if x is falsy and never touches y. With or, it stops at the first truthy value. The result is the operand where evaluation stopped, kept as-is, not converted to True or False.


The drills

Click a question to open it. The answer stays hidden until you ask for it. Predict first.

14 drills. Predict first, then open the answer

Want more drills?

When these stop surprising you, let an AI generate fresh ones. Copy this prompt into ChatGPT, Claude, or any assistant you like:

You are my Python drill coach. Quiz me on: operators, chained
comparisons, short-circuit and/or, bitwise vs logical operators,
operator precedence, the walrus operator, and f-string formatting.
 
Rules:
- Ask ONE question at a time. Show a short code snippet and ask me to
  predict the output (or the error) before running it.
- Wait for my answer. Do not reveal anything until I reply.
- If I am right, say so in one line and raise the difficulty slightly.
- If I am wrong, explain the why in 2-3 sentences, then give me a
  similar question later to check I really got it.
- Prefer tricky edge cases over textbook questions: 5 > 1 == True,
  True & False == False, 0 or "default", "Z" < "a", f"{x=:.2f}",
  "3" * 2, print(sep=, end=).
- Keep score. After 10 questions, list the concepts I missed so I can
  add them to my redo list.
 
Start with a medium question.

The predict-first rule matters more than the tool. Whatever generates the questions, always commit to an answer before you look.


Done?

Score yourself honestly and queue up your misses. The precedence trap (True & False == False) and the lexicographic one ("10" < "9") are the two most people get wrong. If that was you, you are in good company, redo them Friday.

Next chapter: control flow, where for ... else means something nobody expects and match is more than a switch statement.

Edit this page on GitHubโ†—