Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 1.8 — Comparisons and Logic
Python · Example 3 of 3

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.

01

Set age = 15 and print whether it is in the teenage range 13 to 19 (inclusive) with a chained comparison.

02

Set nickname = "" and print nickname if it has text, otherwise Guest, using or.

03

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

01

Explain the difference between == and is, and when each is correct.

02

What is short-circuit evaluation? Give an example that would crash without it.

03

What does a or b return in Python, and why is name or "stranger" risky for numbers?

04

State De Morgan's laws and show an example in code.

Defaults: when 0 and empty text are real answers
Sign in to run this example in your browser.

Expected output

3 0
'Hello' ''
30 30
False True False
True True True True