COMPUTER PROGRAMMING 1 (PYTHON) • PYTHON FUNDAMENTALS

Python Types — I can distinguish common Python types (int, float, str, bool) and predict expression results at my level.

Understanding data types is the foundation for writing correct, bug-free Python programs.

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
One of the first high-level languages, FORTRAN, required programmers to declare whether a variable held an integer or a floating-point number before using it.
1972
C Language and Static Typing
The C language popularized strict type declarations like int x = 5;, making the programmer responsible for choosing and managing types at compile time.
1991
Python 0.9 Released
Guido van Rossum released Python with dynamic typing, meaning a variable's type is determined at runtime by the value assigned to it—no explicit declarations needed.
2000
Python 2 and Built-in bool
Python 2.3 introduced 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
Modern Python supports optional type hints (e.g., 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.

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.

1

int — Integer

Whole numbers with no decimal point: 7, -3, 0. Python integers have unlimited size, so you can store astronomically large numbers without overflow.
2

float — Floating-Point

Numbers with a decimal point: 3.14, -0.5, 2.0. Even 2.0 is a float, not an int—the decimal point is the key indicator.
3

str — String

Text enclosed in quotes: "hello", 'Python', "42". Notice that "42" is a string, not a number—the quotes make all the difference.
4

bool — Boolean

Exactly two possible values: True and False (capitalized). Booleans are the result of comparisons like 5 > 3 and drive every if statement.
✦ KEY TAKEAWAY
Think of data types like containers in a kitchen. A measuring cup (float) handles liquids with precision. A counting jar (int) tracks whole items like eggs. A label maker (str) prints text. A light switch (bool) is either on or off. You wouldn't pour water into a label maker—and Python won't let you mix types in ways that don't make sense either.

Visual Explanation — The Type Landscape

The four core Python types arranged by category. The top row shows numeric types (int and float), while the bottom row shows text (str) and logical (bool) types. Notice that "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.

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
int ⊕ float → float
Where ⊕ represents any arithmetic operator (+, −, ×, /). Example: 3 + 2.0 produces 5.0 (a float, not an int).
TRUE DIVISION RULE
int / int → float
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
str + str → str
The + operator glues strings together. "py" + "thon" produces "python". You cannot add a string to a number—"age: " + 17 raises a TypeError.
STRING REPETITION
str × int → str
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
You can explicitly convert between types using 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".

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.

Common expressions and their resulting types
ExpressionResultResult TypeWhy?
7 + 310intBoth operands are int, result stays int
7 + 3.010.0floatMixed int and float → promoted to float
7 / 23.5floatDivision always returns a float in Python 3
7 // 23intFloor division with two ints returns an int
"hi" + "!""hi!"strString concatenation joins text
"ab" * 3"ababab"strString repetition repeats the text
5 > 3TrueboolComparison operators return a bool
True + True2intbool is a subtype of int; True = 1, False = 0
Follow this decision flowchart to identify any value's type. Start at the top and answer each question. If the value is in quotes, it's a str. If it's True or False, it's a bool. If it has a decimal point, it's a float. Otherwise, it's an int.

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) > 5
Step-by-Step Type and Value Prediction
1
Step 1 — Identify a and ba = 10 assigns the integer 10 (type: int). b = 3.0 assigns the floating-point number 3.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 + bWe are adding an int (10) to a float (3.0). By the type promotion rule, the int is converted to float before addition. So 10 + 3.0 becomes 10.0 + 3.0 which equals 13.0.
c → 13.0 (float)
3
Step 3 — Evaluate int(c)The int() function converts a float to an integer by dropping the decimal portion. So int(13.0) produces 13 (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) > 5The len() function returns the number of characters in a string. "13 cookies" has 10 characters (including the space). The comparison 10 > 5 evaluates to True. Comparisons always produce a bool.
result → True (bool)

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.

Common type-related mistakes and their fixes
MistakeWhat HappensFix
"Score: " + 95TypeError — cannot add str and int"Score: " + str(95)
Expecting 10 / 2 to give 5It actually gives 5.0 (a float)Use 10 // 2 if you need an int
Using input() without conversioninput() always returns a str, so math failsWrap 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 falseNameError — Python booleans are capitalizedAlways use True and False
✦ KEY TAKEAWAY
When you get a TypeError or an unexpected result, your first question should always be: "What types are my values?" Use 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.

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.

From fundamentals to advanced Python concepts
What You Know NowWhat's Coming Next
A single int like 42A 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 typesUsing 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:

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.

Practice Problems

PROBLEM 1 — CONCEPTUAL
Explain why "7" and 7 are different in Python. What type is each, and what would happen if you tried to evaluate "7" + 7?
PROBLEM 2 — BASIC CALCULATION
What is the value and type of each expression? (a) 15 // 4 (b) 15 / 4 (c) 15 % 4
PROBLEM 3 — INTERMEDIATE
Predict the value and type of result after running this code: x = 5 + 2.0 y = str(x) result = y * 2
PROBLEM 4 — APPLIED
You are building a tip calculator. The user enters a meal cost and a tip percentage using input(). Write a short code snippet that calculates the total and prints it. Explain which type conversions are needed and why.
PROBLEM 5 — CRITICAL THINKING
In Python, 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?

Lesson 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.

Varsity Tutors • Computer Programming 1 (Python) • Python Types — I can distinguish common Python types (int, float, str, bool) and predict expression results at my level.