Python data types are very important in learning and understanding the concept of Python programming.
We use data types to define the type of Variables in Python. Data types are essentially a way of categorising data items so that we know what kind of values they are and what operations can be performed on them while working in Python.
As different data types behave differently in Python, it’s important to understand how and when to use them. In this complete guide, we will look at the major built-in Python data types with examples to help you understand them.
What are Data Types in Python?
In Python, there are different standard data types that define the nature of the data stored by a certain variable. Some of the basic data types that are built in Python include;
Python Data Types | Classes | Includes |
---|---|---|
Numeric Data Type | int, float, complex | data with numeric values |
String Data Type | str | sequence of characters |
Sequence Type | list, tuple, range | an ordered collection of data types |
Mapping Data Type | dict | data in key-value pair |
Boolean | bool | either True or False |
Set | set, frozenset | a collection of unique items |
In the following sections, we will take an in-depth look at each of the following built-in Python data types.
- Numeric Data Type
- Integer
- Float
- Complex Number
- Sequence Type
- String Data Type
- List Data Type
- Tuple
- Boolean
- Set
- Dictionary
Numeric Data Type
Numeric data types are used to represent and store numeric values in Python. These Numeric values can be integers (int), floating numbers (float) or even complex numbers (complex).
Integer Data Type
Integers represent whole numbers in Python and are represented by the int keyword. They can be both positive and negative whole numbers.
You can create an integer data type in Python by using the following syntax;
my_variable = 2
#Check Data Type
print(type(my_variable))
#Output
<class 'int'>
Float Data Type
Float data types represent decimal numbers in Python and are represented by the float keyword. They can be both positive and negative decimal numbers. You can create a float data type in Python by using the following syntax;
x = float(10.5)
Complex Data Type
Complex numbers are represented by the complex keyword in Python and they have real and imaginary parts. The imaginary part is represented by ‘j‘ in Python.
You can create a complex number data type in Python by using the following syntax;
x = complex(1j)
Complex data types are advanced concepts and you might not find it useful as an everyday programmer. It is often used by Physicists!
Sequence Type
As the name suggests, Sequence types are used to represent a sequence of data items in Python. The most common sequence types in Python include Strings, Lists, and Tuples.
String Data Type
Strings are represented by the ‘str‘ keyword in Python and are used to store a sequence of characters. You can create a string data type in Python using single quotes (‘), double quotes (“), and triple quotes (“””).
See the examples of string data type;
#String Data Type Examples
string_example1 = "This is a string"
string_example2 = 'This is also a string'
string1_example3 = """This is a triple quoted string.
You can have multiple lines of text here."""
See the output using the print function
Learn more about String data types in detail in this complete guide to Python Strings.
List Data Type
Lists are represented by the list keyword in Python. They are like arrays in other languages and used to store a collection of items which can be of different data types.
The best thing about Python list type however is that the items in a list don’t need to be of the same type.
You can create a list data type in Python by using square brackets([]) and commas(,) in the following syntax format:
For example;
my_list = [1, 2, 3, 4]
my_list2 = ["John", "Mike", "Smith"]
Tuple Data Type
Tuples are represented by the tuple keyword in Python. They store a sequence of items but unlike lists, tuples are immutable (you can’t change them once they are created).
Like lists, tuples can store items of different data types. You can create a tuple data type in Python by using parenthesis and commas with the following syntax;
x = tuple(("london", "newyork", "tokyo"))
Boolean Data Type
The Boolean data type is used to represent values with two states, either True or False. It is represented by the class bool in Python.
Boolean Objects that are and equal to True are truthy, while those equal to False are falsy.
Boolean values are often used in control structures to determine the flow of execution. For example, if a condition is True, then a certain block of code will be executed. If the condition is False, then that block of code will be skipped.
While boolean values are often associated with conditional statements, they can be used in several other ways. For example, boolean values can be used as flags to track whether a certain condition has been met.
When working with boolean values, it is important to remember that they are not the same as strings or integers. Therefore, they cannot be directly compared to those data types. Instead, you must use comparison operators (such as == or !=) to compare boolean values.
To create a Boolean data type in Python, use the following syntax:
variable_name = bool(value)
For example;
my_boolean1 = bool(True)
my_boolean2 = bool(False)
Set Data Type
The set data type is a Python implementation of the mathematical concept of a finite set.
Sets are unordered collections of unique elements, and can be created in two ways: either by using the set() built-in function, or by using the set literal notation {element1, element2, …}.
Once created, sets cannot be modified; however, new elements can be added with the .add() method, and existing elements can be removed with the .remove() or .discard() methods.
Also, it is important to note that sets are not indexable like other data types in Python; however, they can be used as keys in dictionaries, which we will look in the next section.
The set type is represented by the set keyword in Python.
You can create a Set data type in Python by using the following syntax;
variable_name = set(value)
For example;
my_set = set([1, 2, 3, 4])
Dictionary Data Type
A dictionary is a data type in Python that is used to store data in a key-value format. Keys are unique identifiers for values, and values can be any data type (including other dictionaries).
Dictionaries are mutable, which means they can be modified after they are created. They are typically used to store data that needs to be accessed quickly, such as the results of a database query.
However, they can also be used to store data that does not need to be accessed quickly, such as a list of all the words in a document.
When choosing a data type for storing data, it is important to consider how the data will be accessed. If the data will be accessed frequently, using a dictionary may be more efficient than using a list.
Conversely, if the data type is not going to be accessed frequently, using a list may be more efficient than a dictionary.
The dictionary type is represented by the ‘dict‘ keyword in Python.
You can create a Dictionary data type in Python by using the following syntax;
variable_name = dict(key1=value1, key2=value2 …)
For example;
my_dict = dict(name="John", age=21, address="123 Main Street")
Python Data Type Conversion
It is possible to convert the built-in Python data types that we discussed in the above sections from one data type to another. You can convert to your desired data types by simply using the data name as the function, a process which is referred to as casting in Python.
As data types behave differently in Python, you might have to convert one data type to another before you can actually use certain functions.
For example, you cannot use the + operator on a string and an integer value unless you convert the integer to a string.
In this case, you would have to cast the integer value to a string using the str() function before adding it to the other string.
Data Type Conversion Functions
The following table shows some of the most common data type conversion functions in Python;
Function | Description |
---|---|
int(x) | Converts x to an integer data type |
float(x) | Converts x to a floating point number |
str(x) | Converts object x to a string representation |
repr(x) | Converts object x to an expression string |
hex(x) | Converts integer x to a hexadecimal string |
oct(x) | Converts integer x to an octal string |
Python Data Type Conversion Examples
Let’s take a look at some examples of data type conversion in Python. In the first example, we convert an integer value to a floating point value.
Confirming Data Type with type() Function
As you saw in the examples above, we use the type() function to display and confirm the data type if necessary.
This can be handy if you need to confirm the data type at any point in your program.
For example, you might need to convert a string data type to an integer for mathematical operations. However, you may want to confirm that the input is actually a string before conversion. You can use the type function to do this as follows;
type(value)
You can further use the print() function to display the data type of the value entered if required.
print(type(value))
Python Data Types FAQs
Here are some of the most frequently asked questions about Python Data Types to help you improve your understanding of the basics.
-
What are the data types in Python?
Data types are a way of classifying data items so that we know what type of values they are and what operations can be performed on them while working in Python.
-
What are the main data types in Python?
Numeric data types such as strings and floats and Sequence type data types such as Strings, and Lists are the most common data types in Python. However, there are other data types such as Boolean, Set, and Dictionary that can be important in some use cases.
-
What is the difference between a mutable and an immutable data type?
Mutable data types can be changed after they are created, while immutable data types cannot.
-
Can we convert one data type to another in Python?
Yes, we can convert one data type to another in Python by using the built-in function called type().
-
What is the difference between a list and a tuple in Python?
Lists are mutable, meaning they can be changed after they are created. Tuples are immutable, meaning they cannot be changed after they are created.
-
What is the difference between a list and an array in Python?
Python doesn’t have arrays, but it has lists. Lists are very similar to arrays, but they can contain elements of different data types, whereas arrays can only contain elements of the same data type.
Start Learning Python Data Types in Detail
This was a short overview of all the Python built-in data types. You can continue learning more about them in our detailed individual guides.
- Python Numbers Guide
- Python Strings Guide
- Python Lists Guide
To recap, here’re the most important built-in data types that you can use in Python.
Python Data Type | Description |
---|---|
str | string, sequence of letters, numbers and symbols |
int | integers |
float | floating point numbers |
bool | boolean values: True or False |
complex | complex numbers with real and imaginary components |
list | list, formed with [ ] |
dict | dictionary, formed with {'key'=value} |
tuple | an immutable list, formed with ( ) |
As you go through your learning journey, be sure to check out our other Python guides and other structure learning materials.