Data Handling
Data Handling
Data Handling
Data Handling
Introduction, Data Types, Mutable and Immutable Types, Operators, Expressions, Introduction to Python
Standard Library Modules, Debugging
DATA TYPES
Data types are used to identify type of data and set of valid operations for it. Python offers the following
built-in core data types:
1. Numbers
2. String
3. List
4. Tuple
5. Dictionary
1. Data Types for Numbers
Python offers the following data types to store and process different types of numeric data:
a. Integers
Integers (signed). Python provides single data type (int) to store any positive or negative integer
(i.e. whole number), whether big or small.
Booleans. Subtype of integers, and Boolean values False and True behave like the values 0 and
1, respectively. To get the Boolean equivalent of 0 or 1, you can type bool(0) or bool(1), Python
will return False or True respectively.
b. Floating-Point Numbers. Represent machine-level double precision floating point numbers (15 digit
precision). The range of these numbers is limited by underlying machine architecture subject to
available (virtual) memory.
c. Complex Numbers. Represent complex number in the form A + B j, where 𝑗 = √−1. Complex
numbers are made of two parts: the real part (A) and the imaginary part (B), both are represented
internally as float values.
The Range of Python Numbers
Data type Range
Floating point an unlimited range, subject to available (virtual) memory on underlying machine
numbers architecture.
Complex numbers same as floating point numbers because the real and imaginary parts are represented
as floats.
Data types strings, lists, tuples, dictionary, etc., are all iterables. An iterable is any Python object that can
return its members, one at a time.
MUTABLE AND IMMUTABLE TYPES
Immutable Types
1. The immutable types are those that can never change their value in place. The following types are
immutable:
integers, floating point numbers, Booleans, strings, tuples.
2. Python frontloads some commonly used values in memory.
3. In immutable types, each variable referring to that value actually store that memory addresses of the
value.
4. Multiple variables / identifiers can refer to a value.
5. Internally, Python keeps count of how many identifiers / variables are referring to a value.
6. Each time you change the value, the variable’s memory address changes.
Example to Illustrate Immutable Types
p=5
q=p
r=5
.
.
.
p = 10
r=7
q=r
Mutable Types
7. Mutability means that in the same memory address, new value can be stored as and when you want.
8. The mutable types are those whose values can be changed in place. The following types are mutable:
lists, dictionaries, and sets.
Example: To change a member of a list, you may write:
chk = [2, 4, 6]
chk [1] = 40
The above statement will make the list namely chk as [2, 40, 6].
>>> chk=[2,4,6]
>>> id(chk)
150195536
Even after changing a value in the list chk, the reference memory address has
>>> chk[1]=40
remained same. That means, the change has taken in place – the lists are
>>> id(chk)
mutable.
150195536
>>> chk
[2,40,6]
Variable Internals
Since Python is an object oriented language, it calls every entity that stores any value or any type of data as
an object. An object is an entity that has certain properties and that exhibit a certain type of behavior, e.g.,
integers are objects – they hold whole numbers only and they have infinite precision (properties); they
support all arithmetic operations (behavior).
A variable is also an object that refers to a value. Python objects have three key attributes associated to it:
1. The type of an object: The type determines the operations that can be performed on the object. Built-in
function type() returns the type of an object.
>>> a=4
>>> type(4)
<class ‘int’>
>>> type(a)
<class ‘int’>
2. The value of an object: It is the data item contained in the object. For a literal, the value is the literal
itself and for a variable the value is the data item it is currently referencing. Using print statement you
can display value of an object.
>>> a=4
>>> print(4)
4
>>> print(a)
4
3. The id of an object: The id is the memory location of that object. Built-in function id() returns the
memory location of an object.
>>> id(4)
30899132
>>> a=4
>>> id(a)
30899132
The id() of a variable is same as the id() of value it is storing.
OPERATORS
Operators perform some computation / action when applied to variables and other objects in an expression.
The data on which operation is being carried out, i.e., the variables or objects are referred to as operands.
The operators of Python are:
1. Arithmetic Operators (+, –, *, /, %, **, //): Each of these operators is a binary operator, i.e., it requires
two operands to perform the operation. Python also provides two unary arithmetic operators (that require
one operand), which are unary + and unary –.
EXPRESSIONS
An expression is any valid combination of operators, literals and variables. The expressions can be of any
type:
1. Arithmetic expression – An expression having literals and / or variables of numeric type and arithmetic
operators.
Example: 2 + 5 ** 3, –8 * 6 / 5
2. Relational expression – An expression having literals and / or variables of any valid type and relational
operators.
Example: x > y, y <= z, z != x, z == q, x < y > z, x == y != z
3. Logical expression – An expression having literals and / or variables of any valid type and logical
operators.
Example: a or b, b and c, a and not b, not c or not b
4. String expression – An expression involving string operands, integers and string operators + and *.
Example: “and” + “then” # would result in ‘andthen’ – concatenation
“and” * 2 # would result in ‘andand’ – replication
Evaluating Arithmetic Operations
The following are the rules to evaluate an arithmetic expression:
1. Determine the order of evaluation in an expression considering the operator precedence.
2. As per the evaluation order, for each of the sub-expression (generally of the form
<value><operator><value>, e.g., 13 % 3):
Evaluate each of its operands.
Perform any implicit conversions (e.g., promoting int to float, or bool to int, for arithmetic on mixed
types).
Compute its result based on the operator.
Replace the subexpression with the computed result and carry on the expression evaluation.
Repeat till the final result is obtained.
In a mixed arithmetic expression, Python converts all operands up to the type of the largest operand (type
promotion).
In an expression of the simplest type op1 operator op2 (e.g., x / y), if both operands are standard numeric
types, the following implicit type conversions (Coercions), i.e., conversions performed by the compiler
without programmer’s intervention, are applied:
1. If either operand is a complex number, the other is converted to complex;
2. Otherwise, if either operand is a floating point number, the other is converted to floating point;
3. No conversion if both operands are integers, except for division (/) operator. For a division operation, the
evaluated value will be of floating point type.
Evaluating Relational Operations
1. All relational operations have the same priority, and lower than that of any arithmetic operations.
2. All relational expressions yield Boolean values only, i.e., True or False.
3. Chained expressions such as a < b < c is internally treated as a < b and b < c.
Evaluating Logical Operations
1. The precedence of logical operators is lower than the arithmetic operators, and hence arithmetic
subexpression (if any) is evaluated first and then logical operators are applied.
Example: 25 / 5 or 2.0 + 20 / 10 will be evaluated as 5.0 or 4.0 = 5.0
2. The precedence of logical operators among themselves is not, and, or. So, the expression
a or b and not c will be evaluated as (a or (b and (not c))).
Similarly, the expression
p and q or not r will be evaluated as ((p and q) or (not r)).
3. While evaluating or operation, Python evaluates the second argument only if the first one is False.
Similarly, for and operation, the second argument is evaluated only if the first one is True.
Examples:
Find the final result and data type for the expressions given below.
1) a, b = 3, 6 2) a, b = 3, 6 3) a, b = 3, 6.0
c=b/a c = b // a c=b%a
4) (5 < 10) and (10 < 5) or (3 < 18) and not 8 < 18
Ans. (True and False) or (True and (not True))
= False or (True and False) = False or False = False
Type Casting (Explicit Type Conversion)
A user-defined conversion that forces an expression to be of specific type.
Syntax: <data-type> (expression)
where <data-type> is the data type to which you want to type cast your expression.
For example, if we have (a = 3 and b = 5.0), then
int (b)
will type cast the data-type of the float expression b = 5.0 as int.
Python Data Conversion Functions:
S. Conversion Conversion
Examples
No. From To Function
int(7.8) will give 7
Any number-convertible
(floating-point number to integer conversion)
1 type, e.g., a float, a string Integer int()
int('34') will give 34
having digits
(string to integer conversion)
float(7) will give 7.0
Any number-convertible Floating-
(integer to floating-point number conversion)
2 type, e.g., a float, a string point float()
float('34') will give 34.0
having digits number
(string to floating-point number conversion)
S. Conversion Conversion
Examples
No. From To Function
complex(7) will give 7+0j
(1 argument – integer to complex number
Complex conversion)
3 Numbers complex()
number complex(3,2) will give 3+2j
(2 arguments – integer to complex number
conversion)
str(3)will give '3'
(integer to string conversion)
str(5.78) will give '5.78'
(floating-point number to string conversion)
str(0o17) will give '15'
4 Number Booleans String str()
(octal number to string conversion)
str(1+2j) will give '(1+2j)'
(complex number to string conversion)
str(True) will give 'True'
(Boolean to string conversion)
bool(0)will give False
bool(0.0)will give False
bool(1) will give True
bool(3) will give True
bool('') will give False
5 Any type Boolean bool()
bool('a') will give True
bool('hello') will give True
With bool(), non-zero & non-empty values
of any type will give True and rest (i.e., zero,
empty values) will give False.
Type Casting Issues: Assigning a value to a type with a greater range (e.g., from int to float) poses no
problem. However, assigning a value of larger data type to a smaller data type (e.g., from float to int)
may result in losing some precision.
Floating-point type to integer type conversion results in loss of fractional part. Original value may be out of
range for target type, in which case result is undefined.