Python Data Types
Python data types are used to represent various kinds of data. We can do classification and categorization of data by using data types. We create variables in Python and variables used to store values. Every value has a data type in Python. The interpreter reads that data type and processes begin further according to that data type. For example:
Code:
text = 'Python Programming'
Here text
is a variable that stores a value. Now this value has a data type. This is a string data type. Just like strings, Python has several data types. We will learn about them further in this tutorial.
Python is a dynamically typed language. It means when we are creating variables, we don’t have to specify the data type. We don’t have to declare that we are going to use a particular data type with this variable. Just like in the upper example, when we created a variable, we didn’t define that this variable has a string data type. At the time of the execution of this program, the interpreter defined the data type according to the value we used.
So, data types are used to represent the data in Python. We can use any kind of data, like text, numeric value, binary data, boolean value, etc. Like in the upper example, we use text data as a value.
Technical definition of data type: “Every value in Python is related to a data type. Python is an object-oriented language so everything is treated as an object in Python and we use classes to create objects. So we can say data types are kinds of classes and variables are their instances (example).”
How to Find Data Types in Python?
As we learned in the upper section, every value has a data type in Python. To check the data type of the values, we can use the built-in function type()
. For example.,
Code:
text = 'Python Programming'
print(type(text))
Output:
<class 'str'>
Here text is a variable. It contains a text value. We checked the data type with the help of the type()
function. We simply write the variable name in the parameter of type(text)
. In the output we get 'str'
class. str represents string data type. It means this variable contains a string data type. We can check the other values with type()
. For example,
Code:
info = 100
values = True
first_list = ['Python', 'Java']
print(type(info))
print(type(values))
print(type(first_list))
Output:
<class 'int'>
<class 'bool'>
<class 'list'>
So this is how we can use type()
function to check the data type of any values.
How Many Kinds of Data Types in Python?
Python has several kinds of data types. On the internet, Python data types are divided into different categories. In some places, data types are divided into primitive and non-primitive data types.
Primitive Data Types
Data types that are predefined in Python are called primitive data types. Such data types have a fixed size to hold values. These are fundamental data types. Mostly primitive data types are used in complex data structures or complex data types. For example, Strings, Boolean, Float, Integers, None are examples of primitive data types.
Non-Primitive Data Types
Non-primitive data types are not predefined. We can store multiple values in this data type, and we can also change the order of those values. Mostly non-primitive data types are used to store values or collections of values. For example, Lists, Tuple, Dictionaries, Set are examples of non-primitive data types.
In some places, data types are divided into mutuable and immutable types. The main difference is that we can change the values in mutable data types after creation but not in immutable data types. For example, Tuple is an immutable data type. Once we set a value in a tuple, we cannot change that value.
Most data types are divided into basic data types. Or it is also called built-in or standard data types. We can divide data types into the following categories:
Text Data Type in Python
A string data type is considered a text data type. In some places, the string data type is considered a sequence data type too.
String data type in Python
Strings are a sequence of characters, surrounded by the quotes. We can use text, numeric value, or any kind of character, but inside the quotes. So anything we write inside the quotes makes a string. For example:
Code:
text1 = 'Hello Python'
text2 = '100'
print(type(text1), type(text2))
Output:
<class 'str'> <class 'str'>
Here text1
and text2
are string data types. However, we write different kinds of values; in text1
we write normal text and in text2
we write numbers. But we surrounded numbers with quotes to make this a string. So whenever we write a string, we always use quotes. We can use any type of single, double, or triple quotes in strings. For example:
Code:
text1 = 'Hello Python'
text2 = "Hello Python"
text3 = '''Hello Python'''
print(text1, text2, text3)
print(type(text1), type(text2), type(text3))
Output:
Hello Python Hello Python Hello Python
<class 'str'> <class 'str'> <class 'str'>
String represented 'str'
class. We can find the data type by using type()
function. A string is an immutable data type in Python.
Click to learn more about Python quotes
Sequence Data Type in Python
List, tuple, and range are considered as sequence data types. In some places, strings are also included as a sequence data type.
List data type in Python
A list is a kind of container in which we can store different kinds of values. We use square brackets [] to create a list. And we can separate the values by comma (,). Lists are mutables; we can modify the list values.
Code:
first_list = ['Python', 'Java', 100, True]
print(type(first_list))
print(first_list)
Output:
<class 'list'>
['Python', 'Java', 100, True]
We can store any kind of value in a list. Here we created a list named first_list. We can see the type; we get class ‘list’ in the result. Lists are indexed and ordered, so we can access the list items by their index position. For example:
Code:
first_list = ['Python', 'Java', 100, True]
print(first_list[0])
print(first_list[1])
print(first_list[2])
print(first_list[3])
Output:
Python
Java
100
True
Tuple data type in Python
A tuple is almost the same as a list. We can store multiple values in a tuple. We use parentheses () to create a tuple in Python. And same as a list, we use a comma (,) to separate values in a tuple. The main difference between a tuple and a list is that lists are mutable but tuples are immutable in nature. For example:
Code:
first_tuple = ('Hello', 100, 'Python', 'Java')
print(type(first_tuple))
print(first_tuple)
Output:
<class 'tuple'>
('Hello', 100, 'Python', 'Java')
We get the class ‘tuple’ in the result. Same as a list, we use different kinds of values to store in a tuple. We can also access single values, like a list.
Code:
first_tuple = ('Hello', 100, 'Python', 'Java')
print(first_tuple[0])
print(first_tuple[1])
print(first_tuple[2])
print(first_tuple[3])
Output:
Hello
100
Python
Java
Now here is an interesting fact.
Code:
first = 'Hello' 'Python'
second = 'Hello', 'Python'
Here we created two variables and it looks like they have the same values. But there is a miner difference between both values. We used a comma to separate values in the second variable. Now if we check the data type of both variables.
Code:
first = 'Hello' 'Python'
second = 'Hello', 'Python'
print(type(first))
print(type(second))
Output:
<class 'str'>
<class 'tuple'>
We get the string class for first variable
and tuple class for second variable.
In the second variable, we separated values by commas and we got the tuple as a result. If we use only one value but put a comma at the end, we get the tuple data type in the output. For example:
Code:
first = 'Hello'
second = 'Hello',
print(type(first))
print(type(second))
Output:
<class 'str'>
<class 'tuple'>
In the second variable, we put a comma after the 'Hello'
and we get a tuple in the result. And also, we didn’t use any parenthesis. So we can simply write a tuple without any parenthesis too.
Range data types in Python
range is a sequence data type in Python. Also, we can use range as a built-in function in Python. We use the range function to iterate the values with loops. range is an immutable data type. For example:
Code:
for values in range(3):
print(values)
Output:
0
1
2
Here we use range as a built-in function with a for loop.
End of Python Data Types (Part 1)
4 thoughts on “Python Data Types (Part 1): What is a data type in Python?”