Block 4
Block 4
Block 4
Programming in C
Indira Gandhi National Open University
School of Computer and Information
Sciences (SOCIS)
and Python
Block
4
ADVANCED FEATURES IN PYTHON
UNIT 13
Classes in Python 317
UNIT 14
Exception Handling in Python Programming 339
UNIT 15
Python-Advance Concepts 350
UNIT 16
Data Access Using Python 361
BLOCK 4 INTRODUCTION
Happy Programming!!
Indira Gandhi
National Open University MCS-201
School of Computer and
Information Sciences PROGRAMMING IN
C AND PYTHON
Block
4
ADVANCED FEATURES IN PYTHON
UNIT 13
Classes in Python 317
UNIT 14
Exception Handling in Python Programming 339
UNIT 15
Python-Advance Concepts 350
UNIT 16
Data Access Using Python 361
PROGRAMME/COURSE DESIGN COMMITTEE
All rights reserved. No part of this work may be reproduced in any form, by mimeograph or any other means,
without permission in writing from the Indira Gandhi National Open University.
Further information on the Indira Gandhi National Open University courses may be obtained from the
University’s office at MaidanGarhi, New Delhi-110 068.
UNIT 13 CLASSES IN PYTHON
Structure
13.0 Introduction to Object-Oriented Paradigms
13.1 Objectives
13.2 Classes and instances
13.3 Classes method calls
13.4 Inheritance and Compositions
13.5 Static and Class Methods
13.6 Operator Overloading
13.7 Polymorphism
13.8 Summary
The reason to define a new class helps in solving the structured application
program in terms of object-oriented programming.In structured programming,
language functions are defined. These functions are used by the users, but the
implementations are hidden by the users. Similarly, a class defines methods and
these methods are used by the users but how these methods are implemented are
hidden from the users. A customized namespace is associated with every class and
objects.
13.1 OBJECTIVES
After going through this unit you will be able to :
317
13.2 CLASSES AND INSTANCES
Classes
Classclass_name:
class_variable1= value_of_variable
class_variable2= value_of_variable
class_variable3= value_of_variable
…
defclass_method1(self,arg1,arg2,…):
method1_code
def class_method2(self,arg1,arg2,…):
method2_code
…
The first class we defined is a Rectangle.
>>>
class Rectangle:
defsetsides (self,x,y):
self.height=x
self.width=y
print('The sides of the rectangle are {} and
{}:'.format(self.height,self.width))
def area(self):
return(self.height*self.width)
def parameter(self) :
return(2*(self.height*self.width))
It’s a convention to write the name of a class with the first character in a capital
letter. But it supportsa small letter also. After writing the name of a class,it must be
preceded by a colon ‘:’. In C++ or Java, we were using pair brackets () but python-
support colon ‘:’ only.The statement is written after the colon ‘:’ will be taken by the
Python interpreter as part of the class document.
When we create a class,a namespace is created for the class Rectangle. This
namespace store all the attributes of the class Rectangle. This namespace will
specify the names of class Rectangle method.
318
Object
In Python, a namespace is created for each class. Each namespace is specified with a
name, and this name is same asclass name—all the attributes of the class use this
namespace for the storage.Thus in our example namespace Rectangle must contain
names of all the class methods.
Object Object1 namespace
The function setsides(),area() and parameter() are defined in the name space. The
syntax of methodwould be:
defsetsides(self, x,y):
# implementation of setsides()
def area():
# implementation of area()
def parameter():
# implementation of parameter()
Instance
Variables that point to the object namespace is called instance variables.
Each instance variable containsa different value for different objects. All the
319
variable define inside the __init__valiable are called as instance variables. And all
the variable defined inside the class but outside the __init__valiable is called class
variables.A class variable is also called as static variables.
When we create an object of the class, the constructors declared in the classenvoke
automatically to which it belongs. In Python, there is a unique method _ _init_ _ to
implement constructor of the class in which it is defined. The first argument of the
method _ _init _ _ must be self. Self is a reference to a class to which this object
belongs.
320
Example 2: Program to call a constructor
When two objects obj1 and obj2 are created __init__ method is called automatically.
Class Methods
Predefined word classis used to Predefined word defis used to describe
describe a class. the method of a class.
Each class statement defines a new type A def statement defines a new function
with a given name. with a given name.
Each class name is preceded by a colon Each method name is preceded by a
‘:’ statement. colon ‘:’ statement.
Example -- class Rectangle: Example -- defsetsides(self):
The class which is inherited is called a base class or parent class or superclass, and
the inheriting class is called the derived class or child class or subclass.For our
reference, we will use the term parent class and child class. A child class acquire all
the properties of the parent class, but the vice versa is not true; parent class can’t
access any features of a child class. The main motto behind the inheritance is code
reusability. Once a code is defined in a class if it is required by another class then by
inheriting the class code can be reused.
Syntax of inheritance:
class<Child Class>:
If this child class inherits the parent class, then the statement will be changed to
class<Child Class> (<Parent class>):
321
Consider aclass Parent containing two methods methodA1() and methodA2() and a
class Child with method methodB1() and methodB2().Child class is defined to be
the subclass of Parent class.Therefore it inherits both the methods methodA1() and
methodA2() of Parent class .
Example 3: Inheritance of a parent class by child class
After inheritance class child contains methods methodB1() and methodB2() as well
as methods of class Parent , methodA1() and methodA2().
322
Multilevel inheritance:
Consider three classes class A,class B and class C
Multiple Inheritance:
Consider two classes, class A with two methods methodA1(),methodA2() and class
B with methodB1() and methodB2(). If another class C inherits both class A and
class B,then class B will inherit the features of class A and B.
323
Example 5: Implementation of multiple inheritance
Since the creation of an object of a class will automatically invoke the constructor of
the class. Here in the example class C1 object will call a constructor of class C1
automatically, and creation of an object of class C2 will call the constructor of class
C2 automatically.
324
Example 7:
If we want to call the init method of the parent class also then we can call it with the
help of keyword super.
Thus when we create an object of the child class, it will call the init method of the
child class first. If we have called super, then it will first call init of the parent class
then it will call int of the child class.
If we have two classes, A and B inherited by class C. If init method is defined in all
three classes then what will happen if call inits of the parent class with the help of
super() keyword? Then which class init method will be called?
325
It will first call the init method of the child class then with the help of super()
keyword it will call the init method of the left parent class that is A in our example.
In multiple inheritances, when we inherit the classes, then the method is executed
based on the order specified, and this is called Method Resolution Order (MRO).
If class A is inherited by class B and Class C and then class D inheriting class B and
class C.
Then according to the Method of Resolution Order (MRO) the orderof execution of
the methods and attributes are: class Dclass Bclass Cclass A
A static variable is that variable whose value remainsthe same for all the objects of
the class. It creates one copy of the variable which is shared by all objects of the
class. To declare a static variable, it must be declared inside the class. No method is
used to declare a static variable it is declared without any method.
To use the static variable, we will use the class name to which this static variable
belong, or we will use the reference of the object. But it is always preferred to use
class name in place of reference of the object.
326
In Python, there are three types of methods: Instance methods, class methods and
static methods.
Instance methods:
In the above example first method __init__ is a constructor of the class. The second
method avg(self )is the instance method of the class.This method contains one
parameter self, which points to the instance of the
classMyClass, 3.0 will be printed as an output.
When the method is called Python, replace the self argument with the instance of the
object.Thus instance method always works on the object.
Class method
327
A class method is a method which is bound to the class and not the object of the
class. A special symbol called a decorator ‘ @’ followed by the keyword
classmethod is used to define the class method.
A class method can be called by the class or by the object of the class to which this
method belongs. The first parameter of the class method is class itself.Thus as an
instance method is used to call the instance variable similarly class method is used to
call class variables.
Static method
Suppose we are looking to a method which is not a concern to the instance variable
neither to the class variable. In that case, we will use a static method.Static method
in Python isused when such methods are called another class or methods are used to
perform some mathematic calculations based on the values received as an argument.
A special symbol called as a decorator ‘@’ is used followed by the
keywordstaticmethod is used to define a static method. Thus a static method can be
invoked without the use of the object of the class.
Thus if we want to work with the variables other then class variable and instance
variable, we will use static.
>>>2+5
[2,3,4,5,6]
IGNOU University
>>int(2)._ _ add(5)
7
>>>[2,3,4]._ _add_ _[5,6]
[2,3,4,5,6]
When we add two objects s1 & s2, it will show an error. Here it is not defined to the
Python interpreter to add two objects. Hence we have to overload operator add.
Example 15: Operator overloading
The comparison operator is not defined to the Python interpreter for the objects.
Hence we have to redefine it or overload it. The function which corresponds to the
symbol greater than ‘>’ is __gt__ (refer to table 13.2). So we have to overload this
operator by defining the function.
330
Example 17: Overloading operator ‘>.’
13.7 POLYMORPHISM
Poly means ‘multiple’ and Morph means ‘forms’. Thus polymorphism is multiple
forms. For example, human being behaves differently in a different environment.
The behaviour of a human in the office is different from his behaviour at home,
which is different from his behaviour with friends at a party.
Duck typing
There is a sentence in the English language “if this is a bird which is walking like a
duck,quacking like a duck and swimming like a duck then that bird is a duck”. It
means if the behaviour of the bird is like a duck, then we can say it a Duck.
X=4
and
X= ‘Mohit’
In the first statement, X is a variable storing integer value. The type of the X is int.
However, in the second statement X= ‘Mohit’ the storage memory taken by the
331
variable is a string. In Python, we can’t specify the type explicitly. During the
runtime, whatever value we are storing in a variable the type is considered
automatically. And this is called Duck Typing principle.
In the above example, the obj is an object of class Animal. When we call the code of
Animal class, we have to pass an object ide. So before passing ide, we have to define
it at the object of Duck class,one more ide for the class dog is defined. At the
moment when we assign ide as an object of Dog class, then it will execute the dog
method.
Example 19:
So it doesn’t matter which class object we are passing the matter is that object must
have executed method. And this is called duck typing.
Operator Loading
Consider A=2 and B=5 are two variables. In a programming language, when we are
performing a+b, it will perform the addition of two integer numbers. If X= ‘Hello’
and Y= ‘Hi’ then x+y will perform addition of two strings.
A=2
B=5
332
>>>print(A+B)
…
7
Python interpretor will take it as print(init.__add__(a,b)), where add() is a method
of the init class.
>>>print(init.__add__(a,b))
7
X= ‘Hello’
Y= ‘Hi’
>>>print (X+Y)
HelloHi
Python interpretor will take print(X+Y) as print(str.__add__(X,Y)), where add()is a
method osstr class.
>>>print(str.__add__(X,Y))
HelloHi
Method Overloading
Consider two classes having methods with the same name, but with a different
number of parameters or different types of parameters, then the methods are called
overloaded. In overloaded methods, the number of arguments is different, or types of
arguments are different.
class student:
marks(a,b)
marks(a,b,c)
here two methods marks are defined one with two parameters and another having
three parameters. These two methods are called method overloading. Python does
not support method overloading.
If there are multiple methods in a class having the same name then Python will
consider only the method which is described at the end of the class.
Method Overriding
In Python, we can’t create the same methods with the same name and the same
number of parameters. But we can create methods of the same method in the classes 333
derived in a hierarchy. Thus the child class redefined the method of the parent class,
and this is called method overriding.
A method which is overridden in a child class can also access the method of its
parent class with the help of keyword super().
334
2. Define method overloading and constructor overloading in Python.
------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------
3.Select the correct output generated from the following program code:
class code1:
def __init__(self,st="Welcome to Python World"):
self.st=st
def output(self):
print(self.st)
obj=code1()
obj.output()
a) The code result an error because constructor are defined with default
arguments
b) Output in not displayed
c) “Welcome to Python World” is printed
d) The code result an error because parameters are not defined in a function
------------------------------------------------------------------------------------------------------
-------------------
------------------------------------------------------------------------------------------------------
-------------------
4.What type of inheritance is illustrated in the following Python code?
class Class1():
pass
class Class2(Class1):
pass
class Class3(Class2):
pass
a) Multilevel inheritance
b) Multiple inheritance
c) Hierarchical inheritance
d) Single-level inheritance
------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------
5. What is polymorphism and what is the main reason to use it?
------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------
13.8 SUMMARY
In this unit, we discussed the concepts of classes and objects. Concept of a
namespace to store object and class in memory is also defined in this unit. Different
types of class methods namely static methods ,instance methods and class
methodsare discussed in this unit.
This unit also focused on inheritance and various types of inheritance: single level
inheritance,multilevel inheritance and multiple inheritances.
In this unit, it is described that the same operator can be used for multiple purposes,
and this is called operator overloading. I list of operators and corresponding
methods, also called as magic methods are also given in the unit.
335
Polymorphism means it is the ability to behave differently in a different situation.
The concept of polymorphism and the implementation of polymorphism with Duck
Typing, Operator loading, Method Overloading and Method Overriding are also
described in the unit.
336
Exception Handling in
UNIT 14 EXCEPTION HANDLING IN Python Programming
PYTHON PROGRAMMING
Structure
14.0 Introduction
14.1 Objectives
14.2 Default Exception Handler
14.3 Catching Exceptions
14.4 Raise an exception
14.5 User Defined Exceptions
14.6 Summary
14.0 INTRODUCTION
Programers always try to write an error-free program, but sometimes a
program written by the programmer generates an error or not execute due to
the error generated in the program. Some times the program code is correct,
but still, it generates an error due to the malicious data is given as an input to
the program. This malicious data may be given by the user or from a file that
causes an error generation during the execution. This type of error generally
occurs when we user server-side programs such as web programming and
gaming servers.
Every developer is writing a code that must not generate an error during
execution. We will study various types of errors that generate during the
program execution or before the execution of the program code.
14.1 OBJECTIVES
After going through this unit, you will be able to :
Example 2
>>> list1=[2, 3, 4, 5, 6]
>>>list1[5]
Example 3.
>>>x+3
Example 4.
>>>int('22.5')
Example 5.
>>> '2'*'3'
In each example syntax is correct, but it goes to an invalid state. These are
runtime errors. In such a situation, Python interpreter generates an exception
for run time errors. This exception generates an object which contains all
information related to the error. In Example 5 the error message displayed:
what is happening and at which line number it is happening (here line number
is one because there is only one statement in the code) and the type of error is
also printed.
340
Exception Handling in
14.3 CATCHING EXCEPTIONS Python Programming
In Python, every exception is class, and these classes are the derived class of
the BaseException class. ThusBaseException class is the topmost class in the
hierarchy of exception classes.
Exception handling using try-except:
try:
risky code(generating exception)
except:
corresponding to risky code(handler)
The code which generates an exception must be written in a block with the
name specify by 'try'and the code which will handle the exception will be
given in 'except' block.
If the three lines code are written in Python
print("IGNOU")
print(87/0)
print("Welcome to the University")
>>>
…
Here the first line will be executed, and IGNOU will be printed when Python
interpreter will execute 2nd lines of the program then it generates an
exception, and the default exception handler will print the error code
"division by Zero"then after the program is terminated. Thus the third line of
the program will not execute. And this is also called as abnormal termination.
If we rewrite the code with the try and except then the code will become
print("IGNOU") 341
Example 1: Implementation of try and except block. Excepti
Python
Here program terminated successfully. We can also print the error message
by modifying the code as:
Example 2:
In the above code, the first line executed successfully and "IGNOU" will be
printed. When the python interpreter executes the print statement "(23/0)" an
exception is raised, and a message given in the print statement will be printed
with the name of the exception "division by zero". After that,the interpreter
executes the last statement, and it will print "Welcome to the University."
If there are more than one except block written in the try block,then the try
block will execute the except block which is responsible for the code.
Example 3: Try block with more than one except block.
342
Exception Handling in
Python Programming
With the help of single except block, we can handle multiple exceptions:
Example 4: Multiple exception handling with a single try block and multiple
except block.
343
Corresponding to each error there will be an except block if corresponding Excepti
Python
except block is not there then a except block written at the end of all except
block is used for the error. And it must be the last except block in all.
Example 5: Implementation of default except for block
Some times exceptions may or may not be raised, and sometimes exceptions
may or may not be handled. Irrespective of both of these we still want to
execute some code. And such codes are written in finally block.
try:
Code with Error
except:
Code to handle error
finally:
Necessary code
Example 6: Implementation of finally block.
344
Exception Handling in
Python Programming
In case of different types of error, we can use a default except for block. This
block is generally used to display normal error messages. The default except
block must be the last block of all except blocks.
345
Excepti
14.4 RAISE AN EXCEPTION Python
Some times user want to generate an exception explicitly to inform that this is
the exception in this code. Such type of exceptions is called as user-defined
exceptions or customized exceptions.
This user-defined exception is a class defined by the user, and this class is
derived from the Exception class. Pythoninterpreter does not have any
information related to the user-defined exception class. Thus, it must be
raised explicitly by the user.
The keywordraise is used to raise the class when it is required.
Example 10: Implementation of user-defined exceptions.
classMyException(Exception):
pass
classMyException(Exception)
def __init__(self,argumnet):
346 self.msg=argumnet
Exception Handling in
Python Programming
A raise statement is used to raise the statement.
Example 11: Program to raise an exception.
14.6 SUMMARY
In this unit, it is defined that a program may produce an error even though the
programmer is writing error-free code. These are the mistakes that change the
behaviour of our program code.
In this unit, it is defined that the default exception handlers are there in
Python. How these exception handler works are described in this unit.
By default, there are exception handlers in Python. But a user can also raise
the exception which is a customized exception, not a language defined
exception.
348
UNIT 15 PYTHON-ADVANCE CONCEPTS
Structure
15.0 Introduction
15.1 Objectives
15.2 Decorators
15.3 Iterators
15.4 Generators
15.5 Co-routines
15.6 Summary
15.0 INTRODUCTION
# In-Built Function
Name = “IGNOU-SOCIS”
print(len(Name))
We also learned to define our own function just to recapitulate lets write our
function to add two numbers
# User-Defined Functions
defadd_two(a,b) :
returna+b
total = add_two(5,4)
print(total)
here add_two(a,b) is the user defined function that takes two numbers as
input and return their sum, which is stored in the varable total, the result of
350 total is printed subsequently.
We also learned the concept of anonymous functions i.e lambda functions or
Lambda expressions, the concept was introduced in python and later it was
adopted by many other languages be it C++ or Java. The advantage of using
the lambda function is that, they can we defined with in the code by writing
few lines and they doesn’t need any name, that’s amazing. One example of
Lambda expressions is sited below, here we will write the lambda expression
equivalent for the User-Defined Function add_two, which is given above
def add(a,b)
returna+b
print(add2(2,3))
here, add2 collects the output of the lambda function, we can see the output
by calling lambda function, by writing print(add2(2,3))
Generally lambda functions are not meant for the user defined functions but
are used to facilitate the working of various built in functions like, map,
reduce, filter, etc.
15.1 OBJECTIVES
15.2 DECORATORS
Modern programming paradigm recommends the technique of code
reusability, where we need to customize the code as per our requirements,
without disturbing the actual functionality of the code which is already
written. To achieve this, python introduced the concept of decorators, this 351
concept is used to enhance the functionality of functions, which are already
written, for example say we have two functions func1() and func2() as given
in the python code given below
# Decorators – to enhance functionality of other functions
deffuncA():
print (‘ this is functionA’)
deffuncB():
print (‘ this is functionB’)
funcA()
funcB()
on executing the above mentioned code by running deco.py we will get
output
this is function1
this is function2
but without disturbing the existing code if we want that along with, “this is
function1” the output should contain the line “this is a wonderful function”,
i.e. to enhance the already existing functionality, we need to exercise the
concept of decorators.
To understand this concept lets re-write the code given above
# Decorators – to enhance functionality of other functions
defdecorator_funcn (any_funcn):
defwrapper_funcn():
print(‘this is a wonderful function’)
any_funcn()
returnwrapper_funcn
deffuncA():
print (‘ this is functionA’)
deffuncB():
print (‘ this is functionB’)
variable = decorator_funcn (funcA)
variable( )
Lets understand the concept of Decorators through the above code. Here,
decorator_funcn is defined to enhance the functionality of any function (may
be funcA or funcnB) from printing “this is function A” to “this is a wonderful
function this is function A” or “this is function B” to “this is a wonderful
function this is function B”.
352
Without altering any line of code of already written functions. To achieve this
a decorator function decorator_funcn is defined, this function takes
any_funcn as input argument, it can be any function may it be funcnA or
funcnB. Inside the decorator_funcn, a wrapper function named
wrapper_funcn() is defined to wrapup the new features over the existing
features of the function taken as argument by the decorator_funcn(). The
body of the wrapper_funcn() includes the additional feature, in our case it is
print(‘this is a wonderful function’) an then the original function passed as an
argument to the decorator_funcn() i.e. any_funcn() is called, and finally the
output of wrapper_funcn is returned. To execute the decorator_funcn, the
decorator_funcn with argument funcA is called and its return value is
collected in variable, then variable() is executed and the output received is
“this is a wonderful function this is function A” or “this is a wonderful
function this is function B”.
Lets run some shortcuts also to work with the concept of decorators, which
involves one more concept of syntactic sugar, where w use @ symbol to use
the decorators before executing any function, i.e. to enhance its functionality.
# Decorators – to enhance functionality of other functions
# @ used for decorators
defdecorator_funcn (any_funcn):
defwrapper_funcn():
print(‘this is a wonderful function’)
any_funcn()
returnwrapper_funcn
@decorator_funcn
deffuncA():
print (‘ this is functionA’)
funcA()
@decorator_funcn
deffuncB():
print (‘ this is functionB’)
funcB()
whenever we use @decorator_funcn before any function the output of that
function preceeds with the output “this is a wonderful function”, later the
output of actual function turnsup. As in this code, calling funcA() leads to
output “this is a wonderful function this is function A” or calling funcB()
leads to output “this is a wonderful function this is function B”.
15.3 ITERATORS
In Python Iterator is an object that can be iterated i.e. an object which will
return data or one element at a time. Iterators exist every where, but they are
implemented well in generators, comprehensions and even in for loops; but
are generally hidden in plain sights. In this section we will try to understand
this concept by exploring the functioning of for loops. In our earlier units we
learned about the concepts of Lists, Tuples, Dictionaries, Sets , Strings etc.,
infact most of these built in containers are collectively called, iterables. An
object is called iterable if we can get an iterator from it..In Python any
iterator object must follow the iterator protocol i.e. the implementation of
iter() and next() functions, the iter function returns an iterator, and an object
is called iterable if we can get an iterator from it (for this we use
iter()function). To understand what are iterables lets understand the
functioning of for loop.
# Iterables
numbers = [1,2,3,4]
fori in numbers :
print (i)
now lets understand how for loop works behind the scenes, firstly the for
loop calls a function called iter() function, this iter function changes the
iterable to iterator i.e. it takes list as argument, so in out case we have
iter(numbers), and this is now an iterator. Subsequently the next function is
called whose argument is this iterator i.e. next(iter(numbers)), as the loop
progresses the next function provides the values from the iterable i.e. the list
numbers in our case, first run provides 1, second run provides 2 and so on. i.e
to see how the for loop works we write the logic of for loop as follows
# Iterables
numbers = [1,2,3,4]
number_iter = iter(numbers)
In this way only the for loop works on any of the iterables i.e. List or Tuple
or String. So, iterables are the python data types which uses the iter and next
functions, but iterator can directly use the next function. The iterables uses
the iter() function to generate iterator and then uses next function but iteartors
don’t use iter function they directly use the next function to get the job done.
In Python any iterator object must follow the iterator protocol i.e. the
implementation of iter() and next() functions, the iter function returns an
iterator, and an object is called iterable if we can get an iterator from it (for
this we use iter()function)
# Iterators
In this section we learned about the concept of Iterators and Iterables, now
we will extend our discussion to Generators, in the next section.
15.4 GENERATORS
Generators are also a kind of iterators, but they are quite memory efficient i.e.
needs very less memory hence helps in improving the performance of any
programme. We learned in last section that iterators involves production of
355
any sequence, the generators are also generating the sequence but their
modus operandi is quite different. Like List say L = [1,2,3,4] is a sequence
but it is an iterable, the generator is also a sequence but it is an iterator not a
iterable. You might be thinking that we already have a mechanism to refer a
sequence i.e. say List, then why do we need a generator. To understand this
we need to understand the memory utilization by list and generator. Say, we
are having a list with many numbers, when we create a list then it will take
some time , secondly these numbers will get stored in to the memory i.e
memory usage will also be on higher side. But, I the case of Generators, at
one time only one number is generated and the same is used for further
processing, i.e. both time and memory space are saved, they are
comparatively quite less in case of generators. So, while processing the list
entire list is loaded and processed, but in generators one by one elements are
generated and are processed accordingly.
Now you might be thinking that, when to use lists then ? the answer is that
when you need to use your sequence again and again (may be to perform
some functionality) then list is the best option, but when you simply need to
use the sequence for one time only, then its better to go for generators, you
will understand this, later in this section only.
Lets learn how to write a generator, for this you may use two techniques, i.e.
you may use generator function or generator comprehension, as technique to
develop your own generator. Generator comprehension is the technique
which is quite similar to list comprehension, which is used to generate list.
defnums(n)
fori in range (1, n+1) : # n+1 because for loop goes upto n-1 th term
print(i)
this will print the numbers from 1 to 10, here function is not returning any
thing, but simply printing the numbers. This was quite simple, as you leraned
in your earlier units, but if we need to develop our generator to do the same
task then you need to replace the print command with yield keyword, and the
code will be
defnums(n)
356
fori in range (1, n+1) : # n+1 because for loop goes upto n-1 th term
yield(i)
nums(10)
This yield keyword will create a generator, on executing this code nothing
will be printed, but if in place of nums(10) i.e. the last line of the above code,
we write print(nums(10)) then on execution you will come to know a
generator object nums is produced.
Now, lets understand how a generator function works, for this lets again
explore the functioning with for loop, refer to the code given below
defnums(n)
fori in range (1, n+1) : # n+1 because for loop goes upto n-1 th term
yield(i)
numbers = nums(10)
fornum in numbers :
print(num)
defnums(n)
fori in range (1, n+1) # n+1 because for loop goes upto n-1 th term
yield(i)
fornum in numbers :
print(num)
357
fornum in numbers :
print(num)
execution of the second for loop will not produce any result because the
generaors generates the numbers one by one and they are not retained in to
the memory as in the case of lists. So the execution of first for loop will
produce a sequence from 1 to 10, i.e. the numbers are placed in to the
memory one by one, which is there after refreshed, so past instance is lost.
Thus the execution of second for loop has no databecausenums(n) function
only exists before first for loop not after it. If the nums(n) also exists after
first for loop then data for second for loop is also available.
defnums(n)
fori in range (1, n+1) # n+1 because for loop goes upto n-1 th term
yield(i)
fornum in numbers :
print(num)
fornum in numbers :
print(num)
If we re-execute the above code with list and not generator i.e. with
list(nums(10)) and not nums(10), then the sequence from 1 to 10 will be
printed twice because the content of List persists in the memory, thus the
execution of both for loops will produce a separate sequence from 1 to 10.
15.5 CO-ROUTINES
We learned about functions in our earlier units, and we knew that they are
also referred as procedures, subroutines, sub-processes etc. In fact a function
is packed unit of instructions, required to perform certain task. In a complex
function the logic is to divide its working into several self-contained steps,
which themselves are functions, such functions are called subroutines or
358
helper functions, these subroutines have single entry point. The coordination
of these subroutines is performed by the main function.
Main Function
Subroutines Co-Routines
1. Co-routines have many entry 1. Subroutines have single entry
points for suspending and point for suspending and
resuming execution. resuming execution
2. Co-routine can suspend its 2. Subroutines can’t suspend its
execution and transfer control execution and transfer control to
to other co-routine and can other subroutine and can resume
resume again execution from again execution from the point it
the point it left off. left off.
3. In Co-routines there is no
main function to call co- 3. In Subroutines there is main
routines in particular order function to call subroutines in
and coordinate the results. particular order and coordinate
the results.
From the above discussion it appears that co-routines are quite similar to
threads, both seems to do the same job. But, there is a difference in between
the thread and the Co-routine, in case of threads, it is the operating system i.e.
the run time environment that performs switching in accordance with
scheduler. But, in the case Co-routines the decision making for switching is
performed by the programmer and programming language. In co-routines the
cooperative multitasking, by suspending and resuming at set points is under
the control of programmer.
359
In Python, co-routines are similar to generators but with few extra methods
and slight change in how we use yield statement. Generators produce data for
iteration while co-routines can also consume data. A generator is essentially a
cut down (asymmetric) coroutine. The difference between a coroutine and
generator is that a coroutine can accept arguments after it's been initially
called, whereas a generator can't.In Python the Co-routines are declared with
the async or await syntax, it is the preferred way of writing asyncio
applications.
Example, the following snippet of code (requires Python 3.7+) prints “hello”,
waits 1 second, and then prints “world”:
importasyncio
asyncdef main() :
print (‘hello’)
awaitasyncio.sleep(1)
print (‘world’)
asyncio.run(main())
Example - The following snippet of code will print “hello” after waiting for
1 second, and then print “world” after waiting for another 2 seconds:
importasyncio
import time
asyncdeftell_after(delay_time, what_to_tell):
awaitasyncio.sleep(delay_time)
print(what_to_tell)
360
asyncdef main():
print(f”started at {time.strftime(‘%X’)}”)
awaittell_after(1,’hello’)
awaittell_after(2,’world’)
print(f”finished at {time.strftime(‘%X’)}”)
asyncio.run(main())
Expected output:
Started at 17:11:52
hello
world
finished at 17:11:55
asyncdef main() :
task1 = asyncio.create_task(tell_after(1,’hello’))
task2 = asyncio.create_task(tell_after(2,’world’))
print(f”started at {time.strftime(‘%X’)}”)
# wait until both tasks are completed (should take around 2 seconds)
awaittell_after(1,’hello’)
awaittell_after(2,’world’)
print(f”finished at {time.strftime(‘%X’)}”)
Expected Output:
Started at 17:24:32
hello
world
361
finished at 17:24:34
Note that expected output now shows that the snippet runs 1 second faster
than before
15.6 SUMMARY
In this you learned about decorators, a way to enhance the functionality of
already written functions, which ia useful concept for code reusability.
Further, the discussion was enhanced to the concepts of iterables and
iterators, through the understanding of the execution of For loop. There after
the concept of generators was discussed where the concept of yield keyword
and print command were mentioned, the comparative analysis between the
generator and a function also clars the concept of performance improvement
in python programming. Finally, the understanding of co-routines cleared the
learners understanding towards the cooperative multitasking.
362
Data Access
UNIT 16 DATA ACCESS USING PYTHON Using Python
Structure
16.1 Introduction
16.2 Database Concepts
16.3 Creating Database
16.4 Querying Database
16.5 Using SQL to get more out of Database
16.6 CSV files in Python
16.7 Summary
16.8 Solutions to Check your Progress
16.1 INTRODUCTION
Python is the most popular high-level programming language that can be used for
real-world programming. It is a dynamic and object-oriented programming language
thatcan be used in a vast domain of applications, especially data handling. Python
was designed as a very user-friendly general-purpose language with a collection of
modules and packages and gained popularity in the recent times.Wheneverwe think
of machine learning, big data handling, dataanalysis, statisticalalgorithms, python is
the only name which comes first in our mind. It is a dynamically typed language,
which makes it highly flexible. Furthermore, creating a database and linking of the
database is very fast and secure.It makes the program to compile and run in an
extremely flexible environment can support different styles of programming,
including structural and object-oriented. Its ability to use modular components that
were designed in other programming languages allows a programmer to embed the
code written in python for any interface. Due to its enormous features, python has
become the first choice of learners in the field of data science and machine
learning.This unit is specially designed for beginners to create and connect database
in python. You will learn to connect data through SQL as well as CSV files with the
help of real dataset (PIMA Indian Dataset), used most commonly by researchers and
academia in data analysis.
361
for day to day transactions by hitting the database without worrying about the load
on the processor as it is very fast and consumes power to run the desired query only.
The nature of serendipity in the Python environment is an example of data analysis
in machine learning and datascience.Python is a bucket of packages and libraries
which enables us to perform data classification,prediction and analysis in a very
efficient way.
Now the question is, the database is stored in different forms in different software's
like Oracle, MS-Access etc.,so how any programming language like python can
accessit.For that, we need to learn about database connectivity in python. In this unit
you will learn how to create and connect database in python using MySQL along
with basic knowledge of database and its features.
Database Management system DBMS
Database management systems are software that serves the special purpose of
storing,managing, extracting and modifying data from a database. It contains a set of
programs that allows access to data contained in a database. DBMS also interfaces
with application programs so that data contained in the database can be used by
multiple application and users. It handles all access to the database and responsible
for applying the authorization checks and validation procedures. It acts as a bridge
between users and data, and the bridge is crossed using special query language like
SQL, MYSQL wrote in high-level languages.
A typical DBMS has users with different rights and permissions who use it for
different purposes. Some users retrieve data and some back it up. The users of a
DBMS can be broadly categorized as follows −
END USERS : who actually uses all benefits of DBMS .It could be an
individual or an organisation
DBMS
ADMINISTRATORS: maintain the DBMS and are responsible for
administrating the database.
DESIGNERS:who work to design the layout of the database .they
identify entity,relations and views
Limitations of Database:
DBMS is designed on the basis of architecture. This design can be of various forms
centralized, client-server systems, parallel systems, distributed and hierarchical. The
architecture of a DBMS can be seen as either a single tier or multi-tier. An n-tier
363
architecture divides the whole system into related but independent n modules, which
can be independently modified, altered, changed, or replaced. Generally, three-tier
architecture is followed, which consist of three layers:
Once the architecture of DBMS is designed, then the next phase is modelling. The
data model represents the logical structure of the database. It decides how the data
will be stored, connected to each other, processed and stored. Many data models
were proposed to store the data like network model, hierarchical model and
relational model where each new model was invented with improved features. The
first model was based on Flat File, where data is stored in a single table in a file,
which could be a plain text file or binary file. It is suitable only for a small amount
of data. Records follow a uniform format, and there are no structures for indexing or
recognizing relationships between records. Later on, the concept of relation is
introduced where data is stored in the form of multiple tables and tables are linked
using a common field.It is suitable for handling medium to a large amount of data.
This is the most promising model ever implemented for DBMS solutions. So,let us
see in detail about the structure and features of the relational model.
Entity-Relationship Model
364
Attributes: Entities are represented by means of having properties called Data Access
Using Python
attributes. Every attribute is defined by its set of values called domain. For
example, in an employee database, an employee is considered as an entity. An
employee has various attributes like empname, age, department, salary etc.
Relationship: describes the logical association among entities. These
relationships are mapped with entities in various ways, and the number of
association between two entities defines mapping cardinalities. Mapping
cardinalities are one to one, one to many,many to many and many to one.For
example, a doctor can have many patients in a database which shows one to
many relationships.
These E-R diagrams are represented by special symbols which are listed below:
Rectangle: Represents Entity sets.
Ellipses: Attributes
Diamonds: Relationship Set
Lines: They link attributes to Entity Sets and Entity sets to Relationship Set
Now let us take an example of an organization where they want to design an entity
relationship between various departments and their employees. So, In the following
E-Rdiagram we have taken two entities Employee and Department and showing
their relationship. The relationship between Employee and Department is many to
one as a department can have many employees however aemployeecannotwork in
multiple departments at the same time. Employee entity has attributes such as
Emp_Id, Emp_Name&Emp_Addr and Department entity has attributes such as
Dept_ID&Dept_Name.
Works I n
Employee Department
365
Structure of Database
Schema
Data
The Schema is the structure of data, whereas the Data are the "facts". Schema can
be complex to understand to begin with, but really indicates the rules which the Data
must obey. Let us consider a real-world scenario where we want to store facts about
employees for an organization. Such facts may include Emp_ name, Emp_address,
Date of Birth, and salary. In a database, all the information on all employees would
be held in a single storage "container", called a table. This table is a tabular object
like a spreadsheet page, with different employees as the rows, and the facts (e.g.
their names) as columns..Let's call this table EMP, and it could look something like:
Table EMP
From this information, the Schema would define that EMP has four components,
"Emp_name","Emp_address","Date of Birth","Salary". As database designers, we
can call the columns what we like, a meaningful name helps. In addition to the
name, we want to try and make sure that people don't accidentally store a name in
the DOB column, or some other silly error. We can say things like:
TABLE EMPLOYEE
Primary key COLUMNS OR FIELD OR ATTRIBUTES
,Noida
Row Or record or Tuple 4
104 Tripti 52,Sector-85 ,Noida 91000 8.11.1980
367
The relational model indicates that each row in a table is unique. If you allow
duplicate rows in a table, then there's no way to uniquely address a given row via
programming. This creates all sorts of ambiguities and problems that are best
avoided. You guarantee uniqueness for a table by designating a primary key—a
column that contains unique values for a table. Each table can have only one primary
key, even though several columns or combination of columns may contain unique
values.
All columns (or combination of columns) in a table with unique values are referred
to as candidate keys, from which the primary key must be drawn. All other
candidate key columns are referred to as alternate keys. Keys can be simple or
composite. A simple key is a key made up of one column, whereas a composite key
is made up of two or more columns.
The relational model also includes concepts of foreign keys, which are primary keys
in one relation,that are kept as non-primary key in another relation ,to allow for the
joining of data.
Now let us see how to choose the primary key in the table.Consider the Product
table given below:
Relating to the employee Table presented in the last section, Now define a second
table, order as shown in the figure provided below:
PRODUCT TABLE
In a database, we can define the structure of the data and manipulate the data using
some commands.The most common data manipulation language is SQL. SQL
commands are instructions used to communicate with the database to perform a
specific task that works with data. SQL commands can be used not only for
searching the database but also to perform various other functions like, for example,
you can create tables, add data to tables, or modify data, drop the table, set
permissions for users. SQL commands are grouped into four major categories
depending on their functionality:
368
Data Definition Language (DDL) - These SQL commands are used for Data Access
Using Python
creating, modifying, and dropping the structure of database objects. The
commands are CREATE, ALTER, DROP, RENAME, and TRUNCATE.
Data Manipulation Language (DML) - These SQL commands are used for
storing, retrieving, modifying, and deleting data. These commands are
SELECT, INSERT, UPDATE, and DELETE.
Transaction Control Language (TCL) - These SQL commands are used for
managing changes affecting the data. These commands are COMMIT,
ROLLBACK, and SAVEPOINT.
Data Control Language (DCL) - These SQL commands are used for
providing security to database objects. These commands are GRANT and
REVOKE.
Some of the commonly used datatyes in SQL AND MYSQL are listed below:
Data type Description
CHAR(n) Character string, fixed length n ,with a maximum size of 2000
characters . Values of this data type must be enclosed in single
quotes '‘ .the storage size of the char value is equal to the
maximum size for this column i.e. nColumns , that will not be
used for arithmetic operations usually are assigned data types of
char.
CHARACTER Variable length character string , can store upto 4000 characters .
,VARYING(n) varchar is a variable-length data type, the storage size of the
or varchar value is the actual length of the data entered, not the
VARCHAR(n) maximum size for this column i.e .n
,VARCHAR2(n)
INT A normal-sized integer that can be signed or unsigned. If signed,
the allowable range is from -2147483648 to 2147483647.
DATE This data type allows to store valid date type data from January
1, 4712 BC to December 31, 4712 AD with standard oracle data
format DD-MM-YY.
TIME Stores the time in a HH:MM:SS format.
TIMESTAMP A timestamp between midnight, January 1st, 1970 and sometime
in 2037. This looks like the previous DATETIME format, only
without the hyphens between numbers; 3:30 in the afternoon on
December 30th, 1973 would be stored as 19731230153000
(YYYYMMDDHHMMSS ).
SQL COMMANDS
CREATE
It is the mostly used, Data definition command in SQL . It is used to create table
index or view. This command describes the layout of the table. The create statement
specifies the name of the table , names and types of each column of the table . Each
table must have at least one column. The general syntax for creating table is shown
below:
369
Syntax for CREATE TABLE is:
CREATE TABLE <table name>
(<attribute name><data type>[<size>]<coloumn constraint>,
(<attribute name><data type>[<size>]<coloumn constraint>,
…………………………………………………………………..)
where
<table name > : It is the name, of the table to be created
<attribute name > : It is the name of the column heading or the field in a table.
<data Type> : It defines the type of values that can be stored in the field or the
column of the table .
Example:
CREATE TABLE Student
( roll_no number (5),
name char(20),
birth_data date ) ;
ALTER
Alter is a DDL command in SQL which is used to perform the following operations
in table.
1. Add column to the existing table
4. To modify the column of the existing table.
Example :
This will modify the column called prod_name to be a data type of varchar2(100)
and force the column to not allow null values.
370
ALTER TABLE table_name Data Access
DROP COLUMN column_name; Using Python
Example:
This will drop the column called prod_name from the table called Product.
Syntax :
Example:If you want to insert a row to the employee table, the query would be like,
NOTE: When adding a row, only the characters or date values should be enclosed
with single quotes.
371
Solution
UPDATE
UPDATE table_name
SET column1=value, column2=value2,...
WHERE <condition>
Remember : The WHERE clause in the UPDATE syntax specifies which record or
records that should be updated. If you remove the WHERE clause, all records will
be updated by default.
Example :Modify the marks of Anuja from 80 to 87 which was entered wrongly.
UPDATE student
SET marks=87
372
DELETE Statement Data Access
Using Python
The DELETE statement allows you to delete a single record or multiple records or
all records from the table.
Syntax:
Example :
This would delete all records from the Student table where the course opted by
students is MCA
The DB API provides a minimal standard for working with databases using Python
structures and syntax wherever possible. This API includes the following −
ANACONDA
JUPYTER /SPYDER
NOTEBOOK
MYSQL.CONNECTOR
MYSQL SERVER
5.1.33
Fig: shows all applications required to install for setting up database connectivity in
python with mysql.
NOTE: Anaconda is a python and R distribution that aims to provide everything you
need:
https://downloads.mysql.com/archives/community/
2. During the installation setup, you will be prompted for a "root" password in the
server configuration step.
3. Launch workbench, at the home page, setup a new connection profile with the
configuration (Connection method: Standard (TCP/IP), Hostname:
374
127.0.0.1,Port:3306,Username: root, Password: yourpassword) and test your Data Access
connection. Using Python
4. Double click on your local instance and it should bring you the schemas view
where you can see all your databases and tables.
The following screenshot gives a glimpse of the screen you may get:
Mysql.connector
1. Download Mysql API, exe file and install it.(click here to download)
Connect to MySql
2. In the notebook, create a new python file. In the first cell, write the following
code to test the mysql connection.
importmysql.connector
print(mydb)
3. If successful, you should get an object returned with its memory address
<mysql.connector.connection_cext.CMySQLConnection object at 0x10b4e1320>
376
Data Access
Using Python
Cursor object: The MySQLCursor class instantiates objects that can execute
operations such as SQL statements. Cursor objects interact with the MySQL
server using a MySQLConnection object.
How to create a cursor object and use it import mysql.connector
Example
Following is the example of connecting with MySQL and display the version of the
database.
importmysql.connector
mycursor.execute("SELECT VERSION()")
While running this script, it is producing the following result in my machine. (It
could change in your system)
Database version : ('5.1.33-community',)
In the above code we are creating a cursor to execute the sql query to print
database version next line executes the sql query show databases and store
result in mycursor as collection ,whose values are being fetched in data. On
execution of above program cursor will execute the query and print version of
database shown.
Creating Database
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="admin123",charset=’utf8’
)
mycursor = mydb.cursor()
mycursor.execute("CREATEDATABASETEST_DB")
Next, we will try to connect to this new database.
importmysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="admin123",charset=’utf8’,database= TEST_DB
)
1. How will you create a database in mysql .Write the command to create a
database named as Start_db.
2. Name the four arguments required to set connection with
mysql.connector.connect.
Creating Table
Once a database connection is established, we are ready to create tables or records
into the database tables using the execute method of the created cursor.
Example
To create table EMPLOYEE in database TEST_DB with following data
FIRST_NAME,LAST_NAME,AGE,SEX,INCOME.
378
Data Access
Using Python
INSERT Operation
Insert is used to feed the data in the table created. It is required when you want to
create your records into a database table.
379
Example
The following example, executes SQL INSERT statement to create a record into
EMPLOYEE table –
UPDATE OPERATION
UPDATE Operation on any database means to update one or more records, which
are already available in the database. Here is an example to show where we run the
query to updates all the records having SEX as 'M'. Here, we increase the AGE of
all the males by one year.
Example
The above code will update the age of all the male employees in the table
EMPLOYEE.
DELETE Operation
DELETE operation is required when you want to delete some records from your
database. Following example show the procedure to delete all the records from
EMPLOYEE where AGE is more than 20.
380
Data Access
Using Python
The above code will delete all the records from the table EMPLOYEE where age is
greater than 20.
Please refer to the above examples with their screenshots which will help in better
understanding.
1. Write the syntax of the following commands in SQL : insert, update, delete
READ Operation
READ operation on any database means to fetch some useful information from the
database.
Once our database connection is established, you are ready to make a query into
this database. You can use either fetchone() method to fetch a single record
or fetchall() method to fetch multiple values from a database table.
fetchall() − It fetches all the rows in a result set. If some rows have already
been extracted from the result set, then it retrieves the remaining rows from the
result set.
381
fetchone() − It fetches the next row of a query result set. A result set is an
object that is returned when a cursor object is used to query a table.
rowcount − This is a read-only attribute and returns the number of rows that
were affected by an execute() method.
fetchall()
The method fetches all (or all remaining) rows of a query result set and returns a list
of tuples. If no more rows are available, it returns an empty list. The following
procedure queries all the records from the EMPLOYEE table
fetchone()
To fetch one record from the table fetchone is used in the same manner as fetchall()
is shown in above code. This method retrieves the next row of a query result set and
returns a single sequence, or None if no more rows are available. By default, the
returned tuple consists of data returned by the MySQL server, converted to Python
objects.
record = cursor.fetchone()
rowcount()
Rows affected by the query. We can get a number of rows affected by the query by
using rowcount. We will use one SELECT query here.
382
Data Access
Using Python
In the above code buffered=True. We have used mycursor as buffered cursor which
fetches rows and buffers them after getting output from MySQL database. It is used
as an iterator, but there is no point in using buffered cursor for the single record as in
such case if we don't use a buffered cursor, then we will get -1 as output from
rowcount.
Database transaction represents a single unit of work. Any operation which modifies
the state of the MySQL database is a transaction. Python MySQL Connector
provides the following method to manage database transactions.
The following code shows the role of commit, rollback and auto-commit while
performing SQL queries in python.
383
In the above code if update query is successfully executed then commit() method
will be executed otherwise,anexception error part will be executed.Rollback issued
to revert of update query if happened due to error.Finally, we are closing cursor as
well as connection. Here the purpose of conn. Auto-commit = false is to activate the
role of rollback else rollback will not work.
CSV files are ordinarily made by programs that handle a lot of information. Itis just
like a text file in a human-readable format which is used to store tabular data in a
spreadsheet or database.They are a helpful method to send out information from
spreadsheets and data sets just as import or use it in different projects. For instance,
you may trade the aftereffects of an information mining project to a CSV document
and afterwards import that into a spreadsheet to dissect the information, create charts
for an introduction, or set up a report for distribution. The separator character of
CSV files is called a delimiter.Default delimiter is comma (,) others are tab (\t), (: ),
(;) etc.
384
CSV documents are exceptionally simple to work for database connectivity. Any Data Access
Using Python
language that underpins text record info and string control (like python) can work
with CSV documents straightforwardly.
Some of the features of CSV are highlighted below, which makes it exceptionally
useful in the analysis of the large dataset.
Let us see how to import CSV file in python.In this unit, we will be using pandas
module in python to import the CSVfile.Pandas is a powerful Python package that
can be used to perform statistical analysis. In this chapter, you'll also see how to use
Pandas to calculate stats from an imported CSV file.
CASE STUDY OF PIMA INDIAN DATASET
The dataset used here isPima Indian diabetes data for learning purpose.Pima Indian
diabetes dataset describes the medical records for Pima Indiansand whether or not
each patient will have an onset of diabetes within given years.
Fields description follow:
preg = number of times pregnant
plas = Plasma glucose concentration a 2 hours in an oral glucose tolerance test
pres = Diastolic blood pressure (mm Hg)
skin = Triceps skin fold thickness (mm)
test = 2-Hour serum insulin (mu U/ml)
mass = Body mass index (weight in kg/(height in m)^2)
pedi = Diabetes pedigree function
age = Age (years)
class = Class variable (1:tested positive for diabetes, 0: tested negative for diabetes)
It consists of above mentioned eight features, and one class variableandis very
commonly used in the research of diabetes.Given below are the steps and the code to
import CSV file in python.
385
Step2. Capture the file path where CSV file is stored (don't forget to include
filename and file extension ).
import pandas as pd
df = pd.read_csv ('F:\pimaindiansdiabetescsv\pima-indians-diabetes.csv')
print (df)
Note: #read the csv file (put 'r' before the path string to address any special
characters in the path, such as '\'). Don't forget to put the file name at the end of the
path + ".csv".
This path is taken for reference; it will change as per the location of the file.
Output
The above output consists of 769 rows and ninecolumns.TheCSV file is just like
excel file which consists of data in tabular form arranged as rows and columns.But
the columns are without heading.In the next example, column headings are provided
using
Any userdefined These are the field names or
name column names
Finally you will learn to calculate the following statistics using the Pandas package:
Mean
386 Total sum
Maximum Data Access
Minimum Using Python
Count
Median
Standard deviation
Variance
Simply functions for each of the above mentioned stats is available in pandas
package which can be easily applied in the following manner.
import pandas as pd
colnames=['pre','pgc','dbp','tsf','2hs','bmi','dpf','age','class']
df=pd.read_csv('F:\pimaindiansdiabetescsv\pima-
indiansdiabetes.csv',names=colnames)
mean1 = df['age'].mean()
sum1 = df['age'].sum()
max1 = df['age'].max()
min1 = df['age'].min()
count1 = df['age'].count()
median1 = df['age'].median()
std1 = df['age'].std()
var1 = df['age'].var()
# print block 1
print ('Mean Age: ' + str(mean1))
print ('Sum of Age: ' + str(sum1))
print ('Max Age: ' + str(max1))
print ('Min Age: ' + str(min1))
print ('Count of Age: ' + str(count1))
print ('Median Age: ' + str(median1))
print ('Std of Age: ' + str(std1))
print ('Var of Age: ' + str(var1))
Output:
Mean Age: 33.240885416666664
Sum of Age: 25529
Max Age: 81
Min Age: 21
Count of Age: 768
Median Age: 29.0
Std of Age: 11.76023154067868
Var of Age: 138.30304589037365
We have learnt how to calculate simple stats using Pandas and import any dataset
for machine learning purpose.
16.7 SUMMARY
A database is an organized,logical collection of data.It is designed to facilitate
the access by one or more applications programs for easy access and analysis
and to minimize data redundancy.
DBMS is a software system that enables you to store, modify, and extract
information from a database. It has been designed systematically so that it can
be used by multiple applications and users
A relational database is a collection of related tables
An entity is a person place or things or event.
Tables in the database are entities.
An attribute is a property of entity. Attributes are columns in the table.
A relationship is the association between tables in the database.
Each columns of the table correspond to an attribute of the relation and is
named
Fields : Columns in the table are called fields or attributes.
Rows of the relation is referred as tuples to the relation . A tuple or row
contains all the data of a single instance of the table such as a employee number
201.
Records : Rows in a table often are called records . Rows are also known as
tuples.
The values for an attribute or a column are drawn from a set of values known as
domain.
Primary key : An attribute or a set of attributes which can uniquely identify
each record (tuple) of a relation (table). All values in the primary key must be
unique and not Null, and there can be only one primary key for a table.
Foreign Key : An attribute which is a regular attribute in one table but a
primary key in another table.
Mysql.connector is an interface for connecting to a MySQL database server
from python
connect() is a method used for creating a connection to our database.
Connect has four arguments:hostname,username,password,databasename .
fetchall() − It fetches all the rows in a result set. If some rows have already
been extracted from the result set, then it retrieves the remaining rows from the
result set.
388
fetchone() − It fetches the next row of a query result set. A result set is an Data Access
Using Python
object that is returned when a cursor object is used to query a table.
rowcount − This is a read-only attribute and returns the number of rows that
were affected by an execute() method.
Commit,rollback and autocommit are database transactions commands.
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="admin123",charset=’utf8’
)
mycursor = mydb.cursor()
mycursor.execute("CREATEDATABASEStart_db")
Next, we will try to connect to this new database.
importmysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="admin123",charset=’utf8’,database= Start_db)
1. Syntax of Insert :
INSERT INTO TABLE_NAME (column1, column2, column3,...columnN)
389
VALUES (value1, value2, value3,...valueN);
Syntax of Update:
UPDATE table_name
SET column1 = value1, column2 = value2...., columnN = valueN
WHERE [condition];
Syntax of Delete:
DELETE FROM table_nameWHERE [condition];
2. importmysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="myusername",
passwd="mypassword"
)
mycursor = mydb.cursor()
mycursor.execute("CREATE DATABASE mydatabase")
392