Nearby lessons
14 of 159Python - Data Types
- Understand what data types are and why they matter
- Learn all built-in Python data types
- Know how to check the type of any variable
- Understand dynamic typing in Python
Introduction
When you write Python code, you store values in variables. But here is an important question — how does the computer know if a value is a number, text, or something else?
The answer is: Data Types.
A data type tells Python what kind of value you are storing — is it a whole number, a decimal, text, or something else?
Example of Dynamic Typing
Built-in Data Types in Python
| Data Type | Description | Example |
|---|---|---|
| int | Integer numbers | 10 |
| float | Decimal numbers | 10.5 |
| complex | Complex numbers | 3+5j |
| bool | Boolean values | True |
| str | Text/String | "Hello" |
| list | Ordered mutable collection | [1,2,3] |
| tuple | Ordered immutable collection | (1,2,3) |
| set | Unordered unique values | {1,2,3} |
| dict | Key-value pairs | {"a":1} |
| bytes | Immutable binary data | bytes([1,2]) |
| bytearray | Mutable binary data | bytearray([1,2]) |
| range | Sequence of numbers | range(5) |
| frozenset | Immutable set | frozenset({1,2}) |
| None | No value | None |
Important Built-in Functions
Python provides several built-in functions related to data types.
1. type() Function
The type() function returns the data type of a variable.
2. id() Function
The id() function returns the identity (memory reference) of an object.
Note: The returned value may be different on different systems.
Everything is an Object
In Python, everything is an object — even numbers and strings.
This means every value in Python has:
- An identity (memory address)
- A data type
- A value
id() function to see the memory address of any value, and type() to see its data type.
Classification of Python Data Types
Python data types can be grouped into the following categories.
1. Numeric Data Types
Numeric data types store numbers.
- int
- float
- complex
2. Boolean Data Type
The bool data type stores True or False.
3. String Data Type
The str data type stores text.
4. Sequence Data Types
Sequence data types store multiple values in order.
- list
- tuple
- range
5. Set Data Types
Set data types store unique values.
- set
- frozenset
6. Mapping Data Type
The dict data type stores data as key-value pairs.
7. Binary Data Types
Binary data types store binary values.
- bytes
- bytearray
8. None Data Type
None represents the absence of a value.
- Python has built-in data types: int, float, str, bool, list, tuple, dict, set
- You do not need to declare the data type — Python figures it out automatically
- Use the type() function to check what type of data a variable holds
- Python is called a Dynamically Typed Language
- Each data type has its own rules for how values are stored and used