Python Tutorial for Beginners 6: Conditionals and Booleans - If, Else, and Elif Statements
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.
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?
What’s the practical difference between `==` and `is`?
How do `if`, `elif`, and `else` work together for multiple outcomes?
When should `and` vs `or` be used in a compound condition?
What does `not` do, and why is it useful?
Which values evaluate to False in Python conditionals (Falsey values)?
Review Questions
- In the tutorial’s list example, why does `a == b` return True while `a is b` returns False?
- Give one example of a condition that would be True using `or` but False using `and`.
- List at least four values that evaluate to False in Python conditionals and explain the common theme behind them.
Key Points
- 1
Use `==` to compare values in conditions; `=` assigns and won’t produce a True/False test.
- 2
Indentation determines the code block that runs under `if`, `elif`, and `else`.
- 3
`else` runs only when the preceding `if` condition is False; `elif` chains additional checks in order.
- 4
`and` requires all sub-conditions to be True, while `or` requires only one to be True.
- 5
`not` flips a Boolean condition, enabling readable checks like `if not logged_in:`.
- 6
`is` checks object identity (same memory object), while `==` checks value equality.
- 7
In Python, Falsey values include `False`, `None`, `0`, and empty containers like `''`, `[]`, `()`, and `{}`; everything else is treated as True.