Data Types in Python
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 (Python Numbers, Python String, Python List, Python Tuple & Python Set)
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.
Go through this Python Course in London to get a clear understanding of Python!
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.
# 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.
Learn more about Python from this Python Training in New York to get ahead in your career!
Standard 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
- Immutable Data Types: Data types in python where the value assigned to a variable cannot be changed
Following diagram lists the data types that fall under the categories of the mutable data type and the immutable data type.
Let’s discuss on the above-mentioned standard data types in Python.
- Numbers:The number data type in Python is used to store numerical values. It is used to carry out the 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: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.
Interested in learning Python? Check out the Python Training in Sydney!
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.