Embercode
✕
ConditionalsQuick quiz

Pattern matching and the walrus operator

6 questions. Covers isinstance, match patterns and the walrus operator in Python.

Questions 1 to 5 of 6

  1. 1. What does this program print when show(42) runs? ``python def show(value): if isinstance(value, str): text = value print("after if:", text) show(42) ``
  2. 2. Why does isinstance(value, list[str]) raise TypeError, even though list[str] is valid Python? ``python value = ["a", "b"] print(isinstance(value, list[str])) ``
  3. 3. What does this program print? ``python def describe(value): match value: case str() as text: return f"string {len(text)}" case int() as number: return f"int {number * 2}" return f"other {value}" print(describe(21)) print(describe(None)) ``
  4. 4. What does this code print? ``python value = None if isinstance(value, str) and value.strip() == "": print("blank") else: print("no match") ``
  5. 5. The line case str() as text: checks a type and names the value in one step. Python calls this kind of check a ___.

    Type your answer.

5 question(s) left on this page.

Typed answers ignore extra spaces and, for most questions, capital letters.