Chapter 47: Primitive Types and Expressions
In Chapter 45, you learned to read Python: you could look at age: int = 30 and understand what it meant. Now you write it yourself.
Python has four basic types that cover most values you will work with:
int— whole numbers like42,0,-7(counting items, measuring word counts)float— decimal numbers like3.14,0.5,-2.7(prices, reading times, percentages)str— text like"hello","My Note",""(titles, names, user input)bool—TrueorFalse(is this note a draft? is the user logged in?)
These are called primitive types because they are the simplest building blocks. Every value in Python has a type, and every type determines what you can do with that value. You can divide 42 by 2, but you cannot divide "hello" by 2. The type tells Python (and pyright) what operations are allowed.
This chapter teaches you to write type annotations for these four types, combine values with operators (arithmetic, comparisons, logic), and convert between types when needed. By the end, you will have the vocabulary to specify exactly what data your SmartNotes functions expect.
Why Types Matter for Specification
In Chapter 43, you learned Axiom V: Types Are Guardrails. In Chapter 44, you saw pyright catch errors. This chapter makes types your primary tool. When you write word_count: int = 350, you are not just labeling a variable. You are making a promise: "This value is an integer. Any function that uses it can rely on that fact. Pyright will enforce this promise even when I am not looking."
That promise becomes powerful in Chapter 49 when you write function signatures like def reading_time(word_count: int, wpm: int = 250) -> float. The types in that signature ARE the specification. AI reads them and knows exactly what to build.
What You Will Learn
By the end of this chapter, you will be able to:
- Explain what a data type is and why Python distinguishes
42from"42" - Write type annotations for
int,float,str,bool, andNone - Follow Python's variable naming conventions (snake_case, valid names, reserved words)
- Predict the result of arithmetic, comparison, and boolean expressions
- Convert between types using
int(),float(),str(),bool()and predict which conversions fail - Read pyright error messages and fix type mismatches
Chapter Lessons
| Lesson | Title | What You Do | Duration |
|---|---|---|---|
| 1 | From Reading to Writing Types | Understand data types, write your first annotations, learn naming rules | 25 min |
| 2 | Arithmetic and Number Types | Write expressions with typed variables, discover int vs float division | 20 min |
| 3 | Comparisons and Booleans | Write comparisons that produce booleans, combine with and/or/not | 20 min |
| 4 | None and Type Conversions | Use None for missing values, convert between types, predict failures | 20 min |
PRIMM-AI+ in This Chapter
Every lesson includes a PRIMM-AI+ Practice section following the five-stage cycle from Chapter 42. This is Phase 2: you are now WRITING type annotations, not just reading them.
| Stage | What You Do | Claude Code Tool |
|---|---|---|
| Predict [AI-FREE] | Predict what pyright flags or what an expression evaluates to, with a confidence score (1-5). Press Shift+Tab for Plan Mode. | Plan Mode protects prediction |
| Run | Press Shift+Tab to exit Plan Mode. Execute the code or run uv run pyright, compare to your prediction. | You run it, not Claude Code |
| Investigate | Write a trace artifact explaining why your prediction was right or wrong. Use /investigate for Socratic questions. | /investigate probes mechanics |
| Modify | Change a type or value and predict the new result | You edit, Claude Code reviews |
| Make [Mastery Gate] | Write typed declarations or expressions from scratch. Use /tdg for TDG cycles (Lesson 4). | Your work, Claude Code generates |
If you installed the PRIMM-AI+ starter kit in Chapter 42, your /predict, /investigate, and /bug commands still work. This chapter also uses /tdg for TDG cycles in Lesson 4. If you don't have it, update your kit from panaversity/primm-ai-starter-kit.
The SmartNotes Connection
At the end of this chapter, you will define typed variables for a SmartNotes note: title: str, word_count: int, reading_time: float, is_draft: bool. Then you will write a reading_time_minutes() function stub and let AI generate the body. This is your first taste of using types as specification vocabulary for a real project.