Computational Thinking
© CS-DEPT DPS MATHURA ROAD
and Programming - 1
Python - Data Types
XI
Standard Data Types of Python
Data can be of many types viz. Character, integer, real, string etc. Numbers
© CS-DEPT DPS MATHURA ROAD
without fractions indicate integer data, number with fractions represent real
data, True and False indicate boolean data, anything enclosed within quotes
indicate string data in Python. Since the data to be dealt with are of many types,
a programming language must be capable to handle all types of data.
2
XI
Different Data Types in Python
Every value in Python has a datatype. Python has the following built-in core data
© CS-DEPT DPS MATHURA ROAD
types:
DATA TYPES IN PYTHON
NUMBERS SEQUENCES SETS NONE MAPPINGS
INTEGER COMPLEX STRINGS
DICTIONARIES
FLOATING
LISTS
POINT NUMBERS
BOOLEAN
TUPLES
3
XI
Data Types in Python - Number
Number data types stores numeric values only. It is further classified into int,
© CS-DEPT DPS MATHURA ROAD
float and complex.
Type/Class Description Examples
int Integer numbers -12,-3,0,99,370
float Real or floating numbers -8.09,90.87,8E+9,7.33E-9
complex Complex numbers 8 - 9j, 2 + 3j
Boolean data type (bool) is a subtype of integer. It is a unique data type, consisting
of two constants, True and False. Boolean True is non-zero, non-null and non-empty
and Boolean False is the value zero.
4
XI
Number Data Type
int - These are whole numbers, having no fractional parts. They are numeric values
© CS-DEPT DPS MATHURA ROAD
with no decimal point. They can be positive or negative.
float - A number containing a decimal point or an exponent sign represents float data
type. The float data type can include both positive and negative floating point
numbers. The fractional number can be written in two forms:
● Fractional form (normal decimal notation) e.g. 3.2901, 0.009 etc.
● Exponent Notation e.g. 3.50062E09, 0.43E-02 etc.
They have two advantages over integers, they represent values between the integers
and they represent larger range of values. But they also suffer from one
disadvantage, that the floating point operations are usually slower than integer
operations. In Python, they have a precision of 15 digits (double precision). 5
XI
Number Data Type (contd…)
complex - Mathematically, a complex number is a number of the form A+Bi, where A
© CS-DEPT DPS MATHURA ROAD
represents the real part and i is the imaginary number. The real and imaginary part
of a complex number are floating point number.
Python represents complex number using complex data type by specifying the
number in the form real + imag j i.e the imaginary part of the number in Python is
represented by j (instead of i).
For example for Complex data, 3 + 4j , 3 denotes the real part and 4 denotes the
imaginary part. The real and imaginary part of the complex number is stored as a
float.
6
XI
Number Data Type (contd…)
The range of the numbers represented through the numeric data type is given
© CS-DEPT DPS MATHURA ROAD
below:
Data Type Range
int (Integer) An unlimited range, subject to the memory
availability
float (Floating point An unlimited range, subject to the memory
numbers) availability
complex (Complex Same as the floating point numbers as the
numbers) real and imaginary parts are float values
bool (Boolean) Only two values - True and False
7
XI
Sequence Data Types
A Python sequence is an ordered collection of items where each item is indexed by
© CS-DEPT DPS MATHURA ROAD
an integer. There are three Sequence data types in Python, namely:
● String
● Tuple
● Lists
8
XI
String Data Type
A string data type lets you hold string data i.e, any number of valid characters
© CS-DEPT DPS MATHURA ROAD
enclosed in a set of quotation marks. A string can hold any type of known characters
ie., letters, numbers and special characters.
A Python string is a sequence of characters and each character can be individually
accessed using its index. Strings are stored as individual characters in contiguous
locations, with two way index for each location.
➢ 0, 1, 2,.... In the forward direction
➢ -1, -2, -3.... In the backward direction
For example : “computer science”, ‘DPS MATHURA ROAD’
9
XI
List Data Type
© CS-DEPT DPS MATHURA ROAD
List represents a list of comma separated values of any data type between
square brackets ([ ]).
>>> L=[1,2,3,4,5]
>>> print(L)
[1, 2, 3, 4, 5]
>>> type(L)
<class 'list'>
10
XI
Tuple Data Type
© CS-DEPT DPS MATHURA ROAD
A Tuple is a ordered collection of elements that cannot be changed i.e.
they are not modifiable (immutable). Tuples are group of comma
separated values of any data type within parentheses().
11
XI
NONE
None is a special data type with a single value. It is used to signify the absence of
© CS-DEPT DPS MATHURA ROAD
value in a situation. None supports no special operations, and it is neither False nor 0
(zero).
>>> a=None
>>> type(a)
<class 'NoneType'>
12
XI
Mapping - Dictionary Data Type
Mapping is an unordered data type in Python. Currently, there is only one
© CS-DEPT DPS MATHURA ROAD
standard mapping data type in Python called dictionary. A dictionary is an
unordered set of comma separated key:value pairs, within { }, with the
requirement that within a dictionary, no two keys can be the same (there are
unique keys within a dictionary)
key value
13
XI
Mutable and Immutable Data Types
© CS-DEPT DPS MATHURA ROAD
14
XI
Mutable and Immutable Data Types
● Variables whose values can be changed after they are created and
© CS-DEPT DPS MATHURA ROAD
assigned are called mutable.
● Variables whose values cannot be changed after they are created and
assigned are called immutable. In Python, the following data types are
immutable :
integers, floating point numbers, string, boolean, tuples
● When an attempt is made to update the value of an immutable
variable, the old variable is destroyed and a new variable is created by
the same name in memory.
● Python data types can be classified into mutable and immutable.
15
XI
Mutable and Immutable Data Types
Consider there are three variables as shown:
© CS-DEPT DPS MATHURA ROAD
Variable names are only indicators or references to value objects, ie. data
[Link] variables themselves do not store any values, they are not
storage containers. It appears from the above statements that the value of
the variables are changing, but it is not so.
16
XI
Mutable and Immutable Data Types
In fact the variable names are made to refer to new immutable integer
© CS-DEPT DPS MATHURA ROAD
objects. Changing in place means changing or modifying the same value in the
same memory location.
Each time you change a variable’s
value, its reference memory
address also changes. These
variables are NOT STORAGE
CONTAINERS, they are not fixed
memory address where the value
changes. Hence they are
IMMUTABLE.
17
XI
Mutable and Immutable Data Types
Mutable data types
© CS-DEPT DPS MATHURA ROAD
There are three mutable data types in Python: lists, dictionaries and sets.
Thus we see from this code that
values are modified but still
memory address of mutable data
type remains same.
18
XI
© CS-DEPT DPS MATHURA ROAD
THANK YOU!
DEPARTMENT OF COMPUTER SCIENCE
19
XI