Command Palette

Search for a command to run...

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

Short-circuit and operands

The two True or / False and lines would crash if the right side ran.

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.

Short-circuit and operands
Sign in to run this example in your browser.

Expected output

default
Asha
0 5
last
stranger
True
False
True
True
True False True
1