Python Data Types
3 min read ·
In Python, data types define the type of data a variable can store.
Python automatically assigns a data type when a value is assigned to a variable.
Understanding data types is essential for writing correct, efficient, and error-free programs.
What Are Data Types?
Data types specify:
- What kind of value a variable holds
- What operations can be performed on that value
- How much memory is required
Python is a dynamically typed language, meaning you don’t need to declare the data type explicitly.
Built-in Data Types in Python
Python provides several built-in data types, grouped into categories:
| Category | Data Types |
|---|---|
| Text Type | str |
| Numeric Types | int, float, complex |
| Sequence Types | list, tuple, range |
| Mapping Type | dict |
| Set Types | set, frozenset |
| Boolean Type | bool |
| Binary Types | bytes, bytearray, memoryview |
| None Type | NoneType |
Numeric Data Types
Integer (int)
Used to store whole numbers.
Float (float)
Used to store decimal numbers.
Complex (complex)
Used to store complex numbers.
Text Data Type
String (str)
Used to store text data.
Boolean Data Type
Boolean (bool)
Used to store True or False values.
Sequence Data Types
List (list)
Used to store multiple values in a single variable.
Lists are ordered and mutable.
Tuple (tuple)
Used to store multiple values.
Tuples are ordered but immutable.
Range (range)
Used to generate a sequence of numbers.
Mapping Data Type
Dictionary (dict)
Stores data in key-value pairs.
Set Data Types
Set (set)
Stores unique values.
Sets are unordered and mutable.
Frozenset (frozenset)
Immutable version of a set.
Binary Data Types
Bytes (bytes)
Immutable sequence of bytes.
Bytearray (bytearray)
Mutable sequence of bytes.
None Data Type
NoneType
Represents absence of a value.
Get the Data Type
Use the
type() function to check the data type.Dynamic Typing in Python
Python allows changing the data type of a variable at runtime.
Exercise
- Create one variable for each data type
- Use
type()to check them - Change a variable’s data type and observe the result