Variables and Type Hints – Describing Intent
You've learned what Python is. You've got it installed. Now you're ready for your first real Python code.
In this lesson, you'll learn the most fundamental concept in programming: variables. A variable is simply a named container that holds data. Type hints are how you describe what kind of data goes in that container.
But here's where Chapter 18 differs from traditional Python courses: Type hints aren't optional or advanced. They're core. Type hints are how you describe intent—and describing intent is the core of AI-Driven Development.
Variables: Names for Values
Imagine you have a filing cabinet with labeled drawers. Each drawer holds something specific.
In the drawer labeled "age," you store the number 25.
In the drawer labeled "name," you store the text "Alice."
In the drawer labeled "is_student," you store the fact that the person is (True) or isn't (False) a student.
In Python, these drawers are called variables.
Creating a variable looks like this:
Loading Python environment...
The variable is the name (age, name). The value is what's inside (25, "Alice").
But here's the innovation: You can tell Python what kind of data goes in each drawer before you use it.
Loading Python environment...
The part after the variable name (: int, : str) is called a type hint. It says "this variable holds an integer" or "this variable holds a string."
The Four Core Primitive Types
Python has many data types, but the four most fundamental are:
int – Whole Numbers
Loading Python environment...
Use int when your data is a whole number (no decimals). Ages, counts, scores, quantities—all int.
str – Text (Strings)
Loading Python environment...
Use str when your data is text. Always put text in quotes. str stands for "string," which is programmer jargon for "text."
float – Decimal Numbers
Loading Python environment...
Use float when your data is a decimal number. "Float" is short for "floating-point number," which is the technical term for decimals in programming.
bool – True or False
Loading Python environment...
Use bool (short for boolean) when your data is a yes/no or true/false value. Booleans are surprisingly useful for checking conditions and making decisions—concepts you'll see in later chapters.
Type Hints: Describing Intent
Here's why type hints matter so much:
Without type hints: age = 25
This works, but what is age? Is it a year? A count? A score? A price? The code doesn't say.
With type hints: age: int = 25
Now it's crystal clear. age is an integer. Anyone reading this code (including AI) knows exactly what to expect.
Type hints are specifications. When you write a type hint, you're saying "I intend for this variable to hold this kind of data."
This matters enormously for AI collaboration. When you ask your AI companion to help with your code, the type hints tell the AI exactly what you're building.
Python Naming Conventions (PEP 8)
Python has a style guide called PEP 8 that professional developers follow. Here are the naming rules:
Use lowercase with underscores:
Loading Python environment...
Be descriptive:
Loading Python environment...
Start with a letter or underscore:
Loading Python environment...
No spaces:
Loading Python environment...
Avoid Python keywords:
Loading Python environment...
Examples of well-named variables:
user_name,total_price,is_valid,item_count,customer_email
Examples of poorly-named variables:
x,a,data,stuff,value1(all too vague)
When you follow these conventions, your code is easier to read, easier for AI to understand, and easier for other developers to work with.
Collection Types Awareness (Preview)
Python has ways to store multiple values together. You'll learn these in detail in Chapters 18–19, but it's useful to know they exist:
- list — An ordered collection of items:
[1, 2, 3]or["apple", "banana"] - dict — Key-value pairs:
{"name": "Alice", "age": 25} - tuple — An immutable ordered collection:
(1, 2, 3) - set — An unordered collection of unique items:
{1, 2, 3}
You don't need to understand these now. Just know they exist. When you see them in later chapters, you'll recognize them.
Working With Variables: Checking Types
Now that you have variables, you might want to check what type they are. Python gives you functions for this.
The print() function — The print() function displays output to your screen. It's how you see what's inside variables.
Loading Python environment...
The type() function — The type() function tells you what kind of data a variable holds.
Loading Python environment...
The isinstance() function — The isinstance() function checks if a variable is a specific type. It returns True or False.
Loading Python environment...
This function is especially useful for validation—checking whether data is the right type before using it.
Code Examples
Here's a complete program that demonstrates variables and type hints:
Loading Python environment...
This example shows:
- Creating variables with type hints
- Using
print()to display them - Using
type()to check what kind they are - Using
isinstance()to validate types
💬 AI Colearning Prompt
Ask your AI companion: "Explain how type hints help an AI (like you) generate better code. Give a specific example: What's the difference between understanding age = 25 vs. age: int = 25?"
This is a critical insight. Type hints are how you make your intent explicit. Your AI uses that explicitness to understand your specifications and generate appropriate code.
🤝 Practice Exercise
Ask your AI: "Create 5 typed variables representing a person: name, age, height in meters, student status (boolean), favorite programming language. Use realistic values. Then write print statements with f-strings displaying each. Explain why the type hint for height is
floatand notint."
Expected Outcome: You'll practice writing typed variables, understand the difference between int and float, see f-string formatting in action, and learn how type hints make code self-documenting.
🎓 Expert Insight
In AI-native development, type hints aren't "advanced Python"—they're foundational specifications. When you write
age: int = 25, you're declaring intent that AI understands and can validate. Professional developers use type hints everywhere because they prevent bugs, enable better IDE support, and make code self-documenting. In this book, type hints are mandatory—not as busywork, but as practice for specification-first thinking.
Common Mistakes
Mistake 1: Forgetting the colon in type hints
Loading Python environment...
Python needs that colon to understand the type hint. Without it, you'll get a syntax error.
Mistake 2: Using quotes around numbers
Loading Python environment...
The quotes tell Python "this is text." Numbers don't need quotes.
Mistake 3: Confusing type hints with enforcement
Many students think Python will refuse to run if you violate type hints. Actually, Python doesn't enforce type hints at runtime. You can write:
Loading Python environment...
Python won't complain. But this violates the intent you declared with the type hint. Use isinstance() to check types yourself.
Mistake 4: Non-descriptive variable names
Loading Python environment...
"x" might work in math class, but in programming, variable names should describe purpose.
Mistake 5: Invalid variable names
Loading Python environment...
Remember: Start with letter/underscore, use lowercase_with_underscores, no spaces, avoid keywords.
Try With AI
How do you design a data structure with appropriate types and naming conventions?
🔍 Explore Type Selection:
"I'm building a user profile system needing: username (text), age (whole number), account balance (dollars and cents), and verification status (yes/no). What Python variables with type hints should I use? Show me the code and explain why you chose
floatfor balance instead ofint."
🧪 Test PEP 8 Compliance:
"Review this variable naming:
userName,AccountBalance,is_verified,age1. Which names violate PEP 8 style guidelines? Fix them to use properlowercase_with_underscoresconvention and explain why PEP 8 naming matters for professional code."
🎯 Practice Design Iteration:
"Add 3 more fields to the user profile: email address, registration date (text like '2025-01-15'), and premium membership status (yes/no). Update the code with correct type hints and PEP 8 naming for all 7 variables. Why did you use
strfor registration date instead of a datetime type?"
🚀 Validate Type Understanding:
"Write validation code using
isinstance()to verify all 7 variables have the correct types. Show me the complete code with checks likeisinstance(username, str). Then explain: What happens if I assign the wrong type to a variable—does Python catch it at assignment time or when I useisinstance()?"