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)


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

Capturing Query Parameters of request.get in Django

In Django, the request object contains a variety of information about the current HTTP request, including the query parameters. Query parameters are a way to pass additional information in the URL and are used to filter or sort data. The request object p…
Read more →

2 min read

Understanding related_name in Django Models

In Django, related_name is an attribute that can be used to specify the name of the reverse relation from the related model back to the model that defines the relation. It is used to specify the name of the attribute that will be used to access the relat…
Read more →

2 min read