Python Datatypes


There are two categories of Datatype found in Python, Immutable Datatype and Mutable Datatype


These datatypes cannot be changed once declared. They consist of Numbers, Strings, Tuples


These datatypes can be changed anytime. They consist of Lists, Dictionary, and Sets.
Python🐍 Datatypes 


As discussed previously, Python has four types of literals: Integer, Long Integer, Floating number and Complex. Even Python does not require the datatype to the variable in the declaration. Python will automatically convert one type to another datatype.
Let’s see with an example
Python🐍 Datatypes 
Here, we have used type() function to get the data type of the variable.


Anything written in single, double or triple quotes is called a String.
We will see with an example of how to define two string variables. One variable, ps, is declared as “Hello Python”. Another one, ps2, is declared as “I Love Python”. Now, we want to apply an operation on these strings. From the first string, I want to print the first letter, and from the second string, I want to print the last letter. For that we have to use an index with the first letter as [0]th index, and the last letter as [-1].
The first character is H from ps, and the second string, ps2, the last character 'n' is printed as the output string.
Python🐍 Datatypes 
Print the sub-string from “Hello Python”. When we start the index, it’s from 0 to 4 for the word “Hello”. However, as we want to extracts the word, we have to add +1. It’s index should be [0:5]. All the string characters start from 0 until 5. Check the code below:
Python🐍 Datatypes 

Operations of String

Some functions/operations are going to be applied with a string as shown below.
This function is used to return the position of the string. For an example: we defined a string variable ‘pf’ with a value: ‘programming’, from this we have to find the position of ‘am’. After applying the find() function it will return the 5. The reason is p[0],r[1],o[2],g[3],r[4],a[5],m[6],m[7],i[8],n[9],g[10]. Check in the image Ln[5].
This function is used to replace one character or string with another.
Same as in pf variable we want to replace programming to the programmer. Or you can replace programming with the program. We will see this in the below example. Check in the image In[6], In[10].
This function splits the word or string on the basis of the character. In the example, we split the string on the base of, Check-in In[9].
This function counts the character in a string. Check-in In[11].
This function converts the character string to upper case. Check-in In[12].
This function returns the maximum or minimum value in ASCII. Check-in In[23].
Python🐍 Datatypes 


Tuples are a group of values in parentheses (). It’s an immutable datatype, so the values within the tuples cannot be changed. How to define it? Check out the ‘pytuple’ in In[24]
There are some operations perform with it-
This operation is used to add two strings or characters. Check-in In[24].
This operation is used to perform the duplication of the string or character a given number of times. Check-in In[25].
This operation is used to get the indexed character or a string in a group or within the tuple. Check-in In[26].
It shows the specific set of indexed character or string. Check-in In[27]. It will show the output on the basis of In[25].
Python🐍 Datatypes 


A list is a mutable Python data type that is defined within the []-square brackets. This group of values can be easily changed. First, we will define a list variable having multiple datatypes of values in it. Check-in In[28].
There are also some operations applied on the list,
Used to add two or more lists/elements together, as we learned in tuples. Check-in In[29].
It’s used to duplicate a list a given number of times. Check-in In[30].
Same as tuples, if you want the specific set of the list mentioned the starting index number and ending number+1, and it will return the sliced list. Check-in In[44].
For adding value to a list. Check-in In[42].
It’s another function as concatenation. Check-in In[56].
By using the index value you can specify any value in between the list. Check-in In[55].
Python🐍 Datatypes 
Python🐍 Datatypes 


Python Dictionary is a group of values within a {} -curly braces. While defining a value, there are two elements: Key, and its Value, or we can say like we define an index for each element. Check-in In[57].
There are also some operations applied on a dictionary,
There is no value inside the dictionary.
Integer key
Key values in the dictionary are integer only.
Mixed key
There are mixed types of keys used in the Dictionary.
This is a sequence where each item has a pair.
Accessing dictionary
You can access the dictionary by directly using a key.
If I want to find the Length of the Dictionary, I will use this function. It will return the output in numeric as the number of elements is there in Dictionary. Check-in In[59].
Returns all the values inside the dictionary. Check-in In[69] and In[72].
If I want to find the key to my dictionary, then I will choose this function. Check-in In[70] and In[71].
Python🐍 Datatypes 
Python🐍 Datatypes


Sets are the unordered collection of items within the curly braces {}. Every element in the set is unique and we don’t need to define the key-value to that element.
For example, pySet={1,2,3}: All the elements are different and they don’t consist of any key-value. It’s the only difference between set and dictionary.
Let’s check with the functions of the set-
Creating a set
Check in In[74].
When we create two sets, it returns a common value from both sets. Check-in In[75].
Returns the common element from both sets. Check-in In[76].
Omits all common values from both sets. It will return only that value which is unique and written in the first set. Check-in In[79].
Python🐍 Datatypes


In this article, we learned about Python Datatypes. You can also find an attachment of the file where you can practice yourself with Python Notebook.