You are on page 1of 188

CHAPTER -1

ANACONDA IDE - INSTALLATION

Anaconda IDE:
Open Source
Anaconda Individual Edition is the world’s most
popular Python distribution platform with over 20 million
users worldwide. You can trust in our long-term
commitment to supporting the Anaconda open-source
ecosystem, the platform of choice for Python data
science.
It is the easiest way to perform Python/R data
science and machine learning on a single machine.
Developed for solo practitioners, it is the toolkit that
equips you to work with thousands of open-source
packages and libraries.
Conda Packages
Cloud-based repository to find and install over
7,500 data science and machine learning packages. With
the conda-install command, you can start using
thousands of open-source Conda, R, Python and many
other packages.
Manage Environments
It provides the utilities to build, distribute, install,
update, and manage software in a cross-platform
manner. Conda makes it easy to manage multiple data
1
environments that can be maintained and run separately
without interference from each other.
Anaconda Installation Link
https://www.anaconda.com/products/individual#Downloads

Fig1. Anaconda Installers .exe Download Page


Installation Documentation Link
https://docs.anaconda.com/anaconda/install/windows/
https://docs.anaconda.com/anaconda/install/mac-os/
https://docs.anaconda.com/anaconda/install/linux/
https://docs.anaconda.com/anaconda/install/linux-
power8/
Updating from older version
https://docs.anaconda.com/anaconda/install/update-
version/

2
Introduction to Python
Python is a general purpose, dynamic, high-level,
and interpreted programming language. It supports
Object Oriented programming approach to develop
applications. It is simple and easy to learn and provides
lots of high-level data structures.
Python is easy to learn yet powerful and versatile
scripting language which makes it attractive for
Application Development.
Python's syntax and dynamic typing with its
interpreted nature make it an ideal language for scripting
and rapid application development.
Python supports multiple programming pattern,
including object-oriented, imperative, and functional or
procedural programming styles.
Python is not intended to work in a particular area,
such as web programming. That is why it is known as
multipurpose programming language because it can be
used with web, enterprise, 3D CAD, etc.
We don't need to use data types to declare
variable because it is dynamically typed so we can write
a=10 to assign an integer value in an integer variable.
Python makes the development and debugging
fast because there is no compilation step included in
Python development and edit-test-debug cycle is very
fast.
3
It is used for:
Web development (server-side),
Software development,
Mathematics,
System scripting.

What can Python do?


 Python can be used on a server to create web
applications.
 Python can be used alongside software to create
workflows.
 Python can connect to database systems. It can
also read and modify files.
 Python can be used to handle big data and
perform complex mathematics.
 Python can be used for rapid prototyping, or for
production-ready software development.
Why Python?
 Python works on different platforms (Windows,
Mac, Linux, Raspberry Pi, etc.).
 Python has a simple syntax similar to the English
language.
 Python has syntax that allows developers to write
programs with fewer lines than some other
programming languages.

4
 Python runs on an interpreter system, meaning
that code can be executed as soon as it is written.
This means that prototyping can be very quick.
 Python can be treated in a procedural way, an
object-orientated way or a functional way.

Python Syntax compared to other programming


languages
 Python was designed for readability and has some
similarities to the English language with influence
from mathematics.
 Python uses new lines to complete a command, as
opposed to other programming languages which
often use semicolons or parentheses.
 Python relies on indentation, using whitespace, to
define scope, such as the scope of loops,
functions and classes. Other programming
languages often use curly-brackets for this
purpose.
Python Quickstart
Python is an interpreted programming language;
this means that as a developer you write Python (.py)
files in a text editor and then put those files into the
python interpreter to be executed.
The way to run a python file is like this on the
command line:
5
C:\Users\Your Name>python helloworld.py
Where "helloworld.py" is the name of your python file.
Let's write our first Python file, called helloworld.py, which
can be done in any text editor.

Example
#helloworld.py
print("Hello, World!")
Output
Hello, World!
Example
C:\Users\Your Name>python
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45)
[MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more
information.
>>> print("Hello, World!") Hello, World!
Whenever you are done in the python command
line, you can simply type the following to quit the python
command line interface:
exit()
Basics of Loops
In general, statements are executed sequentially:
The first statement in a function is executed first, followed
by the second, and so on. There may be a situation when

6
you need to execute a block of code several number of
times.
Programming languages provide various control
structures that allow for more complicated execution
paths.
A loop statement allows us to execute a statement
or group of statements multiple times. The following
diagram illustrates a loop statement.

Fig 2. Condition Flow chart

7
Python programming language provides following
types of loops to handle looping requirements.
Table1. Loops Type with Description
S.No. Loop Type & Description
1 while loop
Repeats a statement or group of statements while a given
condition is TRUE. It tests the condition before executing the
loop body.
2 for loop
Executes a sequence of statements multiple times and
abbreviates the code that manages the loop variable.
3 nested loops
You can use one or more loop inside any another while, for
or do..while loop.

While Loop
In python, while loop is used to execute a block of
statements repeatedly until a given a condition is
satisfied. And when the condition becomes false, the line
immediately after the loop in program is executed.
Syntax:
while expression:
statement(s)
All the statements indented by the same number of
character spaces after a programming construct are

8
considered to be part of a single block of code. Python
uses indentation as its method of grouping statements.
Example:
# Python program to illustrate
# while loop
count = 0
while (count < 3):
count = count + 1
print("Welcome to while loop statement")
Output
Welcome to while loop statement
Welcome to while loop statement
Welcome to while loop statement
Using else statement with while loops:
As discussed above, while loop executes the block
until a condition is satisfied. When the condition becomes
false, the statement immediately after the loop is
executed.
The else clause is only executed when your while
condition becomes false. If you break out of the loop, or if
an exception is raised, it won’t be executed.
If else like this:
if condition:
# execute these statements
else:
# execute these statements
9
and while… else loop like this which are similar
Syntax:
while condition:
# execute these statements
else:
# execute these statements
Example:
#Python program to illustrate
# combining else with while
count = 0
while (count < 3):
count = count + 1
print("welcome to while…else example")
else:
print("Else Block Executed")
Output
welcome to while…else example
welcome to while…else example
welcome to while…else example
Else Block Executed
Single statement while block
Just like the if block, if the while block consists of a
single statement the we can declare the entire loop in a
single line as shown below:
Example:
# Python program to illustrate
10
# Single statement while block
count = 0
while (count == 0): print("Welcome to While Block")
Note: It is suggested not to use this type of loops as it is
a never-ending infinite loop where the condition is always
true and you have to forcefully terminate the compiler.
See this for an example where while loop is used
for iterators. As mentioned in the article, it isn’t
recommended to use while loop for iterators in python.
for in Loop
For loops are used for sequential traversal. For
example: traversing a list or string or array etc. In Python,
there is no C style for loop, i.e., for (i=0; i<n; i++). There is
“for in” loop which is similar to for each loop in other
languages. Let us learn how to use for in loop for
sequential traversals.
Syntax:
for iterator_var in sequence:
statement(s)
It can be used to iterate over a range and iterators.
Example
# Python program to illustrate
# Iterating over range 0 to n-1
n=4
for i in range(0, n):
print(i)
11
Output
0
1
2
3
Example:
# Python program to illustrate
# Iterating over a list
print("List Iteration")
l = ["Welcome", "to", "For Loop"]
for i in l:
print(i)
# Iterating over a tuple (immutable)
print("\nTuple Iteration")
t = ("welcome", "to", "Tuple")
for i in t:
print(i)
# Iterating over a String
print("\nString Iteration")
s = "String"
for i in s :
print(i)
# Iterating over dictionary
print("\nDictionary Iteration")
d = dict()
d['xyz'] = 123
12
d['abc'] = 345 for i in d :
print("%s %d" %(i, d[i]))
Output
List Iteration
Welcome
to
For Loop

Tuple Iteration
welcome
to
Tuple
String Iteration

S
t
r
i
n
g
Dictionary Iteration
xyz 123
abc 345
Iterating by index of sequences: We can also use the
index of elements in the sequence to iterate. The key

13
idea is to first calculate the length of the list and in iterate
over the sequence within the range of this length.
# Python program to illustrate
# Iterating by index
list = ["welcome", "to", "index"]
for index in range(len(list)):
print list[index]
Output
Welcome
to
index
Using else statement with for loops
We can also combine else statement with for loop
like in while loop. But as there is no condition in for loop
based on which the execution will terminate so the else
block will be executed immediately after for block finishes
execution.
Example
# Python program to illustrate
# combining else with for
list = ["welcome", "to", "for…else"]
for index in range(len(list)):
print list[index]
else: print "Inside Else Block"
Output
welcome
14
to
for…else
print "Inside Else Block"
Inside Else Block
Nested Loops
Python programming language allows to use one
loop inside another loop. Following section shows few
examples to illustrate the concept.
Syntax:
for iterator_var in sequence:
for iterator_var in sequence:
statement(s)
statement(s)
The syntax for a nested while loop statement in

Python programming language is as follows:


Syntax:
while expression:
while expression:
statement(s)
statement(s)
A final note on loop nesting is that we can put any
type of loop inside of any other type of loop. For example,
a for loop can be inside a while loop or vice versa.
Example
# Python program to illustrate
15
# nested for loops in Python
from future import print_function
for i in range(1, 5):
for j in range(i):
print(i, end=' ')
print()
Output: 1
22
333
4444
Loop Control Statements
Loop control statements change execution from its
normal sequence. When execution leaves a scope, all
automatic objects that were created in that scope are
destroyed.

Python supports the following control statements.

Table 2: Control Statements


S.No. Control Statement & Description
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
16
and immediately retest its condition prior to
reiterating.
3 pass statement
The pass statement in Python is used when a
statement is required syntactically but you do not
want any command or code to execute.

Continue Statement:
It returns the control to the beginning of the loop.
Example:
# Prints all letters except 'e' and 's'
for letter in 'Continue Statement Example':
if letter == 'e' or letter == 's':
continue
print 'Current Letter :', letter
var = 10
Output
Current Letter : C
Current Letter : o
Current Letter : n
Current Letter : t
Current Letter : i
Current Letter : n
Current Letter : u
Current Letter :
Current Letter : S
17
Current Letter : t
Current Letter : a
Current Letter : t
Current Letter : m
Current Letter : n
Current Letter : t
Current Letter :
Current Letter : E
Current Letter : x
Current Letter : a
Current Letter : m
Current Letter : p
Current Letter : l
Break Statement: It brings control out of the loop

Example:
for letter in 'Brake Statement Example':
# break the loop as soon it sees 'e'
# or 's'
if letter == 'e' or letter == 's':
break
print 'Current Letter :', letter
Output
Current Letter : e

18
Pass Statement: We use pass statement to write empty
loops. Pass is also used for empty control statement,
function and classes.
Example:
# An empty loop
for letter in 'Pass Statement Example':
pass
print 'Last Letter :', letter
Output:
Last Letter : e

19
CHAPTER -2
BASICS OF OOPS CONCEPTS

Basics of OOPs Concepts


OOP concepts
Python is an object-oriented programming
language. We can easily create and use classes and
objects in Python.
Object-oriented programming is based on the
imperative programming paradigm, which uses
statements to change a program's state. It focuses on
describing how a program should operate. Examples of
imperative programming languages are C, C++, Java,
Go, Ruby and Python. This stands in contrast to
declarative programming, which focuses on what the
computer program should accomplish, without specifying
how. Examples are database query languages like SQL
and XQuery, where one only tells the computer what data
to query from where, but now how to do it.
OOP uses the concept of objects and classes. A
class can be thought of as a 'blueprint' for objects. These
can have their own attributes (characteristics they
possess), and methods (actions they perform).
Major object-oriented concepts of Python
programming language are given below:
1. Object,
20
2. Class,
3. Method,
4. Inheritance,
5. Polymorphism,
6. Data Abstraction,
7. Encapsulation.
Object
Object is an entity that has state and behaviour. It
may be physical and logical. For example: mouse,
keyboard, chair, table, pen etc. Everything in Python is an
object, and almost everything has attributes and
methods.
Class
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 a student class then it
should contain an attribute and method i.e. an email id,
name, age, roll number etc.
Method
Method is a function that is associated with an
object. In Python, method is not unique to class
instances. Any object type can have methods.
Inheritance
Inheritance specifies that one object acquires all
the properties and behaviors of parent object. By using
inheritance, we can define a new class with a little or no
21
changes to the existing class. The new class is known as
derived class or child class and from which it inherits the
properties is called base class or parent class. It provides
re- usability of the code.
Polymorphism
Polymorphism defines that one task can be
performed in different ways. For example: We have a
class animal and all animals talk. But they talk differently.
Here, the "talk" behaviour totally depends on the animal.
So, the abstract "animal" does not actually "talk", but
specific animals have a concrete implementation of the
action "talk".
Encapsulation
Encapsulation 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 are
synonymous as data abstraction is achieved through
encapsulation.
Abstraction is used to hide internal details and
show only functionalities. Abstracting something means
to give names to things, so that the name captures the
basic idea of what a function or a whole program does.

22
Object-oriented vs. Procedure-oriented Programming
languages
The difference between object-oriented and
procedure-oriented programming is given below:
Table 3: Object Oriented Programming vs Procedural
Oriented Programming
Index Object-oriented Programming Procedural Programming
1. Object-oriented programming is Procedural programming
the problem-solving approach uses a list of instructions to
and used where computation is do computation step by
done by using objects. step.
2. It makes the development In procedural programming,
and maintenance easier. it is not easy to maintain the
codes when the project
becomes lengthy.
3. It simulates the real-world It doesn't simulate the real
entity. So real-world problems world. It works on step-by-
can be easily solved through step instructions divided into
oops. small parts called functions.
4. It provides data hiding. So, it Procedural language
is more secure than doesn't provide any proper
procedural languages. You way for data binding, so it is
cannot access private data less secure.
from anywhere.

23
5. Example of object-oriented Example of procedural
programming languages is languages are: C, Fortran,
C++, Java, .Net, Python, C#, Pascal, VB etc.
etc.

OOP in Python
Python is a great programming language that
supports OOP. You will use it to define a class with
attributes and methods, which you will then call. Python
offers a number of benefits compared to other
programming languages like Java, C++ or R. It's a
dynamic language, with high-level data types. This
means that development happens much faster than with
Java or C++. It does not require the programmer to
declare types of variables and arguments. This also
makes Python easier to understand and learn for
beginners, its code being more readable and intuitive.
How to create a class
To define a class in Python, you can use the class
keyword, followed by the class name and a colon. Inside
the class, an __init__ method has to be defined with def.
This is the initializer that you can later use to instantiate
objects. It's similar to a constructor in Java. __init__ must
always be present! It takes one argument: self, which
refers to the object itself. Inside the method, the pass
keyword is used as of now, because Python expects you
24
to type something there. Remember to use correct
indentation!
Syntax:
class Dog:
def init (self):
pass
Note: self in Python is equivalent to this in C++ or Java.
In this case, you have a (mostly empty) Dog class, but no
object yet. Let's create one!
Instantiating objects
To instantiate an object, type the class name,
followed by two brackets. You can assign this to a
variable to keep track of the object.
Syntax:
tommy = Dog()
And print it:
print(tommy)
< main __.Dog object at 0x111f47278>
Adding attributes to a class
After printing tommy, it is clear that this object is a
dog. But you haven't added any attributes yet. Let's give
the Dog class a name and age, by rewriting it:
Example
class Dog:
def __init (self, name, age):
self.name = name
25
self.age = age
You can see that the function now takes two
arguments after self: name and age. These then get
assigned to self.name and self.age respectively. You can
now create a new tommy object, with a name and age:

Example
tommy= Dog("Tommy", 2)
To access an object's attributes in Python, you can
use the dot notation. This is done by typing the name of
the object, followed by a dot and the attribute's name.
Example
print(tommy.name)
print(tommy.age)
Output
Tommy
2
This can also be combined in a more elaborate sentence:
Example
print(tommy.name + " is " + str(tommy.age) + " year(s)
old.")
Output
Tommy is 2 year(s) old.
The str() function is used here to convert the age
attribute, which is an integer, to a string, so you can use it
in the print() function.
26
Define methods in a class
Now that you have aDog class, it does have a
name and age which you can keep track of, but it doesn't
actually do anything. This is where instance methods
come in. You can rewrite the class to now include a
bark() method. Notice how the def keyword is used again,
as well as the self argument.
Example
class Dog:
def __init (self, name, age):
self.name = name
self.age = age
def bark(self):
print("bark bark!")
The bark method can now be called using the dot
notation, after instantiating a new tommy object. The
method should print "bark bark!" to the screen. Notice the
parentheses (curly brackets) in .bark(). These are always
used when calling a method. They're empty in this case,
since the bark() method does not take any arguments.
tommy = Dog("Tommy", 2)
tommy.bark()
bark bark!
Example
class Dog:
def __init (self, name, age):
27
self.name = name
self.age = age
def bark(self):
print("bark bark!")
def doginfo(self):
print(self.name + " is " + str(self.age) + " year(s) old.")
tommy = Dog("tommy", 2)
scooby = Dog("Scooby", 12)
filo = Dog("Filo", 8)
tommy.doginfo()
scooby.doginfo()
filo.doginfo()
Output
Tommy is 2 year(s) old.
Scooby is 12 year(s) old.
Filo is 8 year(s) old.
Passing arguments to methods
You would like for our dogs to have a buddy. This
should be optional, since not all dogs are as sociable.
Take a look at the setBuddy() method below. It takes self,
as per usual, and buddy as arguments. In this case,
buddy will be another Dog object. Set the self.buddy
attribute to buddy, and the buddy.buddy attribute to self.
This means that the relationship is reciprocal; you are
your buddy's buddy.

28
Example
class Dog:
def __init (self, name, age):
self.name = name
self.age = age
def bark(self):
print("bark bark!")
def doginfo(self):
print(self.name + " is " + str(self.age) + " year(s) old.")
def birthday(self):
self.age +=1
def setBuddy(self, buddy):
self.buddy = buddy
buddy.buddy = self
You can now call the method with the dot notation,
and pass it another Dog object. In this case, Tommy's
buddy will be Filou:
tommy = Dog("Tommy", 2)
filo = Dog("Filo", 8)
tommy.setBuddy(filo)
If you now want to get some information about
Tommy's buddy, you can use the dot notation twice:.
First, to refer to Tommy's buddy, and a second time to
refer to its attribute.
print(tommy.buddy.name)
print(tommy.buddy.age)
29
Output
Filou
8
Notice how this can also be done for Filo.
print(filo.buddy.name)
print(filo.buddy.age)
Output
Tommy
2
The buddy's methods can also be called. The self
argument that gets passed to doginfo() is now
tommy.buddy, which is filo.
tommy.buddy.doginfo()

Output
Filo is 8 year(s) old.
Delete the Object
We can delete the properties of the object or object itself
by using the del keyword. Consider the following
example.
Example
class Dog:
def __init (self, name, age):
self.name = name
self.age = age
def bark(self):
30
print("bark bark!")
def doginfo(self):
print(self.name + " is " + str(self.age) + " year(s) old.")
def birthday(self):
self.age +=1
def setBuddy(self, buddy):
self.buddy = buddy
buddy.buddy = self
del bark
del doginfo
del birthday

Python Constructor
A constructor is a special type of method (function)
which is used to initialize the instance members of the
class.
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.
Constructors can be of two types.
1. Parameterized Constructor
2. Non-parameterized Constructor
Constructor definition is executed when we create
the object of this class. Constructors also verify that there
are enough resources for the object to perform any start-
up task.
31
Creating the constructor in python
In Python, the method the _init_() simulates the
constructor of the class. This method is called when the
class is instantiated. It accepts the self-keyword as a first
argument which allows accessing the attributes or
method of the class.
We can pass any number of arguments at the time
of creating the class object, depending upon the _init_ ()
definition. It is mostly used to initialize the class attributes.
Every class must have a constructor, even if it simply
relies on the default constructor.
Consider the following example to initialize the
Employee class attributes.
Example
class Employee:
def __init__ (self, name, id):
self.id = id
self.name = name
def display(self):
print("ID: %d \nName: %s" % (self.id, self.name))
emp1 = Employee("John", 1001)
emp2 = Employee("David", 1002)
# accessing display() method to print employee 1
information
emp1.display()

32
# accessing display() method to print employee 2
information
emp2.display()
Output:
ID: 1001
Name: John ID: 1002
Name: David
Counting the number of objects of a class
The constructor is called automatically when we
create the object of the class. Consider the following
example.
Example
class Student:
count = 0
def __init__ (self):
Student.count = Student.count + 1
s1=Student()
s2=Student()
s3=Student()
print("The number of students:",Student.count)
Output:
The number of students: 3
Python Non-Parameterized Constructor
The non-parameterized constructor uses when we
do not want to manipulate the value or the constructor

33
that has only self as an argument. Consider the following
example.
Example
class Student:
# Constructor - non parameterized
Def __init__ (self):
print("This is non parametrized constructor")
def show(self,name):
print("Hello",name)
student = Student()
student.show("John")
Python Parameterized Constructor
The parameterized constructor has multiple
parameters along with the self. Consider the following
example.

Example
class Student:
# Constructor – parameterized
Def __init__ (self, name):
print("This is parametrized constructor")
self.name = name
def show(self):
print("Hello",self.name)
student = Student("John")
student.show()
34
Output
This is parametrized constructor
Hello John
Python Default Constructor
When we do not include the constructor in the
class or forget to declare it, then that becomes the default
constructor. It does not perform any task but initializes the
objects. Consider the following example.
Example
class Student:
roll_num = 101
name = "Joseph"
def display(self):
print(self.roll_num,self.name)
st = Student()
st.display()

Output
101 Joseph
More than One Constructor in Single class
Let's have a look at another scenario, what happen
if we declare the two same constructors in the class.
Example
class Student:
def __init__ (self):
print("The First Constructor")
35
def __init__ (self):
print("The second contructor")
st = Student()
Output:
The Second Constructor
In the above code, the object st called the second
constructor whereas both have the same configuration.
The first method is not accessible by the st object.
Internally, the object of the class will always call the last
constructor if the class has multiple constructors.

Python built-in class functions


The built-in functions defined in the class are
described in the following table.

Table4. Built-in class Functions


S.No. Function Description

1 getattr(obj,name,default) It is used to access the


attribute of the object.
2 setattr(obj, name,value) It is used to set a
particular value to the
specific attribute of an
object.
3 delattr(obj, name) It is used to delete a
specific attribute.

36
4 hasattr(obj, name) It returns true if the
object contains some
specific attribute.

Example
class Student:
def __init__ (self, name, id, age):
self.name = name
self.id = id
self.age = age
# creates the object of the class Student
s = Student("John", 101, 22)
# prints the attribute name of the object s
print(getattr(s, 'name'))
# reset the value of attribute age to 23
setattr(s, "age", 23)
# prints the modified value of age
print(getattr(s, 'age'))
# prints true if the student contains the attribute with
name id
print(hasattr(s, 'id'))
# deletes the attribute age
delattr(s, 'age')
# this will give an error since the attribute age has been
deleted
print(s.age)
37
Output
John 23
True
AttributeError: 'Student' object has no attribute 'age'
Built-in class attributes
Along with the other attributes, a Python class also
contains some built-in class attributes which provide
information about the class.
The built-in class attributes are given in the below table.
Table 5: Built-in class Attributes
S.No. Attribute Description
1 __dict__ It provides the dictionary
containing the information about
the class 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
class Student:
def __init__ (self,name,id,age):
38
self.name = name;
self.id = id;
self.age = age
def display_details(self):
print("Name:%s, ID:%d, age:%d"%(self.name,self.id))
s = Student("John",101,22)
print(s._doc_)
print(s._dict_)
print(s._module_)
Output:
None
{'name': 'John', 'id': 101, 'age': 22}
__main__

Python Inheritance
Inheritance is an important aspect of the object-
oriented paradigm. Inheritance provides code reusability
to the program because we can use an existing class to
create a new class instead of creating it from scratch.
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 implementation to the functions of
the parent class. In this section of the tutorial, we will
discuss inheritance in detail.

39
In python, a derived class can inherit base class by
just mentioning the base in the bracket after the derived
class name. Consider the following syntax to inherit a
base class into the derived class.

Fig 2. Inheritance
Syntax
class derived-class(base class):
<class-suite>
A class can inherit multiple classes by mentioning
all of them inside the bracket. Consider the following
syntax.
Syntax
class derive-class(<base class 1>, <base class 2>,<base
class n>): <class - suite>
Example
class Animal:
def speak(self):
print("Animal Speaking")
#child class Dog inherits the base class Animal
class Dog(Animal):
def bark(self):
print("dog barking")
40
d = Dog()
d.bark()
d.speak()
Output
dog barking
Animal Speaking
Python Multi-Level inheritance
Multi-Level inheritance is possible in python like
other object-oriented languages. Multi-level inheritance is
archived when a derived class inherits another derived
class. There is no limit on the number of levels up to
which, the multi-level inheritance is archived in python.

Fig. 3 Multilevel Inheritance


Syntax
class class1:
<class-suite>
class class2(class1):
<class suite>
class class3(class2):
<class suite>

41
Example
class Animal:
def speak(self):
print("Animal Speaking")
#The child class Dog inherits the base class Animal
class Dog(Animal):
def bark(self):
print("dog barking")
#The child class Dogchild inherits another child class Dog
class DogChild(Dog):
def eat(self):
print("Eating bread...")
d = DogChild()
d.bark()
d.speak()
d.eat()
Output:
dog barking
Animal Speaking
Eating bread...
Python Multiple inheritance
Python provides us the flexibility to inherit multiple
base classes in the child class.

42
Fig 4. Multiple Inheritance
Syntax
class Base1:
<class-suite>

class Base2:
<class-suite>
.
.
.
class BaseN:
<class-suite>

class Derived(Base1, Base2,…….BaseN):


class-suite>
Example
class Calculation1:
def Summation(self,a,b):
return a+b;
class Calculation2:
def Multiplication(self,a,b):

43
return a*b;
class Derived(Calculation1,Calculation2):
def Divide(self,a,b):
return a/b;
d = Derived()
print(d.Summation(10,20))
print(d.Multiplication(10,20))
print(d.Divide(10,20))
Output:
30
200
0.5
The issubclass(sub,sup) method
The issubclass(sub, sup) method is used to check
the relationships between the specified classes. It returns
true if the first class is the subclass of the second class,
and false otherwise.

Consider the following example.


Example
class Calculation1:
def Summation(self,a,b):
return a+b;
class Calculation2:
def Multiplication(self,a,b):
return a*b;
44
class Derived(Calculation1,Calculation2):
def Divide(self,a,b):
return a/b;
d = Derived()
print(issubclass(Derived,Calculation2))
print(issubclass(Calculation1,Calculation2))
Output:
True
False

The isinstance (obj, class) method


The isinstance() method is used to check the
relationship between the objects and classes. It returns
true if the first parameter, i.e., obj is the instance of the
second parameter, i.e., class.
Consider the following example.

Example
class Calculation1:
def Summation(self,a,b):
return a+b;
class Calculation2:
def Multiplication(self,a,b):
return a*b;
class Derived(Calculation1,Calculation2):
def Divide(self,a,b):
45
return a/b;
d = Derived()
print(isinstance(d,Derived))

Output:
True

Method Overriding
We can provide some specific implementation of
the parent class method in our child class. When the
parent class method is defined in the child class with
some specific implementation, then the concept is called
method overriding. We may need to perform method
overriding in the scenario where the different definition of
a parent class method is needed in the child class.
Consider the following example to perform method
overriding in python.
Example
class Animal:
def speak(self):
print("speaking")
class Dog(Animal):
def speak(self):
print("Barking")
d = Dog()
d.speak()
46
Output:
Barking
Real Life Example of method overriding
Example
class Bank:
def getroi(self):
return 10;
class SBI(Bank):
def getroi(self):
return 7;
class ICICI(Bank):
def getroi(self):
return 8;
b1 = Bank()
b2 = SBI()
b3 = ICICI()
print("Bank Rate of interest:",b1.getroi());
print("SBI Rate of interest:",b2.getroi());
print("ICICI Rate of interest:",b3.getroi());
Output:
Bank Rate of interest: 10
SBI Rate of interest: 7
ICICI Rate of interest: 8
Data abstraction in python
Abstraction is an important aspect of object-
oriented programming. In python, we can also perform
47
data hiding by adding the double underscore ( ) as a
prefix to the attribute which is to be hidden. After this, the
attribute will not be visible outside of the class through the
object.
Consider the following example.
Example
class Employee:
__count = 0;
Def __init__ (self):
Employee. count = Employee. count+1
def display(self):
print("The number of employees",Employee.
count)
emp = Employee()
emp2 = Employee()
try:
print(emp. count)
finally:
emp.display()
Output:
The number of employees 2
AttributeError: 'Employee' object has no attribute ' count'

48
CHAPTER -3
FUNCTIONS
(LAM BDA, METHOD, CLASS)

Python Functions
A function is a block of code which only runs when
it is called. You can pass data, known as parameters, into
a function.
A function can return data as a result.
Python gives you many built-in functions like
print(), etc. but you can also create your own functions.
These functions are called user-defined functions.
Functions help break our program into smaller and
modular chunks. As our program grows larger and larger,
functions make it more organized and manageable.
Furthermore, it avoids repetition and makes the
code reusable.
Syntax
def function_name(parameters):
"""docstring"""
statement(s)
Creating a Function
In Python a function is defined using the def keyword:
Example
def my_function():
print("Hello from a function")
49
Calling a Function
To call a function, use the function name followed by
parenthesis:
Example
def my_function():
print("Hello from a function")
my_function()
Arguments
Information can be passed into functions as arguments.
Arguments are specified after the function name,
inside the parentheses. You can add as many arguments
as you want, just separate them with a comma.
The following example has a function with one
argument (fname). When the function is called, we pass
along a first name, which is used inside the function to
print the full name:
Example
def my_function(fname):
print(fname + " Refsnes")
my_function("Emil")
my_function("Tobias")
my_function("Linus")
Output
Emil Refsnes
Tobias Refsnes
50
Linus Refsnes

Arbitrary Arguments, *args


If you do not know how many arguments that will
be passed into your function, add a
* before the parameter name in the function definition.
This way the function will receive a tuple of
arguments, and can access the items accordingly:
Example
If the number of arguments is unknown, add a *
before the parameter name:
def my_function(*kids):
print("The youngest child is " + kids[2])
my_function("Emil", "Tobias", "Linus")
Output
The youngest child is Linus
Passing a List as an Argument
You can send any data types of argument to a
function (string, number, list, dictionary etc.), and it will be
treated as the same data type inside the function.
E.g. if you send a List as an argument, it will still
be a List when it reaches the function:
Example
def my_function(food):
for x in food:
print(x)
51
fruits = ["apple", "banana", "cherry"]
my_function(fruits)

Output
apple
banana
cherry
Return Values
To let a function return a value, use the return statement:
Example
def my_function(x):
return 5 * x
print(my_function(3))
print(my_function(5))
print(my_function(9))
Output
15
25
45
Recursion
Python also accepts function recursion, which
means a defined function can call itself.
Recursion is a common mathematical and
programming concept. It means that a function calls itself.
This has the benefit of meaning that you can loop through
data to reach a result.
52
The developer should be very careful with
recursion as it can be quite easy to slip into writing a
function which never terminates, or one that uses excess
amounts of memory or processor power. However, when
written correctly recursion can be a very efficient and
mathematically-elegant approach to programming.
In this example, tri_recursion() is a function that we
have defined to call itself ("recurse"). We use the k
variable as the data, which decrements (-1) every time
we recurse. The recursion ends when the condition is not
greater than 0 (i.e. when it is 0).
To a new developer it can take some time to work
out how exactly this works, best way to find out is by
testing and modifying it.
Example
def tri_recursion(k):
if(k > 0):
result = k + tri_recursion(k - 1)
print(result)
else:
result = 0
return result
print("\n\nRecursion Example Results")
tri_recursion(6)
Output
Recursion Example Results
53
1
3
6
10
15
Python Lambda
A lambda function is a small anonymous function.
A lambda function can take any number of arguments but
can only have one expression.
Syntax
lambda arguments : expression
The expression is executed and the result is returned:
Example
A lambda function that adds 10 to the number passed in
as an argument, and print the result:
x = lambda a : a + 10
print(x(5))
Output
15
Lambda functions can take any number of arguments:
Example
A lambda function that multiplies argument a with
argument b and print the result:
x = lambda a, b : a * b
print(x(5, 6))

54
Output
30
Example
A lambda function that sums argument a, b, and c and
print the result:
x = lambda a, b, c : a + b + c
print(x(5, 6, 2))
Output
13
Why Use Lambda Functions?
The power of lambda is better shown when you
use them as an anonymous function inside another
function.
Say you have a function definition that takes one
argument, and that argument will be multiplied with an
unknown number:
Syntax
def myfunc(n):
return lambda a : a * n
Example
def myfunc(n):
return lambda a : a * n
mydoubler = myfunc(2)
print(mydoubler(11))
Output
22
55
r, use the same function definition to make a function that
always triples the number you send in:
Example
def myfunc(n):
return lambda a : a * n
mytripler = myfunc(3)
print(mytripler(11))

Output
33
Or, use the same function definition to make both
functions, in the same program:
Example
def myfunc(n):
return lambda a : a * n
mydoubler = myfunc(2)
mytripler = myfunc(3)
print(mydoubler(11))
print(mytripler(11))

Output
22
33
Use lambda functions when an anonymous
function is required for a short period of time.

56
Python Classes/Objects
Python is an object oriented programming language.
Almost everything in Python is an object, with its
properties and methods.
A Class is like an object constructor, or a
"blueprint" for creating objects.
Create a Class
To create a class, use the keyword class:

Example
Create a class named MyClass, with a property named x:
class MyClass:
x=5
Create Object
Now we can use the class named MyClass to
create objects:
Example
Create an object named p1, and print the value of x:
p1 = MyClass()
print(p1.x)
The __init__ () Function
The examples above are classes and objects in
their simplest form, and are not really useful in real life
applications.
To understand the meaning of classes we have to
understand the built-in __init__ () function.
57
All classes have a function called __init__ (), which
is always executed when the class is being initiated.
Use the __init__ () function to assign values to
object properties, or other operations that are necessary
to do when the object is being created:
Example
Create a class named Person, use the __init__ () function
to assign values for name and age:
class Person:
def __init__ (self, name, age):
self.name = name
self.age = age

p1 = Person("John", 36)
print(p1.name)
print(p1.age)
Output
John 36
Object Methods
Objects can also contain methods. Methods in
objects are functions that belong to the object.
Let us create a method in the Person class:
Example
Insert a function that prints a greeting, and execute
it on the p1 object:
class Person:
58
def __init__ (self, name, age):
self.name = name
self.age = age
def myfunc(self):
print("Hello my name is " + self.name)
p1 = Person("John", 36)
p1.myfunc()
Output
Hello my name is John

Delete Object Properties


You can delete properties on objects by using the
del keyword:

Example
Delete the age property from the p1 object:
del p1.age
Delete Objects
You can delete objects by using the del keyword:
Example
Delete the p1 object:
del p1

59
The pass Statement
class definitions cannot be empty, but if you for
some reason have a class definition with no content, put
in the pass statement to avoid getting an error.
Example
class Person:
pass

60
CHAPTER -4
FILE OPERATIONS

Python File Handling Operations


Python provides us with an important feature for
reading data from the file and writing data into a file.
Mostly, in programming languages, all the values
or data are stored in some variables which are volatile in
nature.
Because data will be stored into those variables
during run-time only and will be lost once the program
execution is completed. Hence it is better to save these
data permanently using files.
How Python Handle Files?
If you are working in a large software application
where they process a large number of data, then we
cannot expect those data to be stored in a variable as the
variables are volatile in nature.
Hence when you about to handle such situations,
the role of files are will come into the picture.
As files are non-volatile in nature, the data will be
stored permanently in a secondary device like Hard Disk
and using python we will handle these files in our
applications.

61
Types of File in Python
There are two types of files in Python and each of
them are explained below in detail with examples for your
easy understanding.
They are:
1. Binary file
2. Text file
Binary files in Python
Most of the files that we see in our computer
system are called binary files.
Example:
 Document files: .pdf, .doc, .xls etc.
 Image files: .png, .jpg, .gif, .bmp etc.
 Video files: .mp4, .3gp, .mkv, .avi etc.
 Audio files: .mp3, .wav, .mka, .aac etc.
 Database files: .mdb, .accde, .frm, .sqlite etc.
 Archive files: .zip, .rar, .iso, .7z etc.
 Executable files: .exe, .dll, .class etc.
All binary files follow a specific format. We can
open some binary files in the normal text editor but we
can’t read the content present inside the file. That’s
because all the binary files will be encoded in the binary
format, which can be understood only by a computer or
machine.

62
For handling such binary files we need a specific
type of software to open it.

Text files in Python


Text files don’t have any specific encoding and it
can be opened in normal text editor itself.
Example:
 Web standards: html, XML, CSS, JSON etc.
 Source code: c, app, js, py, java etc.
 Documents: txt, tex, RTF etc.
 Tabular data: csv, tsv etc.
 Configuration: ini, cfg, reg etc.
In this tutorial, we will see how to handle both text
as well as binary files with some classic examples.
Python File Handling Operations
Most importantly there are 4 types of operations
that can be handled by Python on files:
1. Open
2. Read
3. Write
4. Close
Other operations include:
1. Rename
2. Delete

63
Python Create and Open a File
Python has an in-built function called open () to
open a file.
It takes a minimum of one argument as mentioned
in the below syntax. The open method returns a file
object which is used to access the write, read and other
in-built methods.
Syntax:
file_object = open(file_name, mode)
The mode in the open function syntax will tell
Python as what operation you want to do on a file.
 ‘r' – Read Mode: Read mode is used only to read
data from the file.
 ‘w' – Write Mode: This mode is used when you
want to write data into the file or modify it.
Remember write mode overwrites the data present
in the file.
 ‘a' – Append Mode: Append mode is used to
append data to the file. Remember data will be
appended at the end of the file pointer.
 ‘r+' – Read or Write Mode: This mode is used
when we want to write or read the data from the
same file.
 ‘a+' – Append or Read Mode: This mode is used
when we want to read data from the file or append
the data into the same file.
64
While using binary files, we have to use the same
modes with the letter ‘b' at the end. So that Python can
understand that we are interacting with binary files.
‘wb’ – Open a file for write only mode in the binary format.
‘rb’ – Open a file for the read-only mode in the binary
format.
‘ab’ – Open a file for appending only mode in the binary
format. ‘rb+’ – Open a file for read and write only mode in
the binary format.
‘ab+’ – Open a file for appending and read-only mode in
the binary format.
Example 1:
fo = open(“C:/Documents/Python/test.txt”, “r+”)
In the above example, we are opening the file named
‘test.txt’ present at the location ‘C:/Documents/Python/’
and we are opening the same file in a read-write mode
which gives us more flexibility.
Example 2:
fo = open(“C:/Documents/Python/img.bmp”, “rb+”)
In the above example, we are opening the file named
‘img.bmp’ present at the location
“C:/Documents/Python/”, But, here we are trying to open
the binary file.
Python Read from File
In order to read a file in python, we must open the file in
read mode.
65
There are three ways in which we can read the files in
python.
read([n])
readline([n])
readlines()
Here, n is the number of bytes to be read.
First, let's create a sample text file as shown below.

Fig 5. Test file


Example 1:
my_file = open(“C:/Documents/Python/test.txt”, “r”)
print(my_file.read(5))
Output:
Hello
Here we are opening the file test.txt in a read-only mode
and are reading only the first 5 characters of the file using
the my_file.read(5) method.
Reading File using my_file.read(5) method
Output:
output - Reading File using my_file.read(5) method
66
Example 2:
my_file = open(“C:/Documents/Python/test.txt”, “r”)
print(my_file.read())
Output:
Hello World
Hello Python
Good Morning
Here we have not provided any argument inside the read
() function. Hence it will read all the content present
inside the file.
Example for Read file in Python
Output:
Output of Example for Read file in Python
Example 3:
my_file = open(“C:/Documents/Python/test.txt”, “r”)
print(my_file.readline(2))
Output:
He
This function returns the first 2 characters of the next line.
Example of Reading File using readline(2) method
Output:
Ouput of Reading File using readline(2) method
Example 4:
my_file = open(“C:/Documents/Python/test.txt”, “r”)
print(my_file.readline())

67
Output:
Hello World
Using this function we can read the content of the file on
a line by line basis.
Example of Reading File using readline() method
Output:
Output - Reading File using readline() method
Example 5:
my_file = open(“C:/Documents/Python/test.txt”, “r”)
print (my_file.readlines())
Output:
[‘Hello World\n’, ‘Hello Python\n’, ‘Good Morning’]
Here we are reading all the lines present inside the
text file including the newline characters.
Reading_File_using_readlines()_method
Output:
Reading_File_using_readlines()_method_output
Now let’s see some more practical examples of reading a
file.
Reading a specific line from a File
line_number = 4
fo = open(“C:/Documents/Python/test.txt”, ’r’)
currentline = 1
for line in fo:
if(currentline == line_number):
print(line)
68
break
currentline = currentline +1
Output:
How are You
In the above example, we are trying to read only the 4th
line from the ‘test.txt’ file using a “for loop”.
Reading specific line from a file
Output:
Reading specific line from a file - Output
Reading the entire file at once
filename = “C:/Documents/Python/test.txt”
filehandle = open(filename, ‘r’)
filedata = filehandle.read()
print(filedata)
Output:
Hello World
Hello Python
Good Morning
How are You
Reading entire file at once
Output:
Reading entire file at once - Output
Python Write to File
In order to write data into a file, we must open the
file in write mode.

69
We need to be very careful while writing data into
the file as it overwrites the content present inside the file
that you are writing, and all the previous data will be
erased.
We have two methods for writing data into a file as shown
below.
write(string)
writelines(list)
Example 1:
my_file = open(“C:/Documents/Python/test.txt”, “w”)
my_file.write(“Hello World”)

Example 2:
my_file = open(“C:/Documents/Python/test.txt”, “w”)
my_file.write(“Hello World\n”)
my_file.write(“Hello Python”)
The first line will be ‘Hello World’ and as we have
mentioned \n character, the cursor will move to the next
line of the file and then write ‘Hello Python’.
Remember if we don’t mention \n character, then
the data will be written continuously in the text file like
‘Hello WorldHelloPython’
Example 3:
fruits = [“Apple\n”, “Orange\n”, “Grapes\n”, “Watermelon”]
my_file = open(“C:/Documents/Python/test.txt”, “w”)
my_file.writelines(fruits)
70
The above code writes a list of data into the
‘test.txt’ file simultaneously.
Output:

Fig 6. Write operation output

Python Append to File


To append data into a file we must open the file in
‘a+’ mode so that we will have access to both the append
as well as write modes.
Example 1:
my_file = open (“C:/Documents/Python/test.txt”, “a+”)
my_file.write (“Strawberry”)
The above code appends the string ‘Apple’ at the end of
the ‘test.txt’ file.

71
Output:

Fig 7. Append operation output


Example 2:
fruits = [“\nBanana”, “\nAvocado”, “\nFigs”, “\nMango”]
my_file = open(“C:/Documents/Python/test.txt”, “a+”)
my_file.writelines(fruits)
The above code appends a list of data into a ‘test.txt’ file.

Output:

Fig 8. Append test file output


72
Example 3:
text=["\nHello","\nHi","\nPython"]
my_file=open("C:/Documents/Python/test.txt",mode="a+")
my_file.writelines(text)
print("where the file cursor is:",my_file.tell())
my_file.seek(0)
for line in my_file: print(line)
In the above code, we are appending the list of
data into the ‘test.txt’ file. Here, you can observe that we
have used the tell() method which prints where the cursor
is currently at.

seek(offset): The offset takes three types of arguments


namely 0,1 and 2.
When the offset is 0: Reference will be pointed at the
beginning of the file.
When the offset is 1: Reference will be pointed at the
current cursor position.
When the offset is 2: Reference will be pointed at the end
of the file.
Python Close File
In order to close a file, we must first open the file.
In python, we have an in-built method called close() to
close the file which is opened.
Whenever you open a file, it is important to close
it, especially, with write method. Because if we don’t call
73
the close function after the write method then whatever
data we have written to a file will not be saved into the
file.
Example 1:
my_file = open(“C:/Documents/Python/test.txt”, “r”)
print(my_file.read())
my_file.close()
Example 2:
my_file = open(“C:/Documents/Python/test.txt”, “w”)
my_file.write(“Hello World”)
my_file.close()

Python Rename or Delete File


Python provides us with an “os” module which has
some in-built methods that would help us in performing
the file operations such as renaming and deleting the file.
In order to use this module, first of all, we need to
import the “os” module in our program and then call the
related methods.
rename() method:
This rename() method accepts two arguments i.e.
the current file name and the new file name.
Syntax:
os.rename(current_file_name, new_file_name)
Example 1:
import os
74
os.rename(“test.txt”, “test1.txt”)
Here ‘test.txt’ is the current file name and ‘test1.txt’ is the
new file name. You can specify the location as well as
shown in the below example.
Example 2:
import os
os.rename(“C:/Documents/Python/test.txt”,
“C:/Documents/Python/test1.txt”)
remove() method:
We use the remove() method to delete the file by
supplying the file name or the file location that you want
to delete.
Syntax:
os.remove(file_name)
Example 1:
import os os.remove(“test.txt”)
Here ‘test.txt’ is the file that you want to remove.
Similarly, we can pass the file location as well to the
arguments as shown in the below example
Example 2:
import os
os.remove(“C:/Documents/Python/test.txt”)
Encoding in Files
Writing and Reading Data from a Binary File
Binary files store data in the binary format (0’s and
1’s) which is understandable by the machine. So when
75
we open the binary file in our machine, it decodes the
data and displays in a human-readable format.
Example:
#Let’s create some binary file.
my_file = open(“C:/Documents/Python/bfile.bin”, “wb+”)
message = “Hello Python”
file_encode = message.encode(“ASCII”)
my_file.write(file_encode)
my_file.seek(0)
bdata = my_file.read()
print(“Binary Data:”, bdata)
ntext = bdata.decode(“ASCII”)
print(“Normal data:”, ntext)
In the above example, first we are creating a
binary file ‘bfile.bin’ with the read and write access and
whatever data you want to enter into the file must be
encoded before you call the write method.
Also, we are printing the data without decoding it,
so that we can observe how the data exactly looks inside
the file when it’s encoded and we are also printing the
same data by decoding it so that it can be readable by
humans.
Output:
Binary Data: b'Hello Python'
Normal data: Hello Python

76
File I/O Attributes
Table 5. File I/O Attributes
Attribute Description
Name Return the name of the file
Mode Return mode of the file
Encoding Return the encoding format of the file
Closed Return true if the file closed else returns false

Example:
my_file = open(“C:/Documents/Python/test.txt”, “a+”)
print(“What is the file name? ”, my_file.name)
print(“What is the file mode? ”, my_file.mode)
print(“What is the encoding format? ”, my_file.encoding)
print(“Is File closed? ”, my_file.closed)
my_file.close()
print(“Is File closed? ”, my_file.closed)
Output:
What is the file name? C:/Documents/Python/test.txt
What is the file mode? r
What is the encoding format? cp1252
Is File closed? False
Is File closed? True
Example:
my_file = open(“C:/Documents/Python/test.txt”, “w+”)
my_file.write(“Hello Python\nHello World\nGood
Morning”)
77
my_file.seek(0)
print(my_file.read())
print(“Is file readable: ?”, my_file.readable())
print(“Is file writeable: ?”, my_file.writable())
print(“File no:”, my_file.fileno())
my_file.close()
Output:
Hello Python
Hello World
Good Morning
Is file readable:? True
Is file writeable:? True
File no: 3

Python File Methods


Table 6. File Methods in python
Function Explanation
open() To open a file
close() Close an open file
fileno() Returns an integer number of the file
read(n) Reads ‘n’ characters from the file till end of the
file
readable() Returns true if the file is readable
readline() Read and return one line from the file
readlines() Reads and returns all the lines from the file

78
seek(offset) Change the cursor position by bytes as
specified by the offset
seekable() Returns true if the file supports random access

tell() Returns the current file location


writable() Returns true if the file is writable
write() Writes a string of data to the file
writelines() Writes a list of data to the file

Let’s see what we have discussed so far in an end-end


program.
Example:
my_file = open("C:/Documents/Python/test.txt",
mode="w+")
print("What is the file name? ", my_file.name)
print("What is the mode of the file? ", my_file.mode)
print("What is the encoding format?", my_file.encoding)
text = ["Hello Python\n", "Good Morning\n", "Good Bye"]
my_file.writelines(text)
print("Size of the file is:", my_file. sizeof ())
print("Cursor position is at byte:", my_file.tell())
my_file.seek(0)
print("Content of the file is:", my_file.read())
my_file.close()
file = open("C:/Documents/Python/test.txt", mode="r")
line_number = 3
current_line = 1
79
data = 0
for line in file:
if current_line == line_number:
data = line
print("Data present at current line is:", data) break
current_line = current_line + 1
bin_file = open("C:/Documents/Python/bfile.exe",
mode="wb+")
message_content = data.encode("utf-32")
bin_file.write(message_content)
bin_file.seek(0)
bdata = bin_file.read()
print("Binary Data is:", bdata)
ndata = bdata.decode("utf-32")
print("Normal Data is:", ndata)
file.close()
bin_file.close()

Output:
What is the file name? C:/Documents/Python/test.txt
What is the mode of the file? w+
What is the encoding format? cp1252
Size of the file is: 192
Cursor position is at byte: 36
Content of the file is: Hello Python
80
Good Morning
Good Bye
Data present at the current line is: Good Bye
Binary Data is:
b'\xff\xfe\x00\x00G\x00\x00\x00o\x00\x00\x00o\x00\x00\x
00d\x00\x00\x00
\x00\x00\x00B\x00\x00\x00y\x00\x00\x00e\x00\x00\x00′
Normal Data is: Good Bye

81
CHAPTER -5
BUILTIN LIBRARY FUNCTIONS

Python Built in Functions


Table 7: Built in Functions
Function Description
abs() Returns the absolute value of a number
all() Returns True if all items in an iterable object are true
any() Returns True if any item in an iterable object is true
ascii() Returns a readable version of an object.
Replaces none-ascii characters with escape
character
bin() Returns the binary version of a number
bool() Returns the boolean value of the specified object
bytearray() Returns an array of bytes
bytes() Returns a bytes object
callable() Returns True if the specified object is callable,
otherwise False
chr() Returns a character from the specified Unicode
code.
classmethod() Converts a method into a class method
compile() Returns the specified source as an object, ready to
be executed
complex() Returns a complex number
delattr() Deletes the specified attribute (property or method)

82
from the specified object
dict() Returns a dictionary (Array)
dir() Returns a list of the specified object's properties and
methods
divmod() Returns the quotient and the remainder when
argument1 is divided by argument2
enumerate() Takes a collection (e.g. a tuple) and returns it as an
enumerate object
eval() Evaluates and executes an expression
exec() Executes the specified code (or object)
filter() Use a filter function to exclude items in an iterable
object
float() Returns a floating point number
format() Formats a specified value
frozenset() Returns a frozenset object
getattr() Returns the value of the specified attribute (property
or method)
globals() Returns the current global symbol table as a
dictionary
hasattr() Returns True if the specified object has the specified
attribute (property/method)
hash() Returns the hash value of a specified object
help() Executes the built-in help system
hex() Converts a number into a hexadecimal value
id() Returns the id of an object

83
input() Allowing user input
int() Returns an integer number
isinstance() Returns True if a specified object is an instance of a
specified object
issubclass() Returns True if a specified class is a subclass of a
specified object
iter() Returns an iterator object
len() Returns the length of an object
list() Returns a list
locals() Returns an updated dictionary of the current local
symbol table
map() Returns the specified iterator with the specified
function applied to each item
max() Returns the largest item in an iterable
memoryview() Returns a memory view object
min() Returns the smallest item in an iterable
next() Returns the next item in an iterable
object() Returns a new object
oct() Converts a number into an octal
open() Opens a file and returns a file object
ord() Convert an integer representing the Unicode of the
specified character
pow() Returns the value of x to the power of y
print() Prints to the standard output device
property() Gets, sets, deletes a property

84
range() Returns a sequence of numbers, starting from 0 and
increments by 1 (by default)
repr() Returns a readable version of an object
reversed() Returns a reversed iterator
round() Rounds a numbers
set() Returns a new set object
setattr() Sets an attribute (property/method) of an object
slice() Returns a slice object
sorted() Returns a sorted list
@staticmethod() Converts a method into a static method
str() Returns a string object
sum() Sums the items of an iterator
super() Returns an object that represents the parent class
tuple() Returns a tuple
type() Returns the type of an object
vars() Returns the dict property of an object
zip() Returns an iterator, from two or more iterators
Python String Methods
Table 8: String Methods
Method Description
capitalize() Converts the first character to upper case
casefold() Converts string into lower case
center() Returns a centered string
count() Returns the number of times a specified value occurs
in a string

85
encode() Returns an encoded version of the string
endswith() Returns true if the string ends with the specified value
expandtabs() Sets the tab size of the string
find() Searches the string for a specified value and returns
the position of where it was found
format() Formats specified values in a string
format_map() Formats specified values in a string
index() Searches the string for a specified value and returns
the position of where it was found
isalnum() Returns True if all characters in the string are
alphanumeric
isalpha() Returns True if all characters in the string are in the
alphabet
isdecimal() Returns True if all characters in the string are decimals
isdigit() Returns True if all characters in the string are digits
isidentifier() Returns True if the string is an identifier
islower() Returns True if all characters in the string are lower
case
isnumeric() Returns True if all characters in the string are numeric
isprintable() Returns True if all characters in the string are printable
isspace() Returns True if all characters in the string are
whitespaces
istitle() Returns True if the string follows the rules of a title
isupper() Returns True if all characters in the string are upper
case

86
join() Joins the elements of an iterable to the end of the
string
ljust() Returns a left justified version of the string
lower() Converts a string into lower case
lstrip() Returns a left trim version of the string
maketrans() Returns a translation table to be used in translations
partition() Returns a tuple where the string is parted into three
parts
replace() Returns a string where a specified value is replaced
with a specified value
rfind() Searches the string for a specified value and returns
the last position of where it was found
rindex() Searches the string for a specified value and returns
the last position of where it was found
rjust() Returns a right justified version of the string
rpartition() Returns a tuple where the string is parted into three
parts
rsplit() Splits the string at the specified separator, and returns
a list
rstrip() Returns a right trim version of the string
split() Splits the string at the specified separator, and returns
a list
splitlines() Splits the string at line breaks and returns a list
startswith() Returns true if the string starts with the specified value
strip() Returns a trimmed version of the string

87
swapcase() Swaps cases, lower case becomes upper case and
vice versa
title() Converts the first character of each word to upper case
translate() Returns a translated string
upper() Converts a string into upper case
zfill() Fills the string with a specified number of 0 values at
the beginning
Python Dictionary Methods
Table 9: Dictionary Methods
Method Description
clear() Removes all the elements from the dictionary
copy() Returns a copy of the dictionary
fromkeys() Returns a dictionary with the specified keys and value
get() Returns the value of the specified key
items() Returns a list containing a tuple for each key value
pair
keys() Returns a list containing the dictionary's keys
pop() Removes the element with the specified key
popitem() Removes the last inserted key-value pair
setdefault() Returns the value of the specified key. If the key does
not exist: insert the key, with the specified value
update() Updates the dictionary with the specified key-value
pairs
values() Returns a list of all the values in the dictionary

88
abs(x)
Return the absolute value of a number. The argument
may be an integer or a floating point number. If the
argument is a complex number, its magnitude is returned.
If x defines _abs_ (), abs(x) returns x._abs_().

all(iterable)
Return True if all elements of the iterable are true (or if
the iterable is empty).
Equivalent to:
def all(iterable):
for element in iterable:
if not element:
return False
return True
any(iterable)
Return True if any element of the iterable is true. If the
iterable is empty, return False.
Equivalent to:
def any(iterable):
for element in iterable:
if element:
return True
return False

89
ascii(object)
As repr(), return a string containing a printable
representation of an object, but escape the non-ASCII
characters in the string returned by repr() using \x, \u or
\U escapes. This generates a string similar to that
returned by repr() in Python 2.
bin(x)
Convert an integer number to a binary string
prefixed with “0b”. The result is a valid Python
expression. If x is not a Python int object, it has to define
an index () method that returns an integer. Some
examples:
>>>bin(3) '0b11'
bin(-10)
'-0b1010'
If prefix “0b” is desired or not, you can use either of the
following ways.
>>>format(14, '#b'), format(14, 'b')
('0b1110', '1110')
f'{14:#b}', f'{14:b}'
('0b1110', '1110')
See also format() for more information.
class bool([x])
Return a Boolean value, i.e. one of True or False. x is
converted using the standard truth testing procedure. If x
is false or omitted, this returns False; otherwise it returns
90
True. The bool class is a subclass of int (see Numeric
Types - int, float, complex). It cannot be subclassed
further. Its only instances are False and True (see
Boolean Values).
Changed in version 3.7: x is now a positional-only
parameter.
breakpoint(*args, **kws)
This function drops you into the debugger at the call site.
Specifically, it calls sys.breakpointhook(), passing args
and kws straight through. By default,
sys.breakpointhook() calls pdb.set_trace() expecting no
arguments. In this case, it is purely a convenience
function so you don’t have to explicitly import pdb or type
as much code to enter the debugger. However,
sys.breakpointhook() can be set to some other function
and breakpoint() will automatically call that, allowing you
to drop into the debugger of choice.
Raises an auditing event builtins.breakpoint with
argument breakpointhook.
New in version 3.7.
class bytearray([source[, encoding[, errors]]])
Return a new array of bytes. The bytearray class is a
mutable sequence of integers in the range 0 <= x < 256.
It has most of the usual methods of mutable sequences,
described in Mutable Sequence Types, as well as most

91
methods that the bytes type has, see Bytes and
Bytearray Operations.
The optional source parameter can be used to
initialize the array in a few different ways:
If it is a string, you must also give the encoding (and
optionally, errors) parameters; bytearray() then converts
the string to bytes using str.encode().
If it is an integer, the array will have that size and will be
initialized with null bytes. If it is an object conforming to
the buffer interface, a read-only buffer of the object will be
used to initialize the bytes array.
If it is an iterable, it must be an iterable of integers in the
range 0 <= x < 256, which are used as the initial contents
of the array.
Without an argument, an array of size 0 is created.
See also Binary Sequence Types - bytes, bytearray,
memoryview and Bytearray Objects.
class bytes([source[, encoding[, errors]]])
Return a new “bytes” object, which is an immutable
sequence of integers in the range 0 <= x < 256. bytes is
an immutable version of bytearray - it has the same non-
mutating methods and the same indexing and slicing
behavior.
Accordingly, constructor arguments are interpreted as for
bytearray(). Bytes objects can also be created with
literals, see String and Bytes literals.
92
See also Binary Sequence Types - bytes, bytearray,
memoryview, Bytes Objects, and Bytes and Bytearray
Operations.
callable(object)
Return True if the object argument appears callable,
False if not. If this returns True, it is still possible that a
call fails, but if it is False, calling object will never
succeed. Note that classes are callable (calling a class
returns a new instance); instances are callable if their
class has a call () method.
New in version 3.2: This function was first removed in
Python 3.0 and then brought back in Python 3.2.
chr(i)
Return the string representing a character whose
Unicode code point is the integer i. For example, chr(97)
returns the string 'a', while chr(8364) returns the string '€'.
This is the inverse of ord().
The valid range for the argument is from 0 through
1,114,111 (0x10FFFF in base 16). ValueError will be
raised if i is outside that range.
@classmethod
Transform a method into a class method.
A class method receives the class as implicit first
argument, just like an instance method receives the
instance. To declare a class method, use this idiom:
class C:
93
@classmethod
def f(cls, arg1, arg2, ...): ...
The @classmethod form is a function decorator – see
Function definitions for details.
A class method can be called either on the class (such as
C.f()) or on an instance (such as C().f()). The instance is
ignored except for its class. If a class method is called for
a derived class, the derived class object is passed as the
implied first argument.
Class methods are different than C++ or Java static
methods. If you want those, see staticmethod().
For more information on class methods, see The
standard type hierarchy.
compile(source, filename, mode, flags=0,
dont_inherit=False, optimize=-1)
Compile the source into a code or AST object. Code
objects can be executed by exec() or eval(). source can
either be a normal string, a byte string, or an AST object.
Refer to the ast module documentation for information on
how to work with AST objects.
The filename argument should give the file from which
the code was read; pass some recognizable value if it
wasn’t read from a file ('<string>' is commonly used).
The mode argument specifies what kind of code must be
compiled; it can be 'exec' if source consists of a
sequence of statements, 'eval' if it consists of a single
94
expression, or 'single' if it consists of a single interactive
statement (in the latter case, expression statements that
evaluate to something other than None will be printed).
The optional arguments flags and dont_inherit control
which future statements affect the compilation of source.
If neither is present (or both are zero) the code is
compiled with those future statements that are in effect in
the code that is calling compile(). If the flags argument is
given and dont_inherit is not (or is zero) then the future
statements specified by the flags argument are used in
addition to those that would be used anyway. If
dont_inherit is a non-zero integer then the flags argument
is it – the future statements in effect around the call to
compile are ignored.
Future statements are specified by bits which can be
bitwise ORed together to specify multiple statements. The
bitfield required to specify a given feature can be found
as the compiler_flag attribute on the _Feature instance in
the future module.
The optional argument flags also controls whether the
compiled source is allowed to contain top-level await,
async for and async with. When the bit
ast.PyCF_ALLOW_TOP_LEVEL_AWAIT is set, the
return code object has CO_COROUTINE set in co_code,
and can be interactively executed via await
eval(code_object).
95
The argument optimize specifies the optimization level of
the compiler; the default value of -1 selects the
optimization level of the interpreter as given by -O
options. Explicit levels are 0 (no optimization; debug is
true), 1 (asserts are removed, debug is false) or 2
(docstrings are removed too).
This function raises SyntaxError if the compiled source is
invalid, and ValueError if the source contains null bytes.
If you want to parse Python code into its AST
representation, see ast.parse().
Raises an auditing event compile with arguments source
and filename. This event may also be raised by implicit
compilation.
Note When compiling a string with multi-line code in
'single' or 'eval' mode, input must be terminated by at
least one newline character. This is to facilitate detection
of incomplete and complete statements in the code
module.
Warning It is possible to crash the Python interpreter with
a sufficiently large/complex string when compiling to an
AST object due to stack depth limitations in Python’s AST
compiler.
Changed in version 3.2: Allowed use of Windows and
Mac newlines. Also input in 'exec' mode does not have to
end in a newline anymore. Added the optimize
parameter.
96
Changed in version 3.5: Previously, TypeError was raised
when null bytes were encountered in source.
New in version 3.8:
ast.PyCF_ALLOW_TOP_LEVEL_AWAIT can now be
passed in flags to enable support for top-level await,
async for, and async with.
class complex([real[, imag]])
Return a complex number with the value real + imag*1j or
convert a string or number to a complex number. If the
first parameter is a string, it will be interpreted as a
complex number and the function must be called without
a second parameter. The second parameter can never be
a string. Each argument may be any numeric type
(including complex). If imag is omitted, it defaults to zero
and the constructor serves as a numeric conversion like
int and float. If both arguments are omitted, returns 0j.
For a general Python object x, complex(x) delegates to
x._ complex_(). If _complex_() is not defined then it falls
back to _float_(). If _float_() is not defined then it falls
back to _index_().
Note When converting from a string, the string must not
contain whitespace around the central + or - operator. For
example, complex('1+2j') is fine, but complex('1 + 2j')
raises ValueError.
The complex type is described in Numeric Types — int,
float, complex.
97
Changed in version 3.6: Grouping digits with underscores
as in code literals is allowed.
Changed in version 3.8: Falls back to _index_() if
_complex_() and _float_() are not defined.
delattr(object, name)
This is a relative of setattr(). The arguments are an object
and a string. The string must be the name of one of the
object’s attributes. The function deletes the named
attribute, provided the object allows it. For example,
delattr(x, 'foobar') is equivalent to del x.foobar.
class dict(**kwarg)
class dict(mapping, **kwarg) class dict(iterable, **kwarg)
Create a new dictionary. The dict object is the dictionary
class. See dict and Mapping Types — dict for
documentation about this class.
For other containers see the built-in list, set, and tuple
classes, as well as the collections module.
dir([object])
Without arguments, return the list of names in the current
local scope. With an argument, attempt to return a list of
valid attributes for that object.
If the object has a method named _dir_(), this method will
be called and must return the list of attributes. This allows
objects that implement a custom _getattr_() or
getattribute_() function to customize the way dir() reports
their attributes.
98
If the object does not provide dir_(), the function tries its
best to gather information from the object’s _dict_
attribute, if defined, and from its type object. The resulting
list is not necessarily complete, and may be inaccurate
when the object has a custom getattr_().
The default dir() mechanism behaves differently with
different types of objects, as it attempts to produce the
most relevant, rather than complete, information:
If the object is a module object, the list contains the
names of the module’s attributes.
If the object is a type or class object, the list contains the
names of its attributes, and recursively of the attributes of
its bases.
Otherwise, the list contains the object’s attributes’ names,
the names of its class’s attributes, and recursively of the
attributes of its class’s base classes.
The resulting list is sorted alphabetically. For example:
>>>
import struct
dir() # show the names in the module namespace ['
builtins ', ' name ', 'struct']
dir(struct) # show the names in the struct module
['Struct', '__all__', '__builtins__', '__cached__', '__doc__',
'__file__', '__initializing__', '__loader__', '__name__',
'__package__', '_clearcache', 'calcsize', 'error', 'pack',
'pack_into',
99
'unpack', 'unpack_from']
class Shape:
def __dir__ (self):
return ['area', 'perimeter', 'location']
s = Shape()
dir(s)
['area', 'location', 'perimeter']
Note Because dir() is supplied primarily as a convenience
for use at an interactive prompt, it tries to supply an
interesting set of names more than it tries to supply a
rigorously or consistently defined set of names, and its
detailed behavior may change across releases. For
example, metaclass attributes are not in the result list
when the argument is a class.
divmod(a, b)
Take two (non complex) numbers as arguments and
return a pair of numbers consisting of their quotient and
remainder when using integer division. With mixed
operand types, the rules for binary arithmetic operators
apply. For integers, the result is the same as (a // b, a %
b). For floating point numbers the result is (q, a % b),
where q is usually math.floor(a / b) but may be 1 less
than that. In any case q * b + a % b is very close to a, if a
% b is non-zero it has the same sign as b, and 0 <= abs(a
% b) < abs(b).

100
enumerate(iterable, start=0)
Return an enumerate object. iterable must be a
sequence, an iterator, or some other object which
supports iteration. The _next_() method of the iterator
returned by enumerate() returns a tuple containing a
count (from start which defaults to 0) and the values
obtained from iterating over iterable.
>>>
seasons = ['Spring', 'Summer', 'Fall', 'Winter']
list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
Equivalent to:
def enumerate(sequence, start=0):
n = start
for elem in sequence:
yield n, elem
n += 1
eval(expression[, globals[, locals]])
The arguments are a string and optional globals and
locals. If provided, globals must be a dictionary. If
provided, locals can be any mapping object.
The expression argument is parsed and evaluated as a
Python expression (technically speaking, a condition list)
using the globals and locals dictionaries as global and
101
local namespace. If the globals dictionary is present and
does not contain a value for the key builtins_, a reference
to the dictionary of the built-in module builtins is inserted
under that key before expression is parsed. This means
that expression normally has full access to the standard
builtins module and restricted environments are
propagated. If the locals dictionary is omitted it defaults to
the globals dictionary.
If both dictionaries are omitted, the expression is
executed with the globals and locals in the environment
where eval() is called. Note, eval() does not have access
to the nested scopes (non-locals) in the enclosing
environment.
The return value is the result of the evaluated expression.
Syntax errors are reported as exceptions. Example:
>>>
x=1
eval('x+1')
2
This function can also be used to execute arbitrary code
objects (such as those created by compile()). In this case
pass a code object instead of a string. If the code object
has been compiled with 'exec' as the mode argument,
eval()’s return value will be None.
Hints: dynamic execution of statements is supported by
the exec() function. The globals() and locals() functions
102
returns the current global and local dictionary,
respectively, which may be useful to pass around for use
by eval() or exec().

See ast.literal_eval() for a function that can safely


evaluate strings with expressions containing only literals.
Raises an auditing event exec with the code object as the
argument. Code compilation events may also be raised.
exec(object[, globals[, locals]])
This function supports dynamic execution of Python code.
object must be either a string or a code object. If it is a
string, the string is parsed as a suite of Python
statements which is then executed (unless a syntax error
occurs). 1 If it is a code object, it is simply executed. In all
cases, the code that’s executed is expected to be valid as
file input (see the section “File input” in the Reference
Manual). Be aware that the return and yield statements
may not be used outside of function definitions even
within the context of code passed to the exec() function.
The return value is None.
In all cases, if the optional parts are omitted, the code is
executed in the current scope. If only globals is provided,
it must be a dictionary (and not a subclass of dictionary),
which will be used for both the global and the local
variables. If globals and locals are given, they are used
for the global and local variables, respectively. If
103
provided, locals can be any mapping object. Remember
that at module level, globals and locals are the same
dictionary. If exec gets two separate objects as globals
and locals, the code will be executed as if it were
embedded in a class definition.
If the globals dictionary does not contain a value for the
key _builtins_, a reference to the dictionary of the built-in
module builtins is inserted under that key. That way you
can control what builtins are available to the executed
code by inserting your own _builtins_ dictionary into
globals before passing it to exec().
Raises an auditing event exec with the code object as the
argument. Code compilation events may also be raised.
Note The built-in functions globals() and locals() return
the current global and local dictionary, respectively, which
may be useful to pass around for use as the second and
third argument to exec().
Note The default locals act as described for function
locals() below: modifications to the default locals
dictionary should not be attempted. Pass an explicit
locals dictionary if you need to see effects of the code on
locals after function exec() returns.
filter(function, iterable)
Construct an iterator from those elements of iterable for
which function returns true. iterable may be either a
sequence, a container which supports iteration, or an
104
iterator. If function is None, the identity function is
assumed, that is, all elements of iterable that are false
are removed.
Note that filter(function, iterable) is equivalent to the
generator expression (item for item in iterable if
function(item)) if function is not None and (item for item in
iterable if item) if function is None.
See itertools.filterfalse() for the complementary function
that returns elements of iterable for which function returns
false.
class float([x])
Return a floating point number constructed from a
number or string x.
If the argument is a string, it should contain a decimal
number, optionally preceded by a sign, and optionally
embedded in whitespace. The optional sign may be '+' or
'-'; a '+' sign has no effect on the value produced. The
argument may also be a string representing a NaN (not-
a-number), or a positive or negative infinity. More
precisely, the input must conform to the following
grammar after leading and trailing whitespace characters
are removed:
sign ::= "+" | "-"
infinity ::= "Infinity" | "inf"
nan ::= "nan"
numeric_value ::= floatnumber | infinity | nan
105
numeric_string ::= [sign] numeric_value
Here floatnumber is the form of a Python floating-point
literal, described in Floating point literals. Case is not
significant, so, for example, “inf”, “Inf”, “INFINITY” and
“iNfINity” are all acceptable spellings for positive infinity.
Otherwise, if the argument is an integer or a floating point
number, a floating point number with the same value
(within Python’s floating point precision) is returned. If the
argument is outside the range of a Python float, an
OverflowError will be raised.
For a general Python object x, float(x) delegates to
x._float_(). If _float_() is not defined then it falls back to
_index_().
If no argument is given, 0.0 is returned.
Examples:
>>>
>>> float('+1.23')
1.23
>>> float(' -12345\n')
-12345.0
>>> float('1e-003') 0.001
>>> float('+1E6') 1000000.0
>>> float('-Infinity')
-inf
The float type is described in Numeric Types — int, float,
complex.
106
Changed in version 3.6: Grouping digits with underscores
as in code literals is allowed.
Changed in version 3.7: x is now a positional-only
parameter.
Changed in version 3.8: Falls back to _index_() if
_float_() is not defined.

format(value[, format_spec])
Convert a value to a “formatted” representation, as
controlled by format_spec. The interpretation of
format_spec will depend on the type of the value
argument, however there is a standard formatting syntax
that is used by most built-in types: Format Specification
Mini-Language.
The default format_spec is an empty string which usually
gives the same effect as calling str(value).
A call to format(value, format_spec) is translated to
type(value)._format_(value, format_spec) which
bypasses the instance dictionary when searching for the
value’s _format_() method. A TypeError exception is
raised if the method search reaches object and the
format_spec is non-empty, or if either the format_spec or
the return value are not strings.
Changed in version 3.4: object(). format (format_spec)
raises TypeError if format_spec is not an empty string.

107
class frozenset([iterable])
Return a new frozenset object, optionally with elements
taken from iterable. frozenset is a built-in class. See
frozenset and Set Types — set, frozenset for
documentation about this class.
For other containers see the built-in set, list, tuple, and
dict classes, as well as the collections module.

getattr(object, name[, default])


Return the value of the named attribute of object. name
must be a string. If the string is the name of one of the
object’s attributes, the result is the value of that attribute.
For example, getattr(x, 'foobar') is equivalent to x.foobar.
If the named attribute does not exist, default is returned if
provided, otherwise AttributeError is raised.
globals()
Return a dictionary representing the current global
symbol table. This is always the dictionary of the current
module (inside a function or method, this is the module
where it is defined, not the module from which it is
called).
hasattr(object, name)
The arguments are an object and a string. The result is
True if the string is the name of one of the object’s
attributes, False if not. (This is implemented by calling

108
getattr(object, name) and seeing whether it raises an
AttributeError or not.)
hash(object)
Return the hash value of the object (if it has one). Hash
values are integers. They are used to quickly compare
dictionary keys during a dictionary lookup. Numeric
values that compare equal have the same hash value
(even if they are of different types, as is the case for 1
and 1.0).
Note For objects with custom _hash_() methods, note
that hash() truncates the return value based on the bit
width of the host machine. See _hash_() for details.
help([object])
Invoke the built-in help system. (This function is intended
for interactive use.) If no argument is given, the
interactive help system starts on the interpreter console.
If the argument is a string, then the string is looked up as
the name of a module, function, class, method, keyword,
or documentation topic, and a help page is printed on the
console. If the argument is any other kind of object, a
help page on the object is generated.
Note that if a slash(/) appears in the parameter list of a
function, when invoking help(), it means that the
parameters prior to the slash are positional-only. For
more info, see the FAQ entry on positional-only
parameters.
109
This function is added to the built-in namespace by the
site module.
Changed in version 3.4: Changes to pydoc and inspect
mean that the reported signatures for callables are now
more comprehensive and consistent.
hex(x)
Convert an integer number to a lowercase hexadecimal
string prefixed with “0x”. If x is not a Python int object, it
has to define an _index_() method that returns an integer.
Some examples:
>>>
hex(255) '0xff' hex(-42)
'-0x2a'
If you want to convert an integer number to an uppercase
or lower hexadecimal string with prefix or not, you can
use either of the following ways:
>>>
'%#x' % 255, '%x' % 255, '%X' % 255
('0xff', 'ff', 'FF')
format(255, '#x'), format(255, 'x'), format(255, 'X')
('0xff', 'ff', 'FF')
f'{255:#x}', f'{255:x}', f'{255:X}'
('0xff', 'ff', 'FF')
See also format() for more information.
See also int() for converting a hexadecimal string to an
integer using a base of 16.
110
Note To obtain a hexadecimal string representation for a
float, use the float.hex() method.
id(object)
Return the “identity” of an object. This is an integer which
is guaranteed to be unique and constant for this object
during its lifetime. Two objects with non-overlapping
lifetimes may have the same id() value.
CPython implementation detail: This is the address of
the object in memory.
input([prompt])
If the prompt argument is present, it is written to standard
output without a trailing newline. The function then reads
a line from input, converts it to a string (stripping a trailing
newline), and returns that. When EOF is read, EOFError
is raised. Example:
>>>
>>> s = input('--> ')
--> Monty Python's Flying Circus
>>> s
"Monty Python's Flying Circus"
If the readline module was loaded, then input() will use it
to provide elaborate line editing and history features.
Raises an auditing event builtins.input with argument
prompt before reading input
Raises an auditing event builtins.input/result with the
result after successfully reading input.
111
class int([x])
class int(x, base=10)
Return an integer object constructed from a number or
string x, or return 0 if no arguments are given. If x defines
_int_(), int(x) returns x._int_(). If x defines _index_(), it
returns x._index_(). If x defines trunc_(), it returns
x._trunc_(). For floating point numbers, this truncates
towards zero.
If x is not a number or if base is given, then x must be a
string, bytes, or bytearray instance representing an
integer literal in radix base. Optionally, the literal can be
preceded by + or - (with no space in between) and
surrounded by whitespace. A base- n literal consists of
the digits 0 to n-1, with a to z (or A to Z) having values 10
to 35. The default base is 10. The allowed values are 0
and 2–36. Base-2, -8, and -16 literals can be optionally
prefixed with 0b/0B, 0o/0O, or 0x/0X, as with integer
literals in code. Base 0 means to interpret exactly as a
code literal, so that the actual base is 2, 8, 10, or 16, and
so that int('010', 0) is not legal, while int('010') is, as well
as int('010', 8).
The integer type is described in Numeric Types — int,
float, complex.
Changed in version 3.4: If base is not an instance of int
and the base object has a base. index method, that
method is called to obtain an integer for the base.
112
Previous versions used base._int_instead of
base._index_.
Changed in version 3.6: Grouping digits with underscores
as in code literals is allowed.
Changed in version 3.7: x is now a positional-only
parameter.
Changed in version 3.8: Falls back to _index_() if _int_()
is not defined.
isinstance(object, classinfo)
Return True if the object argument is an instance of the
classinfo argument, or of a (direct, indirect or virtual)
subclass thereof. If object is not an object of the given
type, the function always returns False. If classinfo is a
tuple of type objects (or recursively, other such tuples),
return True if object is an instance of any of the types. If
classinfo is not a type or tuple of types and such tuples, a
TypeError exception is raised.
issubclass(class, classinfo)
Return True if class is a subclass (direct, indirect or
virtual) of classinfo. A class is considered a subclass of
itself. classinfo may be a tuple of class objects, in which
case every entry in classinfo will be checked. In any other
case, a TypeError exception is raised.
iter(object[, sentinel])
Return an iterator object. The first argument is interpreted
very differently depending on the presence of the second
113
argument. Without a second argument, object must be a
collection object which supports the iteration protocol (the
_iter_() method), or it must support the sequence protocol
(the getitem () method with integer arguments starting at
0). If it does not support either of those protocols,
TypeError is raised. If the second argument, sentinel, is
given, then object must be a callable object. The iterator
created in this case will call object with no arguments for
each call to its _next_() method; if the value returned is
equal to sentinel, StopIteration will be raised, otherwise
the value will be returned.
See also Iterator Types.
One useful application of the second form of iter() is to
build a block-reader. For example, reading fixed-width
blocks from a binary database file until the end of file is
reached:
from functools import partial
with open('mydata.db', 'rb') as f:
for block in iter(partial(f.read, 64), b''):
process_block(block)
len(s)
Return the length (the number of items) of an object. The
argument may be a sequence (such as a string, bytes,
tuple, list, or range) or a collection (such as a dictionary,
set, or frozen set).

114
class list([iterable])
Rather than being a function, list is actually a mutable
sequence type, as documented in Lists and Sequence
Types — list, tuple, range.
locals()
Update and return a dictionary representing the current
local symbol table. Free variables are returned by locals()
when it is called in function blocks, but not in class
blocks. Note that at the module level, locals() and
globals() are the same dictionary.
Note The contents of this dictionary should not be
modified; changes may not affect the values of local and
free variables used by the interpreter.
map(function, iterable, ...)
Return an iterator that applies function to every item of
iterable, yielding the results. If additional iterable
arguments are passed, function must take that many
arguments and is applied to the items from all iterables in
parallel. With multiple iterables, the iterator stops when
the shortest iterable is exhausted. For cases where the
function inputs are already arranged into argument
tuples, see itertools.starmap().
max(iterable, *[, key, default]) max(arg1, arg2, *args[,
key])
Return the largest item in an iterable or the largest of two
or more arguments.
115
If one positional argument is provided, it should be an
iterable. The largest item in the iterable is returned. If two
or more positional arguments are provided, the largest of
the positional arguments is returned.
There are two optional keyword-only arguments. The key
argument specifies a one- argument ordering function like
that used for list.sort(). The default argument specifies an
object to return if the provided iterable is empty. If the
iterable is empty and default is not provided, a ValueError
is raised.
If multiple items are maximal, the function returns the first
one encountered. This is consistent with other sort-
stability preserving tools such as sorted(iterable,
key=keyfunc, reverse=True)[0] and heapq.nlargest(1,
iterable, key=keyfunc).
New in version 3.4: The default keyword-only argument.
Changed in version 3.8: The key can be None.
class memoryview(obj)
Return a “memory view” object created from the given
argument. See Memory Views for more information.
min(iterable, *[, key, default])
min(arg1, arg2, *args[, key])
Return the smallest item in an iterable or the smallest of
two or more arguments.
If one positional argument is provided, it should be an
iterable. The smallest item in the iterable is returned. If
116
two or more positional arguments are provided, the
smallest of the positional arguments is returned.
There are two optional keyword-only arguments. The key
argument specifies a one- argument ordering function like
that used for list.sort(). The default argument specifies an
object to return if the provided iterable is empty. If the
iterable is empty and default is not provided, a ValueError
is raised.
If multiple items are minimal, the function returns the first
one encountered. This is consistent with other sort-
stability preserving tools such as sorted(iterable,
key=keyfunc)[0] and heapq.nsmallest(1, iterable,
key=keyfunc).
New in version 3.4: The default keyword-only argument.
Changed in version 3.8: The key can be None.
next(iterator[, default])
Retrieve the next item from the iterator by calling its next
() method. If default is given, it is returned if the iterator is
exhausted, otherwise StopIteration is raised.
class object
Return a new featureless object. object is a base for all
classes. It has the methods that are common to all
instances of Python classes. This function does not
accept any arguments.
Note object does not have a _dict_, so you can’t assign
arbitrary attributes to an instance of the object class.
117
oct(x)
Convert an integer number to an octal string prefixed with
“0o”. The result is a valid Python expression. If x is not a
Python int object, it has to define an _index_() method
that returns an integer. For example:
>>>
oct(8)
'0o10'
oct(-56)
'-0o70'
If you want to convert an integer number to octal string
either with prefix “0o” or not, you can use either of the
following ways.
>>>
'%#o' % 10, '%o' % 10
('0o12', '12')
format(10, '#o'), format(10, 'o')
('0o12', '12')
f'{10:#o}', f'{10:o}'
('0o12', '12')
ord(c)
Given a string representing one Unicode character, return
an integer representing the Unicode code point of that
character. For example, ord('a') returns the integer 97
and ord('€') (Euro sign) returns 8364. This is the inverse
of chr().
118
pow(base, exp[, mod])
Return base to the power exp; if mod is present, return
base to the power exp, modulo mod (computed more
efficiently than pow(base, exp) % mod). The two-
argument form pow(base, exp) is equivalent to using the
power operator: base**exp.
The arguments must have numeric types. With mixed
operand types, the coercion rules for binary arithmetic
operators apply. For int operands, the result has the
same type as the operands (after coercion) unless the
second argument is negative; in that case, all arguments
are converted to float and a float result is delivered. For
example, 10**2 returns 100, but 10**-2 returns 0.01.
For int operands base and exp, if mod is present, mod
must also be of integer type and mod must be nonzero. If
mod is present and exp is negative, base must be
relatively prime to mod. In that case, pow(inv_base, -exp,
mod) is returned, where inv_base is an inverse to base
modulo mod.
Here’s an example of computing an inverse for 38
modulo 97:
>>>
>>> pow(38, -1, mod=97) 23
>>> 23 * 38 % 97 == 1
True

119
Changed in version 3.8: For int operands, the three-
argument form of pow now allows the second argument
to be negative, permitting computation of modular
inverses.
Changed in version 3.8: Allow keyword arguments.
Formerly, only positional arguments were supported.
print(*objects, sep=' ', end='\n', file=sys.stdout,
flush=False)
Print objects to the text stream file, separated by sep and
followed by end. sep, end, file and flush, if present, must
be given as keyword arguments.
All non-keyword arguments are converted to strings like
str() does and written to the stream, separated by sep
and followed by end. Both sep and end must be strings;
they can also be None, which means to use the default
values. If no objects are given, print() will just write end.
The file argument must be an object with a write(string)
method; if it is not present or None, sys.stdout will be
used. Since printed arguments are converted to text
strings, print() cannot be used with binary mode file
objects. For these, use file.write(...) instead.
Whether output is buffered is usually determined by file,
but if the flush keyword argument is true, the stream is
forcibly flushed.
Changed in version 3.3: Added the flush keyword
argument. repr(object)
120
Return a string containing a printable representation of an
object. For many types, this function makes an attempt to
return a string that would yield an object with the same
value when passed to eval(), otherwise the
representation is a string enclosed in angle brackets that
contains the name of the type of the object together with
additional information often including the name and
address of the object. A class can control what this
function returns for its instances by defining a _repr_()
method.
reversed(seq)
Return a reverse iterator. seq must be an object which
has a reversed () method or supports the sequence
protocol (the _len_() method and the _getitem_() method
with integer arguments starting at 0).
round(number[, ndigits])
Return number rounded to ndigits precision after the
decimal point. If ndigits is omitted or is None, it returns
the nearest integer to its input.
For the built-in types supporting round(), values are
rounded to the closest multiple of 10 to the power minus
ndigits; if two multiples are equally close, rounding is
done toward the even choice (so, for example, both
round(0.5) and round(-0.5) are 0, and round(1.5) is 2).
Any integer value is valid for ndigits (positive, zero, or
negative). The return value is an integer if ndigits is
121
omitted or None. Otherwise the return value has the
same type as number.
For a general Python object number, round delegates to
number._round_.
Note: The behavior of round() for floats can be surprising:
for example, round(2.675, 2) gives 2.67 instead of the
expected 2.68. This is not a bug: it’s a result of the fact
that most decimal fractions can’t be represented exactly
as a float. See Floating Point Arithmetic: Issues and
Limitations for more information.
class set([iterable])
Return a new set object, optionally with elements taken
from iterable. set is a built-in class. See set and Set
Types — set, frozenset for documentation about this
class.
For other containers see the built-in frozenset, list, tuple,
and dict classes, as well as the collections module.
setattr(object, name, value)
This is the counterpart of getattr(). The arguments are an
object, a string and an arbitrary value. The string may
name an existing attribute or a new attribute. The function
assigns the value to the attribute, provided the object
allows it. For example, setattr(x, 'foobar', 123) is
equivalent to x.foobar = 123.
class slice(stop)
class slice(start, stop[, step])
122
Return a slice object representing the set of indices
specified by range(start, stop, step). The start and step
arguments default to None. Slice objects have read-only
data attributes start, stop and step which merely return
the argument values (or their default). They have no other
explicit functionality; however they are used by Numerical
Python and other third party extensions. Slice objects are
also generated when extended indexing syntax is used.
For example: a[start:stop:step] or a[start:stop, i]. See
itertools.islice() for an alternate version that returns an
iterator.
sorted(iterable, *, key=None, reverse=False) Return a
new sorted list from the items in iterable.
Has two optional arguments which must be specified as
keyword arguments.
key specifies a function of one argument that is used to
extract a comparison key from each element in iterable
(for example, key=str.lower). The default value is None
(compare the elements directly).
reverse is a boolean value. If set to True, then the list
elements are sorted as if each comparison were
reversed.
Use functools.cmp_to_key() to convert an old-style cmp
function to a key function.
The built-in sorted() function is guaranteed to be stable. A
sort is stable if it guarantees not to change the relative
123
order of elements that compare equal - this is helpful for
sorting in multiple passes (for example, sort by
department, then by salary grade).
For sorting examples and a brief sorting tutorial, see
Sorting HOW TO.
@staticmethod
Transform a method into a static method.
A static method does not receive an implicit first
argument. To declare a static method, use this idiom:
class C:
@staticmethod
def f(arg1, arg2, ...): ...
The @staticmethod form is a function decorator – see
Function definitions for details.
A static method can be called either on the class (such as
C.f()) or on an instance (such as C().f()).
Static methods in Python are similar to those found in
Java or C++. Also see classmethod() for a variant that is
useful for creating alternate class constructors.
Like all decorators, it is also possible to call staticmethod
as a regular function and do something with its result.
This is needed in some cases where you need a
reference to a function from a class body and you want to
avoid the automatic transformation to instance method.
For these cases, use this idiom:
class C:
124
builtin_open = staticmethod(open)
For more information on static methods, see the standard
type hierarchy.
class str(object='')
class str(object=b'', encoding='utf-8', errors='strict')
Return a str version of object. See str() for details.
str is the built-in string class. For general information
about strings, see Text Sequence Type — str.
sum(iterable, /, start=0)
Sums start and the items of an iterable from left to right
and returns the total. The iterable’s items are normally
numbers, and the start value is not allowed to be a string.
For some use cases, there are good alternatives to
sum(). The preferred, fast way to concatenate a
sequence of strings is by calling ''.join(sequence). To add
floating point values with extended precision, see
math.fsum(). To concatenate a series of iterables,
consider using itertools.chain().
Changed in version 3.8: The start parameter can be
specified as a keyword argument.
super([type[, object-or-type]])
Return a proxy object that delegates method calls to a
parent or sibling class of type. This is useful for accessing
inherited methods that have been overridden in a class.

125
The object-or-type determines the method resolution
order to be searched. The search starts from the class
right after the type.
For example, if _mro_ of object-or-type is D -> B -> C ->
A -> object and the value of type is B, then super()
searches C -> A -> object.
The _mro_ attribute of the object-or-type lists the method
resolution search order used by both getattr() and
super(). The attribute is dynamic and can change
whenever the inheritance hierarchy is updated.
If the second argument is omitted, the super object
returned is unbound. If the second argument is an object,
isinstance(obj, type) must be true. If the second argument
is a type, issubclass(type2, type) must be true (this is
useful for classmethods).
There are two typical use cases for super. In a class
hierarchy with single inheritance, super can be used to
refer to parent classes without naming them explicitly,
thus making the code more maintainable. This use
closely parallels the use of super in other programming
languages.
The second use case is to support cooperative multiple
inheritance in a dynamic execution environment. This use
case is unique to Python and is not found in statically
compiled languages or languages that only support single
inheritance. This makes it possible to implement
126
“diamond diagrams” where multiple base classes
implement the same method. Good design dictates that
this method have the same calling signature in every
case (because the order of calls is determined at runtime,
because that order adapts to changes in the class
hierarchy, and because that order can include sibling
classes that are unknown prior to runtime).
For both use cases, a typical superclass call looks like
this:
class C(B):
def method(self, arg):
super().method(arg) # This does the same thing as: #
super(C, self).method(arg)
In addition to method lookups, super() also works for
attribute lookups. One possible use case for this is calling
descriptors in a parent or sibling class.
Note that super() is implemented as part of the binding
process for explicit dotted attribute lookups such as
super()._getitem_(name). It does so by implementing its
own getattribute_() method for searching classes in a
predictable order that supports cooperative multiple
inheritance. Accordingly, super() is undefined for implicit
lookups using statements or operators such as
super()[name].
Also note that, aside from the zero argument form,
super() is not limited to use inside methods. The two
127
argument form specifies the arguments exactly and
makes the appropriate references. The zero argument
form only works inside a class definition, as the compiler
fills in the necessary details to correctly retrieve the class
being defined, as well as accessing the current instance
for ordinary methods.
For practical suggestions on how to design cooperative
classes using super(), see guide to using super().
class tuple([iterable])
Rather than being a function, tuple is actually an
immutable sequence type, as documented in Tuples and
Sequence Types — list, tuple, range.
class type(object)
class type(name, bases, dict)
With one argument, return the type of an object. The
return value is a type object and generally the same
object as returned by object._class_.
The isinstance() built-in function is recommended for
testing the type of an object, because it takes subclasses
into account.
With three arguments, return a new type object. This is
essentially a dynamic form of the class statement. The
name string is the class name and becomes the _name
attribute; the bases tuple itemizes the base classes and
becomes the bases attribute; and the dict dictionary is the
namespace containing definitions for class body and is
128
copied to a standard dictionary to become the _dict_
attribute. For example, the following two statements
create identical type objects:
>>>
class X: a = 1
X = type('X', (object,), dict(a=1))
See also Type Objects.
Changed in version 3.6: Subclasses of type which don’t
override type._new_ may no longer use the one-
argument form to get the type of an object.
vars([object])
Return the _dict_ attribute for a module, class, instance,
or any other object with a _dict_ attribute.
Objects such as modules and instances have an
updateable dict__ attribute; however, other objects may
have write restrictions on their dict attributes (for
example, classes use a types.MappingProxyType to
prevent direct dictionary updates).
Without an argument, vars() acts like locals(). Note, the
locals dictionary is only useful for reads since updates to
the locals dictionary are ignored.
zip(*iterables)
Make an iterator that aggregates elements from each of
the iterables.
Returns an iterator of tuples, where the i-th tuple contains
the i-th element from each of the argument sequences or
129
iterables. The iterator stops when the shortest input
iterable is exhausted. With a single iterable argument, it
returns an iterator of 1-tuples. With no arguments, it
returns an empty iterator. Equivalent to:
def zip(*iterables):
# zip('ABCD', 'xy') --> Ax By
sentinel = object()
iterators = [iter(it) for it in iterables] while iterators:
result = []
for it in iterators:
elem = next(it, sentinel) if elem is sentinel:
return result.append(elem)
yield tuple(result)
The left-to-right evaluation order of the iterables is
guaranteed. This makes possible an idiom for clustering a
data series into n-length groups using zip(*[iter(s)]*n).
This repeats the same iterator n times so that each output
tuple has the result of n calls to the iterator. This has the
effect of dividing the input into n-length chunks.
zip() should only be used with unequal length inputs
when you don’t care about trailing, unmatched values
from the longer iterables. If those values are important,
use itertools.zip_longest() instead.
zip() in conjunction with the * operator can be used to
unzip a list:
>>>
130
>>> x = [1, 2, 3]
>>> y = [4, 5, 6]
>>> zipped = zip(x, y)
>>> list(zipped)
[(1, 4), (2, 5), (3, 6)]
>>> x2, y2 = zip(*zip(x, y))
>>> x == list(x2) and y == list(y2)
True
_import_ (name, globals=None, locals=None, fromlist=(),
level=0)
Note This is an advanced function that is not needed in
everyday Python programming, unlike
importlib.import_module().
This function is invoked by the import statement. It can be
replaced (by importing the builtins module and assigning
to builtins._import_) in order to change semantics of the
import statement, but doing so is strongly discouraged as
it is usually simpler to use import hooks (see PEP 302) to
attain the same goals and does not cause issues with
code which assumes the default import implementation is
in use. Direct use of _import_() is also discouraged in
favor of importlib.import_module().
The function imports the module name, potentially using
the given globals and locals to determine how to interpret
the name in a package context. The fromlist gives the
names of objects or submodules that should be imported
131
from the module given by name. The standard
implementation does not use its locals argument at all,
and uses its globals only to determine the package
context of the import statement.
level specifies whether to use absolute or relative
imports. 0 (the default) means only perform absolute
imports. Positive values for level indicate the number of
parent directories to search relative to the directory of the
module calling _import_() (see PEP 328 for the details).
When the name variable is of the form package.module,
normally, the top-level package (the name up till the first
dot) is returned, not the module named by name.
However, when a non-empty fromlist argument is given,
the module named by name is returned.
For example, the statement import spam results in
bytecode resembling the following code:
spam = __import_ ('spam', globals(), locals(), [], 0)
The statement import spam.ham results in this call:
spam = __import_ ('spam.ham', globals(), locals(), [], 0)
Note how __import_() returns the toplevel module here
because this is the object that is bound to a name by the
import statement.
On the other hand, the statement from spam.ham import
eggs, sausage as saus results in
_temp = __import_ ('spam.ham', globals(), locals(),
['eggs', 'sausage'], 0)
132
eggs = _temp.eggs
saus = _temp.sausage
Here, the spam.ham module is returned from __import_().
From this object, the names to import are retrieved and
assigned to their respective names.
If you simply want to import a module (potentially within a
package) by name, use importlib.import_module().
Changed in version 3.3: Negative values for level are no
longer supported (which also changes the default value to
0).

133
CHAPTER -6
DATA PROCESSING & VISUALIZATION

Python provides numerous libraries for data analysis and


visualization mainly numpy, pandas, matplotlib, seaborn
etc. In this section, we are going to discuss pandas library
for data analysis and visualization which is an open
source library built on top of numpy.
It allows us to do fast analysis and data cleaning and
preparation.Pandas also provides numerous built-in
visualization features which we are going to see below.
Installation
To install pandas, run the below command in your
terminal-
Pipinstall pandas
Orwe have anaconda, you can use
Condainstall pandas
DataFrame and Series
First we need to understand two main basic data
structure of pandas .i.e. DataFrame and Series. We need
to have solid understanding of these two data structure in
order to master pandas.
Series
Series is an object which is similar to python built-in type
list but differs from it because it has associated lable with
each element or index.
134
>>> import pandas as pd
>>> my_series = pd.Series([12, 24, 36, 48, 60, 72,
84])
>>>
my_s
erie
s0
12
1 24
2 36
3 48
4 60
5 72
6 84
dtype: int64

In the above output, the ‘index’ is on the left side and


‘value’ on the right. Also each Series object has data
type(dtype), in our case its int64.
We can retrieve elements by their index number:

>>>my_series[6]
84

135
>>> my_series = pd.Series([12,24,36,48,60,72,84], index
=['ind0','ind1','ind2','ind3','ind4','ind5','ind6'])
>>>
my_series
ind0 12
ind1 24
ind2 36
ind3 48
ind4 60
ind5 72
ind6 84
dtype:
int64
To provide index(labels) explicity, use:
Also it is very easy to retrieve several elements by their
indexes or make grop assignment:
>>> my_series[['ind0','ind3','ind6']]
ind0 12
ind3 48
ind6 84
dtype: int64
>>> my_series[['ind0','ind3','ind6']]=36
>>> my_series
ind0 36
ind1 24
ind2 36
Filtering and math operations are easy as well:
ind3 36
ind4 60
ind5 72
ind6 36

dtype: int64
136
>>> my_series[my_series>24]

ind0 36

ind2 36

ind3 36

ind4 60

ind5 72

ind6 36

dtype: int64

>>> my_series[my_series <24]*2

Series([], dtype: int64)

>>> my_series

ind0 36

ind1 24

ind2 36

ind3 36

ind4 60

ind5 72

ind6 36

dtype: int64

>>>

137
Below are some other common operations on Series.
>>>#Work as dictionaries

>>> my_series1 = pd.Series({'a':9,'b':18,'c':27,'d':36})


>>> my_series1
a 9
b 18
c 27
d 36
dtype: int64
>>>#Label attributes
>>> my_series1.name ='Numbers'
>>> my_series1.index.name ='letters'
>>> my_series1
letters
a 9
b 18
c 27
d 36
Name:Numbers, dtype: int64
>>>#chaning Index
>>> my_series1.index =['w','x','y','z']
>>> my_series1
w 9
x 18
y 27
z 36
Name:Numbers, dtype: int64
>>>

138
DataFrame
DataFrame acts like a table as it contains rows and
columns. Each column in a DataFrame is a Series object
and rows consist of elements inside Series.
DataFrame can be constructed using built-in Python

>>> df = pd.DataFrame({

'Country':['China','India','Indonesia','Pakistan'],
dicts:

139
Accessing elements
There are several ways to provide row index explicitly.
>>> df = pd.DataFrame({
'Country':['China','India','Indonesia','Pakistan'],
'Population':[1420062022,1368737513,269536482,204596442],
'Landarea':[9388211,2973190,1811570,770880]
}, index =['CHA','IND','IDO','PAK'])
>>> df
CHA China93882111420062022
CountryLandareaPopulation
IND India29731901368737513
IDO Indonesia1811570269536482
PAK Pakistan770880204596442
>>> df.index =['CHI','IND','IDO','PAK']
>>> df.index.name ='Country Code'
>>> df
CountryLandareaPopulation
CountryCode
CHI China93882111420062022
IND India29731901368737513
IDO Indonesia1811570269536482
PAK Pakistan770880204596442
>>> df['Country']
CountryCode
CHI China
IND India
IDO Indonesia
PAK Pakistan
Name:Country, dtype:object

140
Row access using index can be performed in several
ways
• Using .loc and providing index label
• Using .iloc and providing index number
Reading and Writing files
Pandas supports many popular file formats including
CSV, XML, HTML, Excel, SQL, JSON many more. Most
commonly CSV file format is used.

>>> df = pd.read_csv('GDP.csv', sep = ',')


To read a csv file, just run:

Named argument sep points to a separator character in


CSV file called GDP.csv.
Aggregating and Grouping
In order to group data in pandas we can use .groupby
method. To demonstrate the use of aggregates and
grouping in pandas I have used the Titanic dataset, you
can find the same from below link:
https://yadi.sk/d/TfhJdE2k3EyALt

141
23 Allison,MrHudsonJoshuaCreighton1st

30.00

34 Allison,MrsHudson JC (BessieWaldoDaniels)1st

25.00

45 Allison,MasterHudsonTrevor1st0.92

Sex Survived SexCode


0 female 1 1

1 female 0 1

2 male 0 0

3 female 0 1
4 male 1 0

>>>

Let’s calculate how many passengers (women and men)


survived and how many did not, we will use .groupby

>>>print(titanic_df.groupby(['Sex','Survived'])['PassengerID
'].count())
Sex Survived

female 0 154

1 308

male 0 709
1 142

Name:PassengerID, dtype: int64 142


Above data based on cabin class:

>>>print(titanic_df.groupby(['PClass','Survived'])['Passenge
rID'].count())
PClass Survived

* 0 1

1st 0 129

1 193
2nd 0 160

1 119

3rd 0 573

1 138

Name: PassengerID, dtype: int64

Time Series analysis using pandas


Pandas was created to analyse time series data. In order
to illustrate, I have used the amazon 5 years stock prices.
You can download it from below link,
https://finance.yahoo.com/quote/AMZN/history?period1=1
397413800&period2=155
5180200&interval=1mo&filter=history&frequency=1mo
Above we have created a DataFRame with
DatetimeIndex by Date column and then sort it.
And the mean closing price is,

>>> amzn_df.loc['2015-04',
'Close'].mean()421.779999

143
Visualization
We can use matplotlib library to visualize pandas. Let’s
take our amazon stock historical dataset and look into its
price movement of specific time period over graph.

>>>import matplotlib.pyplot as plt

>>> df = pd.read_csv('AMZN.csv', index_col ='Date',


parse_dates =True)

>>> new_df = df.loc['2014-06':'2018-08',['Close']]

>>> new_df=new_df.astype(float)

>>> new_df.plot()

<matplotlib.axes._subplots.AxesSubplotobject at 0x0B9B8930>

>>> plt.show()

Pandas-DataFrames
Data framesa re the main tools when we are working with
pandas.
Example
import numpy as np
import pandas as pd
from numpy.random import randn
np.random.seed(50)
df = pd.DataFrame(randn(6,4),
['a','b','c','d','e','f'],['w','x','y','z'])
df

144
Output
w x y z
a -1.560352 -0.030978 -0.620928 -1.464580
b 1.411946 -0.476732 -0.780469 1.070268
c -1.282293 -1.327479 0.126338 0.862194
d 0.696737 -0.334565 -0.997526 1.598908
e 3.314075 0.987770 0.123866 0.742785
f -0.393956 0.148116 -0.412234 -0.160715

145
Pandas-Missing Data
Weare going to see some convenient ways to deal with
missing data inpandas, which automatically gets filled
with zero's or nan.
import numpy as np
import pandas as pd
from numpy.random import randn
d = {'A': [1,2,np.nan], 'B': [9, np.nan, np.nan], 'C': [1,4,9]}
df = pd.DataFrame(d)
df
Output
A B C
0 1.0 9.0 1
1 2.0 NaN 4
2 NaN NaN 9

So, we are having 3 missing value in above.


df.dropna()
A B C
0 1.0 9.0 1

df.dropna(axis = 1)
C
0 1
1 4

146
2 9

df.dropna(thresh = 2)
A B C
0 1.0 9.0 1
1 2.0 NaN 4

df.fillna(value = df.mean())
A B C
0 1.0 9.0 1
1 2.0 9.0 4
2 1.5 9.0 9

Pandas − Import data


We are going to read the csv file which is either stored in
our local machine(in my case) or we can directly fetch
from the web.
#import pandas library
import pandas as pd
#Read csv file and assigned it to dataframe variable
df = pd.read_csv("SYB61_T03_Population Growth Rates
in Urban areas and Capital cities.csv",encoding = "ISO-
8859-1")

147
#Read first five element from the dataframe

df.head()
Output
Toread the number of rows and columns in our dataframe
or csv file.
#Countthe number of rows and columns in our dataframe.
df.shape
Output
(4166,9)
Pandas − Dataframe Math
Operationson dataframes can be done using various
tools of pandas forstatistics
#To computes various summary statistics, excluding NaN
values
df.describe()

148
Output

# computes numerical data ranks


df.rank()

Output
.....
.....

149
Pandas − plot graph
import matplotlib.pyplot as plt
years = [1981, 1991, 2001, 2011, 2016]
Average_populations = [716493000,
891910000, 1071374000, 1197658000,
1273986000]
plt.plot(years, Average_populations)
plt.title("Census of India: sample registration system")
plt.xlabel("Year")
plt.ylabel("Average_populations")
plt.show()

Output
Scatter plot of above data:
plt.scatter(years,Average_populations)

150
Histogram:
import matplotlib.pyplot as plt
Average_populations = [716493000,
891910000, 1071374000, 1197658000,
1273986000]
plt.hist(Average_populations, bins = 10)
plt.xlabel("Average_populations") plt.ylabel("Frequency")
plt.show()

Output

151
Sometimes there is a requirement to convert a string to a
number (int/float) in data analysis. For each string, we
can assign a unique integer value to differentiate string
values.
For this, we use the data in Comma Separated
Values(CSV) files. Say we have an excel file containing
CSV data as follow –
Company Industry Recommendation
HDFC Bank Finance Hold
Apollo Healthcare Buy
Hero Automobile Underperform
Yes Bank Finance Hold
M&M Automobile Underperform
Fortis Healthcare Buy
Maruti Automobile Underperform

Above is just a few lines from a large dataset, we need to


give different recommendation .i.e. Buy, Hold,
Underperform etc. integer values, which will link to our
metadata. So for the above input, our expected output will
be something like-
Company Industry Recommendation
HDFC Bank Finance 2
Apollo Healthcare 1
Hero Automobile 3

152
Yes Bank Finance 2
M&M Automobile 3
Fortis Healthcare 1
Maruti Automobile 3

Here is a way to replace our string(column values) to


integers.

#Import required library

import pandas as pd

#Import the CSV file into Python using read_csv() from


pandas

dataframe = pd.read_csv("data_pandas1.csv")

#Create the dictionary of key-value pair, where key is

#your old value(string) and value is your new


value(integer).

Recommendation={'Buy':1,'Hold':2,'Underperform':3}

#Assign these different key-value pair from above


dictiionary to your table

dataframe.Recommendation=[Recommendation[item]for item in
dataframe.Recommendation]

#New table

print(dataframe)
Code 1

153
Result
Company Industry Recommendation
0 HDFC Bank Finance 2
1 Apollo Healthcare 1
2 Hero Automobile 3
3 Yes Bank Finance 2
4 M&M Automobile 3
5 Fortis Healthcare 1
6 Maruti Automobile 3

There is another way to write above code, where we don’t


deal with a dictionary instead we directly assign another
value to the columns field (Recommendations here) if
condition matches.

154
#Import required

libraryimport

pandas as pd

#Import the CSV file into Python using


read_csv() frompandas

dataf = pd.read_csv("data_pandas1.csv")

#Directly assigning individual fields of


Recommendationcolumn different integer value

#if condition matches .i.e.In the dataframe,


recommendationcolumns we have "Buy" we'll assign

# integer 1 to it.

dataf.Recommendation[data.Recommendation=='Buy']=1

dataf.Recommendation[data.Recommendation=='Hold']=2

dataf.Recommendation[data.Recommendation=='Underperform

']=3

print(dataf)

155
Result
Company Industry Recommendation
0 HDFC Bank Finance 2
1 Apollo Healthcare 1
2 Hero Automobile 3
3 Yes Bank Finance 2
4 M&M Automobile 3
5 Fortis Healthcare 1
6 Maruti Automobile 3

156
CHAPTER -7
NATURAL LANGUAGE PROCESSING

Natural Language Processing with Python


In this chapter, we will learn about language processing
using Python. The following features make Python
different from other languages −
Python is interpreted − We do not need to compile our
Python program before executing it because the
interpreter processes Python at runtime.
Interactive − We can directly interact with the interpreter
to write our Python programs. Object-oriented − Python is
object-oriented in nature and it makes this language
easier to write programs because with the help of this
technique of programming it encapsulates code
within objects.
Beginner can easily learn − Python is also called
beginner’s language because it is very easy to
understand, and it supports the development of a wide
range of applications.
Prerequisites
The latest version of Python 3 released is Python 3.7.1 is
available for Windows, Mac OS and most of the flavors of
Linux OS.

157
For windows, we can go to the link
www.python.org/downloads/windows/ to download and
install Python.
For MAC OS, we can use the link
www.python.org/downloads/mac-osx/.
In case of Linux, different flavors of Linux use different
package managers for installation of new packages.
For example, to install Python 3 on Ubuntu Linux, we can
use the following command from terminal −
$sudo apt-get install python3-minimal
To study more about Python programming, read Python 3
basic tutorial – Python
Getting Started with NLTK
We will be using Python library NLTK (Natural Language
Toolkit) for doing text analysis in English Language. The
Natural language toolkit (NLTK) is a collection of Python
libraries designed especially for identifying and tag parts
of speech found in the text of natural language like
English.
Installing NLTK
Before starting to use NLTK, we need to install it. With
the help of following command, we can install it in our
Python environment −
pip install nltk
If we are using Anaconda, then a Conda package for
NLTK can be built by using the following command −
158
conda install -c anaconda nltk
Downloading NLTK’s Data
After installing NLTK, another important task is to
download its preset text repositories so that it can be
easily used. However, before that we need to import
NLTK the way we import any other Python module. The
following command will help us in importing NLTK −
import nltk
Now, download NLTK data with the help of the following
command − nltk.download()
It will take some time to install all available packages of
NLTK.
Other Necessary Packages
Some other Python packages like gensim and pattern are
also very necessary for text analysis as well as building
natural language processing applications by using NLTK.
the packages can be installed as shown below −
gensim
gensim is a robust semantic modeling library which can
be used for many applications. We can install it by
following command −
pip install gensim pattern
It can be used to make gensim package work properly.
The following command helps in installing pattern −
pip install pattern

159
Tokenization may be defined as the Process of breaking
the given text, into smaller units called tokens. Words,
numbers or punctuation marks can be tokens. It may also
be called word segmentation.
Example
Input − Bed and chair are types of furniture. Bed and
Chair
We have different packages for tokenization provided by
NLTK. We can use these packages based on our
requirements. The packages and the details of their
installation are as follows −
sent_tokenize package
This package can be used to divide the input text into
sentences. We can import it by using the following
command −
from nltk.tokenize import sent_tokenize word_tokenize
package
This package can be used to divide the input text into
words. We can import it by using the following command

from nltk.tokenize import word_tokenize
WordPunctTokenizer package
This package can be used to divide the input text into
words and punctuation marks. We can import it by using
the following command −
from nltk.tokenize import WordPuncttokenizer
160
Stemming
Due to grammatical reasons, language includes lots of
variations. Variations in the sense that the language,
English as well as other languages too, have different
forms of a word. For example, the words like democracy,
democratic, and democratization. For machine learning
projects, it is very important for machines to understand
that these different words, like above, have the same
base form. That is why it is very useful to extract the base
forms of the words while analyzing the text.
Stemming is a heuristic process that helps in extracting
the base forms of the words by chopping of their ends.
The different packages for stemming provided by NLTK
module are as follows − PorterStemmer package
Porter’s algorithm is used by this stemming package to
extract the base form of the words. With the help of the
following command, we can import this package −
from nltk.stem.porter import PorterStemmer
For example, ‘write’ would be the output of the word
‘writing’ given as the input to this stemmer.
LancasterStemmer package
Lancaster’s algorithm is used by this stemming package
to extract the base form of the words. With the help of
following command, we can import this package −
from nltk.stem.lancaster import LancasterStemmer

161
For example, ‘writ’ would be the output of the word
‘writing’ given as the input to this stemmer.
SnowballStemmer package
Snowball’s algorithm is used by this stemming package to
extract the base form of the words. With the help of
following command, we can import this package −
from nltk.stem.snowball import SnowballStemmer
For example, ‘write’ would be the output of the word
‘writing’ given as the input to this stemmer.

Lemmatization
It is another way to extract the base form of words,
normally aiming to remove inflectional endings by using
vocabulary and morphological analysis. After
lemmatization, the base form of any word is called
lemma.
NLTK module provides the following package for
lemmatization − WordNetLemmatizer package
This package will extract the base form of the word
depending upon whether it is used as a noun or as a
verb. The following command can be used to import this
package −
from nltk.stem import WordNetLemmatizer
Counting POS Tags–Chunking
The identification of parts of speech (POS) and short
phrases can be done with the help of chunking. It is one
162
of the important processes in natural language
processing. As we are aware about the process of
tokenization for the creation of tokens, chunking actually
is to do the labeling of those tokens. In other words, we
can say that we can get the structure of the sentence with
the help of chunking process.
Example
In the following example, we will implement Noun-Phrase
chunking, a category of chunking which will find the noun
phrase chunks in the sentence, by using NLTK Python
module.
Consider the following steps to implement noun-phrase
chunking –
Step 1: Chunk grammar definition
In this step, we need to define the grammar for chunking.
It would consist of the rules, which we need to follow.
Step 2: Chunk parser creation
Next, we need to create a chunk parser. It would parse
the grammar and give the output.
Step 3: The Output
In this step, we will get the output in a tree format.
Running the NLP Script
Start by importing the the NLTK package − import nltk
Now, we need to define the sentence. Here,
DT is the determinant VBP is the verb
JJ is the adjective IN is the preposition NN is the noun
163
sentence = [("a",
"DT"),("clever","JJ"),("fox","NN"),("was","VBP"),
("jumping","VBP"),("over","IN"),("the","DT"),("wall","NN")]
Next, the grammar should be given in the form of regular
expression. grammar = "NP:{<DT>?<JJ>*<NN>}"
Now, we need to define a parser for parsing the
grammar. parser_chunking =
nltk.RegexpParser(grammar)
Now, the parser will parse the sentence as follows −
parser_chunking.parse(sentence)

Next, the output will be in the variable as follows:-


Output = parser_chunking.parse(sentence)
Now, the following code will help you draw your output in
the form of a tree.
output.draw()

164
CHAPTER -8
CREATE WEB APPLICATIONS

Create Web Applications


Creating a Web App From Scratch Using Python
Flask and MySQL
In this series, we'll be using Python, Flask and MySQL to
create a simple web application from scratch. It will be a
simple bucket list application where users can register,
sign in and create their bucket list.This tutorial assumes
that you have some basic knowledge of the Python
programming language. We'll be using Flask, a Python
web application framework, to create our application, with
MySQL as the back end.
Introduction to Python Flask
Flask is a Python framework for creating web
applications. From the official site,Flask is a
microframework for Python based on Werkzeug, Jinja 2
and good intentions.When we think about Python, the de
facto framework that comes to our mind is the Django
framework. But from a Python beginner's perspective,
Flask is easier to get started with, when compared to
Django.
Setting Up Flask
Setting up Flask is pretty simple and quick. With pip
package manager, all we need to do is:
165
pip install flask
Once you're done with installing Flask, create a folder
called FlaskApp. Navigate to the FlaskApp folder and
create a file called app.py. Import the flask module and
create an app using Flask as shown:
from flask import Flask app = Flask(_name_)
Now define the basic route / and its corresponding
request handler:
@app.route("/")
def main():
return "Welcome!"
Next, check if the executed file is the main program and
run the app:
If _name_== "_main_":
app.run()
Save the changes and execute app.py:
python app.py
Point your browser to http://localhost:5000/ and you
should have the welcome message.
Creating a Home Page
First, when the application runs we should show a home
page with the latest bucket list items added by users. So
let's add our home page to our application folder.

166
Flask looks for template files inside the templates folder.
So navigate to the PythonApp folder and create a folder
called templates. Inside templates, create a file called
index.html. Open up index.html and add the following
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Python Flask Bucket List App</title>
<link
href="http://getbootstrap.com/dist/css/bootstrap.min.css"
rel="stylesheet">
<link href="http://getbootstrap.com/examples/jumbotron-
narrow/jumbotron-narrow.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="header">
<nav>
<ul class="nav nav-pills pull-right">
<li role="presentation" class="active"><a
href="#">Home</a>
</li>
<li role="presentation"><a href="#">Sign In</a>
</li>

167
<li role="presentation"><a href="showSignUp">Sign
Up</a>
</li>
</ul>
</nav>
<h3 class="text-muted">Python Flask App</h3>
</div>
<div class="jumbotron">
<h1>Bucket List App</h1>
<p class="lead"></p>
<p><a class="btn btn-lg btn-success" href="showSignUp"
role="button">Sign up today</a>
</p>
</div>
<div class="row marketing">
<div class="col-lg-6">
<h4>Bucket List</h4>
<p>Donec id elit non mi porta gravida at eget metus.
Maecenas faucibus mollis interdum.</p>
<h4>Bucket List</h4>
<p>Morbi leo risus, porta ac consectetur ac, vestibulum
at eros. Cras mattis consectetur purus sit amet
fermentum.</p>
<h4>Bucket List</h4>
<p>Maecenas sed diam eget risus varius blandit sit amet
non magna.</p>
168
</div>
<div class="col-lg-6">
<h4>Bucket List</h4>
<p>Donec id elit non mi porta gravida at eget metus.
Maecenas faucibus mollis interdum.</p>
<h4>Bucket List</h4>
<p>Morbi leo risus, porta ac consectetur ac, vestibulum
at eros. Cras mattis consectetur purus sit amet
fermentum.</p>
<h4>Bucket List</h4>
<p>Maecenas sed diam eget risus varius blandit sit amet
non magna.</p>
</div>
</div>
<footer class="footer">
<p>&copy; Company 2015</p>
</footer>
</div>
</body>
</html>
Open up app.py and import render_template, which we'll
use to render the template files. from flask import Flask,
render_template
Modify the main method to return the rendered template
file. def main():
return render_template('index.html')
169
Save the changes and restart the server. Point your
browser to http://localhost:5000/ and you should have the
below screen:

Creating a Signup Page


Step 1: Setting Up the Database
We'll be using MySQL as the back end. So log into
MySQL from the command line, or if you prefer a GUI like
MySQL work bench, you can use that too. First, create a
database called BucketList.
From the command line:
mysql -u <username> -p
Enter the required password and when logged in, execute
the following command to create the database:
CREATE DATABASE BucketList;

170
Once the database has been created, create a table
called tbl_user as shown: CREATE TABLE
`BucketList`.`tbl_user` (
`user_id` BIGINT NULL AUTO_INCREMENT,
`user_name` VARCHAR(45) NULL,
`user_username` VARCHAR(45) NULL,
`user_password` VARCHAR(45) NULL, PRIMARY KEY
(`user_id`));
We'll be using Stored procedures for our Python
application to interact with the MySQL database. So,
once the table tbl_user has been created, create a stored
procedure called sp_createUser to sign up a user.
When creating a stored procedure to create a user in the
tbl_user table, first we need to check if a user with the
same username already exists. If it exists we need to
throw an error to the user, otherwise we'll create the user
in the user table. Here is how the stored procedure
sp_createUser would look:
DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE
`sp_createUser`( IN p_name VARCHAR(20),
IN p_username VARCHAR(20), IN p_password
VARCHAR(20)
) BEGIN

171
if ( select exists (select 1 from tbl_user where
user_username = p_username) ) THEN select 'Username
Exists !!';
ELSE
insert into tbl_user (
user_name, user_username, user_password
)
values (
p_name, p_username, p_password
); END IF;
END$$ DELIMITER ;
Step 2: Create a Signup interface
Navigate to the PythonApp/templates directory and
create an HTML file called signup.html. Add the following
HTML code to signup.html:
body {
padding-top: 40px;
padding-bottom: 40px;
}
.form-signin {
max-width: 330px; padding: 15px; margin: 0 auto;
}
.form-signin .form-signin-heading,
.form-signin .checkbox { margin-bottom: 10px;
}
.form-signin .checkbox { font-weight: normal;
172
}
.form-signin .form-control { position: relative;
height: auto;

-webkit-box-sizing: border-box;
-moz-box-sizing: border-box; box-sizing: border-box;
padding: 10px; font-size: 16px;
}
.form-signin .form-control:focus { z-index: 2;
}
.form-signin input[type="email"] { margin-bottom: -1px;
border-bottom-right-radius: 0;
border-bottom-left-radius: 0;
}
.form-signin input[type="password"] { margin-bottom:
10px;
border-top-left-radius: 0;
border-top-right-radius: 0;
}
In app.py add another method called showSignUp to
render the signup page once a request comes to
/showSignUp:
@app.route('/showSignUp') def showSignUp():
return render_template('signup.html')

173
Save the changes and restart the server. Click on the
Sign Up button on the home page and you should have
the signup page as shown:

Step 3: Implement a Signup method


Next, we need a server-side method for the UI to interact
with the MySQL database. So navigate to PythonApp and
open app.py. Create a new method called signUp and
also add a route /signUp. Here is how it looks:
@app.route('/signUp') def signUp():
# create user code will be here !!
We'll be using jQuery AJAX to post our signup data to the
method, so we'll specify the
method in the route definition.
@app.route('/signUp',methods=['POST']) def signUp():
# create user code will be here !!

174
In order to read the posted values we need to import
request from Flask. from flask import Flask,
render_template, request.
Using request we can read the posted values as shown
below:
@app.route('/signUp',methods=['POST']) def signUp():
# read the posted values from the UI
_name = request.form['inputName']
_email = request.form['inputEmail']
_password = request.form['inputPassword']
Once the values are read, we'll simply check if they are
valid and for the time being let's just return a simple
message:
@app.route('/signUp',methods=['POST']) def signUp():
# read the posted values from the UI
_name = request.form['inputName']
_email = request.form['inputEmail']
_password = request.form['inputPassword'] # validate the
received values
if _name and _email and _password:
return json.dumps({'html':'<span>All fields good
!!</span>'}) else:
return json.dumps({'html':'<span>Enter the required
fields</span>'})
Also import json from Flask, since we are using it in the
above code to return json data.
175
from flask import Flask, render_template, json, request
Step 4: Create a Signup request
We'll be using jQuery AJAX to send the signup request to
the Python method. Download and place jQuery inside
PythonApp/static/js and add a link to it from the signup
page. Once jQuery has been included, we'll add a jQuery
POST request when the user clicks the Sign Up button.
So, let's attach the signup button click event as shown:
$(function() {
$('#btnSignUp').click(function()
{
$.ajax({
url: '/signUp',
data: $('form').serialize(), type: 'POST',
success: function(response) { console.log(response);
},
error: function(error) { console.log(error);
}
});
});
});
Save all the changes and restart the server. From the
Sign up page, fill in the details and click Sign Up. Check
the browser console and you should have the below
message:
{"html": "<span>All fields good !!</span>"}
176
Step 5: Call the MySQL Stored Procedure
Once we have the name, email address and password,
we can simply call the MySQL stored procedure to create
the new user.
To connect with MySQL, we'll be using Flask-MySQL,
which is a Flask extension. In order to get started with
Flask-MySQL, install it using pip package manager:

pip install flask-mysql


Import MySQL inside app.py:
from flask.ext.mysql import MySQL Earlier we defined our
app as shown:
app = Flask(_name_ )
Along with that include the following MySQL
configurations: mysql = MySQL()
# MySQL configurations
app.config['MYSQL_DATABASE_USER'] = 'jay'
app.config['MYSQL_DATABASE_PASSWORD'] = 'jay'
app.config['MYSQL_DATABASE_DB'] = 'BucketList'
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
mysql.init_app(app)
First, let's create the MySQL connection:
conn = mysql.connect()
Once the connection is created, we'll require a cursor to
query our stored procedure. So, using conn connection,
create a cursor.
177
cursor = conn.cursor()
Before calling the create user stored procedure, let's
make our password salted using a helper provided by
Werkzeug. Import the module into app.py:
from werkzeug import generate_password_hash,
check_password_hash Use the salting module to create
the hashed password.
_hashed_password =
generate_password_hash(_password) Now, let's call the
procedure sp_createUser:
Now, let's call the procedure
sp_createUser:cursor.callproc('sp_createUser',(_name,_e
mail,_hashed_password))
If the procedure is executed successfully, then we'll
commit the changes and return the success message.
data = cursor.fetchall() if len(data) is 0:
conn.commit()
return json.dumps({'message':'User created successfully
!'}) else:
return json.dumps({'error':str(data[0])})
Save the changes and restart the server. Go to the
signup page and enter the name, email address and
password and click the Sign Up button. On successful
user creation, you'll be able to see a message in your
browser console.
{"message": "User created successfully !"}
178
CHAPTER -9
CONNECTION WITH DATABASE

MySQL database connectivity in Python


For any application it is very important to store
database on a server for easy data access. It is quite
complicated to connect to database remotely because
every service provider doesn’t provide remote access to
mysql database. Here I am using python’s MySQLdb
module for connecting to our database which is at any
server that provide remote access.
Benefits of Python for database programming
There are many good reasons to use Python for
programming database applications:
 Programming in Python is arguably more efficient
and faster compared to other languages.
 Python is famous for its portability.
 It is platform independent.
 Python supports SQL cursors.
 In many programming languages, the application
developer needs to take care of the open and
closed connections of the database, to avoid
further exceptions and errors. In Python, these
connections are taken care of.
 Python supports relational database systems.

179
 Python database APIs are compatible with various
databases, so it is very easy to migrate and port
database application interfaces.
What is MYSQLdb?
MySQLdb is an interface for connecting to a
MySQL database server from Python. It implements the
Python Database API v2.0 and is built on top of the
MySQL C API.
Packages to Install
mysql-connector-python
mysql-python
If you are using anaconda
conda install -c anaconda mysql-python
conda install -c anaconda mysql-connector-python
else
pip install MySQL-python
pip install MySQL-python-connector
Import Package
import MYSQLdb
Arguments required to connect MySQL from Python
You need to know the following detail of the
MySQL server to perform the connection from Python.
 Username – i.e., the username that you use to
work with MySQL Server. The default username
for the MySQL database is a root

180
 Password – Password is given by the user at the
time of installing the MySQL database. If you are
using root then you won’t need the password.
 Host Name – is the server name or Ip address on
which MySQL is running. if you are running on
localhost, then you can use localhost, or it’s IP, i.e.
127.0.0.0
 Database Name – Database name to which you
want to connect. Here we are using Database
named ‘Electronics‘because we have already
created this for our example.
Note: We are using the MySQL Connector Python
module to communicate with MySQL Server.
Python Database Application Programmer’s Interface
DB-API (SQL-API) for Python:
Python DB-API is independent of any database engine,
which enables you to write Python scripts to access any
database engine. The Python DB API implementation for
MySQL is MySQLdb. For PostgreSQL, it supports
psycopg, PyGresQL and pyPgSQL modules. DB-API
implementations for Oracle are dc_oracle2 and
cx_oracle. Pydb2 is the DB-API implementation for DB2.
Python’s DB-API consists of connection objects, cursor
objects, standard exceptions and some other module
contents, all of which we will discuss.

181
Steps to connect MySQL database in Python using
MySQL Connector Python
1. Install MySQL Connector Python using pip.
2. Use the mysql.connector.connect() method of
MySQL Connector Python with required
parameters to connect MySQL.
3. Use the connection object returned by a connect()
method to create a cursor object to perform
Database Operations.
4. The cursor.execute() to execute SQL queries from
Python.
5. Close the Cursor object using a cursor.close() and
MySQL database connection using
connection.close() after your work completes.
6. Catch Exception if any that may occur during this
process.

182
Python Example to connect MySQL Database
To connect the MySQL database, you must know
the database name you want to connect. Run below
query on MySQL console if you have not created any
database in MySQL. Otherwise, you can skip the below
query.
Create Database in MySQL

Create database Electronics;


Below is the last step, i.e. using methods of
MySQL Connector Python to connect MySQL database.
Let see the example now.
import mysql.connector
from mysql.connector import Error
try:
connection = mysql.connector.connect(host='localhost',
database='Electronics',
user='pynative',
password='pynative@#29')
if connection.is_connected():
db_Info = connection.get_server_info()
print("Connected to MySQL Server version ", db_Info)
cursor = connection.cursor()
cursor.execute("select database();")
record = cursor.fetchone()
print("You're connected to database: ", record)
183
except Error as e:
print("Error while connecting to MySQL", e)
finally:
if (connection.is_connected()):
cursor.close() connection.close()
print("MySQL connection is closed")
After connecting to MySQL Server, you should get
below output.

Connected to MySQL Server version 5.7.19

You're connected to database: ('electronics',)


MySQL connection is closed

Understand the Python MySQL Database connection


program import mysql.connector
• This line imports the MySQL Connector Python
module in your program so you can use this module’s API
to connect MySQL.
from mysql.connector import Error
• mysql connector Error object is used to show us
an error when we failed to connect Databases or if any
other database error occurred while working with the
database. Example ACCESS DENIED ERROR when
username or password is wrong.

184
mysql.connector.connect()
• Using this method we can connect the MySQL
Database, this method accepts four required parameters:
Host, Database, User and Password that we already
discussed.
• connect() method established a connection to the
MySQL database from Python application and returned a
MySQLConnection object. Then we can use
MySQLConnection object to perform various operations
on the MySQL Database.
• The Connect() method can throw an exception, i.e.
Database error if one of the required parameters is
wrong. For example, if you provide a database name that
is not present in MySQL, then Python application throws
an exception. So check the arguments that you are
passing to this method.
connection.is_connected()
• is_connected() is the method of the
MySQLConnection class through which we can verify is
our python application connected to MySQL.
connection.cursor()
• This method returns a cursor object. Using a
cursor object, we can execute SQL queries.
• The MySQLCursor class instantiates objects that
can execute operations such as SQL statements.

185
Cursor objects interact with the MySQL server using a
MySQLConnection object.
cursor.close()
• Using the cursor’s close method we can close the
cursor object. Once we close the cursor object, we can
not execute any SQL statement.

connection.close()
• At last, we are closing the MySQL database
connection using a close() method of MySQLConnection
class.
Now you know how to connect to MySQL server
from python let’s proceed with creating a table from
Python.
Python MySQL Create Table
In this section, we will learn how to create a table
in MySQL from Python. In this example, I am creating a
Laptop table under the Electronics database.
import mysql.connector
from mysql.connector import Error
try:
connection = mysql.connector.connect(host='localhost',
database='Electronics',
user='pynative',
password='pynative@#29')

186
mySql_Create_Table_Query = """CREATE TABLE
Laptop (
Id int(11) NOT NULL,
Name varchar(250) NOT NULL,
Price float NOT NULL,
Purchase_date Date NOT NULL,
PRIMARY KEY (Id)) """
cursor = connection.cursor()
result = cursor.execute(mySql_Create_Table_Query)
print("Laptop Table created successfully ")
except mysql.connector.Error as error:
print("Failed to create table in MySQL: {}".format(error))
finally:
if (connection.is_connected()):
cursor.close() connection.close()
print ("MySQL connection is closed")

Laptop Table created successfully


MySQL connection is closed
OUTPUT:

187

You might also like