Get AI summaries of any video or article — Sign up free
Python Tutorial for Beginners 6: Conditionals and Booleans - If, Else, and Elif Statements thumbnail

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

Corey Schafer·
4 min read

Based on Corey Schafer's video on YouTube. If you like this content, support the original creators by watching, liking and subscribing to their content.

TL;DR

Use `==` to compare values in conditions; `=` assigns and won’t produce a True/False test.

Briefing

Python conditionals let programs choose which blocks of code run based on whether expressions evaluate to True or False—an idea built on Python’s Boolean rules. The tutorial starts with the simplest form: an `if` statement that runs a `print` only when its condition is True, and does nothing when the condition is False. It then moves to real-world checks using variables, showing how `if language == 'python':` uses a double equals (`==`) to test equality rather than assign values.

From there, the lesson lays out the core comparison operators that feed conditions: `==` for equality, `!=` for inequality, `>`/`<` for greater-than and less-than, `>=`/`<=` for inclusive comparisons, and `is` for object identity. The distinction between `==` and `is` becomes a key takeaway through a concrete example with lists: two separate lists containing the same values compare equal with `==`, but `is` returns False because they live at different memory locations. When one variable is assigned to reference the other list object (e.g., `b = a`), both `==` and `is` become True because the objects are now the same in memory.

The tutorial then expands control flow with `else` and `elif`. An `else` block provides a fallback path when the `if` condition fails, while `elif` chains multiple checks so only the first matching condition runs. A practical example compares `language` against `python`, then falls back to `else` for non-matches, and finally uses `elif` to handle multiple languages like Python and Java. It also addresses a common question from other languages: Python doesn’t have a `switch`/`case` statement because `if`/`elif` chains are considered sufficient for multi-value branching.

Next comes Boolean logic for combining conditions. Using `and`, both sub-conditions must be True to enter the block (e.g., `user == 'admin'` and `logged_in == True`). Using `or`, only one True condition is enough. The `not` operator flips a Boolean value, turning `False` into `True` and vice versa—useful for checks like `if not logged_in:` to show a “please log in” message when a user isn’t authenticated.

The final major section clarifies what Python treats as False in conditionals, which matters because many values aren’t strictly `True` or `False`. The tutorial highlights that `False` itself, `None`, numeric `0`, and empty containers evaluate to False: empty lists `[]`, empty tuples `()`, empty strings `''`, and empty dictionaries `{}`. Everything else defaults to True, including non-zero numbers and non-empty containers. This behavior is presented as a practical tool for writing cleaner checks—such as verifying whether a returned string or dictionary is empty—without calling extra methods. The lesson closes by previewing loops as the next step after mastering conditional logic.

Cornell Notes

Python conditionals run code blocks only when expressions evaluate to True. The tutorial distinguishes equality (`==`) from assignment (`=`) and from object identity (`is`), showing that two different objects can be equal in value but not identical in memory. It demonstrates `if`, `else`, and `elif` for branching, and notes that Python doesn’t use `switch`/`case` because `if` chains handle multi-value logic cleanly. Boolean operators `and`, `or`, and `not` combine or invert conditions, enabling checks like “admin and logged in” or “not logged in.” Finally, it lists common Falsey values in Python: `False`, `None`, `0`, and empty containers like `''`, `[]`, `()`, and `{}`—everything else is treated as True.

Why does Python use `==` instead of `=` inside an `if` condition?

`=` assigns values, while `==` compares for equality. In the example `if language == 'python':`, the double equals evaluates to True or False, which determines whether the indented block runs. Using a single `=` would be an assignment, not a True/False test.

What’s the practical difference between `==` and `is`?

`==` checks whether two values are equal in content, while `is` checks whether two variables reference the exact same object in memory. Two separate lists like `a = [1,2,3]` and `b = [1,2,3]` can be equal with `a == b` but still not identical with `a is b` because their `id(a)` and `id(b)` differ. If `b = a`, then `id(a) == id(b)` and `a is b` becomes True.

How do `if`, `elif`, and `else` work together for multiple outcomes?

`if` starts the first condition. If it’s False, Python checks the next `elif` condition (if present). The first condition that evaluates to True runs its block, and the rest are skipped. If none of the `if`/`elif` conditions match, the `else` block runs as the fallback. The tutorial uses language checks (Python, Java) to illustrate this branching order.

When should `and` vs `or` be used in a compound condition?

Use `and` when all conditions must be True. Example: `if user == 'admin' and logged_in == True:` requires both. Use `or` when any one condition being True is enough. Example: `if user == 'admin' or logged_in == True:` allows entry if either condition holds, even if the other is False.

What does `not` do, and why is it useful?

`not` flips a Boolean value. If `logged_in` is False, then `not logged_in` becomes True, so `if not logged_in:` triggers the “please log in” branch. The tutorial frames it as “not false” → True.

Which values evaluate to False in Python conditionals (Falsey values)?

Common Falsey values include: `False`, `None`, numeric `0`, and empty containers—empty strings `''`, empty lists `[]`, empty tuples `()`, and empty dictionaries `{}`. The key pattern is that empty sequences/mappings and zero-like values are treated as False, while non-empty/non-zero values are treated as True.

Review Questions

  1. In the tutorial’s list example, why does `a == b` return True while `a is b` returns False?
  2. Give one example of a condition that would be True using `or` but False using `and`.
  3. List at least four values that evaluate to False in Python conditionals and explain the common theme behind them.

Key Points

  1. 1

    Use `==` to compare values in conditions; `=` assigns and won’t produce a True/False test.

  2. 2

    Indentation determines the code block that runs under `if`, `elif`, and `else`.

  3. 3

    `else` runs only when the preceding `if` condition is False; `elif` chains additional checks in order.

  4. 4

    `and` requires all sub-conditions to be True, while `or` requires only one to be True.

  5. 5

    `not` flips a Boolean condition, enabling readable checks like `if not logged_in:`.

  6. 6

    `is` checks object identity (same memory object), while `==` checks value equality.

  7. 7

    In Python, Falsey values include `False`, `None`, `0`, and empty containers like `''`, `[]`, `()`, and `{}`; everything else is treated as True.

Highlights

Two lists with the same contents can still fail `a is b` because `is` compares memory identity, not just equality.
Python’s `if`/`elif`/`else` chain replaces the need for `switch`/`case` in typical multi-value branching.
Boolean operators combine conditions: `and` demands both True, `or` needs only one True, and `not` reverses the result.
Python treats many non-Boolean values as False in conditionals—especially `0`, `None`, and empty strings/lists/tuples/dicts.
Knowing Falsey values helps write cleaner checks for empty results without extra method calls.

Topics

  • If Statements
  • Else and Elif
  • Boolean Operators
  • Comparison Operators
  • Truthiness and Falsey Values

Mentioned