Python Tutorials: Basic Data Types In Python


Read Time4 Minutes, 5 Seconds

In the last post, we understand some of the basics of the Python and started our first code. Now in this article, we are going to learn some of the basic data types in Python. The basic datatypes mentioned here are List, Tuple, Dictionary, and Boolean. Let us start with Boolean.

Boolean Datatype

Boolean datatype has two values, True and False. These two values always work along with the boolean operators such as and, or, and not. These operators always take two boolean values so they are binary operators. The and operator will always return False if any one of the two boolean values is false. Both the values should be true to return True. In Or operator, if any one of the value is True, then it will return true. If both the values are false, then the result will be False. And in case of Not operator, it’ll give the result as False for every True expression and vice versa. And operator is a unary operator as it takes only one expression.

List Datatype

Another important datatype in Python is List. The list is a datatype in which multiple values are stored in ordered sequence. You might find this similar to the array in other programming languages. The list can be defined by using any name and the elements inside the List are written inside []. The elements can also be called as items and are written in ‘ ‘ if they are characters.

Examples :

numbers=[1,2,3,4]

names=[‘shubham’,’shalaka’,’pratik’,’piyush’]

Now In List, if you want to access any particular item, you can call it by its index which is started from 0. So as in above case, shubham have index 0, Shalaka has 1, Pratik has 2 and Piyush has 3. So if you want to print the shalaka, then the syntax will be :

names[1]

>>> shalaka

Similarly, for Pratik

names[2]

>>pratik

If you want to say ‘hey shubham’ to be printed then

print(‘hey’) + names[0]

or simply ‘hey’ + names[0]

Note: Calling names[0:2] will return elements from index 0 to index 2-1 that is 1. This is a way of getting 2 or more elements from the List.

the only thing you should remember at this stage is that you can not use float value for the indices. The only integer value can be used. if we use a negative index then it will start from the last element and will return one.

You can change the names of the items in the List unlike in Tuple. Either you can change name directly as names[2]=’XYZ’ or if you want to interchange 1st and 2nd name then you can do that in this way: names[0]=names[1]

Some of the other functions of the List we will see in next articles such as removing an element, splitting list, list concatenation, list replication, in not in operators in List and much more.

Tuple Datatype

As mentioned in the List datatype, the Tuple datatype is somewhat similar looking as the List but has its own different properties. Consider a situation where you want to make a list and save it as a backup. And also you want to copy that list and make some changes in the copied one.

So it is hard to understand what I wrote here :p but it is easier than it sounds. Look at the image attached below.

Now as you can see in the image, we took names1 as a backup copy of names but changes made in the names1 also reflected in the previous one. So this is not a good thing if we have to consider larger data. So this tuple concept comes in handy in this situation.

Tuple can be declared similarly as the list but instead of [], it uses ().

Note: Tuple’s values cannot be modified in any way, unlike Lists. So you can not modify, append, remove elements from the Tuple. We are going to use this tuple in upcoming articles. Now its time to see another datatype.

 Dictionary Datatype

Just like lists, Dictionary also has many items. But as there are indexes in the list, the dictionary has pairs of keys and values. Let us see the simple syntax of the dictionary. Take an example given above on the list.

dict1={1:’Shubham’, 2:’Shalaka’, 3:’Pratik’, 4:’Piyush’}

As you can see, dictionary’s elements are in {} brackets and there are some pairs as 1:’Shubham’. Here 1 is a key and ‘Shubham’ is a value.  Defining key at 0 is not mandatory and can be anything you want.

Note: Dictionaries are not ordered. There are some methods like values(), keys(),  get(), setdefault() in the dictionary. The in operator can be used if you want to know the key is in the dictionary or not.

Values() returns values in dictionary, keys() will return keys. We will see more in details in the upcoming articles. For now, this is it.

©2018, copyright Shubham Shrimant


Shubham

Shubham is an ambitious coder. He mostly writes about cloud computing and upcoming technical gadgets. He has widely influenced by the technological world and love to express his judicial opinion on happening activities. And yes he's massive Liverpool Fan.