Defaults: when 0 and empty text are real answers
or replaces every falsy value, so a retries setting of 0 and an empty greeting are lost. Testing is None keeps them. The last lines show nan inside a list and a chain. The def lines are functions (Phase 5): read each as a named recipe that gives back one value. The dictionary is Phase 4.
Comparison operators (==, !=, <, >, <=, >=) ask questions and answer with True or False. and, or and not combine answers. == asks "same value?", is asks "same object?", and and/or stop early and return one of their operands.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 6 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Set age = 15 and print whether it is in the teenage range 13 to 19 (inclusive) with a chained comparison.
Set nickname = "" and print nickname if it has text, otherwise Guest, using or.
Set raining = True, has_umbrella = False. Print whether you get wet: you do if it rains and you have no umbrella.
Explain it without notes
Explain the difference between == and is, and when each is correct.
What is short-circuit evaluation? Give an example that would crash without it.
What does a or b return in Python, and why is name or "stranger" risky for numbers?
State De Morgan's laws and show an example in code.
Expected output
3 0
'Hello' ''
30 30
False True False
True True True True