Datatypes in Python
The
main objective of this lecture is to enlighten the concept of data types and
their classification in the python programming language.
Python
Datatypes
Data
type in python can be defined as a kind of data that a variable or an object
can hold and perform operations based on the data. It helps to determine the
type and size of the data a programmer intends to interpret.
Being
an interpretable programming language python implicitly assigns the data type
to a variable. No specific declaration is required. Based on the data type,
memory is allocated which means the space required for the data and for its
operation is dynamically secured in memory.
Example: Defining various types of datatypes
age = 17 #age is a variable of datatype number
first_name = "groc" #first_name is
a variable of datatype string
X = 21.5 #X is a variable of datatype float
Classification
of Datatypes
In the Python programming language, Data types are broadly classified into 7 built types namely numeric, text sequence, sequence, mapping, set, boolean, and binary sequences. Each type is again classified into basic data types based on their unique feature.
Number
Datatype in Python
In
python, when a variable is assigned with a numeric value then it comes under
the Python Number Datatype. A number datatype comprises of an integer value,
float value, and a complex type value which are precisely stated as
Int
Datatype
Int
is a number data type in python that comprises both positive and negative whole
numbers and zero. It does not hold the fractional part.
Float
Datatype
Float,
also known as floating-point number comprises of signed fractional numbers.
Complex
Datatype
Complex
numbers are numbers comprised of a real part and an imaginary part to take the
form a+ib where a and b are real numbers and b is associated with an imaginary
unit, i.
Example: Number based datatypes
i = 10 #i is a variable of type int
print("i = ",i)
f = 10.55 #f is a variable of type
float
print("f = ",f)
C =-5-6i #C is a variable of type
Complex
print("C =",C)
Output:
i
= 10
f
= 10.55
C
= -5-6i
String
Datatype in Python
In
the Python programming language, the string data type is a text sequence-based
datatype. A string is usually a finite series or sequence of Unicode characters
bounded either in double quotes or single quotes. To represent a multiple-line
string we can use either ‘ ‘ ‘ or “””.
Example: String Datatype in Python
Str1 = ' First String '
Str2 = " Second String "
Str3 = ' ' ' Multiple line …
…String example ' ' '
print(str1)
print(str2)
print(str3)
Output:
First
String
Second
String
Mutliple
line…
…String
example
List
Datatype in Python
On
the ground of sequences, Python data types are classified into three. They are
List
A
list can be defined as a well-ordered collection of values that are mutable. In
python, values in a list are separated using commas and are enclosed in a
square bracket [ ].
Tuple
Python
tuples are also a well-ordered collection of values that are immutable in
nature. In python, values in a tuple are separated using commas and are
enclosed in a round bracket or parenthesis ().
Range
Python
range type is used to represent a fixed series of numbers and also used in for
loop and while loop to denote fixed iterations.
Example: Sequence-based Datatypes
colorlist = ['violet', 'yellow', 'blue', 'green',
'indigo']
colourtupe = ('violet', 'yellow',
'blue', 'green', 'indigo')
x=range(10)
y=list(range(10))
print( "List of colours
:",colorlist)
print( "Tuple of colours
:",colorlist)
print(x)
print('List values in range of 10
:',y)
Output:
List
of colours : ['violet', 'yellow', 'blue', 'green', 'indigo']
Tuple
of colours : ('violet', 'yellow', 'blue', 'green', 'indigo')
range(0,
10)
List
values in range of 10 : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Set
Datatype in Python
In
the Python programming language, a set entity is an unordered collection of
unique hashable entities. Two built-in set types are:
Set
Python
set is a group of distinctive items which are neither indexed nor ordered
and are mutable. Set values are not hashable. Python sets are usually
surrounded by curly braces { }, separated using commas.
Frozenset
Python
Frozenset contains a collection of distinct objects which are
immutable and hashable in nature.
Example: Set Datatypes in Python
colorset =
{'violet','indigo','blue','green','yellow'}
print("Set of colours are
:",colorset)
flowerset
=frozenset({'jasmine','marigold','sunflower'})
print("Frozen set of flowers
are:",flowerset)
Output:
Set
of colours are : {'violet', 'yellow', 'blue', 'green', 'indigo'}
Frozen
set of flowers are:
frozenset({'jasmine', 'marigold', 'sunflower'})
Dictionary
Datatype in Python
In
the Python programming language, a dictionary is a collection of combination
pairs Key and Values which are indexed, mutable but not in order. Combination
pairs Key: Values are encapsulated in
curly braces { } and can be of any data type. Values are extracted using Key
and the opposite way is not possible.
Example: Dictionary Datatypes in Python
D = {
"Flower":"Rose",
"Fruit":"Cherry",
"Age":20
}
A=D['Age']
F=D["Flower"]
print(D)
print("Age is ",A)
print("Flower is",F)
Output:
{'Flower':
'Rose', 'Fruit': 'Cherry', 'Age': 20}
Age
is 20
Flower
is Rose
Boolean
Datatype in Python
Boolean
Datatype is a datatype which holds one of the possible value, True(1) or
False(0).
In
python, Boolean datatype returns the truth value of an expression using the
python keywords, True or False.
Boolean
is typically used to evaluate an expression. The expression can be logical,
arithmetic, etc which you will learn in the succeeding tutorial.
Example: Boolean Datatypes in Python
a = 100
b = 200
print(a > b)
print(a < b)
print(a == b)
print(a != b)
if a < b:
print(a,'is less than', b)
else:
print(a,' is greater than',b)
Output:
False
True
False
True
100
is less than 200
Binary
Sequence Datatypes in Python
Binary
sequence Datatypes are categorized into three types in the python programming language.
They are
bytes
Bytes
are primarily built-in data types used to manage binary data. bytes entities
are unchangeable series of single bytes.
bytearray
bytearray
is another primary built-in datatype used to control binary data. Unlike bytes,
bytearray is mutable in nature.
memoryview
memoryview
is used in python code to acquire the internal data of a specific object. It
also supports bytes and bytearray with the help of buffer protocol.
a=b"Python" #bytes datatype
x= bytes(10) #bytes datatype
y=bytearray(5) #bytearray
z=memoryview(bytes(10)) #memoryview
print('a = ',a)
print('x = ',x)
print('y = ',y)
print('z = ',z)
Output:
a
= b'Python'
x
=
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
y
= bytearray(b'\x00\x00\x00\x00\x00')
z
= memory at 0x034D8268
From
the table, we can infer the following points.
- Sequence
datatypes like string, list, tuple, and range are ordered datatypes that
supports both indexing and slicing.
- Among
the sequence data types, the only list is mutable.
- String,
List, and Tuple allow duplicate values.
- Set
is mutable while fozenset is immutable.
- Bytes
and bytearrays support order, indexing, and slicing. The only difference
is bytes are immutable whereas bytearray is mutable.
type
() function in Python
We
have already learned that python is a highly type-based programming language.
Each data has its own type. In order to retrieve the type of data, we use a
built-in function,type( ) function. type(object) function returns the
specific object type on passing a single argument to the function
Syntax
of the type function :type(object)
i = type(10)
print(i) #returns
<class ‘int’>
f = type(10.55)
print(f) #returns
<class ‘float’>
STR = type(‘Welcome’)
print(STR) #returns
<class ‘str’>
List_ex =type [1,2,3,4,5,6]
print(List_ex) #returns
<class ‘list’>
Set_ex =type({5,3,1,2})
Print(Set_ex) #returns <class ‘set’>
D = {
“Flower” : ”Rose“
}
Print(type(D)) #returns <class ‘dict’>
Output:
<class
‘int’>
<class
‘float’>
<class
‘str’>
<class
‘list’>
<class
‘set’>
<class
‘dict’>