Data types are a very important part of programming languages that allows a programmer to use a specific type of data. Basic data types that we hear of while working are char, int, float, string, boolean, etc. all these types of data have the ability to store the data in a particular form. Say, for example, if we take an int as our data type, it will store integer values only. Or if we take char, it will store the character values. The data always come in various forms depending on the requirement of the user. Similarly in python language also, we have data types doing the same work. Python has a feature of dynamic interpreting where if the user writes a=2, it will automatically understand that the data type is an integer, as the value passed, is numeric.
There are a few data types defined in python. Every data type will define the type of storage method happening inside the program. Below is a list of a few important data types in python:
These data types store numeric values. There are int, char, floating numbers, and complex numbers under this category. Python has a type() function which is used to identify the data type of the variable. There’s another function called isinstance() which is used to identify the class to which an object belongs.
Below is a python code to understand the number data types in detail:
x = 1
print("The type of x", type(x))
y = 60.2
print("The type of y", type(y))
z = 1+4j
print("The type of z", type(z))
print(" z is a complex no", isinstance(1+4j,complex))
Output:
The type of x <class 'int'>
The type of y <class 'float'>
The type of z <class 'complex'>
z is a complex no True
Python has basic 3 categories of data types falling under numeric:
1. Int: There is no rule on the length of int. They can be both positive and negative. For example 20, -34, 1, 4, -70, etc.
2. Float: It is taken to store the decimal numbers or we can say the floating values such as 1.2, 3.44, 7.11, etc.
3. Complex: It consists of a pair of in order such as x+iy where x is the real part and y is the imaginary part. For example 2+i8, 3+i6, etc.
The sequence form of data types consists of 3 main data storage forms. They are strings, lists, and tuples.
This data type is a sequence of characters present inside double-quotes. In python, we have built-in functions to perform operations on strings. Such as if a user wants to concatenate two strings, it can perform it by using the ‘+’ sign. For example, perform concatenation of two words: ‘welcome’ + ‘to HRX’. The output will be welcome to HRX.
Below is a python code to understand strings in python better:
str = "Welcome to HRX training"
print(str)
x = '''How may we help you?
string'''
print(x)
Output:
Welcome to HRX training
How may we help you?
string
>
This data type is defined as an ordered sequence of objects. We can say that lists are the most important and widely used data types in python. This is because lists provide a benefit that the items inside lists need not be of a similar type. Hence, the list provides sequential order for distinguished data as well.
Below is a python code to understand list in python better:
a = [1,9,2,45,25,30,65,74]
print("a[2] = ", a[2])
print("a[0:5] = ", a[0:3])
print("a[6:] = ", a[5:])
Output:
a[2] = 2
a[0:5] = [1, 9, 2]
a[6:] = [30, 65, 74]
>
In python, lists and tuples are very similar to some extent. Similar to lists in python, tuples also consist of objects with non-similar data types. The only difference is that the objects in the tuple are separated by a comma and present inside parentheses. The format of tuples is read-only as the user is not allowed to modify any of the items’ size or value. The benefit of working with tuples is that they are faster than lists.
Below is a python code to understand tuples in python better:
x = (2,'HRX', 1+3j)
print("x[1] = ", x[1])
print("x[0:3] = ", x[0:3])
x[0] = 10
Output:
x[1] = HRX
x[0:3] = (2, 'HRX', (1+3j))
Traceback (most recent call last):
File "", line 7, in
TypeError: 'tuple' object does not support item assignment
>
The last argument above generates an error as tuples are immutable.
Become a Python Certified professional by learning this HKR Python Training !
Dictionary in python is referred to as an unordered form of items. The items are present in the form of key-values meaning each key has a value in a dictionary. Each object inside is separated using a comma inside curly brackets. The key and value do not need to be of a similar type.
Below is a python code to understand tuples in python better:
x = {1:'Alphabet a', 2:'Alphabet b', 3:'Alphabet c', 4:'Alphabet d'}
print (x)
print("1st alphabet is "+x[1])
print("4th alphabet is "+ x[4])
print (x.keys())
print (x.values())
Output:
{1: 'Alphabet a', 2: 'Alphabet b', 3: 'Alphabet c', 4: 'Alphabet d'}
1st alphabet is Alphabet a
4th alphabet is Alphabet d
dict_keys([1, 2, 3, 4])
dict_values(['Alphabet a', 'Alphabet b', 'Alphabet c', 'Alphabet d'])
>
Very similar to dictionaries, these are unordered forms of data types with unique values. The main difference between both of them is that data in sets can be changed or modified even after creation. If the user forgets to mention the sequence of the objects, the set will automatically return the updated sequence.
Want to know more about Python, visit here Python Tutorial
Below is a python code to understand tuples in python better:
x = {2,5,1,4,3}
print("x = ", x)
print(type(x))
Output:
x = {1, 2, 3, 4, 5}
<class 'set'>
>
This data type in python has 2 built-in values which are True(T) and False(F). These two values help the user in determining whether the given input statement is true or false. It is denoted as class ‘bool’. False is represented as 'F' or zero however True is represented as 'T' or any value which is not 0.
Top 30 frequently asked Python Interview Questions !
Below is a python code to understand tuples in python better:
x = {2,5,1,4,3}
print("x = ", x)
print(type(x))
Output:
x = {1, 2, 3, 4, 5}
<class 'set'>
>
Conclusion
In this article, we are understanding the details of how data types work with python. We have functions to perform conversions in data types as well using int(), float(), etc. This is a vast topic of understanding to proceed with further python topics.
Related Articles:
Batch starts on 29th Sep 2023, Fast Track batch
Batch starts on 3rd Oct 2023, Weekday batch
Batch starts on 7th Oct 2023, Weekend batch