Support Our Site

To ensure we can continue delivering content and maintaining a free platform for all users, we kindly request that you disable your adblocker. Your contribution greatly supports our site's growth and development.

Data Types In Python

4 min read

In Python, variables are objects, data types are the classes and these variables are the instance of these classes.

There are different kinds of data types in Python which specifies what kind of values can be assigned to a variable.  We don't need to declare the datatypes while assigning variables, Python being dynamic programming language will do that automatically

In this article, we will go over the essential data types in Python.

String Data Type

A string is identified as a continuous set of characters wrapped in quotation marks. Python allows either pair of single or double quotes. Strings are an immutable sequence data type, i.e. each time one makes any changes to a string, entirely new string object is created.

You can either go for single quotes or double quotes, but you should be consistent with it in the entire program.

my_string = "This is my awesome String"
This is my awesome String

You can also perform various actions on Strings,

>>> print(my_string[0])
 # to print the first letter
>>> print(my_string[0:4])
 # to print first four letters

Numbers data type

In Python, there are 3 numeric data types - Integer, Float, and Complex. Integer and Float being the most used datatype.

Integers - Any whole number can be classified as an Integer sometimes it is addressed as an int. There is no limit on how long the integer value can be.

Float numbers - Any real number along with a decimal can be classified as a Float number. 12 is an integer, but 12.0 is a float.

Complex -  Complex numbers are written in the form, x + yj, where x is the real part and y is the imaginary part.

We don't need to declare the datatypes while assigning variables, Python being dynamic language will do that automatically. When you enter a number without a decimal Python will treat it as an integer, and when you enter a number with a decimal, then python will treat it as a Float number.

We can assign numbers to variables directly,

int_num = 100 # int value
float_num = 100.269 # float value
complex_num = 4.18j # complex value

We can also change the data type of a variable in Python,

>>> x = 23.45
>>> int(x)23


A boolean data type can have one of the following Two values, True or False.

We can get boolean output on basic math operations:

>>> 6 > 9
>>> 6< 9

We can also assign this to a variable; we will see how to use boolean datatype effectively in our Django tutorials.


A list contains items separated by commas and enclosed within square brackets []. Lists are almost similar to arrays in C. One difference is that all the items belonging to a list can be of different data types.

Each value inside a list is called an item, Lists are very flexible data type because they are mutable which means items can be added, deleted or updated in a list.

We can make a list of similar data types,

int_list = [  1, 2, 3, 4]
string_list = [ "this" , "is" , "a", "list" ]
float_list = [ 0.1, 0.2, 0.3, 0.4]

We can print them out by calling the print function. Additionally, we can also make a list of different data types.

>>> list = [12345, "some_text", 123.456, "moretext"]
>>> print(list)
[12345, 'some_text', 123.456, 'moretext']


Similar to List tuple are also used to group data of same or different data types but they immutable ordered sequence of items, i.e. like the list we can't update or delete items of a tuple.

Tuples are enclosed in parenthesis (), a tuple looks like:

>>> my_tuple = (2, 2.8, "sometext")
>>> print(my_tuple)
(2, 2.8, 'sometext')

We can always verify the data type of any variable with type() function,

 >>> type(my_tuple)
<class 'tuple'>


Dictionary is an unordered pair of key and values. It is used to store related data.

Dictionary is heavily optimized for storing data and retrieving the data from keys. It is enclosed in curly braces {} and values can be assigned and accessed using square brackets[].

A dictionary looks like this :

user = {'email' : '[email protected]'}

Apart from curly braces, there is a colon (:) in the dictionary, The word in the left of the colon is Key and the word at right is the Value of that key.

We can use the key to retrieve the data but not the other way around. To retrieve a data from the dictionary, we need to pass the key in square brackets[] like this,

 >>> print(user['email'])
[email protected]

Don't forget to wrap the keys in either single or double quotes.

A large dictionary can look something like this:

user = { 'email' : '[email protected]', 'password' : 'somepass', 'country' : 'India', 'name' : 'Jhon' }

Each key-value pair is separated by a comma(,). We can retrieve all the keys and values like this:

 >>> print(user.keys())
dict_keys(['email', 'password', 'country', 'name'])
>>> print(user.values())
dict_values(['[email protected]', 'somepass', 'India', 'Jhon Cena'])

To Print specific value we need to pass the key in square brackets like this:

 >>> print(user['country'])

Now that you about all the Python data types, You should learn how to convert datatypes in Python.


Latest Articles

Latest from djangocentral

How to Use Subquery() in Django With Practical Examples

In the realm of web development, Django stands as a powerful and versatile framework for building robust applications. One of the key aspects of developing efficient and optimized web applications is handling database queries effectively. In this article…
Read more →

4 min read

DRF Serializer: Handling OrderedDict and Converting It to a Dictionary or JSON

In Django Rest Framework (DRF) tests, when you access, you might encounter an OrderedDict instead of a regular dictionary. This behavior is intentional and reflects the design of DRF's serialization process.Understanding the Problem The u…
Read more →

3 min read

Django Rest Framework CheetSheet: Mastering API Development

Django Rest Framework (DRF) is a powerful toolkit that makes building robust and scalable web APIs with Django a breeze. Whether you're a seasoned Django developer or a newcomer, having a comprehensive cheat sheet at your disposal can be a game-changer. …
Read more →

5 min read

How to Perform NOT Queries in Django ORM

In Django, performing NOT queries allows you to exclude certain records from the query results based on specific conditions. The NOT operator, represented by the tilde (~) when used in conjunction with the Django ORM's Q object, helps you construct compl…
Read more →

3 min read