Python · live now

Conditional logic

How a program chooses between one course of action and another: if to run a block only when a condition is true, elif and else for the alternatives, conditions nested inside one another, and the ternary operator for choosing a value on a single line.

No account needed to read a lesson · 30-day money-back guarantee

See the full learning path

5

lessons

29

practice questions

22

prerequisite lessons

See it taught

1 of the 5 lessons here is free to preview - the whole explanation and worked example, with no account and no email.

The editor you get

A still of the editor, not a live one

Python Editor

1 2 3 4 5
temp = 18 if temp > 20: print("warm") else: print("cool")

Editor Output

1

Output:

cool

You write and run Python in the browser - nothing to install, no setup. Questions are marked on the values your code produces. The snippet here is an example of the kind of code you write.

The 5 lessons

The 5 lessons in this unit. Expand any one to see what it covers. Each unlocks when its own prerequisites are passed.

01

Conditional logic

Preview

12 questions

Learn how to control program flow in Python using if and else statements, applying correct indentation to execute different code blocks based on whether conditions are true or false.

Covers

The if statement

Indentation in conditional logic

The else clause

Preview this lesson - no account needed

02

elif

4 questions

Learn how to use the elif statement in Python to handle multiple branching conditions in decision-making, ensuring only the first true condition executes its associated code block.

Covers

elif

03

Nested conditionals

4 questions

Understand how to construct and interpret nested conditionals in code to handle complex decision-making scenarios that depend on multiple related conditions.

Covers

Nested conditionals

04

Conditional ternary operator

4 questions

Learn how to use Python's conditional ternary operator to assign values based on a condition in a concise, readable way.

Covers

Conditional ternary operator

05

Activity: adventure game decision engine

5 questions

Learn how to build a decision engine for a text-based adventure game by writing logic that checks player health, inventory, and weather conditions to determine the next appropriate action.

Covers

Check player health

Determine player action

Check weather

Count items

Make decision

Where this unit sits in the curriculum

This unit is part of our Python Done Right learning path, which contains 100 lessons. Every one of them is drawn below.

The diagnostic test lets you skip any lesson you already know, including the ones in this unit.

Every dot is a lesson and every line a prerequisite. The 5 red dots are the lessons in Conditional logic; the 22 blue dots feeding into them are the lessons Conditional logic depends on; the 73 pale dots are the rest of the learning path, which come after Conditional logic or alongside it.Conditional logicFirst lessons on the left100 lessons

Hover any dot to name its lesson.

5 lessons in this unit

22 prerequisite lessons across the unit, counting every step back to the start

73 other lessons in the learning path - after this unit, or alongside it

Every dot is a lesson, every line a prerequisite. You can only start a lesson once you have mastered all of its prerequisites, so you are always building on solid foundations.

Start Conditional logic

One subscription covers every learning path, and you can test out of anything you already know.

One subscription covers every learning path · 30-day money-back guarantee

More in Conditional logic & comparisons: Comparisons