
An enormously complex tangle of piping and valves with numerous loops and on/off spigots that control the flow. MidJourney 5
Almost every question we ask of a dataset this week starts as a yes-or-no question about a single value. Was this day above freezing? Is this reading from the ozone monitor? Did this park get more than a million visitors?
Python has a type for the answer to a question like that, and it is called a boolean: a value that is either True or False. This morning we will learn how to write those questions, how to combine them, and what to do with the answers. By the end of the session you will have asked a question of an entire column at once, which is the idea the rest of today is built on.
How a live coding session works
We all start from an empty notebook and write everything together, live. There is no code on this page to copy, on purpose.
- Kelly writes a line, explains it, and runs it. You write the same line and run it.
- When something breaks, we fix it together. Things will break, and the fixing is the useful part.
- If you fall behind, say so. Cella and Kelly would both much rather slow down than lose you.
- Type the code yourself rather than copying it from a neighborβs screen. Typing a line out, and then fixing it when it does not run, is how the syntax gets into your fingers.
Set up your notebook
Create a new notebook named
Session_3A_Booleans_and_Conditionals.ipynb.Add a title cell:
# Day 3: Session 3A - Booleans & Conditionals
Date: 09/02/2026- Add the following markdown cells now, in this order, leaving space under each. We will fill in the code as we go, and having the structure in place should keep your notes organized instead of turning them into one long scroll.
## 1. A yes-or-no question
## 2. The comparison operators
## 3. Asking two questions at once
## 4. if / elif / else
## 5. Asking a whole column
## 6. Counting the Trues
## 7. Where this goes next- Under each heading, add an empty code cell.
Your seven headings are the scaffold for the session. Read the next two sections before you switch to your notebook, because the file we use is named there.
The data
Weβll use the Toolik Field Station weather record, the same file you wrote a biography of yesterday:
https://eds-217-essential-python.github.io/data/toolik_weather.csv
You explored the file yesterday, so nothing this morning should be a surprise about the data, which leaves your attention free for the questions.
What weβll cover
Booleans and comparisons (about 8 minutes)
TrueandFalse, and whattype()says about them- the six comparison operators, including the difference between
=and== - reading one value out of a DataFrame with
.loc[] - combining questions with
and,or, andnot
Conditionals (about 8 minutes)
if, and why the colon and the indentation are not optionalelsefor the leftover caseeliffor sorting a value into one of several categories- writing a small classifier: cold day, cool day, warm day
Asking a whole column (about 9 minutes)
- what happens when you compare an entire column to a number
- the boolean mask, a column of
TrueandFalsewith the row labels still attached &,|, and~, which are what pandas uses in place ofand,or, andnot- why every piece of a combined mask needs its own parentheses
- counting with
.sum(), becauseTruecounts as 1
What you should be able to do afterwards
- write a comparison and predict whether it gives
TrueorFalse - write an
if/elif/elseblock that puts one number into one of three categories - compare a column to a value and explain what you get back
- combine two column comparisons with
&and get the parentheses right the first time - say, in one sentence, what a boolean mask is for
Where this goes next
In the very next session youβll put a mask inside square brackets and get back a smaller table. The line you write there is the filter pattern, and the booleans, the operators and the masks we practice this morning are the pieces it is made of.