Comparisons, chains, == versus is
Note first == second is True but first is second is False.
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
True True False False
True False
True False
False True False
True True True True False
True False True
True False