Skip to content
Tactiverge

Learning Rooms/△ Level 2 · Builder/Electronics

Python Tutorial for Beginners 6: Conditionals and Booleans - If, Else, and Elif Statements

Corey Schafer · Length: see YouTube · captions on YouTube · nothing loads until you press play

By the end you can…

  • Use if / elif / else to choose one action from a measured value.
  • Explain that only the first true branch runs, so the order of conditions matters.
  • Use comparison operators (<, <=, >, >=, ==, !=) and indentation correctly.
?Predict▶Watch✓Practise⚙Build

Watch it all: no segment has been checked by hand for this video yet.

1.Predict before you press play

In the charge_mode function, what does charge_mode(0) return?

Pick one before you press play. A wrong guess is fine: it is what makes the video stick.

2.See the idea first

Play with this diagram for a minute. It gives you something to hang the video on.

Loading the interactive…

Can't use the diagram? Read a description instead

The boundary tester shows the charge_mode function in three versions and a temperature slider from −10 to 60 °C in 0.5 °C steps. It highlights the line that returns and compares the result with the specification (charge only from 0 °C to 45 °C inclusive). Version B matches everywhere; Version A wrongly blocks at exactly 0 °C; Version C wrongly blocks at exactly 45 °C.

3.Watch, actively

Walks through if, elif and else, comparisons and the Boolean operators and, or and not.

Length: see YouTube · captions on YouTube · source

Nothing loads from YouTube until you press play.

Watch for…

  • → Watch how elif and else attach to an if, and notice the colon and indentation on every branch.

Pause whenever you like. Rewatching a hard minute is a good habit, not a failure.

Can't stream it? Read the idea instead

An if statement runs code only when a condition is true; elif tries further conditions in order and else catches everything left over. Comparisons produce True or False, and and, or and not combine them. Only the first matching branch runs, so order matters.

What the video covers (our own note, written when we verified it): Walks through if, elif and else, comparisons and the Boolean operators and, or and not.

Written by Tactiverge in our own words, not a transcript. The full lesson below explains the idea without the video.

Read it instead

Jot down one thing that surprised you and one question you still have.

Private: saved only on this device, never sent to Tactiverge.

4.Practise what you just saw

These come from the lesson that use this video and are checked on the server. Wrong answers get a hint and another try.

Practice 1

Which condition tests whether a reading equals the limit?

Practice 2

When does the else block in charge_mode run?

Practice 3 · a fresh version every time

A cooling fan is set by: if temp_c >= 45: fan = 100, elif temp_c >= 35: fan = 50, else: fan = 0. What is fan, in %, when temp_c = 44.5?

%

Practice 4

A rule is written: if temp_c >= 35: fan = 50, elif temp_c >= 50: fan = 100. What is wrong?

5.Try it yourself

No simulation models this idea yet. The lesson below has an interactive diagram instead.

6.Where engineers use it

Lithium-ion battery management systems really do block charging outside a temperature window (a common one is 0–45 °C, but each cell's datasheet sets its own), because charging a cold cell can damage it permanently.

From the applied view in the lesson Decisions from measurements: if, elif, else. Checked by automated tests only.

7.Build something with it

Follow-up project

Write classify(level_pct) for a water tank: 'LOW' below 20 %, 'OK' from 20 % to 90 % inclusive, 'HIGH' above 90 %. Print it for 0, 19.5, 20, 90 and 90.5 and check each by hand.

Where this video lives

In the course catalogue: CSE-210 Python for engineers

The video is by Corey Schafer, embedded from YouTube with privacy-enhanced mode and never autoplayed. The room around it (prompts, notes, practice, projects) is Tactiverge's, written with AI assistance and checked by automated tests only.