In this article, we will go over the essential data types in Python. Before we get started if you don’t know how to write comments, please go through the following article: Write Comments In Python.

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 a dynamic programming language will do that automatically

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, an 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.

You can also perform various actions on Strings,

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 a 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,

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

Booleans

A boolean data type can have one of the following two values, True or False. We can get boolean output on basic math operations:

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

Lists

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,

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

Tuples

Similar to List tuple are also used to group data of the 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:

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

Dictionaries

Dictionary is an unordered pair of keys 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 :

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 data from the dictionary, we need to pass the key in square brackets[] like this,

Don’t forget to wrap the keys in either single or double-quotes. A large dictionary can look something like this:

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

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

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

Next Recommended Article: How To Use String Formatting In Python

Data Types In Python

The article was published on September 16, 2020 @ 3:30 AM

Leave a Comment