Variables and Data types in python

Variables in Python:

Variables are the reserved memory locations to store the data. Which means for each variable in your program, separate memory location is reserved.

As soon as you assign some value to the variable in you program, python interpreter utilizes the reserved memory space of that variable. But how much memory to be reserved and what type of data you can store in a variable depends upon data type of variable.

To understand variables in detail let’s first try to understand what is meant by data types in python.

Data Types in Python:

Data type is basically a classification of data, that specifies the type and size of value that variable is going to store. It tells more detailed information about the variable to an interpreter. Using which interpreter allocates the memory space.

Let’s see one example. Suppose you want to store the information of an employee. So the employee may have information like Employee ID, Name, Address and Contact number. So from python data type perspective if you look at this information. Employee ID and Contact numbers are the Integers data type while Name and address are of String data type. So in real world every data available around us belongs to some specific data type.

Data type also lets the interpreter know, what type of operations can be performed on the variable. Like on Integer data type you can perform all the mathematical operations, Like addition, subtraction, multiplication and division. Whereas on String data type it will not be allowed to perform operation like addition or subtraction etc., interpreter will not allow you to do these operation and will raise an error.

So what are the different data types in python:

Python Strings:

In technical words Strings data type in python is a sequence of Unicode characters. But in simple language it is a word or group of words and symbols. In python program string data exists within single or double ” quotes. To create a string, enclose the string data between single or double quotes.

Create strings in python

In above image there are three different strings created so lets see the difference between them.

  • In first print statement we have included the strings in single quote.
  • Second print statement we have included the string in double quote. To understand when to use single or double quote lets see the third print statement.
  • In third print statement there is already a single quote present in “Let’s” word. So if we could have used the single quotes  while creation of strings as well then it would have shown the error. Because interpreter could have treated the single quote from word “Let’s” as a closing quote.

There are many operations we can do with the strings like string slicing, string concatenation which we are going to learn in the next article. So in this article lets only focus on python data types. The next data type in python is python list.

Python Lists:

As name suggests, python list is a data type in which you can store the data in list of elements or values format. This list data is an ordered sequence of elements which is mutable/changeable. Values of list are mutable, which means these values or elements of lists are not fixed and it is possible to change them.

Lists data in python is defined in between square brackets [ ]. Each value in a list is called as item and each item is separated by comma.

what is list datatype in python

As you can see in the example above we have created two separate list in python. First is integer list of numbers and second is string list of fruits.

Python Tuples:

Python tuple data type is very similar to lists data type. It is also used for grouping the data in list of item format. Only difference between them is items of tuple are immutable which means items of tuple can’t be changed. Also in lists items are included in between square bracket whereas in tuples items are included in between parenthesis ( ).

what is tuple in python programming

In above example we have created tuple of color.

Python Integer:

Like in math, In python as well this data type is used to store the whole numbers. That means integer data type can store positive, negative and zero number in it. All the numbers without decimal are treated as an integer in python.

Compared to other programming languages in python you don’t need to declare the data type earlier. In the programming languages like Java, C++ you have to specify the datatype of a variable at the time of variable creation itself. Whereas in python any number entered in code without decimal point is considered as an integer (e.g 158 is an integer) and the number with decimal point in it is considered as float (e.g 158.65).

On integer data type you can perform all the mathematical operations like addition, subtraction, multiplication, division etc.

Python Floating point integer or float:

This data type is for the real numbers, It can hold the rational or irrational values in it. In simple words it is the number with decimal in it. Which means along with the integer value it has its fractional value part as well.

After going through both of these integer and float data type you should you should be clear about, number 5.0 is not equal to 5. Both of these numbers belongs to two different data types. 5.0 is a float and 5 is an integer.

Python Dictionary:

Like lists and tuples this data type also stores the data in list format. But the only difference is that, For the lists and tuples only values are stored. But in case of dictionary, data is stored in key: value pair format. Storing the data in key pair format is pythons inbuilt mapping capability.

A dictionary is constructed between curly braces { } and key value pair is written on either side of colon : mark. The word which is written on the left side of colon are the keys. whereas the word written on the right side of colon are the values.

Dictionary data type is also mutable means its values are not fixed. Those can be changed.

what is dictionary data type in python

In above example you can see that Name, age and gender are the keys and John, 21 and Male are the values of respective keys.

So guys that was all about the data types in python. If you are having any doubt please comment below. Also please go through the other python articles as well to more information about python programming.

To continue learning python you can go through following python articles.

String concatenation and replication in python

Identifiers and keywords in Python

Leave a Comment