Skip to main content

Keywords and Capstone: Building a Type-Safe Calculator

You've mastered four types of operators. Now it's time to learn one more important concept about Python's language structure: keywords. Keywords are reserved words that Python has claimed for itself. You can't use them as variable names because Python needs them for language features.

Then we'll bring everything together in a capstone project: a calculator that demonstrates all four operator types, combined with keyword awareness and type validation.

What It Is: Python's Reserved Words

A keyword is a word that Python uses for language syntax. These words have special meaning and control how Python works. Because Python needs them for specific purposes, you can't use them as variable names.

Python has 35 keywords (as of Python 3.14). You don't need to memorize them—you need to recognize common ones and know how to check if a word is reserved.

Discovering Python Keywords

Python provides a simple way to see all keywords:

Loading Python environment...

These 35 keywords are Python's grammar. They define language structure: loops (for, while), conditionals (if, else), functions (def), classes (class), and more.

💬 AI Colearning Prompt

"Why can't I use keywords as variable names? What would happen to Python's design if if, for, or def weren't reserved? Explain the problem and why reservation is necessary."

This question helps you understand language design—why certain words must be protected.

Why Keywords Are Reserved

Let's see what happens when you try to use a keyword as a variable name:

Loading Python environment...

The reason for, if, def are reserved is that Python needs them for language structure. If you could write for = 5, Python would get confused: "Is this the start of a for loop, or is this assigning 5 to the variable for?"

🎓 Expert Insight

In AI-native development, you don't memorize all 35 keywords. You develop a reflex: "If I get a SyntaxError when naming a variable, it might be a keyword. Let me check with keyword.iskeyword()." This defensive habit will save you debugging time.

Diagram showing Python variable naming rules and conventions: valid identifiers must start with letter or underscore, can contain letters numbers underscores, cannot be keywords, with examples of valid and invalid names plus PEP 8 style guidelines

Checking if a Word is Reserved

A smart programming practice is to check before using a word as a variable name:

Loading Python environment...

This pattern—checking before using—is good defensive programming.

🤝 Practice Exercise

Ask your AI: "Create a program that:

  1. Lists all 35 Python keywords
  2. Groups them by category (control flow, function definition, special values, etc.)
  3. Explains what each category does

Then explain: Why do you think keywords are organized this way? What would happen if Python had no keywords?"

Expected Outcome: You'll see that keywords follow logical categories; understand that language design requires structure; appreciate why Python protects certain words.

Capstone Project: Calculator with Type Safety

Now let's bring everything together. You'll create a calculator that demonstrates:

  • Arithmetic operators (Lesson 1): +, -, *, /, //, %
  • Comparison operators (Lesson 2): ==, !=, >, <, >=, <=
  • Logical operators (Lesson 3): and, or, not
  • Assignment operators (Lesson 4): =, +=, -=, *=, /=
  • Type validation: Using type() to verify results
  • Keyword awareness: Recognizing reserved words

Simplified Calculator (Beginner-Friendly Version)

If you haven't learned functions yet, here's a straightforward version:

Loading Python environment...

Output:

==================================================
SIMPLE CALCULATOR WITH TYPE SAFETY
==================================================

--- ARITHMETIC OPERATORS ---
10.0 + 3.0 = 13.0 (type: float)
10.0 - 3.0 = 7.0 (type: float)
10.0 * 3.0 = 30.0 (type: float)

--- COMPARISON OPERATORS ---
10.0 == 3.0: False (type: bool)
10.0 > 3.0: True (type: bool)
10.0 < 3.0: False (type: bool)

--- LOGICAL OPERATORS ---
Both positive: True (type: bool)
Either > 10: False (type: bool)
Not equal: True (type: bool)

--- ASSIGNMENT OPERATORS ---
After adding result: total = 13.0
After subtracting 5: total = 8.0
After halving: total = 4.0 (type: float)

--- KEYWORD AWARENESS ---
Checking variable names:
'count' is OK - can use as variable name
'for' is RESERVED - don't use as variable name
'result' is OK - can use as variable name
'if' is RESERVED - don't use as variable name
'my_var' is OK - can use as variable name

--- SAFE DIVISION ---
10.0 / 3.0 = 3.3333333333333335 (type: float)

==================================================
Calculation complete!
==================================================

That's the complete calculator! You've now integrated all four operator types (arithmetic, comparison, logical, assignment) plus keyword awareness and type validation—all using only the Python concepts you've learned so far (Chapters 15-15).

What about functions? You might wonder why the calculator code is all in one block instead of organized into functions. That's intentional! Functions are taught in Chapter 25. For now, focus on understanding how operators work together. When you reach Chapter 25, you can come back and refactor this calculator using functions as practice.


Try With AI

Ready to integrate all 4 operator types into one comprehensive calculator?

🔍 Explore Multi-Operator Integration:

"Show me how arithmetic, comparison, logical, and assignment operators work together. Create a calculator that: (1) performs 7 arithmetic ops (+,-,*,/,//,%,**) on two numbers, (2) uses comparisons to validate result > 0 and < 1000, (3) uses logical operators to check (num1 > 0) and (num2 > 0), (4) uses += to track running total. Explain how each operator type serves a different purpose."

🎯 Practice Comprehensive Validation:

"Build a production-ready calculator with type hints and validation: (1) Check num2 != 0 before division operations, (2) Validate inputs are positive using logical operators, (3) Compare results against expected ranges, (4) Use assignment operators to accumulate totals, (5) Check if proposed variable names avoid Python keywords using keyword.iskeyword(). Show complete code with clear error messages."

🧪 Test Edge Cases:

"Test this calculator with 4 cases: (1) normal (10, 3), (2) zero divisor (10, 0), (3) negative inputs (-5, -3), (4) large numbers (1000000, 500000). For each test, show: arithmetic results, comparison validations, logical checks, and accumulated total. What breaks? How do we handle division by zero gracefully? Should we allow negative numbers or reject them?"

🚀 Apply to Your Domain Calculator:

"I need a calculator for [describe your domain: financial calculations, game scoring, data analysis, etc.]. Help me integrate all operator types: (1) domain-specific arithmetic operations, (2) validation ranges using comparisons, (3) multi-condition checks using logical operators, (4) state tracking with assignment operators, (5) keyword validation for user-defined names. Make it robust against bad input."



Chapter 20 Complete

Congratulations! You've mastered all four operator types: arithmetic, comparison, logical, and assignment. You understand how to combine them, validate types, and recognize Python's language boundaries (keywords).

What's Next (Chapter 21): Strings and text manipulation—you'll see that operators work with strings too (concatenation with +, repetition with *).

Looking Ahead (Chapter 22): Control flow and loops—you'll use comparison and logical operators in if statements and loops, applying everything you learned here to make decisions and repeat code.

Your foundation is solid. Keep this chapter's concepts close as you move forward.