Basic Types: int, float, str, bool, None
Python has five fundamental built-in types that you'll use constantly. Understanding how they work โ and how they differ from each other โ is essential for writing correct Python code.
Integers (int)
Integers are whole numbers with no decimal point. Python's integers have arbitrary precision โ they can be as large as your memory allows, unlike C or Java where integers overflow.
Note that / always returns a float in Python 3, even if the result is a whole number:
You can write integer literals with underscores for readability:
Floating-Point Numbers (float)
Floats represent real numbers with a decimal point. Python uses IEEE 754 double-precision floating-point (64-bit).
The Floating-Point Precision Trap
Floats cannot represent all decimal values exactly because they're stored in binary:
This is not a Python bug โ it's how IEEE 754 works in all languages. To compare floats safely:
Special float values:
Strings (str)
Strings are sequences of Unicode characters. Python has no separate char type โ a single character is just a string of length 1.
String immutability โ you cannot change a string in place:
Booleans (bool)
Booleans represent truth values. In Python, bool is actually a subclass of int:
Truthiness and Falsiness
Every Python value has a boolean interpretation. These values are falsy (evaluate to False):
None
None is Python's null value โ it represents the absence of a value. It's the single instance of the NoneType class.
The type() Function
type() returns the type of any value:
isinstance() โ Type Checking
isinstance() is the preferred way to check types because it handles inheritance:
Type Coercion / Conversion
You can convert between types using built-in functions:
Summary Table
| Type | Example | Mutable? | Notes |
|---|---|---|---|
int | 42 | No | Arbitrary precision |
float | 3.14 | No | IEEE 754 double |
str | "hello" | No | Unicode characters |
bool | True | No | Subclass of int |
NoneType | None | No | Singleton |
All basic types are immutable โ operations on them always create new objects rather than modifying in place.
Knowledge Check
What is the result of 10 / 2 in Python 3?
Which of these values is falsy in Python?
What is the correct way to check if a variable x is None?