Python Basic Data Types With Examples

In this part, we will learn about the data type used in Python. Every programming language has this same concept as the data type. Similarly, Python has a data type of every variable that is used in the code. The datatype is very important in every programming language. Data can be stored with various types in a variable. Different data types can do different actions in the code. In Python, every component is treated as an object. In that object, data types are treated as classes, and variables are treated as an instance of that class. Python has several built-in data types. Those data types are classified below as a category format,

Category Data type
Numeric int, float, complex
Text str
Sequence List, tuple, range
Mapping dist
Boolean bool
Set set, frozenset
Binary byte, bytearray, memoryview

Getting Data type

To know the data type of a variable, we have to write the function type() and pass the parameter of that assigned variable. To check if an object belongs to a particular class or not, we have to write the function isinstance().

Example:

a = 10

print(type(a))

# the output of this code will be <class 'int'>

b = 20.5

print(type(b))

# the output of this code will be <class 'float'>

c = 3+5j

print(isinstance(3+5j,complex))

# the output of this code will be True

Various Data types with Examples

Example Data type
a = "Hello Python" str
a = 100 integer
a = 10.550 float
a = 6j complex
a = ["TV", "Mobile", "Computer"] list
a = ("TV", "Mobile", "Computer") tuple
a = range(10) range
a = {"Name" : "Devis", "Age" : 50} dict
a = {"TV", "Mobile", "Computer"} set
a = frozenset({"TV", "Mobile", "Radio"}) frozenset
a = False bool
a = b"PHP" bytes
a = bytearray(2) bytearray
a = memoryview(bytes(8)) memoryview

Change Data type

It is possible to convert from one data type to another data type in Python. Some examples are given below

Example

print(float(5))

# it will print 5.0

print(int(10.6))

# it will print 10

print(str(25))

# it will print '25'

Python List

It is one of the famous data types used in Python. It holds various items and is very much flexible. The list values can be used by brackets []. We will discuss more on this later. Here we are giving the basic idea of List.

Example

L = [20, 15.99, 'World']

#this is a list and the name of the list is L. It holds 3 values such as 20, 15.99, and World

We can do various operations with the list. Those are given below

L1 = [50, 20, 35, 10, 55, 32, 78, 99]

# the value location is starting from 0. So the list L1 has a length of 8 bits (0-7).

# L1[2] = 35

print("L1[2] = ", L1[2]) # it will print 35

# L1[0:3] = [50, 20, 35], it means from 0 to before 3 position will print

print("L1[0:3] = ", L1[0:3]) # it will print [50,20,35]

Python Tuple

It is one of the famous data types used in Python. It holds various items and is very much flexible. It is the same as the list but the only difference is that tuples are immutable. The tuple values can be used by brackets (). We will discuss more on this later. Here we are giving the basic idea of Tuple.

Example:

T = (10,'code', 3+5j)

print("T[1] = ", T[1])

# it will print code

Python Dictionary

It is one of the most important data types in python. It is a collection of key-value pairs. It is used by brackets {}. We will discuss more on this later. Here we are giving the basic idea of a dictionary.

Example:

D = {1:'value',2: 'key'}

print(D[1]);

# it will print the value