Python Data Types
In this module of the Python tutorial, we will learn about the Python data types.We will further learn about the dynamic typing feature in Python and different data types in Python such as Numbers, Strings, Lists, Tuples, Sets etc.
Python Data Types: An Overview
One of the most crucial part of learning any programming language is to understand how data is stored and manipulated in that language. Users are often inclined toward Python because of its ease of use and the number of versatile features it provides. One of those features is dynamic typing.
Watch this ‘Python Data Types’ video:
In Python, unlike statically typed languages like C or Java, there is no need to specifically declare the data type of the variable. In dynamically typed languages such as Python, the interpreter itself predicts the data type of the Python Variable based on the type of value assigned to that variable.
In C, the declaration of any variable might look like the following code block:
/* C code block */
int sum = 0
for (int i=0;i<10;i++)
{
sum += i;
}
The equivalent code for the above operation in Python would look like the following code block:
sum = 0
for i in range (10):
sum += i
As we can observe from the above example, in C, all variables are explicitly declared with their respective data types; while in Python, a variable is directly taken and then just assigned a value to it rather than declaring the data type of that variable. In Python, the data types of variables are dynamically inferred. We can assign any value to the variable, and the Python interpreter will identify the data type. We can also re-assign a different type of value to an already assigned variable, unlike in C or Java where if we have declared a variable to be an integer and then assign a string value to it then we will get an error.
Example:
# Python code block
a = 10
a = ‘Intellipaat’
The equivalent code for the above operation in C would look like:
/* C code Block */
int a = 10;
a = ‘Intellipaat’; //fails
This type of flexibility that comes as a result of being a dynamically typed language is one of the reasons that Python is considered to be easy and convenient to learn and use.
Core Data Types in Python
As the name suggests, a data type is the classification of the type of values that can be assigned to variables. We have already seen that, here in Python, we don’t need to declare a variable with explicitly mentioning the data type, but it’s still important to understand the different types of values that can be assigned to variables in Python. After all the data type of a variable is decided based on the value assigned.
Python data types are categorized into two as follows:
- Mutable Data Types: Data types in python where the value assigned to a variable can be changed. Some mutable data types in Python include set, list, user-defined classes and dictionary.
- Immutable Data Types: Data types in python where the value assigned to a variable cannot be changed. Some immutable data types in Python are int, decimal, float, tuple, bool, range and string.
Get 100% Hike!
Master Most in Demand Skills Now!
Following diagram lists the data types that fall under the categories of the mutable and immutable data types in python:
Let’s discuss the above-mentioned core data types in Python.
- Numbers: The number data type in Python is used to store numerical values. It is used to carry out normal mathematical operations.
- Strings: Strings in Python are used to store textual information. They are used to carry out operations that perform positional ordering among items.
- Lists: The list data type is the most generic Python data type. Lists can consist of a collection of mixed data types, stored by relative positions.
- Tuples: Python Tuples are one among the immutable Python data types that can store values of mixed data types. They are basically a list that cannot be changed.
- Sets: Sets in Python are a data type that can be considered as an unordered collection of data without any duplicate items.
- Dictionaries: Dictionaries in Python can store multiple objects, but unlike lists, in dictionaries, the objects are stored by keys and not by positions.
Built-in Data Types in Python
The categories of built-in data types in Python are:
- Binary Types – bytes, memory view, bytearray
- Mapping Type – dict
- Numeric Type – int, float, complex
- Text Type – str
- Boolean Type – bool
- Set Types – set, frozenset
- Sequence Types – list, range, tuple
How to check Data Type in Python
Now that you know about the built-in data types and the core data types in Python, let’s see how to check data types in Python.
You can get the data type of any object by using the type() function:
This brings us to the end of this module in Python Tutorial. The above-mentioned Python data types will be further discussed in detail in their respective separate modules. Let’s meet there!
Meanwhile, take a look at Intellipaat’s Python Certification Course, along with our Python Interview Questions For Freshers.