Skip to content
Start free

Read the AI's code line by line

Predict what each line of the AI's three-line count prints, then say in your own words what it actually counted.

2 parts · 5 and 4 min

Free course. Your Maestro, an AI, .

On the previous page, you saw the question an AI assistant was asked ("How many customers have at least one paid order?") and the answer it gave back: 6. Along with that number, the assistant showed the code it ran on the eight-row orders table (orders "P01" through "P08").

Before you read a single line of that code, recall the one fact about the table that decides what any row count means: its , or what one single row stands for.

Check yourself: In the eight-row orders table from the previous page, what does one single row stand for?

Now look at the code the AI ran to get 6. To read a calculation without guessing, break it into its smallest steps and predict what each piece produces before you run it.

In the exercise below, the same eight orders ("P01" through "P08") are written right inside the script so every value is in front of you. The first case runs only the comparison inside the brackets: orders["status"] == "paid". The second case puts that comparison inside orders[...] to create paid, and then runs print(len(paid)).

Try it

Commit your prediction for each case before you click Run. For the first case, say whether the comparison prints a single number, a filtered table, or a True/False answer for each row. For the second case, predict the number print(len(paid)) prints and notice which step actually drops the cancelled rows.

The same eight orders as on the last page, typed into the code so you can see them. Predict what each program prints before you run it.

The comparison line

Example 1 of 2

example.py
import pandas as pdorders = pd.DataFrame({    "order_id": ["P01", "P02", "P03", "P04", "P05", "P06", "P07", "P08"],    "customer_id": ["C01", "C01", "C02", "C03", "C04", "C03", "C05", "C06"],    "status": ["paid", "paid", "cancelled", "paid", "paid", "paid", "cancelled", "paid"],})print(orders["status"] == "paid")

A guess is fine. What you write stays in this browser and waits in your course.

Example 2 of 2, opens once you explain this one

A good place to stop. Up next: What each of the three lines computes · 4 min

What each of the three lines computes

Although the AI wrote its calculation at the bottom of the script in two lines, Python carried out three distinct operations in sequence: first a comparison, then a filter, and finally a row count. In the first prediction case above, you pulled the inner comparison out on its own line so you could see what it returns before the filter uses it.

Here are those three lines written out in the order Python evaluates them:

ai_count.pylines 2, 5, 8
# Line 1 (Compare): check each of the 8 rows and answer True or FalseStart of lines 2, 5, 8. orders["status"] == "paid" End of lines 2, 5, 8.# Line 2 (Filter): keep only the rows where the comparison answered TrueStart of lines 2, 5, 8. paid = orders[orders["status"] == "paid"] End of lines 2, 5, 8.# Line 3 (Count rows): count how many rows are in the filtered tableStart of lines 2, 5, 8. print(len(paid)) End of lines 2, 5, 8.
The AI's calculation unpacked into its three steps: compare each row's status to "paid", filter the table to the True rows, and count the rows that remain.

1. Compare (orders["status"] == "paid")

In Python, a single equals sign (=) assigns a variable name to a value, whereas a double equals sign (==) asks a yes-or-no question. When Python runs orders["status"] == "paid", it walks down the status column from row position 0 to row position 7 and checks each row against "paid".

This comparison does not change the orders table or delete any rows. All eight rows remain in place. Instead, it produces a list of eight True and False answers: True on the six rows where status is "paid" ("P01", "P02", "P04", "P05", "P06", and "P08"), and False on row position 2 ("P03") and row position 6 ("P07"), where status is "cancelled". If the row numbers 0 through 7 on the left of the output or the == operator look unfamiliar, ask your Maestro to walk through that output with you.

2. Filter (paid = orders[orders["status"] == "paid"])

Wrapping orders[...] around that comparison turns the True and False answers into a sieve. pandas keeps every row where the inner comparison answered True and drops every row where it answered False.

That drops the two cancelled orders ("P03" and "P07") and leaves a smaller table holding the six remaining rows: "P01", "P02", "P04", "P05", "P06", and "P08". Writing paid = at the start saves this six-row filtered table under the variable name paid. Notice what stayed the same: every row in paid still has the exact same grain as orders. One row is still one order, and all the columns (order_id, customer_id, and status) are still there, including multiple rows for any customer who placed more than one paid order.

3. Count rows (print(len(paid)))

The last line calls len(paid) and prints the result. When you pass a table to len(...), it always counts one thing: the number of rows in that table. Because the filtered table paid has 6 rows, len(paid) returns 6. If you are wondering whether len(paid) ever checks for repeated values inside a column, ask your Maestro how len() treats a table.

Common mistake: A variable name is a label, not what was counted

The AI stored its filtered table in a variable named paid. Even if the AI had named that variable paid_customers, the result would still be 6. Python does not read variable names to decide what to count. Neither orders[orders["status"] == "paid"] nor len(paid) ever mentions the customer_id column: orders[...] filters rows, and len(...) counts how many rows survived.

Putting all three steps together lets you state in one plain sentence what the AI's code computed: it checked which of the eight rows had status equal to "paid", kept those six rows, and counted how many rows were left, giving 6 paid orders.

On the next page, you will run this code on the orders dataset and list the customer on each of those six rows by hand. Before moving on, explain each step of the AI's calculation in your own words below. If you want to talk through any of the three lines first, ask your Maestro.

Check yourself: In your own words, explain what each of the three steps in the AI's code does: (1) orders["status"] == "paid", (2) paid = orders[orders["status"] == "paid"], and (3) print(len(paid)). Then state what the final number 6 actually counts in this table.

Check yourself: In your own words, explain what each of the three steps in the AI's code does: (1) orders["status"] == "paid", (2) paid = orders[orders["status"] == "paid"], and (3) print(len(paid)). Then state what the final number 6 actually counts in this table.

Ctrl+Enter or ⌘+Enter sends it.

Answer it yourself