5 Data Types in Python
- Know the different data types in python
- Understand that different data types can have advantages and disadvantages
- Be able to choose when to use different data types
- Specify and change data types
- Be aware that packages and modules can enable access to more data types and structures
5.1 Inbuilt Data Types
In programming, data type is an important concept. Variables can store data of different types, and different types can do different things.
Python has the following data types built-in without needing any other libraries:
Simple/primitive data types:
None Type:
NoneType
Boolean Type:
bool
Text Type:
str
Numeric Types:
int
,float
,complex
Binary Types:
bytes
Composite Datatypes / data structures
Sequence Types:
list
,tuple
,range
Set Types:
set
,frozenset
Mapping Type:
dict
Binary composite type:
bytearray
,memoryview
We will also look at the numpy arrays and pandas dataframes, you can also use the decimal module for correctly rounded arithmetic
In Python, each data type has specific use cases and trade-offs. Let’s look at each category and examine when it might be used, along with its benefits and disadvantages:
Category | Type | Common Use Case | Benefits | Disadvantages |
---|---|---|---|---|
None Type | NoneType | Represents absence of value | Useful for optional values and placeholders | Can cause errors if operations expect a value |
Boolean | bool | Represents True or False | Simple and efficient for control flow | Limited to two states (True/False) |
Text | str | Text representation | Immutable, Unicode support- multilingual, rich manipulation methods | Inefficient for heavy modifications |
Numeric | int | Represents whole numbers | No precision issues | Limited to integers |
float | Represents decimal numbers | Supports real numbers | Precision issues due to storage | |
complex | Represents complex numbers | Direct support for complex arithmetic | Rarely needed in general programming | |
Sequence | list | Mutable sequence of items | Flexible, dynamic size, rich methods | Slower resizing operations |
tuple | Immutable sequence of items | Memory-efficient, safe | Cannot change values once set | |
range | Sequence of numbers | Memory-efficient | Less flexible than lists | |
Mapping | dict | Key-value pairs | Fast lookups, flexible types | Higher memory usage due to hashing |
Set | set | Unordered collection of unique items | Fast membership testing; | Unordered in the Python specification; no indexing |
frozenset | Immutable set | Hashable, can be used as dictionary keys | Cannot modify after creation | |
Binary | bytes | Immutable binary data | Efficient, compact for binary data | Not human-readable, cumbersome for some operations |
bytearray | Mutable binary data | Allows modification of binary data | Consumes more memory than bytes | |
memoryview | Efficient access to binary data without copying | No data duplication | More complex to use |
Custom Data types
- In python it is possible to define custom data types with custom syntax and behaviours. Many of the common desired data types have already been made for you by others.
5.2 Initialising and converting between data types
Different data types have different syntax. You will notice that all inbuilt data types in python have inbuilt functions which can be used to convert to that data type or specify as that data type.
It is not possible to convert between all data types; some conversions require additional information and input.
Resources about data types can be found in the python documentation: https://docs.python.org/3/library/functions.html https://docs.python.org/3/library/stdtypes.html
Examples of converting between data types:
1. Converting int to float
x = 5 # integer
y = float(x) # converting to float
print(y)
Output:
5.0
2. Converting float to int
x = 3.23 # float
y = int(x) # converting to int
print(b)
Output:
3
3. Converting int to str
x = 10 # integer
y = str(x) # converting to string
print(y)
Output:
'10'
4. Converting str to int
x = "123" # string
y = int(x) # converting to int
print(y)
Output:
123
5. Converting str to float
x = "45.67" # string
y = float(x) # converting to float
print(x)
Output:
45.67
6. Converting list to tuple
my_list = [1, 2, 3] # list
my_tuple = tuple(my_list) # converting to tuple
print(my_tuple)
Output:
(1, 2, 3)
7. Converting tuple to list
my_tuple2 = (4, 5, 6) # tuple
my_list2 = list(my_tuple2) # converting to list
print(my_list2)
Output:
[4, 5, 6]
8. Converting int to bool
num_bool = 0 # integer
bool_value = bool(num_bool) # converting to boolean
print(bool_value)
Output:
False
5.3 Summary
There are many in-built data types in python. We will explore them further in the next few sections. It is important to use the correct data type for your use case
- Use the table to remember the key information about data types
- Check the python documentation for further information on data types and in-built functions
- It is important to use the correct data types for your use case rather than the same data type all the time