Data Types in Python: Complete Beginner's Guide
Python data types define the kind of value a variable stores and determine the operations that can be performed on it. Whether you're working with numbers, text, or Boolean values, understanding data types is essential for writing accurate and efficient Python programs. In this tutorial, you'll learn the built-in Python data types, how to identify them using the type() function, and the difference between mutable and immutable data types.
What Are Python Data Types?
A Python data type is a classification that tells the interpreter what kind of value a variable stores — a number, text, a true/false state, or something else — and what operations are valid on it.
Every value in Python belongs to exactly one python data type. This affects behavior directly: you can add two numbers together, but adding a number to text raises an error unless you convert one of them first.
age = 25 # int
price = 19.99 # float
name = "Alice" # str
is_active = True # boolPython determines the type automatically from python variables and data types work together this way — you assign a value, and Python infers what kind of data it is.
Why are Data Types Important in Python?
Data types determine what operations are valid on a value, how much memory it uses, and how it behaves when combined with other values.
Without correctly understanding data types, you'll face problems and errors like trying to add a number to a string, or unexpected results from comparing incompatible values. Data types matter because they:
- Define which operations (math, concatenation, comparison) are valid
- Affect how data is stored and processed in memory
- Determine how values behave in conditions, loops, and functions
- Prevent silent bugs caused by mixing incompatible types
quantity = 5
label = "items"
print(quantity + label) # TypeError: unsupported operand type(s)Overview of Python's Built-In Data Types
Python ships with several built-in data types in python that cover the vast majority of beginner programming needs.
| Data Type | Example | Description |
|---|---|---|
| int | 10 | Whole numbers, positive or negative |
| float | 10.5 | Numbers with a decimal point |
| str | "hello" | Text, sequences of characters |
| bool | True | Logical true/false value |
| complex | 3 + 4j | Numbers with a real and imaginary part |
Python also has built-in collection types (list, tuple, dict, set) covered in a separate tutorial, but this guide focuses on the core scalar types above.
What Is the int Data Type in Python?
The python int type represents whole numbers — positive, negative, or zero — with no decimal component and no fixed size limit.
Unlike many languages, Python integers can grow arbitrarily large without overflow errors, limited only by available memory.
population = 33807403
temperature_below_zero = -15
zero_value = 0
print(type(population)) # <class 'int'>Integers support standard arithmetic — addition, subtraction, multiplication, division, and exponentiation:
result = 10 ** 3
print(result) # 1000What Is the float Data Type in Python?
The python float type represents numbers that include a decimal point, used for measurements, prices, and any value requiring fractional precision.
price = 49.99
pi_approx = 3.14159
temperature = -2.5
print(type(price)) # <class 'float'>Note:
Float arithmetic can introduce small rounding errors due to how decimals are stored in binary. For example, 0.1 + 0.2 evaluates to 0.30000000000000004, not exactly 0.3.
What Is the str Data Type in Python?
The python string (python str) type represents text, written as a sequence of characters enclosed in single, double, or triple quotes.
first_name = 'Jack'
message = "Hello, world!"
paragraph = """This spans
multiple lines."""
print(type(message)) # <class 'str'>Strings support indexing, slicing, and concatenation:
greeting = "Hello" + " " + "World"
print(greeting) # Hello World
print(greeting[0]) # H
print(greeting[0:5]) # HelloTip:
Use f-strings for readable string formatting: f"Hello, {first_name}!".
What Is the bool Data Type in Python?
The python bool (python boolean) type represents one of exactly two values — True or False — and is used for logical conditions and comparisons.
is_logged_in = True
has_permission = False
print(type(is_logged_in)) # <class 'bool'>Booleans are the direct result of comparison operators and are central to if statements and loops:
age = 20
is_adult = age >= 18
print(is_adult) # TrueNote:
In Python, bool is technically a subclass of int — True equals 1 and False equals 0 when used in arithmetic.
What Is the complex Data Type in Python?
The python complex type represents numbers with a real part and an imaginary part, written with a trailing j for the imaginary component.
Complex numbers are mainly used in scientific, engineering, and mathematical computations rather than everyday programming.
z = 3 + 4j
print(type(z)) # <class 'complex'>
print(z.real) # 3.0
print(z.imag) # 4.0You can perform standard arithmetic on complex numbers directly:
a = 2 + 3j
b = 1 - 1j
print(a + b) # (3+2j)How Does the type() Function Work in Python?
The python type() function returns the data type of any value or variable, making it the standard way to inspect what kind of data you're working with.
x = 42
y = 3.14
z = "text"
w = True
print(type(x)) # <class 'int'>
print(type(y)) # <class 'float'>
print(type(z)) # <class 'str'>
print(type(w)) # <class 'bool'>type() is especially useful when debugging unexpected behavior, such as a TypeError, since it lets you confirm exactly what type a variable holds at runtime.
value = input("Enter a number: ")
print(type(value)) # <class 'str'> — input() always returns a stringMutable vs Immutable Data Types in Python
Python data types fall into two categories based on whether their value can change after creation: mutable data types in python can be modified in place, while immutable data types in python cannot.
All the scalar types covered above — int, float, str, bool, and complex — are immutable. Once created, their value cannot be changed; any "modification" actually creates a new object.
name = "Alice"
print(id(name))
name = name + " Smith"
print(id(name)) # different id — a new string object was createdCollection types like list and dict are mutable — they can be changed without creating a new object:
numbers = [1, 2, 3]
print(id(numbers))
numbers.append(4)
print(id(numbers)) # same id — the original list was modified in place| Aspect | Immutable | Mutable |
|---|---|---|
| Can change after creation | No | Yes |
| Examples | int, float, str, bool, complex, tuple | list, dict, set |
| "Modifying" creates | A new object | Modifies the same object |
| Safer for shared references | Yes | Requires caution |
Conclusion
Python data types define what kind of value a variable holds and what you can do with it, from basic arithmetic with int and float to text handling with str and logic with bool. Because Python infers types automatically, understanding the built-in types — and whether each is mutable or immutable — is essential for writing predictable, bug-free code. Use the type() function whenever you're unsure what you're working with, and convert values explicitly rather than relying on Python to guess your intent. Mastering Python data types early makes every later topic, from functions to data structures, far easier to learn.