Data types in python with example

What is Data Type?

Data Type is classification of Data that we used in our computer program to process the data. Data can be Integer, Float, Decimal, String, Character, and Boolean. For example, Suppose we need to add two numbers (10+20) These numbers have a specific integer data type.

Data Types in Python

Every value in Python has a datatype. Since everything is an object in Python programming, data types are actually classes and variables are instances (object) of these classes.

Python is a dynamically-typed language, which means it is not required beforehand to declare the variables explicitly to use in a program. Python interpreter sets the variable’s datatype based on the assigned value, the data type is changed if the value of the variable is changed.

There are different data types in Python. Some of them are listed below.

Python Numbers

Integers, floating-point numbers, and complex numbers fall under Python numbers category. Hence, the number of datatype is used to hold numeric values. They are characterized as int, float, and complex classes in Python.

Integer:

Integers are whole numbers without any decimal values. In Python, integers are of unlimited size, hence it’s size depends on the memory available in the system.

w = 105
x = 78658965235632
y = -256
z = 0b00110011

print(type(w))
print(type(x))
print(type(y))
print(type(z))

Output

<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>

Floating Point Numbers:

Real numbers with a decimal part separating the integer and the fractional part are called Floating point numbers. They can be both negative and positive.

A floating point number can be accurate only up to 15 decimal places.

x = 1297.50
print(type(x))
y = 0.50594
print(type(y))

Output

<class 'float'>
<class 'float'>

Complex Numbers:

Complex numbers are represented in the form x+yj, where a is a real number and y is the imaginary one. Python wont accept in the form x+jy.

x = -46j
y = 12 + 45j
z = 2j
print(type(x))
print(type(y))
print(type(z))

Output

<class 'complex'>
<class 'complex'>
<class 'complex'>

Hence in Python Numbers, we can use the type() function to know which class a variable or a value it belongs to. Additionally, the isinstance() function is used to check if an object belongs to a specific class.

Example

a = 10
print(a,"is",type(a),"type")

Output

10 is <class 'int'> type

Python bool()

The bool() function converts the given value to a boolean value(True or False). If the given value is False, the bool function returns False else it returns True.

If we don’t pass any value to bool() function, it returns False. We have different values of different data types and we are printing the return value of bool() function in the output.

Example:

lis = []
print(lis, 'is' , bool(lis))

t = ()
print(t, 'is' ,bool(t))

new = 'Hello'
print(new, 'is' ,bool(new))

num = 99
print(num, 'is' ,bool(num))

comp = 0 + 0j
print(comp, 'is' ,bool(comp))

val = None
print(val, 'is' ,bool(val))

Output:

[] is False

() is False

Hello is True

99 is True

0j is False

None is False

Python List

A list in Python is an arranged grouping of items. It is one of the most used datatype in Python and is entirely adaptable. All the items in a list do not need to be of a similar type i.e. a list can hold values of different datatypes.

Declaring a list is quite straight forward. Items separated by commas and are enclosed within square brackets [ ].

a = [10, 20.5, "Python List"]
print(a)

Output

[10, 20.5, 'Python List']

We can use the slicing operator [ ] to extract an item or a range of items from a list. The list begins from 0 in Python.

a = [10, 20.5, "Python List"]
print(a[1])

Output

20.5

Python Tuple

Tuple in python is an ordered sequence of comma separated values(items or elements). Tuples are the same as the list but the main contrast is that tuples are immutable. Tuples once created can’t be adjusted.

Tuples are used to compose write-protect data and are normally quicker than list as they can’t change progressively.

It is defined inside brackets () where items are separated by commas.

t = (25,'tuple', 1+10j)
print("t[1] = ", t[1])
print("t[0:3] = ", t[0:3])

Output

t[1] =  tuple
t[0:3] =  (25, 'tuple', (1+10j))

Python Strings

String variable in python is used to store series of sequence of characters-letters,numbers and special characters. We can use a single quotation or a double quotation to represent strings. In Python strings are represented using single or double quotes ‘ ‘ or ” “. Multi-line strings can be denoted using a triple quotation, ”’ or “””.

s = "This is a single line string"
print(s)
s2 = '''A multiline
 string'''
print(s2)

Output

This is a single line string
A multiline
 string

Python Set

Set in Python is an unordered collection of unique elements or items. Set are defined by values separated by a comma inside braces { }. It is iterable, mutable(can modify creation), and has unique elements. In set order of elements are is undefined, it may return the changed sequence of the element.

a = {50,20,30,10,40}
print("a = ", a)
print(type(a))

Output

a =  {40, 10, 50, 20, 30}
<class 'set'>

We can perform set activities like union, intersection on two sets. Sets have unique values. They drop the copy elements or items.

Python Dictionary

Dictionary in Python is an unordered collection of key-value pairs.

Dictionary is similar to associative arrays or hashes consists of key-value pairs.Dictionary is used for retrieving information. We should know the way to retrieve the value.

In Python, dictionaries are defined inside braces {} with every item being a pair in the structure key:value. Key and value can be of any type.

d = {1:'val1',2:'val2'}
print(type(d))
print("d[1] = ", d[1])
print("d[2] = ", d[2])

Output

<class 'dict'>
d[1] =  val1
d[2] =  val2