SECTION 1
Historical Context & Motivation
When you store information in a computer, the machine needs to know what kind of information it is dealing with. Is 42 a number you want to add, or is it part of a street address? Early programming languages forced developers to declare every variable's type in advance, which was tedious but necessary. Python took a different approach: it figures out the type automatically when you assign a value, a feature called dynamic typing. Understanding this system—and the four most common types—is the first step toward writing code that behaves the way you expect.
1957 • FORTRAN Introduces Typed Variables
1972 • C Language and Static Typing
int x = 5;, making the programmer responsible for choosing and managing types at compile time.1991 • Python 0.9 Released
2000 • Python 2 and Built-in bool
bool as a proper built-in type with values True and False, completing the set of four fundamental types we study today.2020+ • Type Hints Gain Popularity
x: int = 5) that help tools catch type-related bugs without changing Python's dynamic nature.The central question this lesson addresses is straightforward: given a Python expression like 3 + 2.0 or "hello" + "world", can you predict both the resulting value and the resulting type? Mastering this skill prevents countless bugs and makes you a more confident programmer.
SECTION 2
Core Principles — The Four Fundamental Types
Every value in Python belongs to exactly one type (also called a data type). A type tells Python what operations are legal and how to store the value in memory. You can always check a value's type by calling type(). For example, type(42) returns <class 'int'>. The four types you need to master right now are int, float, str, and bool.
int — Integer
7, -3, 0. Python integers have unlimited size, so you can store astronomically large numbers without overflow.float — Floating-Point
3.14, -0.5, 2.0. Even 2.0 is a float, not an int—the decimal point is the key indicator.str — String
"hello", 'Python', "42". Notice that "42" is a string, not a number—the quotes make all the difference.bool — Boolean
True and False (capitalized). Booleans are the result of comparisons like 5 > 3 and drive every if statement.KEY TAKEAWAY
SECTION 3
Visual Explanation — The Type Landscape
"42" lives in the string box—the quotes make it text, not a number.The diagram above organizes the four types into two rows. The top row holds the numeric types: int for whole numbers and float for decimal numbers. The bottom row contains str for text and bool for true/false logic. A critical insight is that 2.0 is a float even though it represents a whole number—the presence of the decimal point determines the type. Similarly, "42" is a string because it is wrapped in quotation marks, so Python treats it as text rather than a number you can do math with.
SECTION 4
How It Works — Type Rules and Conversions
When you combine values with an operator like + or *, Python follows specific rules to determine the result's type. The most important rule is type promotion: when you mix an int and a float in arithmetic, Python automatically converts the int to a float so no precision is lost. The result is always a float.
TYPE PROMOTION RULE
Where ⊕ represents any arithmetic operator (+, −, ×, /). Example: 3 + 2.0 produces 5.0 (a float, not an int).
TRUE DIVISION RULE
In Python 3, the / operator always returns a float, even if both operands are integers. Example: 10 / 2 produces 5.0. Use // for integer (floor) division.
STRING CONCATENATION
The + operator glues strings together. "py" + "thon" produces "python". You cannot add a string to a number—"age: " + 17 raises a TypeError.
STRING REPETITION
Multiplying a string by an integer repeats it. "ha" * 3 produces "hahaha". This is one of the few cases where mixing types with an operator is allowed.
Type Conversion Functions
int(), float(), str(), and bool(). For example, int("42") converts the string "42" into the integer 42, and str(100) converts the integer 100 into the string "100".SECTION 5
Detailed Breakdown — Type Behaviors and Operators
Each type responds differently to the same operator. Understanding these behaviors is the key to predicting expression results. The table below maps each type to the operators it supports and shows what happens when you apply them.
| Expression | Result | Result Type | Why? |
|---|---|---|---|
7 + 3 | 10 | int | Both operands are int, result stays int |
7 + 3.0 | 10.0 | float | Mixed int and float → promoted to float |
7 / 2 | 3.5 | float | Division always returns a float in Python 3 |
7 // 2 | 3 | int | Floor division with two ints returns an int |
"hi" + "!" | "hi!" | str | String concatenation joins text |
"ab" * 3 | "ababab" | str | String repetition repeats the text |
5 > 3 | True | bool | Comparison operators return a bool |
True + True | 2 | int | bool is a subtype of int; True = 1, False = 0 |
Common expressions and their resulting types
True or False, it's a bool. If it has a decimal point, it's a float. Otherwise, it's an int.SECTION 6
Worked Example — Predicting Expression Results
Let's walk through predicting the value and type of a multi-step expression. Suppose we are given the following code and asked to determine what result holds at the end.
The Code
a = 10
b = 3.0
c = a + b
d = str(int(c)) + " cookies"
result = len(d) > 5Step-by-Step Type and Value Prediction
- 1
Step 1 — Identify a and b
a = 10assigns the integer10(type: int).b = 3.0assigns the floating-point number3.0(type: float). The decimal point in 3.0 is the key indicator.a →10(int), b →3.0(float) - 2
Step 2 — Evaluate c = a + b
We are adding an int (10) to a float (3.0). By the type promotion rule, the int is converted to float before addition. So10 + 3.0becomes10.0 + 3.0which equals13.0.c →13.0(float) - 3
Step 3 — Evaluate int(c)
Theint()function converts a float to an integer by dropping the decimal portion. Soint(13.0)produces13(type: int).int(c)→13(int) - 4
Step 4 — Evaluate str(int(c)) + " cookies"
First,str(13)converts the int to the string"13". Then we concatenate two strings:"13" + " cookies"produces"13 cookies". This is string concatenation—the + operator joins text when both sides are strings.d →"13 cookies"(str) - 5
Step 5 — Evaluate len(d) > 5
Thelen()function returns the number of characters in a string."13 cookies"has 10 characters (including the space). The comparison10 > 5evaluates toTrue. Comparisons always produce a bool.result →True(bool)
SECTION 7
Common Mistakes and Type Pitfalls
Even experienced programmers trip over type-related bugs. The table below highlights the most common mistakes beginners make and how to avoid them. Recognizing these patterns early will save you hours of debugging.
| Mistake | What Happens | Fix |
|---|---|---|
"Score: " + 95 | TypeError — cannot add str and int | "Score: " + str(95) |
Expecting 10 / 2 to give 5 | It actually gives 5.0 (a float) | Use 10 // 2 if you need an int |
Using input() without conversion | input() always returns a str, so math fails | Wrap in int(input()) or float(input()) |
Confusing "42" with 42 | "42" + "8" → "428" (concatenation, not addition) | Convert to int first: int("42") + int("8") → 50 |
Writing true or false | NameError — Python booleans are capitalized | Always use True and False |
Common type-related mistakes and their fixes
KEY TAKEAWAY
type() to check. It's like checking the label on a mystery ingredient before tossing it into a recipe—a quick glance prevents a ruined dish.SECTION 8
Connection to Advanced Concepts
The four types you've learned—int, float, str, and bool—are just the beginning. As you progress in Python, you'll encounter more complex types like lists, dictionaries, and tuples that can hold collections of values. You'll also learn about type hints and object-oriented programming, where you'll create your own custom types called classes. The table below previews how the fundamentals connect to what's ahead.
| What You Know Now | What's Coming Next |
|---|---|
A single int like 42 | A list of ints like [42, 17, 99] |
A single str like "hello" | A dictionary mapping str to int like {"Alice": 95, "Bob": 87} |
Using type() to check types | Using isinstance() for flexible type checking |
Converting with int() and str() | Writing custom conversion methods inside your own classes |
| Dynamic typing (Python infers types) | Adding type hints like def greet(name: str) -> str: |
From fundamentals to advanced Python concepts
The good news is that every advanced concept still builds on the same mental model: every value has a type, and the type determines what you can do with it. Mastering int, float, str, and bool gives you a rock-solid foundation for everything that follows.
SECTION 9
Practice Problems
Question 1: CONCEPTUAL
"7" and 7 are different in Python. What type is each, and what would happen if you tried to evaluate "7" + 7?Show answer
"7" is a str (string) because it is enclosed in quotation marks, while 7 is an int (integer) because it has no quotes and no decimal point. If you tried "7" + 7, Python would raise a TypeError because you cannot concatenate a string with an integer. You would need to convert one to match the other: int("7") + 7 gives 14 (int), or "7" + str(7) gives "77" (str).Question 2: BASIC CALCULATION
15 // 4
(b) 15 / 4
(c) 15 % 4Show answer
15 // 4 performs floor division, giving 3 (int). It divides and rounds down to the nearest whole number. (b) 15 / 4 performs true division, giving 3.75 (float). Remember, the / operator always returns a float in Python 3. (c) 15 % 4 is the modulo operator giving the remainder, which is 3 (int). Since both operands are ints and modulo preserves type, the result is an int.Question 3: INTERMEDIATE
result after running this code:
x = 5 + 2.0
y = str(x)
result = y * 2Show answer
x = 5 + 2.0 adds an int and a float, so type promotion gives 7.0 (float). Next, y = str(7.0) converts that float to the string "7.0". Finally, y * 2 performs string repetition (str × int → str), producing "7.07.0" (str). A common mistake is expecting the number 14.0—but since y is already a string, multiplication repeats the text instead of doing arithmetic.Question 4: APPLIED
input(). Write a short code snippet that calculates the total and prints it. Explain which type conversions are needed and why.Show answer
input() always returns a string, you must convert the values before doing math. Here is a working solution:
cost = float(input("Meal cost: "))
tip_pct = float(input("Tip %: "))
total = cost + cost * (tip_pct / 100)
print("Total: $" + str(round(total, 2)))
We use float() rather than int() because meal costs can include cents (e.g., 23.50). The final str() call converts the numeric total back to a string so it can be concatenated with the dollar sign in the print statement.Question 5: CRITICAL THINKING
bool is actually a subtype of int, meaning True behaves like 1 and False behaves like 0 in arithmetic. Given this, predict the value and type of True + True + False and of True * 3.5. Then explain why Python might have designed booleans this way. What advantage does it give programmers?Show answer
True + True + False evaluates as 1 + 1 + 0, giving 2 (int). True * 3.5 evaluates as 1 * 3.5, and since we're mixing an int with a float, type promotion produces 3.5 (float). This design is powerful because it lets programmers count True values easily. For example, sum([x > 50 for x in scores]) counts how many scores exceed 50 by treating each True as 1 and summing them. This bool-as-int design makes many common patterns—like counting matches or computing weighted sums—elegant and concise.SUMMARY
Lesson Summary
Summary
Python has four fundamental data types that every programmer must master. An int stores whole numbers like 42 and -7. A float stores decimal numbers like 3.14 and 2.0—the decimal point is the distinguishing feature. A str holds text wrapped in quotation marks, including digit characters like "42" that look numeric but are not. A bool holds exactly True or False and powers every conditional decision in your code.
To predict expression results, remember the key rules: type promotion converts int to float when they are mixed in arithmetic. True division with / always returns a float. String concatenation joins text with +, but you cannot mix a string with a number—use str(), int(), or float() to convert between types. Finally, use type() whenever you're unsure—it's your best debugging friend for type-related errors.