مرکزی مواد پر جائیں

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 like 42, 0, -7 (counting items, measuring word counts)
  • float — decimal numbers like 3.14, 0.5, -2.7 (prices, reading times, percentages)
  • str — text like "hello", "My Note", "" (titles, names, user input)
  • boolTrue or False (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 42 from "42"
  • Write type annotations for int, float, str, bool, and None
  • 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

LessonTitleWhat You DoDuration
1From Reading to Writing TypesUnderstand data types, write your first annotations, learn naming rules25 min
2Arithmetic and Number TypesWrite expressions with typed variables, discover int vs float division20 min
3Comparisons and BooleansWrite comparisons that produce booleans, combine with and/or/not20 min
4None and Type ConversionsUse None for missing values, convert between types, predict failures20 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.

StageWhat You DoClaude 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
RunPress Shift+Tab to exit Plan Mode. Execute the code or run uv run pyright, compare to your prediction.You run it, not Claude Code
InvestigateWrite a trace artifact explaining why your prediction was right or wrong. Use /investigate for Socratic questions./investigate probes mechanics
ModifyChange a type or value and predict the new resultYou 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
Starter Kit Update

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.