Python For Data Science - ANR PL - Final
Python For Data Science - ANR PL - Final
UNIT - I
S.NO TOPIC Pg No
1.1 Introduction to Data Science 1 - 3
1.2 Why Python? 3 - 5
1.3 Essential Python 5 - 6
1.4 libraries Python Introduction- 6 - 8
1.5 Features 8 - 9
1.6 Identifiers 9 - 9
1.7 Reserved words 10 - 11
1.8 Indentation 11 - 12
1.9 Comments 12 - 14
1.10 Built-in Data types and their Methods: Strings, List, 14 - 22
Tuples, Dictionary,Set.
1.11 Type Conversion 22 - 28
1.12 Operators. 29 - 44
1.13 Decision Making 45 - 46
1.14 Looping- Loop Control statement 46 - 47
1.15 Math and Random number functions. 48 - 53
1.16 User defined functions 54 - 57
1.17 function arguments & its types. 57 - 60
UNIT - II
S.NO TOPIC Pg No
2.1 User defined Modules and Packages in Python 61 - 74
2.2 Files: File manipulations 75 - 88
2.3 File and Directory related methods 89 - 92
2.4 Python Exception Handling. 93 - 96
2.5 OOPs Concepts 97 - 100
2.6 Class and Objects 101 - 104
2.7 Constructors 105 - 112
2.8 Data hiding 113 - 115
2.9 Data Abstraction 116 - 121
2.10 Inheritance. 122 - 128
Page 2
UNIT - III
S.NO TOPIC Pg No
3.1 NumPy Basics: Arrays The NumPy ndarray 129 - 130
3.2 Creating ndarrays 131 - 134
3.3 Data Types for ndarrays 135 - 135
3.4 Arithmetic with NumPy 136 - 139
3.5 Arrays- Basic Indexing and Slicing, Boolean Indexing, 140 - 144
Transposing Arrays and Swapping Axes. Universal
Functions:
3.6 Mathematical and Statistical Methods-Sorting 145 - 146
UNIT - IV
S.NO TOPIC Pg No
4.1 Introduction to pandas Data Structures: 147 - 149
4.2 Series 150 - 151
4.3 Data Frame 152 - 154
4.4 panels 155 - 156
4.5 Indexing Selection, 157 - 163
4.6 Filtering Function Application 164 - 164
4.7 Mapping 165 - 165
4.8 Sorting 166 - 168
4.9 Ranking. 169 - 169
4.10 Reading and Writing Data in Text Format 170 - 172
UNIT - V
S.NO TOPIC Pg No
5.1 Data Cleaning and Preparation: Handling Missing Data, 173 - 175
Data Transformation: Removing Duplicates
5.2 Transforming Data Using a Function or Mapping, 176 - 179
Replacing Values, Detecting and Filtering Outliers
5.3 String Manipulation: 180 - 183
5.4 Vectorized String Functions in pandas. 184 - 184
5.5 Plotting with pandas: Line Plots, Bar Plots, Histograms 184 - 191
and Density Plots, Scatter or Point Plots.
Page 3
Data Science using Python – Unit I
UNIT - I
Page 4
Data Science using Python – Unit I
problem statement very clearly and precisely. Your whole model and it’s
working depend on your statement. Many scientists considers this as the
main and much important step of Date Science. So make sure what’s
your problem statement and how well can it add value to business or
any other organization.
Data Cleaning: As you have formulated your motive and also you
did collect your data, the next step to do is cleaning. Yes, it is! Data
cleaning is the most favorite thing for data scientists to do. Data cleaning
is all about the removal of missing, redundant, unnecessary and
duplicate data from your collection. There are various tools to do so with
the help of programming in either R or Python. It’s totally on you to
choose one of them. Various scientists have their opinion on which to
choose. When it comes to the statistical part, R is preferred over Python,
as it has the privilege of more than 12,000 packages. While python is
used as it is fast, easily accessible and we can perform the same things
as we can in R with the help of various packages.
Data Analysis and Exploration: It’s one of the prime things in data
science to do and time to get inner Holmes out. It’s about analyzing the
structure of data, finding hidden patterns in them, studying behaviors,
visualizing the effects of one variable over others and then concluding.
We can explore the data with the help of various graphs formed with the
Page 5
Data Science using Python – Unit I
Data Modeling: Once you are done with your study that you have
formed from data visualization, you must start building a hypothesis
model such that it may yield you a good prediction in future. Here, you
must choose a good algorithm that best fit to your model. There different
kinds of algorithms from regression to classification, SVM( Support
Vector Machines), Clustering, etc. Your model can be of a Machine
Learning algorithm. You train your model with the train data and then
test it with test data. There are various methods to do so. One of them is
the K-fold method where you split your whole data into two parts, One is
Train and the other is test data. On these bases, you train your model.
Page 6
Data Science using Python – Unit I
Why Python
Python is open source, interpreted, high level language and
provides great approach for object-oriented programming. It is one of the
best language used by data scientist for various data science
projects/application. Python provide great functionality to deal with
mathematics, statistics and scientific function. It provides great libraries
to deals with data science application.
Page 7
Data Science using Python – Unit I
Page 8
Data Science using Python – Unit I
Pandas: Pandas is one of the most popular Python library for data
manipulation and analysis. Pandas provide useful functions to
manipulate large amount of structured data. Pandas provide easiest
method to perform analysis. It provide large data structures and
Page 9
Data Science using Python – Unit I
Page 10
Data Science using Python – Unit I
Introduction
Python is a widely used general-purpose, high level programming
language. It was created by Guido van Rossum in 1991 and further
developed by the Python Software Foundation. It was designed with an
emphasis on code readability, and its syntax allows programmers to
express their concepts in fewer lines of code. Python is a programming
language that lets you work quickly and integrate systems more
efficiently.
There are two major Python versions: Python 2 and Python 3. Both
are quite different.
1.5 Features
Following are some useful features of Python language:
It uses the elegant syntax, hence the programs are easier to read.
It is a simple to access language, which makes it easy to achieve
the program working.
The large standard library and community support.
The interactive mode of Python makes its simple to test codes.
In Python, it is also simple to extend the code by appending new
modules that are implemented in other compiled language like
C++ or C.
Python is an expressive language which is possible to embed into
applications to offer a programmable interface.
Allows developer to run the code anywhere, including Windows,
Mac OS X, UNIX, and Linux.
Page 11
Data Science using Python – Unit I
1.6 Identifiers
Identifier is a name used to identify a variable, function, class,
module, etc. The identifier is a combination of character digits and
underscore. The identifier should start with a character or Underscore
then use a digit. The characters are A-Z or a-z, an Underscore ( _ ) , and
digit (0-9). we should not use special characters ( #, @, $, %, ! ) in
identifiers.
var1
_var1
_1_var
var_1
!var1
1var
1_var
var#1
Page 12
Data Science using Python – Unit I
Else is used with if and elif conditional statement the else block is
else
executed if the given condition is not true.
Page 13
Data Science using Python – Unit I
1.8 Indentation
Indentation is a very important concept of Python because without
properly indenting the Python code, you will end up seeing
IndentationError and the code will not get compiled.
Python Indentation
Python indentation refers to adding white space before a
statement to a particular block of code. In another word, all the
Page 14
Data Science using Python – Unit I
statements with the same space to the right, belong to the same code
block.
Example 1
if site == 'Hi':
print(Welcome...')
else:
1.9 Comments :
Comments in Python are the lines in the code that are ignored by
the interpreter during the execution of the program. Comments enhance
the readability of the code and help the programmers to understand the
code very carefully.
Page 15
Data Science using Python – Unit I
Multi-Line Comments: Python does not provide the option for multiline
comments. However, there are different ways through which we can
write multiline comments.
Using String Literals: Python ignores the string literals that are not
assigned to a variable so we can use these string literals as a comment.
multiline comments"""
Python Docstring :
Python docstring is the string literals with triple quotes that are
appeared right after the function. It is used to associate documentation
that has been written with Python modules, functions, classes, and
methods. It is added right below the functions, modules, or classes to
describe what they do. In Python, the docstring is then made available
via the __doc__ attribute.
Page 16
Data Science using Python – Unit I
Example:
return a*b
print(multiply.__doc__)
Output:
1. a = 5
The variable a holds integer value five and we did not define its
type. Python interpreter will automatically interpret variables a as an
integer type.
1. a=10
Page 17
Data Science using Python – Unit I
2. b="Hi Python"
3. c = 10.5
4. print(type(a))
5. print(type(b))
6. print(type(c))
Output:
<type 'int'>
<type 'str'>
<type 'float'>
1. Numbers
2. Sequence Type
3. Boolean
4. Set
5. Dictionary
Page 18
Data Science using Python – Unit I
Numbers
Number stores numeric values. The integer, float, and complex
values belong to a Python Numbers data-type. Python provides
the type() function to know the data-type of the variable. Similarly,
the isinstance() function is used to check an object belongs to a
particular class.
a=5
print("The type of a", type(a))
b = 40.5
print("The type of b", type(b))
Page 19
Data Science using Python – Unit I
c = 1+3j
print("The type of c", type(c))
print(" c is a complex number", isinstance(1+3j,complex))
Output:
1. Int - Integer value can be any length such as integers 10, 2, 29, -
20, -150 etc. Python has no restriction on the length of an integer.
Its value belongs to int
Sequence Type
String
The string can be defined as the sequence of characters
represented in the quotation marks. In Python, we can use single,
double, or triple quotes to define a string.
Page 20
Data Science using Python – Unit I
Example - 1
Output:
List
Python Lists are similar to arrays in C. However, the list can
contain data of different types. The items stored in the list are separated
with a comma (,) and enclosed within square brackets [].
We can use slice [:] operators to access the data of the list. The
concatenation operator (+) and repetition operator (*) works with the list
in the same way as they were working with the strings.
Page 21
Data Science using Python – Unit I
Output:
Tuple
A tuple is similar to the list in many ways. Like lists, tuples also
contain the collection of the items of different data types. The items of
the tuple are separated with a comma (,) and enclosed in parentheses ().
Page 22
Data Science using Python – Unit I
4.
5. #Printing the tuple
6. print (tup)
7.
8. # Tuple slicing
9. print (tup[1:])
10. print (tup[0:1])
11.
12. # Tuple concatenation using + operator
13. print (tup + tup)
14.
15. # Tuple repatation using * operator
16. print (tup * 3)
17.
18. # Adding value to tup. It will throw an error.
19. t[2] = "hi"
Output:
<class 'tuple'>
('hi', 'Python', 2)
('Python', 2)
('hi',)
('hi', 'Python', 2, 'hi', 'Python', 2)
('hi', 'Python', 2, 'hi', 'Python', 2, 'hi', 'Python', 2)
Traceback (most recent call last):
File "main.py", line 14, in <module>
t[2] = "hi";
TypeError: 'tuple' object does not support item assignment
Dictionary
Dictionary is an unordered set of a key-value pair of items. It is like
an associative array or a hash table where each key stores a specific
value. Key can hold any primitive data type, whereas value is an
arbitrary Python object.
The items in the dictionary are separated with the comma (,) and
enclosed in the curly braces {}.
Page 23
Data Science using Python – Unit I
Output:
Boolean
Boolean type provides two built-in values, True and False. These
values are used to determine the given statement true or false. It
denotes by the class bool. True can be represented by any non-zero
value or 'T' whereas false can be represented by the 0 or 'F'. Consider
the following example.
Output:
<class 'bool'>
<class 'bool'>
NameError: name 'false' is not defined
Page 24
Data Science using Python – Unit I
Set
Python Set is the unordered collection of the data type. It is
iterable, mutable(can modify after creation), and has unique elements. In
set, the order of the elements is undefined; it may return the changed
sequence of the element. The set is created by using a built-in
function set(), or a sequence of elements is passed in the curly braces
and separated by the comma. It can contain various types of values.
Consider the following example.
Output:
Page 25
Data Science using Python – Unit I
Example:
x = 10
print("x is of type:",type(x))
y = 10.6
print("y is of type:",type(y))
z=x+y
print(z)
print("z is of type:",type(z))
Output:
20.6
As we can see the data type of ‘z’ got automatically changed to the
“float” type while one variable x is of integer type while the other variable
y is of float type. The reason for the float value not being converted into
an integer instead is due to type promotion that allows performing
Page 26
Data Science using Python – Unit I
1. int(a, base): This function converts any data type to integer. ‘Base’
specifies the base in which string is if the data type is a string.
2. float(): This function is used to convert any data type to a floating-
point number.
Python3
# initializing string
s = "10010"
c = int(s,2)
print (c)
Page 27
Data Science using Python – Unit I
e = float(s)
print (e)
Output:
Python3
# initializing integer
s = '4'
c = ord(s)
print (c)
c = hex(56)
print (c)
Page 28
Data Science using Python – Unit I
c = oct(56)
print (c)
Output:
8. list() : This function is used to convert any data type to a list type.
Python3
# initializing string
s = 'geeks'
c = tuple(s)
print (c)
c = set(s)
Page 29
Data Science using Python – Unit I
print (c)
c = list(s)
print (c)
Output:
9. dict() : This function is used to convert a tuple of order (key,value) into a dictionary.
Python3
# initializing integers
a=1
b=2
# initializing tuple
Page 30
Data Science using Python – Unit I
c = complex(1,2)
print (c)
c = str(a)
print (c)
c = dict(tup)
print (c)
Output:
12. chr(number): This function converts number to its corresponding ASCII character.
Python3
a = chr(76)
b = chr(77)
print(a)
print(b)
Output: L M
Page 31
Data Science using Python – Unit I
1..12 Operators:
Python Operators in general are used to perform operations on
values and variables. These are standard symbols used for the purpose
of logical and arithmetic operations. In this article, we will look into
different types of Python operators.
Arithmetic Operators:
Arithmetic operators are used to performing mathematical
operations like addition, subtraction, multiplication, and division.
PRECEDENCE:
P – Parentheses
E – Exponentiation
Page 32
Data Science using Python – Unit I
D – Division
S – Subtraction
# Addition of numbers
add = a + b
# Subtraction of numbers
sub = a - b
# Multiplication of number
mul = a * b
# Division(float) of number
div1 = a / b
# Division(floor) of number
div2 = a // b
Page 33
Data Science using Python – Unit I
# Power
p = a ** b
# print results
print(add)
print(sub)
print(mul)
print(div1)
print(div2)
print(mod)
print(p)
Output:
13
36
2.25
6561
< Less than: True if the left operand is less than the right x<y
Page 34
Data Science using Python – Unit I
a = 13
b = 33
# a > b is False
print(a > b)
# a < b is True
print(a < b)
# a == b is False
print(a == b)
# a != b is True
print(a != b)
Page 35
Data Science using Python – Unit I
# a >= b is False
print(a >= b)
# a <= b is True
print(a <= b)
Output:
False
True
False
True
False
True
Logical Operators:
Logical Operators perform Logical AND, Logical OR, and Logical
NOT operations. It is used to combine conditional statements.
Python3
a = True
b = False
Page 36
Data Science using Python – Unit I
print(a and b)
# Print a or b is True
print(a or b)
print(not a)
Output:
False
True
False
Bitwise Operators:
Bitwise operators act on bits and perform the bit-by-bit operations.
These are used to operate on binary numbers.
| Bitwise OR x|y
~ Bitwise NOT ~x
Python3
# Examples of Bitwise operators
Page 37
Data Science using Python – Unit I
a = 10
b=4
# Print bitwise AND operation
print(a & b)
# Print bitwise OR operation
print(a | b)
# Print bitwise NOT operation
print(~a)
# print bitwise XOR operation
print(a ^ b)
# print bitwise right shift operation
print(a >> 2)
# print bitwise left shift operation
print(a << 2)
Output:
0
14
-11
14
2
40
Assignment Operators:
Assignment operators are used to assign values to the variables.
Add AND: Add right-side operand with left side operand a+=b
+=
and then assign to left operand a=a+b
Divide AND: Divide left operand with right operand and a/=b
/=
then assign to left operand a=a/b
Page 38
Data Science using Python – Unit I
Python3
a = 10
# Assign value
b=a
print(b)
b += a
print(b)
Page 39
Data Science using Python – Unit I
b -= a
print(b)
b *= a
print(b)
b <<= a
print(b)
Output:
10
20
10
100
102400
Identity Operators:
is and is not are the identity operators both are used to check if two
values are located on the same part of the memory. Two variables that
are equal do not imply that they are identical.
a = 10
b = 20
Page 40
Data Science using Python – Unit I
c=a
print(a is not b)
print(a is c)
Output:
True
True
Membership Operators:
in and not in are the membership operators; used to test whether a
value or variable is in a sequence.
Python3
x = 24
y = 20
if (x not in list):
else:
Page 41
Data Science using Python – Unit I
if (y in list):
else:
Output:
x is NOT present in given list
Operator Precedence
Python3
expr = 10 + 20 * 30
print(expr)
Page 42
Data Science using Python – Unit I
name = "Alex"
age = 0
print("Hello! Welcome.")
else:
print("Good Bye!!")
Output:
610
Hello! Welcome.
Operator Associativity
If an expression contains two or more operators with the same
precedence then Operator Associativity is used to determine. It can
either be Left to Right or from Right to Left.
Python3
# Left-right associativity
# 100 / 10 * 10 is calculated as
print(100 / 10 * 10)
# Left-right associativity
# 5 - 2 + 3 is calculated as
Page 43
Data Science using Python – Unit I
# (5 - 2) + 3 and not
# as 5 - (2 + 3)
print(5 - 2 + 3)
# left-right associativity
print(5 - (2 + 3))
# right-left associativity
# 2 ** 3 ** 2 is calculated as
# 2 ** (3 ** 2) and not
# as (2 ** 3) ** 2
print(2 ** 3 ** 2)
Output:
100.0
512
Ternary operators:
Ternary operators are also known as conditional expressions are
operators that evaluate something based on a condition being true or
false. It was added to Python in version 2.5. It simply allows testing a
condition in a single line replacing the multiline if-else making the code
compact.
Page 44
Data Science using Python – Unit I
Syntax :
Python
a, b = 10, 20
print(min)
Output:
10
1.0
>>>10/2
5.0
>>>-10/2
-5.0
>>>20.0/2
10.0
>>>5//5
Page 45
Data Science using Python – Unit I
>>>3//2
>>>10//3
Python3
print (5//2)
print (-5//2)
Output:
2
-3
The first output is fine, but the second one may be surprised if we
are coming Java/C++ world. In Python, the “//” operator works as a floor
division for integer and float arguments. However, the division operator
‘/’ returns always a float value.
Note: The “//” operator is used to return the closest integer value
which is less than or equal to a specified expression or value. So from
the above code, 5//2 returns 2. You know that 5/2 is 2.5, and the closest
integer which is less than or equal is 2[5//2].( it is inverse to the normal
maths, in normal maths the value is 3).
Example:
Page 46
Data Science using Python – Unit I
Python3
print (5.0/2)
print (-5.0/2)
Output:
2.5
-2.5
The real floor division operator is “//”. It returns the floor value for
both integer and floating-point arguments.
Python3
print (5//2)
print (-5//2)
print (5.0//2)
print (-5.0//2)
Output:
2
-3
2.0
-3.0
Page 47
Data Science using Python – Unit I
1 if statements
2 if...else statements
3 nested if statements
Page 48
Data Science using Python – Unit I
Page 49
Data Science using Python – Unit I
1 while loop
2 for loop
3 nested loops
You can use one or more loop inside any another while, for or do..while
loop.
1 break statement
Terminates the loop statement and transfers execution to the statement
immediately following the loop.
2 continue statement
Causes the loop to skip the remainder of its body and immediately retest its
condition prior to reiterating.
3 pass statement
Page 50
Data Science using Python – Unit I
Page 51
Data Science using Python – Unit I
Page 52
Data Science using Python – Unit I
Mathematical Functions
Python includes following functions that perform mathematical
calculations.
1 abs(x)
2 ceil(x)
3 cmp(x, y)
-1 if x < y, 0 if x == y, or 1 if x > y
4 exp(x)
The exponential of x: ex
5 fabs(x)
6 floor(x)
Page 53
Data Science using Python – Unit I
7 log(x)
8 log10(x)
9 max(x1, x2,...)
10 min(x1, x2,...)
11 modf(x)
12 pow(x, y)
13 round(x [,n])
14 sqrt(x)
Page 54
Data Science using Python – Unit I
1 choice(seq)
3 random()
4 seed([x])
5 shuffle(lst)
6 uniform(x, y)
Page 55
Data Science using Python – Unit I
Trigonometric Functions
Python includes following functions that perform trigonometric
calculations.
Sr.No. Function & Description
1 acos(x)
2 asin(x)
3 atan(x)
4 atan2(y, x)
5 cos(x)
6 hypot(x, y)
7 sin(x)
8 tan(x)
9 degrees(x)
Page 56
Data Science using Python – Unit I
Defining a Function
You can define functions to provide the required functionality. Here are
simple rules to define a function in Python.
Function blocks begin with the keyword def followed by the
function name and parentheses ( ( ) ).
Any input parameters or arguments should be placed within these
parentheses. You can also define parameters inside these
parentheses.
The first statement of a function can be an optional statement - the
documentation string of the function or docstring.
The code block within every function starts with a colon (:) and is
indented.
The statement return [expression] exits a function, optionally
passing back an expression to the caller. A return statement with
no arguments is the same as return None.
Syntax
Page 57
Data Science using Python – Unit I
Example
The following function takes a string as input parameter and prints it
on standard screen.
def printme( str ):
"This prints a passed string into this function"
print str
return
Calling a Function
Defining a function only gives it a name, specifies the parameters
that are to be included in the function and structures the blocks of
code.
Once the basic structure of a function is finalized, you can execute
it by calling it from another function or directly from the Python
prompt. Following is the example to call printme() function −
Live Demo
#!/usr/bin/python
Page 58
Data Science using Python – Unit I
a function, the change also reflects back in the calling function. For
example −
Live Demo
#!/usr/bin/python
#!/usr/bin/python
Page 59
Data Science using Python – Unit I
Required arguments
Required arguments are the arguments passed to a function in
correct positional order. Here, the number of arguments in the
function call should match exactly with the function definition.
To call the function printme(), you definitely need to pass one
argument, otherwise it gives a syntax error as follows −
Live Demo
#!/usr/bin/python
Page 60
Data Science using Python – Unit I
Keyword arguments
Keyword arguments are related to the function calls. When you use
keyword arguments in a function call, the caller identifies the
arguments by the parameter name.
This allows you to skip arguments or place them out of order
because the Python interpreter is able to use the keywords
provided to match the values with parameters. You can also make
keyword calls to the printme() function in the following ways −
Live Demo
#!/usr/bin/python
#!/usr/bin/python
Page 61
Data Science using Python – Unit I
Default arguments
A default argument is an argument that assumes a default value if a
value is not provided in the function call for that argument. The
following example gives an idea on default arguments, it prints
default age if it is not passed −
Live Demo
#!/usr/bin/python
Variable-length arguments
You may need to process a function for more arguments than you
specified while defining the function. These arguments are
called variable-length arguments and are not named in the function
definition, unlike required and default arguments.
Pavitra Degree College – B.Sc.(Computers) III Year VI sem Page 59
Page 62
Data Science using Python – Unit I
#!/usr/bin/python
Page 63
Data Science using Python – Unit II
UNIT - II
Introduction of Python
Page 64
Data Science using Python – Unit II
Page 65
Data Science using Python – Unit II
Code:
1. # Python program to show how to create a module.
5. result = number ** 2
6. return result
For this, we make use of the import Python keyword. In the Python
window, we add the next to import keyword, the name of the module we
Page 66
Data Science using Python – Unit II
Code:
1. import example_module
We may use the dot operator to use the functions using the module
name. For instance:
Code:
1. result = example_module.square( 4 )
2. print( "By using the module square of number is: ", result )
Output:
By using the module square of number is: 16
There are several standard modules for Python. The complete list of
Python standard modules is available. The list can be seen using the help
command.
Page 67
Data Science using Python – Unit II
Code:
1. # Python program to show how to import a standard module
3. import math
Output:
The value of euler's number is 2.718281828459045
Code:
1. # Python program to show how to import a module and rename it
3. import math as mt
Output:
The value of euler's number is 2.718281828459045
Page 68
Data Science using Python – Unit II
Please take note that now the scope of our program does not include
the term math. Thus, mt.pi is the proper implementation of the module,
whereas math.pi is invalid.
Code:
1. # Python program to show how to import specific objects from a module
2. # We will import euler's number from the math module using the from keyword
Output:
The value of euler's number is 2.718281828459045
Only the e constant from the math module was imported in this case.
Code:
1. # Python program to show how to import multiple objects from a module
Page 69
Data Science using Python – Unit II
Output:
The value of tau constant is: 6.283185307179586
Syntax:
Code:
1. # importing the complete math module using *
5. print( "Calculating tangent of an angle: ", tan(pi/6) ) # here pi is also imported from the m
ath module
Output:
Page 70
Data Science using Python – Unit II
Code:
1. # We will import the sys module
2. import sys
4. print(sys.path)
Output:
['/home/pyodide', '/home/pyodide/lib/Python310.zip', '/lib/Python3.10', '/lib/Python3.10/lib-
dynload', '', '/lib/Python3.10/site-packages']
Page 71
Data Science using Python – Unit II
Code:
1. # Python program to print the directory of a module
Output:
List of functions:
Page 72
Data Science using Python – Unit II
Code:
1. Number = 204
2. def AddNumber():
Page 73
Data Science using Python – Unit II
4. global Number
6. print( Number )
7. AddNumber()
8. print( Number )
Output:
204
404
Python Packages:
We usually organize our files in different folders and subfolders based
on some criteria, so that they can be managed easily and efficiently. For
example, we keep all our games in a Games folder and we can even
subcategorize according to the genre of the game or something like this.
The same analogy is followed by the Python package.
Creating Package
Let’s create a package named mypckg that will contain two modules
mod1 and mod2. To create this module follow the below steps –
Page 74
Data Science using Python – Unit II
Mod1.py
def gfg():
print("Welcome to GFG")
mypckg
---__init__.py
---mod1.py
---mod2.py
Understanding __init__.py
Page 75
Data Science using Python – Unit II
For example, we can also create the __init__.py file for the above module
as –
__init__.py
This __init__.py will only allow the gfg and sum functions from the
mod1 and mod2 modules to be imported.
Syntax:
import package_name.module_name
We will import the modules from the above created package and will
use the functions inside those modules.
mod1.gfg()
res = mod2.sum(1, 2)
print(res)
Page 76
Data Science using Python – Unit II
Output:
Welcome to GFG
We can also import the specific function also using the same syntax.
gfg()
res = sum(1, 2)
print(res)
Output:
Welcome to GFG
Page 77
Data Science using Python – Unit II
The file handling plays an important role when the data needs to be
stored permanently into the file. A file is a named location on disk to store
related information. We can access the stored information (non-volatile)
after the program termination.
In Python, files are treated in two modes as text or binary. The file
may be in the text or binary format, and each line of a file is ended with the
special character.
o Open a file
Page 78
Data Science using Python – Unit II
Opening a file
Python provides an open() function that accepts two arguments, file
name and access mode in which the file is accessed. The function returns
a file object which can be used to perform various operations like reading,
writing, etc.
Syntax:
The files can be accessed using various modes like read, write, or
append. The following are the details about the access mode to open a file.
Access
SN Description
mode
It opens the file to read-only mode. The file pointer exists at the beginning.
1 r
The file is by default open in this mode if no access mode is passed.
It opens the file to read-only in binary format. The file pointer exists at the
2 rb
beginning of the file.
It opens the file to read and write both. The file pointer exists at the
3 r+
beginning of the file.
It opens the file to read and write both in binary format. The file pointer
4 rb+
exists at the beginning of the file.
It opens the file to write only. It overwrites the file if previously exists or
5 w creates a new one if no file exists with the same name. The file pointer
exists at the beginning of the file.
It opens the file to write only in binary format. It overwrites the file if it
6 wb exists previously or creates a new one if no file exists. The file pointer
exists at the beginning of the file.
It opens the file to write and read both. It is different from r+ in the sense
that it overwrites the previous file if one exists whereas r+ doesn't
7 w+
overwrite the previously written file. It creates a new file if no file exists.
The file pointer exists at the beginning of the file.
Page 79
Data Science using Python – Unit II
It opens the file to write and read both in binary format. The file pointer
8 wb+
exists at the beginning of the file.
It opens the file in the append mode. The file pointer exists at the end of
9 a the previously written file if exists any. It creates a new file if no file exists
with the same name.
It opens the file in the append mode in binary format. The pointer exists at
10 ab the end of the previously written file. It creates a new file in binary format if
no file exists with the same name.
It opens a file to append and read both. The file pointer remains at the end
11 a+ of the file if a file exists. It creates a new file if no file exists with the same
name.
It opens a file to append and read both in binary format. The file pointer
12 ab+
remains at the end of the file.
Let's look at the simple example to open a file named "file.txt" (stored
in the same directory) in read mode and printing its content on the console.
Example:
1. #opens the file file.txt in read mode
2. fileptr = open("file.txt","r")
3.
4. if fileptr:
Output:
<class '_io.TextIOWrapper'>
Page 80
Data Science using Python – Unit II
We can perform any operation on the file externally using the file
system which is the currently opened in Python; hence it is good practice to
close the file once all the operations are done.
Syntax:
1. fileobject.close()
4. fileptr = open("file.txt","r")
5.
6. if fileptr:
8.
10. fileptr.close()
After closing the file, we cannot perform any operation in the file. The
file needs to be properly closed. If any exception occurs while performing
some operations in the file then the program terminates without closing the
file.
Page 81
Data Science using Python – Unit II
2. fileptr = open("file.txt")
4. finally:
5. fileptr.close()
The syntax to open a file using with the statement is given below.
1. with open(<file name>, <access mode>) as <file-pointer>:
2. #statement suite
Example
Pavitra Degree College – B.Sc.(Computers) III Year VI sem Page 79
Page 82
Data Science using Python – Unit II
1. with open("file.txt",'r') as f:
2. content = f.read();
3. print(content)
w: It will overwrite the file if any file exists. The file pointer is at the
beginning of the file.
a: It will append the existing file. The file pointer is at the end of the file. It
creates a new file if no file exists.
Example:
1. # open the file.txt in append mode. Create a new file if no such file exists.
7. fileptr.close()
Output:
File2.txt
Page 83
Data SScience using Python – Unit II
We have opened the file in w mode. The file1.txt file doesn't exist, it
created a new file and we have written the content in the file using
the write() function.
Example 2:
1. #open the file.txt in write mode.
2. fileptr = open("file2.txt","a")
6. fileptr.close()
Output:
Python is the modern day language. It makes things
things so simple.
We can see that the content of the file is modified. We have opened
Page 84
Data Science using Python – Unit II
the file in a mode and it appended the content in the existing file2.txt.
Syntax:
1. fileobj.read(<count>)
Here, the count is the number of bytes to be read from the file starting
from the beginning of the file. If the count is not specified, then it may read
the content of the file until the end.
Example
1. #open the file.txt in read mode. causes error if no such file exists.
2. fileptr = open("file2.txt","r")
3. #stores all the data of the file into the variable content
4. content = fileptr.read(10)
6. print(type(content))
8. print(content)
10. fileptr.close()
Page 85
Data Science using Python – Unit II
Output:
<class 'str'>
Python is
If we use the following line, then it will print all content of the file.
1. content = fileptr.read()
2. print(content)
Output:
Python is the modern-day language. It makes things so simple.
It is the fastest-growing programing language Python has easy an
syntax and user-friendly interaction.
2. fileptr = open("file2.txt","r");
4. for i in fileptr:
Output:
Python is the modern day language.
Page 86
Data Science using Python – Unit II
2. fileptr = open("file2.txt","r");
3. #stores all the data of the file into the variable content
4. content = fileptr.readline()
5. content1 = fileptr.readline()
7. print(content)
8. print(content1)
10. fileptr.close()
Output:
Python is the modern day language.
Page 87
Data Science using Python – Unit II
We called the readline() function two times that's why it read two
lines from the file.
Python provides also the readlines() method which is used for the
reading lines. It returns the list of the lines till the end of file(EOF) is
reached.
2. fileptr = open("file2.txt","r");
3.
4. #stores all the data of the file into the variable content
5. content = fileptr.readlines()
6.
8. print(content)
9.
11. fileptr.close()
Output:
['Python is the modern day language.\n', 'It makes things so
simple.\n', 'Python has easy syntax and user-friendly
interaction.']
Page 88
Data Science using Python – Unit II
a: It creates a new file with the specified name if no such file exists. It
appends the content to the file if the file already exists with the specified
name.
w: It creates a new file with the specified name if no such file exists.
It overwrites the existing file.
Example 1
1. #open the file.txt in read mode. causes error if no such file exists.
2. fileptr = open("file2.txt","x")
3. print(fileptr)
4. if fileptr:
Output:
<_io.TextIOWrapper name='file2.txt' mode='x' encoding='cp1252'>
Page 89
Data Science using Python – Unit II
2. fileptr = open("file2.txt","r")
6. content = fileptr.read();
7. #after the read operation file pointer modifies. tell() returns the location of the fileptr.
Output:
The filepointer is at byte : 0
For this purpose, the Python provides us the seek() method which
enables us to modify the file pointer position externally.
Page 90
Data Science using Python – Unit II
Syntax:
1. <file-ptr>.seek(offset[, from)
offset: It refers to the new position of the file pointer within the file.
from: It indicates the reference position from where the bytes are to be
moved. If it is set to 0, the beginning of the file is used as the reference
position. If it is set to 1, the current position of the file pointer is used as the
reference position. If it is set to 2, the end of the file pointer is used as the
reference position.
Example
1. # open the file file2.txt in read mode
2. fileptr = open("file2.txt","r")
3. #initially the filepointer is at 0
4. print("The filepointer is at byte :",fileptr.tell())
5. #changing the file pointer location to 10.
6. fileptr.seek(10);
7. #tell() returns the location of the fileptr.
Output:
The filepointer is at byte : 0
Page 91
Data Science using Python – Unit II
Syntax:
1. mkdir(directory name)
Example 1
1. import os
3. os.mkdir("new")
Syntax:
1. os.getcwd()
Example
1. import os
2. os.getcwd()
Output:
'C:\\Users\\DEVANSH SHARMA'
Page 92
Data Science using Python – Unit II
Syntax:
1. chdir("new-directory")
Example
1. import os
3. os.chdir("C:\\Users\\DEVANSH SHARMA\\Documents")
5. os.getcwd()
Output:
Deleting directory
The rmdir() method is used to delete the specified directory.
Syntax:
1. os.rmdir(directory name)
Example 1
1. import os
Page 93
Data Science using Python – Unit II
3. os.rmdir("directory_name")
The following example contains two python scripts. The script file1.py
executes the script file.py and writes its output to the text file output.txt.
Example
file.py
1. temperatures=[10,-20,-289,100]
2. def c_to_f(c):
3. if c< -273.15:
5. else:
6. f=c*9/5+32
7. return f
8. for t in temperatures:
9. print(c_to_f(t))
file.py
1. import subprocess
Page 94
Data Science using Python – Unit II
SN Method Description
It reads one line from the file and places the file pointer to
7 File.readline([size])
the beginning of the new line.
It returns a list containing all the lines of the file. It reads the
8 File.readlines([sizehint])
file until the EOF occurs using readline() function.
It modifies the position of the file pointer to a specified offset
9 File.seek(offset[,from)
with the specified reference.
10 File.tell() It returns the current position of the file pointer within the file.
Page 95
Data Science using Python – Unit II
What is an Exception?
An exception in Python is an incident that happens while executing a
program that causes the regular course of the program's commands to be
disrupted. When a Python code comes across a condition it can't handle, it
raises an exception. An object in Python that describes an error is called an
exception.
Code:
3.
4. for s in string:
Page 96
Data Science using Python – Unit II
5. if (s != o:
6. print( s )
Output:
if (s != o:
^
SyntaxError: invalid syntax
Code:
3.
4. for s in string:
5. if (s != o):
6. print( s )
Output:
Page 97
Data Science using Python – Unit II
displays information about the sort of exception error that occurred. It was a
NameError in this situation. Python includes several built-in exceptions.
However, Python offers the facility to construct custom exceptions.
Code:
1. # Python code to catch an exception and handle it using try and except code blocks
2. a = ["Python", "Exceptions", "try and except"]
3. try:
4. #looping through the elements of the array a, choosing a range that goes beyond the l
ength of the array
5. for i in range( 4 ):
6. print( "The index and element from the array is", i, a[i] )
7. #if an error occurs in the try block, then except block will be executed by the Python inter
preter
8. except:
9. print ("Index out of range")
Output:
The code blocks that potentially produce an error are inserted inside
the try clause in the preceding example. The value of i greater than 2
Page 98
Data Science using Python – Unit II
attempts to access the list's item beyond its length, which is not present,
resulting in an exception. The except clause then catches this exception
and executes code without stopping it.
Code:
2. num = [3, 4, 5, 7]
3. if len(num) > 3:
4. raise Exception( f"Length of the given list must be less than or equal to 3 but is {len(nu
m)}" )
Output:
1 num = [3, 4, 5, 7]
2 if len(num) > 3:
----> 3 raise Exception( f"Length of the given list must be
less than or equal to 3 but is {len(num)}" )
Page 99
Data Science using Python – Unit II
o Class
o Object
o Method
o Inheritance
o Polymorphism
o Data Abstraction
o Encapsulation
Class
The class can be defined as a collection of objects. It is a logical
entity that has some specific attributes and methods. For example: if you
have an employee class, then it should contain an attribute and method,
i.e. an email id, name, age, salary, etc.
Page 100
Data Science using Python – Unit II
1. class ClassName:
2. <statement-1>
3. <statement-N>
Object
The object is an entity that has state and behavior. It may be any
real-world object like the mouse, keyboard, chair, table, pen, etc.
Example:
1. class car:
3. self.modelname = modelname
4. self.year = year
5. def display(self):
6. print(self.modelname,self.year)
7. c1 = car("Toyota", 2016)
8. c1.display()
Output:
Toyota 2016
Page 101
Data Science using Python – Unit II
In the above example, we have created the class named car, and it
has two attributes modelname and year. We have created a c1 object to
access the class attribute. The c1 object will allocate memory for these
values. We will learn more about class and object in the next tutorial.
Method
The method is a function that is associated with an object. In Python,
a method is not unique to class instances. Any object type can have
methods.
Inheritance
Inheritance is the most important aspect of object-oriented
programming, which simulates the real-world concept of inheritance. It
specifies that the child object acquires all the properties and behaviors of
the parent object.
Polymorphism
Polymorphism contains two words "poly" and "morphs". Poly means
many, and morph means shape. By polymorphism, we understand that one
task can be performed in different ways. For example - you have a class
animal, and all animals speak. But they speak differently. Here, the "speak"
Page 102
Data Science using Python – Unit II
Encapsulation
Encapsulation is also an essential aspect of object-oriented
programming. It is used to restrict access to methods and variables. In
encapsulation, code and data are wrapped together within a single unit
from being modified by accident.
Data Abstraction
Data abstraction and encapsulation both are often used as
synonyms. Both are nearly synonyms because data abstraction is achieved
through encapsulation.
Page 103
Data Science using Python – Unit II
On the other hand, the object is the instance of a class. The process
of creating an object can be called instantiation.
Syntax:
1. class ClassName:
2. #statement_suite
Page 104
Data Science using Python – Unit II
Example
1. class Employee:
2. id = 10
3. name = "Devansh"
5. print(self.id,self.name)
The self-parameter
The self-parameter refers to the current instance of the class and
accesses the class variables. We can use anything instead of self, but it
must be the first parameter of any function which belongs to the class.
Page 105
Data Science using Python – Unit II
Example
1. class Employee:
2. id = 10
3. name = "John"
7. emp = Employee()
8. emp.display()
Output:
ID: 10
Name: John
In the above code, we have created the Employee class which has
two attributes named id and name and assigned value to them. We can
Page 106
Data Science using Python – Unit II
We can delete the properties of the object or object itself by using the
del keyword. Consider the following example.
Example
1. class Employee:
2. id = 10
3. name = "John"
4. def display(self):
7. emp = Employee()
9. del emp.id
12. emp.display()
It will through the Attribute error because we have deleted the object emp.
Page 107
Data SScience using Python – Unit II
In C++ or Java, the constructor has the same name as its class, but it
treats constructor differently in Python. It is used to create an object.
1. Parameterized Constructor
2. Non-parameterized
parameterized Constructor
Page 108
Data Science using Python – Unit II
Example
1. class Employee:
3. self.id = id
4. self.name = name
5.
6. def display(self):
8.
9.
12.
14.
15. emp1.display()
16.
Page 109
Data Science using Python – Unit II
18. emp2.display()
Output:
ID: 101
Name: John
ID: 102
Name: David
Example
1. class Student:
2. count = 0
3. def __init__(self):
4. Student.count = Student.count + 1
5. s1=Student()
6. s2=Student()
7. s3=Student()
Output:
The number of students: 3
Page 110
Data Science using Python – Unit II
Example
1. class Student:
3. def __init__(self):
5. def show(self,name):
6. print("Hello",name)
7. student = Student()
8. student.show("John")
Example
1. class Student:
2. # Constructor - parameterized
5. self.name = name
6. def show(self):
7. print("Hello",self.name)
8. student = Student("John")
9. student.show()
Output:
Page 111
Data Science using Python – Unit II
Example
1. class Student:
2. roll_num = 101
3. name = "Joseph"
4.
5. def display(self):
6. print(self.roll_num,self.name)
7.
8. st = Student()
9. st.display()
Output:
101 Joseph
Example
1. class Student:
Page 112
Data Science using Python – Unit II
2. def __init__(self):
4. def __init__(self):
6.
7. st = Student()
Output:
The Second Constructor
SN Function Description
1 getattr(obj,name,default) It is used to access the attribute of the object.
It is used to set a particular value to the specific attribute of
2 setattr(obj, name,value)
an object.
3 delattr(obj, name) It is used to delete a specific attribute.
4 hasattr(obj, name) It returns true if the object contains some specific attribute.
Example
1. class Student:
Page 113
Data Science using Python – Unit II
3. self.name = name
4. self.id = id
5. self.age = age
9. print(getattr(s, 'name'))
14. # prints true if the student contains the attribute with name id
18. # this will give an error since the attribute age has been deleted
19. print(s.age)
Output:
John
23
True
AttributeError: 'Student' object has no attribute 'age'
Page 114
Data Science using Python – Unit II
SN Attribute Description
It provides the dictionary containing the information about the class
1 __dict__
namespace.
2 __doc__ It contains a string which has the class documentation
3 __name__ It is used to access the class name.
4 __module__ It is used to access the module in which, this class is defined.
5 __bases__ It contains a tuple including all base classes.
Example
1. class Student:
2. def __init__(self,name,id,age):
3. self.name = name;
4. self.id = id;
5. self.age = age
6. def display_details(self):
7. print("Name:%s, ID:%d, age:%d"%(self.name,self.id))
8. s = Student("John",101,22)
9. print(s.__doc__)
10. print(s.__dict__)
11. print(s.__module__)
Output:
None
{'name': 'John', 'id': 101, 'age': 22}
__main__
Page 115
Data Science using Python – Unit II
Page 116
Data Science using Python – Unit II
Example -
1. class CounterClass:
2. __privateCount = 0
3. def count(self):
4. self.__privateCount += 1
5. print(self.__privateCount)
6. counter = CounterClass()
7. counter.count()
8. counter.count()
9. print(counter.__privateCount)
Output:
1
2
Traceback (most recent call last):
File "<string>", line 17, in <module>
AttributeError: 'CounterClass' object has no attribute
'__privateCount'
However we can access the private member using the class name.
1. print(counter.CounterClass__privatecounter)
Output:
1
2
2
Page 117
Data Science using Python – Unit II
Conclusion
Data hiding is an important aspect when it comes to privacy and
security to particularly within the application. It plays an essential role in
preventing unauthorized access. It has some disadvantages, but these are
avoidable in front of its advantages.
Pavitra Degree College – B.Sc.(Computers) III Year VI sem Page 115
Page 118
Data Science using Python – Unit II
In simple words, we all use the smartphone and very much familiar
with its functions such as camera, voice-recorder, call-dialing, etc., but we
don't know how these operations are happening in the background. Let's
take another example - When we use the TV remote to increase the
volume. We don't know how pressing a key increases the volume of the
TV. We only know to press the "+" button to increase the volume.
Page 119
Data Science using Python – Unit II
Syntax
1. from abc import ABC
2. class ClassName(ABC):
Page 120
Data Science using Python – Unit II
Example -
1. # Python program demonstrate
2. # abstract base class work
3. from abc import ABC, abstractmethod
4. class Car(ABC):
5. def mileage(self):
6. pass
7.
8. class Tesla(Car):
9. def mileage(self):
10. print("The mileage is 30kmph")
11. class Suzuki(Car):
12. def mileage(self):
13. print("The mileage is 25kmph ")
14. class Duster(Car):
15. def mileage(self):
16. print("The mileage is 24kmph ")
17.
18. class Renault(Car):
19. def mileage(self):
20. print("The mileage is 27kmph ")
21.
22. # Driver code
23. t= Tesla ()
24. t.mileage()
25.
26. r = Renault()
27. r.mileage()
28.
29. s = Suzuki()
Page 121
Data Science using Python – Unit II
30. s.mileage()
31. d = Duster()
32. d.mileage()
Output:
The mileage is 30kmph
The mileage is 27kmph
The mileage is 25kmph
The mileage is 24kmph
Explanation -
In the above code, we have imported the abc module to create the
abstract base class. We created the Car class that inherited the ABC class
and defined an abstract method named mileage(). We have then inherited
the base class from the three different subclasses and implemented the
abstract method differently. We created the objects to call the abstract
method.
Example -
1. # Python program to define
2. # abstract class
3.
4. from abc import ABC
5.
6. class Polygon(ABC):
7.
8. # abstract method
9. def sides(self):
10. pass
11.
Page 122
Data Science using Python – Unit II
Page 123
Data Science using Python – Unit II
Output:
Triangle has 3 sides
Square has 4 sides
Pentagon has 5 sides
Hexagon has 6 sides
Explanation -
In the above code, we have defined the abstract base class named
Polygon and we also defined the abstract method. This base class
inherited by the various subclasses. We implemented the abstract method
in each subclass. We created the object of the subclasses and invoke
the sides() method. The hidden implementations for the sides() method
inside the each subclass comes into play. The abstract
method sides() method, defined in the abstract class, is never invoked.
Points to Remember
Below are the points which we should remember about the abstract base
class in Python.
o An Abstract class can contain the both method normal and abstract
method.
Page 124
Data SScience using Python – Unit II
In inheritance, the child class acquires the properties and can access
all the data members and functions defined in the parent class. A child
class can also provide its specific imp
implementation
lementation to the functions of the
parent class. In this section of the tutorial, we will discuss inheritance in
detail.
Syntax
1. class derived-class(base
class(base class):
2. <class-suite>
Page 125
Data Science using Python – Unit II
Syntax
1. class derive-class(<base class 1>, <base class 2>, ..... <base class n>):
2. <class - suite>
Example 1
1. class Animal:
2. def speak(self):
3. print("Animal Speaking")
4. #child class Dog inherits the base class Animal
5. class Dog(Animal):
6. def bark(self):
7. print("dog barking")
8. d = Dog()
9. d.bark()
10. d.speak()
Output:
dog barking
Animal Speaking
Page 126
Data SScience using Python – Unit II
Syntax
11. class class1:
12. <class-suite>
13. class class2(class1):
14. <class suite>
15. class class3(class2):
16. <class suite>
17. .
18. .
Example
1. class Animal:
2. def speak(self):
3. print("Animal Speaking")
4. #The child class Dog inherits the base class Animal
5. class Dog(Animal):
Page 127
Data SScience using Python – Unit II
6. def bark(self):
7. print("dog barking")
8. #The child class Dogchild inherits another child class Dog
9. class DogChild(Dog):
10. def eat(self):
11. print("Eating bread...")
12. d = DogChild()
13. d.bark()
14. d.speak()
15. d.eat()
Output:
dog barking
Animal Speaking
Eating bread...
Page 128
Data Science using Python – Unit II
Syntax
1. class Base1:
2. <class-suite>
3.
4. class Base2:
5. <class-suite>
6. .
7. .
8. .
9. class BaseN:
10. <class-suite>
11.
12. class Derived(Base1, Base2, ...... BaseN):
13. <class-suite>
Example
1. class Calculation1:
2. def Summation(self,a,b):
3. return a+b;
4. class Calculation2:
5. def Multiplication(self,a,b):
6. return a*b;
7. class Derived(Calculation1,Calculation2):
8. def Divide(self,a,b):
9. return a/b;
10. d = Derived()
11. print(d.Summation(10,20))
12. print(d.Multiplication(10,20))
13. print(d.Divide(10,20))
Output:
30
200
0.5
Page 129
Data Science using Python – Unit II
Example
1. class Calculation1:
2. def Summation(self,a,b):
3. return a+b;
4. class Calculation2:
5. def Multiplication(self,a,b):
6. return a*b;
7. class Derived(Calculation1,Calculation2):
8. def Divide(self,a,b):
9. return a/b;
10. d = Derived()
11. print(issubclass(Derived,Calculation2))
12. print(issubclass(Calculation1,Calculation2))
Output:
True
False
Page 130
Data Science using Python – Unit II
Example
1. class Calculation1:
2. def Summation(self,a,b):
3. return a+b;
4. class Calculation2:
5. def Multiplication(self,a,b):
6. return a*b;
7. class Derived(Calculation1,Calculation2):
8. def Divide(self,a,b):
9. return a/b;
10. d = Derived()
11. print(isinstance(d,Derived))
Page 131
Data Science using Python – Unit III
UNIT – III
Page 132
Data Science using Python – Unit III
Page 133
Data Science using Python – Unit III
Page 134
Data Science using Python – Unit III
import numpy as np
Page 135
Data Science using Python – Unit III
print ("\nA sequential array with 10 values between" "0 and 5:\n", g)
# Flatten array
arr = np.array([[1, 2, 3], [4, 5, 6]])
flarr = arr.flatten()
print ("\nOriginal array:\n", arr)
print ("Fattened array:\n", flarr)
Output :
Array created using passed list:
[[ 1. 2. 4.]
[ 5. 8. 7.]]
Page 136
Data Science using Python – Unit III
A random array:
[[ 0.46829566 0.67079389]
[ 0.09079849 0.95410464]]
Original array:
[[1 2 3 4]
[5 2 4 2]
[1 2 0 1]]
Reshaped array:
[[[1 2 3]
[4 5 2]]
[[4 2 1]
[2 0 1]]]
Original array:
[[1 2 3]
[4 5 6]]
Fattened array:
[1 2 3 4 5 6]
Page 137
Data Science using Python – Unit III
Page 138
Data Science using Python – Unit III
import numpy as np
a = np.array([1, 2, 5, 3])
a *= 2
# transpose of array
Page 139
Data Science using Python – Unit III
Output :
Original array:
[[1 2 3]
[3 4 5]
[9 6 0]]
Transpose of array:
[[1 3 9]
[2 4 6]
[3 5 0]]
Page 140
Data Science using Python – Unit III
[[ 1 6 12]
[ 4 11 13]
[ 3 4 13]]
Page 141
Data Science using Python – Unit III
import numpy as np
# add arrays
# matrix multiplication
Output:
Array sum:
[[5 5]
[5 5]]
Array multiplication:
[[4 6]
[6 4]]
Matrix multiplication:
[[ 8 5]
[20 13]]
Page 142
Data Science using Python – Unit III
Example:
import numpy as np
print(arr[1:5])
Page 143
Data Science using Python – Unit III
Negative Slicing
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])
print(arr[-3:-1])
Example
import numpy as np
print(arr[1:5:2])
Example
From the second element, slice elements from index 1 to index 4 (not
included):
import numpy as np
print(arr[1, 1:4])
Page 144
Data Science using Python – Unit III
# indexing in numpy
import numpy as np
# An exemplar array
print ("\nElements at indices (0, 3), (1, 2), (2, 1)," "(3, 0):\n", temp)
temp = arr[cond]
Output :
[[-1. 0.]
Page 145
Data Science using Python – Unit III
[ 4. 6.]]
[ 4. 6. 0. 3.]
[ 2. 4. 4. 6. 2.6 7. 8. 3. 4. 2. ]
Swapping in arrays
Numpy allows you to swap axes without costing anything in
memory, and very little in time. The obvious axis swap is a 2D array
transpose:
>>> import numpy as np
>>> arr = np.arange(10).reshape((5, 2))
>>> arr
array([[0, 1],
[2, 3],
[4, 5],
[6, 7],
[8, 9]])
>>> arr.T
array([[0, 2, 4, 6, 8],
[1, 3, 5, 7, 9]])
The transpose method - and the np.tranpose function does the same
thing as the .T attribute above:
>>> arr.transpose()
array([[0, 2, 4, 6, 8],
[1, 3, 5, 7, 9]])
The advantage of transpose over the .T attribute is that is allows you to
move axes into any arbitrary order.
For example, let’s say you had a 3D array:
>>> arr = np.arange(24).reshape((2, 3, 4))
>>> arr
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
Pavitra Degree College – B.Sc.(Computers) III Year VI sem Page 143
Page 146
Data Science using Python – Unit III
[ 8, 9, 10, 11]],
<BLANKLINE>
[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]])
>>> arr.shape
(2, 3, 4)
>>> arr[:, :, 0]
array([[ 0, 4, 8],
[12, 16, 20]])
Page 147
Data Science using Python – Unit III
import numpy as np
print(np.sort(arr))
Note: This method returns a copy of the array, leaving the original array
unchanged.
You can also sort arrays of strings, or any other data type:
Example
import numpy as np
print(np.sort(arr))
Example
import numpy as np
Page 148
Data Science using Python – Unit III
print(np.sort(arr))
If you use the sort() method on a 2-D array, both arrays will be sorted:
import numpy as np
print(np.sort(arr))
x = np.
(arr)
Page 149
Data Science using Python – Unit IV
UNIT – IV
4.1 Introduction to pandas Data Structures:
Pandas deals with the following three data structures −
Series
DataFrame
Panel
These data structures are built on top of Numpy array, which means they
are fast.
Page 150
Data Science using Python – Unit IV
Note − DataFrame is widely used and one of the most important data
structures. Panel is used much less.
Series
Series is a one-dimensional array like structure with homogeneous
data. For example, the following series is a collection of integers 10, 23,
56, …
10 23 56 17 52 61 73 90 26 72
Key Points
Homogeneous data
Size Immutable
Values of Data Mutable
DataFrame
DataFrame is a two-dimensional array with heterogeneous data. For
example,
Name Age Gender Rating
Steve 32 Male 3.45
Lia 28 Female 4.6
Vin 45 Male 3.9
Katie 38 Female 2.78
Page 151
Data Science using Python – Unit IV
Key Points
Heterogeneous data
Size Mutable
Data Mutable
Panel
Panel is a three-dimensional data structure with heterogeneous
data. It is hard to represent the panel in graphical representation. But a
panel can be illustrated as a container of DataFrame.
Key Points
Heterogeneous data
Size Mutable
Data Mutable
Page 152
Data Science using Python – Unit IV
4.2 Series
Series is a one-dimensional labeled array capable of holding data
of any type (integer, string, float, python objects, etc.). The axis labels
are collectively called index.
pandas.Series
A pandas Series can be created using the following constructor −
pandas.Series( data, index, dtype, copy)
The parameters of the constructor are as follows −
Sr.No Parameter & Description
1 data
data takes various forms like ndarray, list, constants
2 index
Index values must be unique and hashable, same length as
data. Default np.arrange(n) if no index is passed.
3 dtype
dtype is for data type. If None, data type will be inferred
4 copy
Copy data. Default False
Page 153
Data Science using Python – Unit IV
Example
Live Demo
#import the pandas library and aliasing as pd
import pandas as pd
s = pd.Series()
print s
Its output is as follows −
Series([], dtype: float64)
Page 154
Data Science using Python – Unit IV
4.3 DataFrame
A Data frame is a two-dimensional data structure, i.e., data is
aligned in a tabular fashion in rows and columns.
Features of DataFrame
Potentially columns are of different types
Size – Mutable
Labeled axes (rows and columns)
Can Perform Arithmetic operations on rows and columns
Structure
Let us assume that we are creating a data frame with student’s data.
pandas.DataFrame
A pandas DataFrame can be created using the following constructor −
pandas.DataFrame( data, index, columns, dtype, copy)
Page 155
Data Science using Python – Unit IV
2 index
For the row labels, the Index to be used for the resulting frame is
Optional Default np.arange(n) if no index is passed.
3 columns
For column labels, the optional default syntax is - np.arange(n).
This is only true if no index is passed.
4 dtype
Data type of each column.
5 copy
This command (or whatever it is) is used for copying of data, if
the default is False.
Create DataFrame
A pandas DataFrame can be created using various inputs like −
Lists
dict
Series
Numpy ndarrays
Another DataFrame
In the subsequent sections of this chapter, we will see how to create a
DataFrame using these inputs.
Page 156
Data Science using Python – Unit IV
Example
Live Demo
#import the pandas library and aliasing as pd
import pandas as pd
df = pd.DataFrame()
print df
Its output is as follows −
Empty DataFrame
Columns: []
Index: []
Page 157
Data Science using Python – Unit IV
4.4 PANEL
A panel is a 3D container of data. The term Panel data is derived
from econometrics and is partially responsible for the name pandas
− pan(el)-da(ta)-s.
The names for the 3 axes are intended to give some semantic meaning
to describing operations involving panel data. They are −
pandas.Panel()
Parameter Description
Data Data takes various forms like ndarray, series, map, lists,
dict, constants and also another DataFrame
Items axis=0
major_axis axis=1
minor_axis axis=2
Dtype Data type of each column
Copy Copy data. Default, false
Page 158
Data Science using Python – Unit IV
Create Panel
A Panel can be created using multiple ways like −
From ndarrays
From dict of DataFrames
From 3D ndarray
Live Demo
# creating an empty panel
import pandas as pd
import numpy as np
data = np.random.rand(2,4,5)
p = pd.Panel(data)
print p
Its output is as follows −
<class 'pandas.core.panel.Panel'>
Dimensions: 2 (items) x 4 (major_axis) x 5 (minor_axis)
Items axis: 0 to 1
Major_axis axis: 0 to 3
Minor_axis axis: 0 to 4
Page 159
Data Science using Python – Unit IV
2 .iloc()
Integer based
3 .ix()
Both Label and Integer based
.loc()
Pandas provide various methods to have purely label based indexing.
When slicing, the start bound is also included. Integers are valid labels,
but they refer to the label and not the position.
.loc() has multiple access methods like −
A single scalar label
A list of labels
A slice object
A Boolean array
loc takes two single/list/range operator separated by ','. The first one
indicates the row and the second one indicates columns.
Page 160
Data Science using Python – Unit IV
Example 1
Live Demo
#import the pandas library and aliasing as pd
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(8, 4),
index = ['a','b','c','d','e','f','g','h'], columns = ['A', 'B', 'C', 'D'])
df = pd.DataFrame(np.random.randn(8, 4),
index = ['a','b','c','d','e','f','g','h'], columns = ['A', 'B', 'C', 'D'])
Page 161
Data Science using Python – Unit IV
Example 3
Live Demo
# import the pandas library and aliasing as pd
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(8, 4),
index = ['a','b','c','d','e','f','g','h'], columns = ['A', 'B', 'C', 'D'])
df = pd.DataFrame(np.random.randn(8, 4),
index = ['a','b','c','d','e','f','g','h'], columns = ['A', 'B', 'C', 'D'])
Page 162
Data Science using Python – Unit IV
import numpy as np
df = pd.DataFrame(np.random.randn(8, 4),
index = ['a','b','c','d','e','f','g','h'], columns = ['A', 'B', 'C', 'D'])
.iloc()
Pandas provide various methods in order to get purely integer based
indexing. Like python and numpy, these are 0-based indexing.
The various access methods are as follows −
An Integer
A list of integers
A range of values
Example 1
Live Demo
# import the pandas library and aliasing as pd
import pandas as pd
import numpy as np
Page 163
Data Science using Python – Unit IV
Example 2
Live Demo
import pandas as pd
import numpy as np
# Integer slicing
print df.iloc[:4]
print df.iloc[1:5, 2:4]
Its output is as follows −
A B C D
0 0.699435 0.256239 -1.270702 -0.645195
1 -0.685354 0.890791 -0.813012 0.631615
2 -0.783192 -0.531378 0.025070 0.230806
3 0.539042 -1.284314 0.826977 -0.026251
C D
1 -0.813012 0.631615
2 0.025070 0.230806
3 0.826977 -0.026251
4 1.423332 1.130568
Example 3
Live Demo
import pandas as pd
import numpy as np
A B C D
1 -0.685354 0.890791 -0.813012 0.631615
2 -0.783192 -0.531378 0.025070 0.230806
Page 164
Data Science using Python – Unit IV
B C
0 0.256239 -1.270702
1 0.890791 -0.813012
2 -0.531378 0.025070
3 -1.284314 0.826977
4 -0.460729 1.423332
5 -0.512888 0.581409
6 -1.204853 0.098060
7 -0.947857 0.641358
.ix()
Besides pure label based and integer based, Pandas provides a hybrid
method for selections and subsetting the object using the .ix() operator.
Example 1
Live Demo
import pandas as pd
import numpy as np
# Integer slicing
print df.ix[:4]
Its output is as follows −
A B C D
0 0.699435 0.256239 -1.270702 -0.645195
1 -0.685354 0.890791 -0.813012 0.631615
2 -0.783192 -0.531378 0.025070 0.230806
3 0.539042 -1.284314 0.826977 -0.026251
Example 2
Live Demo
import pandas as pd
import numpy as np
Page 165
Data Science using Python – Unit IV
2 -0.783192
3 0.539042
4 -1.044209
5 -1.415411
6 1.062095
7 0.994204
Name: A, dtype: float64
Use of Notations
Getting values from the Pandas object with Multi-axes indexing uses the
following notation −
Object Indexers Return Type
Series s.loc[indexer] Scalar value
DataFrame df.loc[row_index,col_index] Series object
Panel p.loc[item_index,major_index, p.loc[item_index,major_index,
minor_index] minor_index]
Note − .iloc() & .ix() applies the same indexing options and Return
value.
Page 166
Data Science using Python – Unit IV
4.6 Filtration
Filtration filters the data on a defined criteria and returns the
subset of data. The filter() function is used to filter the data.
Live Demo
import pandas as pd
import numpy as np
In the above filter condition, we are asking to return the teams which
have participated three or more times in IPL.
Page 167
Data Science using Python – Unit IV
4.7 Mapping,
map() function returns a map object(which is an iterator) of the results after
applying the given function to each item of a given iterable (list, tuple etc.)
Syntax :
map(fun, iter)
Parameters :
NOTE : You can pass one or more iterable to the map() function.
Returns :
NOTE : The returned value from map() (map object) then can be passed to
functions like list() (to create a list), set() (to create a set) .
CODE 1
# Return double of n
def addition(n):
return n + n
[2, 4, 6, 8]
Page 168
Data Science using Python – Unit IV
4.8 Sorting
The easiest way to sort is with the sorted(list) function, which takes a list
and returns a new list with those elements in sorted order. The original
list is not changed.
a = [5, 1, 4, 3]
print(sorted(a)) ## [1, 3, 4, 5]
print(a) ## [5, 1, 4, 3]
It's most common to pass a list into the sorted() function, but in fact it
can take as input any sort of iterable collection. The older list.sort()
method is an alternative detailed below. The sorted() function seems
easier to use compared to sort(), so I recommend using sorted().
For example with a list of strings, specifying key=len (the built in len()
function) sorts the strings by length, from shortest to longest. The sort
calls len() for each string to get the list of proxy length values, and then
sorts with those proxy values.
Page 169
Data Science using Python – Unit IV
You can also pass in your own MyFn as the key function, like this:
## Say we have a list of strings we want to sort by the last letter of the string.
strs = ['xc', 'zb', 'yd' ,'wa']
## Write a little function that takes a string, and returns its last letter.
## This will be the key function (takes in 1 value, returns 1 value).
def MyFn(s):
return s[-1]
For more complex sorting like sorting by last name then by first name,
you can use the itemgetter or attrgetter functions like:
Page 170
Data Science using Python – Unit IV
sorted(grade, key=itemgetter(1,0))
# [('Anil', 'Frank', 100), ('Freddy', 'Frank', 3), ('Anil', 'Wang', 24)]
sort() method
As an alternative to sorted(), the sort() method on a list sorts that list into
ascending order, e.g. list.sort(). The sort() method changes the
underlying list and returns None, so use it like this:
alist.sort() ## correct
alist = blist.sort() ## Incorrect. sort() returns None
Page 171
Data Science using Python – Unit IV
Live Demo
import pandas as pd
import numpy as np
s = pd.Series(np.random.np.random.randn(5), index=list('abcde'))
s['d'] = s['b'] # so there's a tie
print s.rank()
a 1.0
b 3.5
c 2.0
d 3.5
e 5.0
dtype: float64
Page 172
Data Science using Python – Unit IV
1 r
It is Read Only mode. It opens the text file for reading. When the file is not
present, it raises I/O Error.
2 r+
This mode for Reading and Writing. When the file is not present, it will raise
I/O Error.
3 w
It is for write only jobs. When file is not present, it will create a file first, then
start writing, when the file is present, it will remove the contents of that file, and
start writing from beginning.
4 w+
It is Write and Read mode. When file is not present, it can create the file, or
when the file is present, the data will be overwritten.
5 a
This is append mode. So it writes data at the end of a file.
6 a+
Append and Read mode. It can append data as well as read the data.
Page 173
Data Science using Python – Unit IV
Now see how a file can be written using writelines() and write() method.
Live Demo
Output
Writing Complete
After writing the lines, we are appending some lines into the file.
Live Demo
Output
Appending Done
At last, we will see how to read the file content from the read() and
readline() method. We can provide some integer number 'n' to get first 'n'
characters.
Page 174
Data Science using Python – Unit IV
Output
Show the full content:
This is first line.
This is another line to store into file.
The Third Line for the file.
Another line... !@#$%^&*()_+.
End Line
First 25 characters:
This is first line.
This
Page 175
Data Science using Python – Unit V
UNIT - V
5.1 Data Cleaning and Preparation
Data cleaning is one of the important parts of machine learning. It plays
a significant part in building a model. It surely isn’t the fanciest part of machine
learning and at the same time, there aren’t any hidden tricks or secrets to
uncover. However, the success or failure of a project relies on proper data
cleaning. Professional data scientists usually invest a very large portion of their
time in this step because of the belief that “Better data beats fancier
algorithms”.
Page 176
Data Science using Python – Unit V
Irrelevant observations are any type of data that is of no use to us and can be
removed directly.
For example, the model will treat America and America as different classes
or values, though they represent the same value or red, yellow, and red-yellow
as different classes or attributes, though one class can be included in the other
two classes. So, these are some structural errors that make our model
inefficient and give poor quality results.
Page 177
Data Science using Python – Unit V
Plus, in the real world, you often need to make predictions on new data even if
some of the features are missing!
Even if you build a model to impute your values, you’re not adding any real
information. You’re just reinforcing the patterns already provided by other
features.
Missing data is like missing a puzzle piece. If you drop it, that’s like pretending
the puzzle slot isn’t there. If you impute it, that’s like trying to squeeze in a
piece from somewhere else in the puzzle.
Page 178
Data Science using Python – Unit V
Page 179
Data Science using Python – Unit V
Page 180
Data Science using Python – Unit V
1. Discovery
The first step is to identify and understand data in its
original source format with the help of data profiling tools.
Finding all the sources and data types that need to be
transformed. This step helps in understanding how the data
needs to be transformed to fit into the desired format.
2. Mapping
The transformation is planned during the data mapping
phase. This includes determining the current structure, and the
consequent transformation that is required, then mapping the
data to understand at a basic level, the way individual fields
would be modified, joined or aggregated.
Page 181
Data Science using Python – Unit V
3. Code Generation
The code, which is required to run the transformation
process, is created in this step using a data transformation
platform or tool.
4. Execution
The data is finally converted into the selected format with
the help of the code. The data is extracted from the source(s),
which can vary from structured to streaming, telemetry to log
files. Next, transformations are carried out on data, such as
aggregation, format conversion or merging, as planned in the
mapping stage. The transformed data is then sent to the
destination system which could be a dataset or a data
warehouse. Some of the transformation types, depending on
the data involved, include:
5. Review
Page 182
Data Science using Python – Unit V
String basics
x = "\" \\"
Page 183
Data Science using Python – Unit V
x
#> '" \\'
print(x)
#> " \
String length
Python contains many functions to work with strings. We’ll use the
functions from pandas for use on series. These all start with str. For
example, str.length() tells you the number of characters in a string:
pd.Series(["a", "R for data science", np.nan]).str.len()
#> 0 1.0
#> 1 18.0
#> 2 NaN
Pavitra Degree College – B.Sc.(Computers) III Year VI sem Page 181
Page 184
Data Science using Python – Unit V
Combining strings
To combine two or more strings, use str_c():
pd.Series(["x", "y"]).str.cat()
#> 'xy'
pd.Series(["x", "y", "z"]).str.cat()
#> 'xyz'
Use the sep argument to control how they’re separated:
pd.Series(["x", "y"]).str.cat(sep = '_')
#> 'x_y'
Like most other functions in Python, missing values are contagious. If
you want them to print as "NA", use fillna() or na_rep = 'NA':
x = pd.Series(["abc", np.nan])
x.str.cat()
#> 'abc'
x.str.cat(na_rep = "NA")
#> 'abcNA'
x.fillna('NA').str.cat()
#> 'abcNA'
Subsetting strings
You can extract parts of a string using str[]. As well as the
string, str[] takes start:end arguments which give the (inclusive)
position of the substring:
x = pd.Series(["Apple", "Banana", "Pear"])
x.str[0:3]
# negative numbers count backwards from end
#> 0 App
Pavitra Degree College – B.Sc.(Computers) III Year VI sem Page 182
Page 185
Data Science using Python – Unit V
#> 1 Ban
#> 2 Pea
#> dtype: object
x.str[-3:]
#> 0 ple
#> 1 ana
#> 2 ear
#> dtype: object
Note that str[] won’t fail if the string is too short: it will just return as
much as possible:
pd.Series(["a"]).str[0:5]
#> 0 a
#> dtype: object
You can also use the assign strings using str.slice_replace() to modify
strings:
x.str.slice_replace(0,0, repl = "5")
#> 0 5Apple
#> 1 5Banana
#> 2 5Pear
#> dtype: object
Page 186
Data Science using Python – Unit V
This is perhaps sufficient to work with some data, but it will break if there
are any missing values, so this approach requires putting in extra
checks:
Page 187
Data Science using Python – Unit V
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(10,4),index=pd.date_range('1/1/2000',
periods=10), columns=list('ABCD'))
df.plot()
Page 188
Data Science using Python – Unit V
We can plot
one column
versus another
using
the x and y ke
ywords.
Plotting
methods allow
a handful of plot styles other than the default line plot. These methods
can be provided as the kind keyword argument to plot(). These include
−
Bar Plot
Let us now see what a Bar Plot is by creating one. A bar plot can be
created in the following way −
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.rand(10,4),columns=['a','b','c','d')
df.plot.bar()
Page 189
Data Science using Python – Unit V
import pandas as pd
df = pd.DataFrame(np.random.rand(10,4),columns=['a','b','c','d')
df.plot.bar(stacked=True)
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.rand(10,4),columns=['a','b','c','d')
df.plot.barh(stacked=True)
Page 190
Data Science using Python – Unit V
Histograms
Histograms can be plotted using the plot.hist() method. We can specify
number of bins.
import pandas as pd
import numpy as np
df =
pd.DataFrame({'a':np.random.randn(1000)+1,'b':np.random.randn(1000),'c':
np.random.randn(1000) - 1}, columns=['a', 'b', 'c'])
df.plot.hist(bins=20)
Its output is as follows −
To plot different histograms for each column, use the following code −
import pandas as pd
import numpy as np
df=pd.DataFrame({'a':np.random.randn(1000)+1,'b':np.random.randn(1000),'c':
np.random.randn(1000) - 1}, columns=['a', 'b', 'c'])
df.diff.hist(bins=20)
Page 191
Data Science using Python – Unit V
Box Plots
Boxplot can be drawn calling Series.box.plot() and
DataFrame.box.plot(), or DataFrame.boxplot() to visualize the
distribution of values within each column. For instance, here is a boxplot
representing five trials of 10 observations of a uniform random variable
on [0,1).
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.rand(10, 5), columns=['A', 'B', 'C', 'D', 'E'])
df.plot.box()
Its output is as follows −
Page 192
Data Science using Python – Unit V
Area Plot
Area plot can be created using the Series.plot.area() or
the DataFrame.plot.area() methods.
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.rand(10, 4), columns=['a', 'b', 'c', 'd'])
df.plot.area()
Scatter Plot
Scatter plot can be created using
the DataFrame.plot.scatter() methods.
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.rand(50, 4), columns=['a', 'b', 'c', 'd'])
df.plot.scatter(x='a', y='b')
Page 193
Data Science using Python – Unit V
Pie Chart
Pie chart can be created using the DataFrame.plot.pie() method.
import pandas as pd
import numpy as np
df = pd.DataFrame(3 * np.random.rand(4), index=['a', 'b', 'c', 'd'], columns=['x'])
df.plot.pie(subplots=True)
Its output is as follows −
Page 194