Python Types
int, float, bool, str
Master the four fundamental data types in Python: understand how the interpreter classifies every value and why it matters for your code.
What Is a Data Type?
Every value in Python belongs to a type. A type tells the interpreter what kind of data you are working with and what operations are allowed on it. Think of types as labels that determine the rules.
Why Types Matter
Python is dynamically typed: you don't declare types explicitly, but the interpreter tracks them behind the scenes. Knowing types prevents bugs and unlocks the right operations.
Dynamic Typing in Action
A variable in Python is just a name that refers to a value. The same name can point to different types at different times.
The int Type
An int (integer) represents whole numbers with no decimal point. Python integers have unlimited precision: they can be as large as your memory allows.
The float Type
A float stores decimal (floating-point) numbers. Internally, Python uses 64-bit IEEE 754 representation, which means floats have limited precision.
The bool Type
A bool (boolean) has exactly two possible values: True and False. Booleans are the backbone of conditions and control flow in Python.
The str Type
A str (string) is a sequence of characters, enclosed in single quotes '...' or double quotes "...". Strings are immutable: once created, their contents cannot change.
int vs float: Key Difference
Type Conversion
Python provides built-in functions to convert between types. This is sometimes called casting.
How to Check and Convert
Example 1: Basic Expressions
Predict the type and value of each expression, then verify with type().
Example 2: String Operations
The + operator behaves differently for strings than for numbers. Let's explore.
Example 3: Boolean Logic
Every Python value has a truthiness: it can be interpreted as True or False in a boolean context.
Common Mistakes
Summary Table
| Type | Example | Mutable? | Key Operation |
|---|---|---|---|
| int | 42, -7, 0 | Immutable | +, -, *, //, **, % |
| float | 3.14, -0.5 | Immutable | +, -, *, /, round() |
| bool | True, False | Immutable | and, or, not |
| str | "hello", '42' | Immutable | +, *, [ ], .upper() |
Question 1 of 5
What is the type of the expression 10 / 5 in Python?
Question 2 of 5
What does type(True) return?
Question 3 of 5
Which of the following will cause a TypeError?
Question 4 of 5
What is the value of bool("")?
Question 5 of 5
What does int(7.9) return?