Lesson 4 of 10

Conditionals

Goal

Branching with if, elif and empty values

01

Explanation

if runs the lines under it when its condition is true, elif checks another condition when the ones before it didn't match, and else handles everything left over. Python knows which lines belong to a condition by their indentation: the four spaces at the start of each line under the line that ends with a colon.

Python checks the branches in order, runs the first one whose condition is true and skips the rest, so the order changes the result. If you started with score >= 50, every score of 50 or more would be "Pass" and nobody would ever reach "Excellent".

You can combine conditions with and (both must be true), or (either one is enough) and not (flips the condition). Empty values like "", 0, an empty list and None count as falsy in a condition, as if they were False, so if name: is enough instead of if name != "":. Watch out for a classic mistake here: if city == "Jeddah" or "Mecca": is always true, because "Mecca" on its own is a non-empty string. The correct version is city == "Jeddah" or city == "Mecca".

02

Your task

Set grade to the rating for score: "Excellent" for 90 and above, "Good" from 75, "Pass" from 50, and "Fail" below that. Make certified true only if the student passed (50 or more) and their attendance is 0.75 or more. And display_name is name, or "Unknown" if name is empty.

Checks

4 checks
  1. grade is "Good" for a score of 78
  2. The rating comes from an if/elif chain, not a hard-coded value
  3. certified requires both a pass and enough attendance
  4. An empty name shows "Unknown"
03

Hints

0/3
Track lessons0 of 10 done

Start with the most beginner-friendly language there is, and use it to learn variables, strings, lists, conditions, loops and functions.

Beginner level, 10 lessons, 2 hours in total.

main.py
OutputPython (Pyodide)

Loading Python. This only happens once. Your browser keeps it after that.

–/4

Checks start once the environment is ready.

4 checks listed under “Your task”.

Track lessons0 of 10 done

Start with the most beginner-friendly language there is, and use it to learn variables, strings, lists, conditions, loops and functions.

Beginner level, 10 lessons, 2 hours in total.