Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Python Unit 4

Download as pdf or txt
Download as pdf or txt
You are on page 1of 89

UNIT IV

Python - Files I/O


Printing to the Screen
The simplest way to produce output is using the print statement where you can pass zero or more expressions
separated by commas. This function converts the expressions you pass into a string and writes the result to
standard output as follows −

Live Demo
#!/usr/bin/python

print "Python is really a great language,", "isn't it?"

This produces the following result on your standard screen −

Python is really a great language, isn't it?

Reading Keyboard Input


Python provides two built-in functions to read a line of text from standard input, which by default comes from
the keyboard. These functions are −

• raw_input
• input

The raw_input Function


The raw_input([prompt]) function reads one line from standard input and returns it as a string (removing the
trailing newline).

#!/usr/bin/python

str = raw_input("Enter your input: ")


print "Received input is : ", str

This prompts you to enter any string and it would display same string on the screen. When I typed "Hello
Python!", its output is like this −

Enter your input: Hello Python


Received input is : Hello Python

The input Function


The input([prompt]) function is equivalent to raw_input, except that it assumes the input is a valid Python
expression and returns the evaluated result to you.

237
#!/usr/bin/python

str = input("Enter your input: ")


print "Received input is : ", str

This would produce the following result against the entered input −

Enter your input: [x*5 for x in range(2,10,2)]


Recieved input is : [10, 20, 30, 40]

Opening and Closing Files


Until now, you have been reading and writing to the standard input and output. Now, we will see how to use
actual data files.

Python provides basic functions and methods necessary to manipulate files by default. You can do most of the
file manipulation using a file object.

The open Function


Before you can read or write a file, you have to open it using Python's built-in open() function. This function
creates a file object, which would be utilized to call other support methods associated with it.

Syntax

file object = open(file_name [, access_mode][, buffering])

Here are parameter details −

• file_name − The file_name argument is a string value that contains the name of the file that you want
to access.
• access_mode − The access_mode determines the mode in which the file has to be opened, i.e., read,
write, append, etc. A complete list of possible values is given below in the table. This is optional
parameter and the default file access mode is read (r).
• buffering − If the buffering value is set to 0, no buffering takes place. If the buffering value is 1, line
buffering is performed while accessing a file. If you specify the buffering value as an integer greater
than 1, then buffering action is performed with the indicated buffer size. If negative, the buffer size is
the system default(default behavior).

Here is a list of the different modes of opening a file −

Sr.No. Modes & Description

r
1 Opens a file for reading only. The file pointer is placed at the beginning of the file. This is the default
mode.

rb
2 Opens a file for reading only in binary format. The file pointer is placed at the beginning of the file.
This is the default mode.

238
r+
3
Opens a file for both reading and writing. The file pointer placed at the beginning of the file.

rb+
4 Opens a file for both reading and writing in binary format. The file pointer placed at the beginning of
the file.

w
5 Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a
new file for writing.

wb
6 Opens a file for writing only in binary format. Overwrites the file if the file exists. If the file does not
exist, creates a new file for writing.

w+
7 Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does
not exist, creates a new file for reading and writing.

wb+
8 Opens a file for both writing and reading in binary format. Overwrites the existing file if the file exists.
If the file does not exist, creates a new file for reading and writing.

a
9 Opens a file for appending. The file pointer is at the end of the file if the file exists. That is, the file is
in the append mode. If the file does not exist, it creates a new file for writing.

ab
10 Opens a file for appending in binary format. The file pointer is at the end of the file if the file exists.
That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.

a+
Opens a file for both appending and reading. The file pointer is at the end of the file if the file exists.
11
The file opens in the append mode. If the file does not exist, it creates a new file for reading and
writing.

ab+
Opens a file for both appending and reading in binary format. The file pointer is at the end of the file
12
if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for
reading and writing.

The file Object Attributes


Once a file is opened and you have one file object, you can get various information related to that file.

Here is a list of all attributes related to file object −

Sr.No. Attribute & Description

file.closed
1
Returns true if file is closed, false otherwise.

239
file.mode
2
Returns access mode with which file was opened.

file.name
3
Returns name of the file.

file.softspace
4
Returns false if space explicitly required with print, true otherwise.

Example
Live Demo
#!/usr/bin/python

# Open a file
fo = open("foo.txt", "wb")
print "Name of the file: ", fo.name
print "Closed or not : ", fo.closed
print "Opening mode : ", fo.mode
print "Softspace flag : ", fo.softspace

This produces the following result −

Name of the file: foo.txt


Closed or not : False
Opening mode : wb
Softspace flag : 0

The close() Method


The close() method of a file object flushes any unwritten information and closes the file object, after which no
more writing can be done.

Python automatically closes a file when the reference object of a file is reassigned to another file. It is a good
practice to use the close() method to close a file.

Syntax

fileObject.close()

Example
Live Demo
#!/usr/bin/python

# Open a file
fo = open("foo.txt", "wb")
print "Name of the file: ", fo.name

240
# Close opend file
fo.close()

This produces the following result −

Name of the file: foo.txt

Reading and Writing Files


The file object provides a set of access methods to make our lives easier. We would see how to
use read() and write() methods to read and write files.

The write() Method


The write() method writes any string to an open file. It is important to note that Python strings can have binary
data and not just text.

The write() method does not add a newline character ('\n') to the end of the string −

Syntax

fileObject.write(string)

Here, passed parameter is the content to be written into the opened file.

Example

#!/usr/bin/python

# Open a file
fo = open("foo.txt", "wb")
fo.write( "Python is a great language.\nYeah its great!!\n")

# Close opend file


fo.close()

The above method would create foo.txt file and would write given content in that file and finally it would close
that file. If you would open this file, it would have following content.

Python is a great language.


Yeah its great!!

The read() Method


The read() method reads a string from an open file. It is important to note that Python strings can have binary
data. apart from text data.

Syntax

241
fileObject.read([count])

Here, passed parameter is the number of bytes to be read from the opened file. This method starts reading
from the beginning of the file and if count is missing, then it tries to read as much as possible, maybe until the
end of file.

Example
Let's take a file foo.txt, which we created above.

#!/usr/bin/python

# Open a file
fo = open("foo.txt", "r+")
str = fo.read(10);
print "Read String is : ", str
# Close opend file
fo.close()

This produces the following result −

Read String is : Python is

File Positions
The tell() method tells you the current position within the file; in other words, the next read or write will occur
at that many bytes from the beginning of the file.

The seek(offset[, from]) method changes the current file position. The offset argument indicates the number
of bytes to be moved. The from argument specifies the reference position from where the bytes are to be
moved.

If from is set to 0, it means use the beginning of the file as the reference position and 1 means use the current
position as the reference position and if it is set to 2 then the end of the file would be taken as the reference
position.

Example
Let us take a file foo.txt, which we created above.

#!/usr/bin/python

# Open a file
fo = open("foo.txt", "r+")
str = fo.read(10)
print "Read String is : ", str

# Check current position


position = fo.tell()
print "Current file position : ", position

242
# Reposition pointer at the beginning once again
position = fo.seek(0, 0);
str = fo.read(10)
print "Again read String is : ", str
# Close opend file
fo.close()

This produces the following result −

Read String is : Python is


Current file position : 10
Again read String is : Python is

Renaming and Deleting Files


Python os module provides methods that help you perform file-processing operations, such as renaming and
deleting files.

To use this module you need to import it first and then you can call any related functions.

The rename() Method


The rename() method takes two arguments, the current filename and the new filename.

Syntax

os.rename(current_file_name, new_file_name)

Example
Following is the example to rename an existing file test1.txt −

#!/usr/bin/python
import os

# Rename a file from test1.txt to test2.txt


os.rename( "test1.txt", "test2.txt" )

The remove() Method


You can use the remove() method to delete files by supplying the name of the file to be deleted as the
argument.

Syntax

os.remove(file_name)

Example
243
Following is the example to delete an existing file test2.txt −

#!/usr/bin/python
import os

# Delete file test2.txt


os.remove("text2.txt")

Directories in Python
All files are contained within various directories, and Python has no problem handling these too.
The os module has several methods that help you create, remove, and change directories.

The mkdir() Method


You can use the mkdir() method of the os module to create directories in the current directory. You need to
supply an argument to this method which contains the name of the directory to be created.

Syntax

os.mkdir("newdir")

Example
Following is the example to create a directory test in the current directory −

#!/usr/bin/python
import os

# Create a directory "test"


os.mkdir("test")

The chdir() Method


You can use the chdir() method to change the current directory. The chdir() method takes an argument, which
is the name of the directory that you want to make the current directory.

Syntax

os.chdir("newdir")

Example
Following is the example to go into "/home/newdir" directory −

#!/usr/bin/python
import os

# Changing a directory to "/home/newdir"


244
os.chdir("/home/newdir")

The getcwd() Method


The getcwd() method displays the current working directory.

Syntax

os.getcwd()

Example
Following is the example to give current directory −

#!/usr/bin/python
import os

# This would give location of the current directory


os.getcwd()

The rmdir() Method


The rmdir() method deletes the directory, which is passed as an argument in the method.

Before removing a directory, all the contents in it should be removed.

Syntax

os.rmdir('dirname')

Example
Following is the example to remove "/tmp/test" directory. It is required to give fully qualified name of the
directory, otherwise it would search for that directory in the current directory.

#!/usr/bin/python
import os

# This would remove "/tmp/test" directory.


os.rmdir( "/tmp/test" )

Text Processing in file functions


Reading/Writing Text Files
The fileObj returned after the file is opened maintains a file pointer. It initially positions at the beginning of the file and
advances whenever read/write operations are performed.
245
Reading Line/Lines from a Text File
• fileObj.readline() -> str: (most commonly-used) Read next line (up to and including newline) and return a
string (including newline). It returns an empty string after the end-of-file (EOF).
• fileObj.readlines() -> [str]: Read all lines into a list of strings.
• fileObj.read() -> str: Read the entire file into a string.

Writing Line to a Text File


• fileObj.write(str) -> int: Write the given string to the file and return the number of characters written. You
need to explicitly terminate the str with a '\n', if needed. The '\n' will be translated to the platform-
dependent newline ('\r\n' for Windows or '\n' for Unixes/macOS).

Examples
# Open a file for writing and insert some records
>>> f = open('test.txt', 'w')
>>> f.write('apple\n')
>>> f.write('orange\n')
>>> f.write('pear\n')
>>> f.close() # Always close the file
# Check the contents of the file created

# Open the file created for reading and read line(s) using readline() and readlines()
>>> f = open('test.txt', 'r')
>>> f.readline() # Read next line into a string
'apple\n'
>>> f.readlines() # Read all (next) lines into a list of strings
['orange\n', 'pear\n']
>>> f.readline() # Return an empty string after EOF
''
>>> f.close()

# Open the file for reading and read the entire file via read()
>>> f = open('test.txt', 'r')
>>> f.read() # Read entire file into a string
'apple\norange\npear\n'
>>> f.close()

# Read line-by-line using readline() in a while-loop


>>> f = open('test.txt')
>>> line = f.readline() # include newline
>>> while line:
line = line.rstrip() # strip trailing spaces and newline
# process the line
print(line)
line = f.readline()
apple
orange
pear
>>> f.close()

1.3 Processing Text File Line-by-Line


We can use a with-statement to open a file, which will be closed automatically upon exit, and a for-loop to read line-by-
line as follows:
with open('path/to/file.txt', 'r') as f: # Open file for read
for line in f: # Read line-by-line
line = line.strip() # Strip the leading/trailing whitespaces and newline
# Process the line
246
# File closed automatically upon exit of with-statement
The with-statement is equivalent to the try-finally statement as follows:
try:
f = open('path/to/file.txt')
for line in f:
line = line.strip()
# Process the line
finally:
f.close()

Example: Line-by-line File Copy


The following script copies a file into another line-by-line, prepending each line with the line number.

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
"""
file_copy: Copy file line-by-line from source to destination
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Usage: file_copy <src> <dest>
"""
import sys
import os

def main():
# Check and retrieve command-line arguments
if len(sys.argv) != 3:
print(__doc__)
sys.exit(1) # Return a non-zero value to indicate abnormal termination
fileIn = sys.argv[1]
fileOut = sys.argv[2]

# Verify source file


if not os.path.isfile(fileIn):
print("error: {} does not exist".format(fileIn))
sys.exit(1)

# Verify destination file


if os.path.isfile(fileOut):
print("{} exists. Override (y/n)?".format(fileOut))
reply = input().strip().lower()
if reply[0] != 'y':
sys.exit(1)

# Process the file line-by-line


with open(fileIn, 'r') as fpIn, open(fileOut, 'w') as fpOut:
lineNumber = 0
for line in fpIn:
lineNumber += 1
line = line.rstrip() # Strip trailing spaces and newline
fpOut.write("{}: {}\n".format(lineNumber, line))
# Need \n, which will be translated to platform-dependent newline
print("Number of lines: {}\n".format(lineNumber))

if __name__ == '__main__':

247
main()

3. Text Processing
For simple text string operations such as string search and replacement, you can use the built-in string functions
(e.g., str.replace(old, new)). For complex pattern search and replacement, you need to master regular expression (regex).

3.1 String Operations


The built-in class str provides many member functions for text string manipulation. Suppose that s is a str object.

Strip whitespaces (blank, tab and newline)


• s.strip()-> str: Return a copy of the string s with leading and trailing whitespaces removed. Whitespaces
includes blank, tab and newline.
• s.strip([chars]) -> str: Strip the leading/trailing characters given, instead of whitespaces.
• s.rstrip(), s.lstrip() -> str: Strip the right (trailing) whitespaces and the left (leading) whitespaces,
respectively. s.rstrip() is the most commonly-used to strip the trailing spaces/newline. The leading
whitespaces are usually significant.

Uppercase/Lowercase
• s.upper(), s.lower() -> str: Return a copy of string s converted to uppercase and lowercase, respectively.
• s.isupper(), s.islower() -> bool: Check if the string is uppercase/lowercase, respectively.

Find
• s.find(key_str, [start], [end]) -> int|-1: Return the lowest index in slice s[start:end] (default to entire string);
or -1 if not found.
• s.index(key_str, [start], [end]) -> int|ValueError: Similar to find(), but raises ValueError if not found.
• s.startswith(key_str, [start], [end]), s.endsswith(key_str, [start], [end]) -> bool: Check is the string begins
or ends with key_str.
For examples,
>>> s = '/test/in.txt'
>>> s.find('in')
6
>>> s[0 : s.find('in')] + 'out.txt'
'/test/out.txt'
# You could use str.replace() described below

Find and Replace


• s.replace(old, new, [count]) -> str: Return a copy with all occurrences of old replaced by new. The optional
parameter count limits the number of occurrences to replace, default to all occurrences.
str.replace() is ideal for simple text string replacement, without the need for pattern matching.

For examples,
>>> s = 'hello hello hello, world'
>>> help(s.replace)
>>> s.replace('ll', '**')
'he**o he**o he**o, world'
>>> s.replace('ll', '**', 2)
'he**o he**o hello, world'

Split into Tokens and Join


• s.split([sep], [maxsplit=-1]) -> [str]: Return a list of words using sep as delimiter string. The default
delimiter is whitespaces (blank, tab and newline). The maxSplit limits the maximum number of split
operations, with default -1 means no limit.
248
• sep.join([str]) -> str: Reverse of split(). Join the list of string with sep as separator.
For examples,
>>> 'apple, orange, pear'.split() # default delimiter is whitespaces
['apple,', 'orange,', 'pear']
>>> 'apple, orange, pear'.split(', ') # Set the delimiter
['apple', 'orange', 'pear']
>>> 'apple, orange, pear'.split(', ', maxsplit=1) # Set the split operation
['apple', 'orange, pear']

>>> ', '.join(['apple', 'orange, pear'])


'apple, orange, pear'

Python Modules
In this tutorial, we will explain how to construct and import custom Python modules. Additionally, we may
import or integrate Python's built-in modules via various methods.

What is Modular Programming?


Modular programming is the practice of segmenting a single, complicated coding task into multiple, simpler,
easier-to-manage sub-tasks. We call these subtasks modules. Therefore, we can build a bigger program by
assembling different modules that act like building blocks.

Modularizing our code in a big application has a lot of benefits.

Simplification: A module often concentrates on one comparatively small area of the overall problem instead
of the full task. We will have a more manageable design problem to think about if we are only concentrating
on one module. Program development is now simpler and much less vulnerable to mistakes.u

Flexibility: Modules are frequently used to establish conceptual separations between various problem areas.
It is less likely that changes to one module would influence other portions of the program if modules are
constructed in a fashion that reduces interconnectedness. (We might even be capable of editing a module
despite being familiar with the program beyond it.) It increases the likelihood that a group of numerous
developers will be able to collaborate on a big project.

Reusability: Functions created in a particular module may be readily accessed by different sections of the
assignment (through a suitably established api). As a result, duplicate code is no longer necessary.

Scope: Modules often declare a distinct namespace to prevent identifier clashes in various parts of a program.

In Python, modularization of the code is encouraged through the use of functions, modules, and packages.

What are Modules in Python?


A document with definitions of functions and various statements written in Python is called a Python module.

In Python, we can define a module in one of 3 ways:

249
o Python itself allows for the creation of modules.
o Similar to the re (regular expression) module, a module can be primarily written in C programming
language and then dynamically inserted at run-time.
o A built-in module, such as the itertools module, is inherently included in the interpreter.

A module is a file containing Python code, definitions of functions, statements, or classes. An


example_module.py file is a module we will create and whose name is example_module.

We employ modules to divide complicated programs into smaller, more understandable pieces. Modules also
allow for the reuse of code.

Rather than duplicating their definitions into several applications, we may define our most frequently used
functions in a separate module and then import the complete module.

Let's construct a module. Save the file as example_module.py after entering the following.

Example:

1. # Here, we are creating a simple Python program to show how to create a module.
2. # defining a function in the module to reuse it
3. def square( number ):
4. # here, the above function will square the number passed as the input
5. result = number ** 2
6. return result # here, we are returning the result of the function

Here, a module called example_module contains the definition of the function square(). The function returns
the square of a given number.

How to Import Modules in Python?


In Python, we may import functions from one module into our program, or as we say into, another module.

For this, we make use of the import Python keyword. In the Python window, we add the next to import
keyword, the name of the module we need to import. We will import the module we defined earlier
example_module.

Syntax:

1. import example_module

The functions that we defined in the example_module are not immediately imported into the present program.
Only the name of the module, i.e., example_ module, is imported here.

We may use the dot operator to use the functions using the module name. For instance:

Example:

1. # here, we are calling the module square method and passing the value 4
250
2. result = example_module.square( 4 )
3. print("By using the module square of number is: ", result )

Output:

By using the module square of number is: 16

There are several standard modules for Python. The complete list of Python standard modules is available.
The list can be seen using the help command.

Similar to how we imported our module, a user-defined module, we can use an import statement to import
other standard modules.

Importing a module can be done in a variety of ways. Below is a list of them.

Python import Statement


Using the import Python keyword and the dot operator, we may import a standard module and can access the
defined functions within it. Here's an illustration.

Code

1. # Here, we are creating a simple Python program to show how to import a standard module
2. # Here, we are import the math module which is a standard module
3. import math
4. print( "The value of euler's number is", math.e )
5. # here, we are printing the euler's number from the math module

Output:

The value of euler's number is 2.718281828459045

Importing and also Renaming


While importing a module, we can change its name too. Here is an example to show.

Code

1. # Here, we are creating a simple Python program to show how to import a module and rename it
2. # Here, we are import the math module and give a different name to it
3. import math as mt # here, we are importing the math module as mt
4. print( "The value of euler's number is", mt.e )
5. # here, we are printing the euler's number from the math module

Output:

The value of euler's number is 2.718281828459045

251
The math module is now named mt in this program. In some situations, it might help us type faster in case of
modules having long names.

Please take note that now the scope of our program does not include the term math. Thus, mt.pi is the proper
implementation of the module, whereas math.pi is invalid.

Python from...import Statement


We can import specific names from a module without importing the module as a whole. Here is an example.

Code

1. # Here, we are creating a simple Python program to show how to import specific
2. # objects from a module
3. # Here, we are import euler's number from the math module using the from keyword
4. from math import e
5. # here, the e value represents the euler's number
6. print( "The value of euler's number is", e )

Output:

The value of euler's number is 2.718281828459045

Only the e constant from the math module was imported in this case.

We avoid using the dot (.) operator in these scenarios. As follows, we may import many attributes at the same
time:

Code

1. # Here, we are creating a simple Python program to show how to import multiple
2. # objects from a module
3. from math import e, tau
4. print( "The value of tau constant is: ", tau )
5. print( "The value of the euler's number is: ", e )

Output:

The value of tau constant is: 6.283185307179586


The value of the euler's number is: 2.718281828459045

Import all Names - From import * Statement


To import all the objects from a module within the present namespace, use the * symbol and the from and
import keyword.

Syntax:

1. from name_of_module import *


252
There are benefits and drawbacks to using the symbol *. It is not advised to use * unless we are certain of our
particular requirements from the module; otherwise, do so.

Here is an example of the same.

Code

1. # Here, we are importing the complete math module using *


2. from math import *
3. # Here, we are accessing functions of math module without using the dot operator
4. print( "Calculating square root: ", sqrt(25) )
5. # here, we are getting the sqrt method and finding the square root of 25
6. print( "Calculating tangent of an angle: ", tan(pi/6) )
7. # here pi is also imported from the math module

Output:

Calculating square root: 5.0


Calculating tangent of an angle: 0.5773502691896257

Locating Path of Modules


The interpreter searches numerous places when importing a module in the Python program. Several directories
are searched if the built-in module is not present. The list of directories can be accessed using sys.path. The
Python interpreter looks for the module in the way described below:

The module is initially looked for in the current working directory. Python then explores every directory in
the shell parameter PYTHONPATH if the module cannot be located in the current directory. A list of folders
makes up the environment variable known as PYTHONPATH. Python examines the installation-dependent
set of folders set up when Python is downloaded if that also fails.

Here is an example to print the path.

Code

1. # Here, we are importing the sys module


2. import sys
3. # Here, we are printing the path using sys.path
4. print("Path of the sys module in the system is:", sys.path)

Output:

Path of the sys module in the system is:


['/home/pyodide', '/home/pyodide/lib/Python310.zip', '/lib/Python3.10', '/lib/Python3.10/lib-dynload', '', '/lib/Python3.10/site-
packages']

The dir() Built-in Function


We may use the dir() method to identify names declared within a module.
253
For instance, we have the following names in the standard module str. To print the names, we will use the
dir() method in the following way:

Code

1. # Here, we are creating a simple Python program to print the directory of a module
2. print( "List of functions:\n ", dir( str ), end=", " )

Output:

List of functions:
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__',
'__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__',
'__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__',
'__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format',
'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace',
'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'removeprefix', 'removesuffix', 'replace', 'rfind', 'rindex', 'rjust',
'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

Namespaces and Scoping


Objects are represented by names or identifiers called variables. A namespace is a dictionary containing the
names of variables (keys) and the objects that go with them (values).

Both local and global namespace variables can be accessed by a Python statement. When two variables with
the same name are local and global, the local variable takes the role of the global variable. There is a separate
local namespace for every function. The scoping rule for class methods is the same as for regular functions.
Python determines if parameters are local or global based on reasonable predictions. Any variable that is
allocated a value in a method is regarded as being local.

Therefore, we must use the global statement before we may provide a value to a global variable inside of a
function. Python is informed that Var_Name is a global variable by the line global Var_Name. Python stops
looking for the variable inside the local namespace.

We declare the variable Number, for instance, within the global namespace. Since we provide a Number a
value inside the function, Python considers a Number to be a local variable. UnboundLocalError will be the
outcome if we try to access the value of the local variable without or before declaring it global.

Code

1. Number = 204
2. def AddNumber(): # here, we are defining a function with the name Add Number
3. # Here, we are accessing the global namespace
4. global Number
5. Number = Number + 200
6. print("The number is:", Number)
7. # here, we are printing the number after performing the addition
8. AddNumber() # here, we are calling the function
9. print("The number is:", Number)

254
Output:

The number is: 204


The number is: 404

Python - Packages
We organize a large number of files in different folders and subfolders based on some criteria, so
that we can find and manage them easily. In the same way, a package in Python takes the concept
of the modular approach to next logical level. As you know, a module can contain multiple objects,
such as classes, functions, etc. A package can contain one or more relevant modules. Physically, a
package is actually a folder containing one or more module files.

Let's create a package named mypackage, using the following steps:

• Create a new folder named D:\MyApp.


• Inside MyApp, create a subfolder with the name 'mypackage'.
• Create an empty __init__.py file in the mypackage folder.
• Using a Python-aware editor like IDLE, create modules greet.py and functions.py with the
following code:

greet.py
Copy
def SayHello(name):
print("Hello ", name)
functions.py
Copy
def sum(x,y):
return x+y

def average(x,y):
return (x+y)/2

def power(x,y):
return x**y

255
That's it. We have created our package called mypackage. The following is a folder structure:

Package
Folder Structure

Using Packages
Importing a Module from a Package
Now, to test our package, navigate the command prompt to the MyApp folder and invoke the Python
prompt from there.

D:\MyApp>python

Import the functions module from the mypackage package and call its power() function.

>>> from mypackage import functions

>>> functions.power(3,2)

It is also possible to import specific functions from a module in the package.

256
>>> from mypackage.functions import sum

>>> sum(10,20)

30

>>> average(10,12)

Traceback (most recent call last):

File "<pyshell#13>", line 1, in <module>

NameError: name 'average' is not defined

2.2M
76
Entity Framework Core Quiz
NOW
PLAYING

__init__.py
The package folder contains a special file called __init__.py , which stores the package's content. It
serves two purposes:

1. The Python interpreter recognizes a folder as the package if it contains __init__.py file.
2. __init__.py exposes specified resources from its modules to be imported.

An empty __init__.py file makes all functions from the above modules available when this package
is imported. Note that __init__.py is essential for the folder to be recognized by Python as a package.
You can optionally define functions from individual modules to be made available.

Note:
We shall also create another Python script in the MyApp folder and import the mypackage package in it. It
should be at the same level of the package to be imported.

The __init__.py file is normally kept empty. However, it can also be used to choose specific functions
from modules in the package folder and make them available for import. Modify __init__.py as
below:

__init__.py
Copy
from .functions import average, power
from .greet import SayHello

The specified functions can now be imported in the interpreter session or another executable script.
257
Create test.py in the MyApp folder to test mypackage.

test.py
Copy
from mypackage import power, average, SayHello
SayHello()
x=power(3,2)
print("power(3,2) : ", x)

Note that functions power() and SayHello() are imported from the package and not from their
respective modules, as done earlier. The output of the above script is:

D:\MyApp>python test.py

Hello world

power(3,2) : 9

Install a Package Globally


Once a package is created, it can be installed for system-wide use by running the setup script. The
script calls setup() function from the setuptools module.

Let's install mypackage for system-wide use by running a setup script.

Save the following code as setup.py in the parent folder MyApp. The script calls the setup() function
from the setuptools module. The setup() function takes various arguments such as name, version,
author, list of dependencies, etc. The zip_safe argument defines whether the package is installed in
compressed mode or regular mode.

Example: setup.py
Copy
from setuptools import setup
setup(name='mypackage',
version='0.1',
description='Testing installation of Package',
url='#',
author='auth',
author_email='author@email.com',
license='MIT',
packages=['mypackage'],
zip_safe=False)

Now execute the following command to install mypackage using the pip utility. Ensure that the
command prompt is in the parent folder, in this case D:\MyApp.

258
D:\MyApp>pip install mypackage

Processing d:\MyApp

Installing collected packages: mypack

Running setup.py install for mypack ... done

Successfully installed mypackage-0.1

Now mypackage is available for system-wide use and can be imported in any script or interpreter.

D:\>python

>>> import mypackage

>>>mypackage.average(10,20)

15.0

>>>mypackage.power(10,2)

100

IMPLEMENTING PACKAGES: NumPy


Numpy is a general-purpose array-processing package. It provides a high-performance
multidimensional array object, and tools for working with these arrays. It is the fundamental package for
scientific computing with Python.
Besides its obvious scientific uses, Numpy can also be used as an efficient multi-dimensional container
of generic data.

Arrays in Numpy

Array in Numpy is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive
integers. In Numpy, number of dimensions of the array is called rank of the array.A tuple of integers giving the
size of the array along each dimension is known as shape of the array. An array class in Numpy is called
as ndarray. Elements in Numpy arrays are accessed by using square brackets and can be initialized by using
nested Python Lists.
Creating a Numpy Array
Arrays in Numpy can be created by multiple ways, with various number of Ranks, defining the size of the Array.
259
Arrays can also be created with the use of various data types such as lists, tuples, etc. The type of the resultant
array is deduced from the type of the elements in the sequences.
Note: Type of array can be explicitly defined while creating the array.
# Python program for
# Creation of Arrays
import numpy as np

# Creating a rank 1 Array


arr = np.array([1, 2, 3])
print("Array with Rank 1: \n",arr)

# Creating a rank 2 Array


arr = np.array([[1, 2, 3],
[4, 5, 6]])
print("Array with Rank 2: \n", arr)

# Creating an array from tuple


arr = np.array((1, 3, 2))
print("\nArray created using "
"passed tuple:\n", arr)
Run on IDE
Output:
Array with Rank 1:
[1 2 3]
Array with Rank 2:
[[1 2 3]
[4 5 6]]

Array created using passed tuple:


[1 3 2]

Accessing the array Index


In a numpy array, indexing or accessing the array index can be done in multiple ways. To print a range of an
array, slicing is done. Slicing of an array is defining a range in a new array which is used to print a range of
elements from the original array. Since, sliced array holds a range of elements of the original array, modifying
content with the help of sliced array modifies the original array content.
# Python program to demonstrate
# indexing in numpy array
import numpy as np

# Initial Array
arr = np.array([[-1, 2, 0, 4],
[4, -0.5, 6, 0],
[2.6, 0, 7, 8],
[3, -7, 4, 2.0]])
print("Initial Array: ")
print(arr)

260
# Printing a range of Array
# with the use of slicing method
sliced_arr = arr[:2, ::2]
print ("Array with first 2 rows and"
" alternate columns(0 and 2):\n", sliced_arr)

# Printing elements at
# specific Indices
Index_arr = arr[[1, 1, 0, 3],
[3, 2, 1, 0]]
print ("\nElements at indices (1, 3), "
"(1, 2), (0, 1), (3, 0):\n", Index_arr)
Run on IDE
Output:
Initial Array:
[[-1. 2. 0. 4. ]
[ 4. -0.5 6. 0. ]
[ 2.6 0. 7. 8. ]
[ 3. -7. 4. 2. ]]
Array with first 2 rows and alternate columns(0 and 2):
[[-1. 0.]
[ 4. 6.]]

Elements at indices (1, 3), (1, 2), (0, 1), (3, 0):
[0. 6. 2. 3.]

Basic Array Operations


In numpy, arrays allow a wide range of operations which can be performed on a particular array or a combination
of Arrays. These operation include some basic Mathematical operation as well as Unary and Binary operations.
# Python program to demonstrate
# basic operations on single array
import numpy as np

# Defining Array 1
a = np.array([[1, 2],
[3, 4]])

# Defining Array 2
b = np.array([[4, 3],
[2, 1]])

# Adding 1 to every element


print ("Adding 1 to every element:", a + 1)

# Subtracting 2 from each element

261
print ("\nSubtracting 2 from each element:", b - 2)

# sum of array elements


# Performing Unary operations
print ("\nSum of all array "
"elements: ", a.sum())

# Adding two arrays


# Performing Binary operations
print ("\nArray sum:\n", a + b)
Run on IDE
Output:
Adding 1 to every element:
[[2 3]
[4 5]]

Subtracting 2 from each element:


[[ 2 1]
[ 0 -1]]

Sum of all array elements: 10

Array sum:
[[5 5]
[5 5]]

More on Numpy Arrays


• Basic Array Operations in Numpy
• Advanced Array Operations in Numpy
• Basic Slicing and Advanced Indexing in NumPy Python

Data Types in Numpy

Every Numpy array is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive
integers. Every ndarray has an associated data type (dtype) object. This data type object (dtype) provides
information about the layout of the array. The values of an ndarray are stored in a buffer which can be thought
of as a contiguous block of memory bytes which can be interpreted by the dtype object. Numpy provides a large
set of numeric datatypes that can be used to construct arrays. At the time of Array creation, Numpy tries to guess
a datatype, but functions that construct arrays usually also include an optional argument to explicitly specify the
datatype.

Constructing a Datatype Object


In Numpy, datatypes of Arrays need not to be defined unless a specific datatype is required. Numpy tries to
guess the datatype for Arrays which are not predefined in the constructor function.
# Python Program to create
262
# a data type object
import numpy as np

# Integer datatype
# guessed by Numpy
x = np.array([1, 2])
print("Integer Datatype: ")
print(x.dtype)

# Float datatype
# guessed by Numpy
x = np.array([1.0, 2.0])
print("\nFloat Datatype: ")
print(x.dtype)

# Forced Datatype
x = np.array([1, 2], dtype = np.int64)
print("\nForcing a Datatype: ")
print(x.dtype)
Run on IDE
Output:
Integer Datatype:
int64

Float Datatype:
float64

Forcing a Datatype:
int64

Math Operations on DataType array


In Numpy arrays, basic mathematical operations are performed element-wise on the array. These operations are
applied both as operator overloads and as functions. Many useful functions are provided in Numpy for
performing computations on Arrays such as sum: for addition of Array elements, T: for Transpose of elements,
etc.
# Python Program to create
# a data type object
import numpy as np

# First Array
arr1 = np.array([[4, 7], [2, 6]],
dtype = np.float64)

# Second Array
arr2 = np.array([[3, 6], [2, 8]],
dtype = np.float64)

# Addition of two Arrays


263
Sum = np.add(arr1, arr2)
print("Addition of Two Arrays: ")
print(Sum)

# Addition of all Array elements


# using predefined sum method
Sum1 = np.sum(arr1)
print("\nAddition of Array elements: ")
print(Sum1)

# Square root of Array


Sqrt = np.sqrt(arr1)
print("\nSquare root of Array1 elements: ")
print(Sqrt)

# Transpose of Array
# using In-built function 'T'
Trans_arr = arr1.T
print("\nTranspose of Array: ")
print(Trans_arr)
Run on IDE
Output:
Addition of Two Arrays:
[[ 7. 13.]
[ 4. 14.]]

Addition of Array elements:


19.0

Square root of Array1 elements:


[[2. 2.64575131]
[1.41421356 2.44948974]]

Transpose of Array:
[[4. 2.]
[7. 6.]]

More on Numpy Data Type


• Data type Object (dtype) in NumPy

Methods in Numpy

264
all()

any()

take()

put()

apply_along_axis()

apply_over_axes()

argmin()

argmax()

nanargmin()

nanargmax()

amax()

amin()

insert()

delete()

append()

around()

flip()

fliplr()

flipud()

265
triu()

tril()

tri()

empty()

empty_like()

zeros()

zeros_like()

ones()

ones_like()

full_like()

diag()

diagflat()

diag_indices()

asmatrix()

bmat()

eye()

roll()

identity()

arange()

266
place()

extract()

compress()

rot90()

tile()

reshape()

ravel()

isinf()

isrealobj()

isscalar()

isneginf()

isposinf()

iscomplex()

isnan()

iscomplexobj()

isreal()

isfinite()

isfortran()

exp()

267
exp2()

fix()

hypot()

absolute()

ceil()

floor()

degrees()

radians()

npv()

fv()

pv()

power()

float_power()

log()

log1()

log2()

log10()

dot()

vdot()

268
trunc()

divide()

floor_divide()

true_divide()

random.rand()

random.randn()

ndarray.flat()

expm1()

bincount()

rint()

equal()

not_equal()

less()

less_equal()

greater()

greater_equal()

prod()

square()

cbrt()

269
logical_or()

logical_and()

logical_not()

logical_xor()

array_equal()

array_equiv()

sin()

cos()

tan()

sinh()

cosh()

tanh()

arcsin()

arccos()

arctan()

arctan2()

Python Pandas

270
The term "Pandas" refers to an open-source library for manipulating high-performance data in Python. This
instructional exercise is intended for the two novices and experts.

It was created in 2008 by Wes McKinney and is used for data analysis in Python. Pandas is an open-source
library that provides high-performance data manipulation in Python. All of the basic and advanced concepts
of Pandas, such as Numpy, data operation, and time series, are covered in our tutorial.

Pandas Introduction
The name of Pandas is gotten from the word Board Information, and that implies an Econometrics from Multi-
faceted information. It was created in 2008 by Wes McKinney and is used for data analysis in Python.

Processing, such as restructuring, cleaning, merging, etc., is necessary for data analysis. Numpy, Scipy,
Cython, and Panda are just a few of the fast data processing tools available. Yet, we incline toward Pandas
since working with Pandas is quick, basic and more expressive than different apparatuses.

Since Pandas is built on top of the Numpy bundle, it is expected that Numpy will work with Pandas.

Before Pandas, Python was able for information planning, however it just offered restricted help for
information investigation. As a result, Pandas entered the picture and enhanced data analysis capabilities.
Regardless of the source of the data, it can carry out the five crucial steps that are necessary for processing
and analyzing it: load, manipulate, prepare, model, and analyze.

Key Features of Pandas


o It has a DataFrame object that is quick and effective, with both standard and custom indexing.
o Utilized for reshaping and turning of the informational indexes.
o For aggregations and transformations, group by data.
o It is used to align the data and integrate the data that is missing.
o Provide Time Series functionality.
o Process a variety of data sets in various formats, such as matrix data, heterogeneous tabular data, and
time series.
o Manage the data sets' multiple operations, including subsetting, slicing, filtering, groupBy, reordering,
and reshaping.

271
o It incorporates with different libraries like SciPy, and scikit-learn.
o Performs quickly, and the Cython can be used to accelerate it even further.

Benefits of Pandas
The following are the advantages of pandas overusing other languages:

Representation of Data: Through its DataFrame and Series, it presents the data in a manner that is appropriate
for data analysis.

Clear code: Pandas' clear API lets you concentrate on the most important part of the code. In this way, it gives
clear and brief code to the client.

DataFrame and Series are the two data structures that Pandas provides for processing data. These data
structures are discussed below:

1) Series
A one-dimensional array capable of storing a variety of data types is how it is defined. The term "index" refers
to the row labels of a series. We can without much of a stretch believer the rundown, tuple, and word reference
into series utilizing "series' technique. Multiple columns cannot be included in a Series. Only one parameter
exists:

Data: It can be any list, dictionary, or scalar value.

Creating Series from Array:

Before creating a Series, Firstly, we have to import the numpy module and then use array() function in the
program.

1. import pandas as pd
2. import numpy as np
3. info = np.array(['P','a','n','d','a','s'])
4. a = pd.Series(info)
5. print(a)

Output

0 P
1 a
2 n
3 d
4 a
5 s
dtype: object

Explanation: In this code, firstly, we have imported the pandas and numpy library with the pd and np alias.
Then, we have taken a variable named "info" that consist of an array of some values. We have called
the info variable through a Series method and defined it in an "a" variable. The Series has printed by calling
the print(a) method.

272
Python Pandas DataFrame
It is a generally utilized information design of pandas and works with a two-layered exhibit with named
tomahawks (lines and segments). As a standard method for storing data, DataFrame has two distinct indexes-
row index and column index. It has the following characteristics:

The sections can be heterogeneous sorts like int, bool, etc.

It can be thought of as a series structure dictionary with indexed rows and columns. It is referred to as
"columns" for rows and "index" for columns.

Create a DataFrame using List:

We can easily create a DataFrame in Pandas using list.

1. import pandas as pd
2. # a list of strings
3. x = ['Python', 'Pandas']
4.
5. # Calling DataFrame constructor on list
6. df = pd.DataFrame(x)
7. print(df)

Output

0
0 Python
1 Pandas

Django Frame works


What is Django
Django is a web application framework written in Python programming language. It is based on MVT (Model
View Template) design pattern. The Django is very demanding due to its rapid development feature. It takes
less time to build application after collecting client requirement.

This framework uses a famous tag line:The web framework for perfectionists with deadlines.

By using Django, we can build web applications in very less time. Django is designed in such a manner that it
handles much of configure things automatically, so we can focus on application development only.

History
Django was design and developed by Lawrence journal world in 2003 and publicly released under BSD license
in July 2005. Currently, DSF (Django Software Foundation) maintains its development and release cycle.

Django was released on 21, July 2005. Its current stable version is 2.0.3 which was released on 6 March, 2018.

273
Django Version History

Version Date Description

0.90 16 Nov
2005

0.91 11 Jan magic removal


2006

0.96 23 Mar newforms, testing tools


2007

1.0 3 Sep API stability, decoupled admin,


2008 unicode

1.1 29 Jul Aggregates, transaction based tests


2009

1.2 17 May Multiple db connections, CSRF,


2010 model validation

1.3 23 Mar Timezones, in browser testing, app


2011 templates.

1.5 26 Feb Python 3 Support, configurable user


2013 model

1.6 6 Nov Dedicated to Malcolm Tredinnick, db


2013 transaction management, connection
pooling.

1.7 2 Sep Migrations, application loading and


2014 configuration.

1.8 LTS 2 Sep Migrations, application loading and


2014 configuration.

1.8 LTS 1 Apr Native support for multiple template


2015 engines.Supported until at least April
2018

1.9 1 Dec Automatic password validation. New


2015 styling for admin interface.

1.10 1 Aug Full text search for PostgreSQL. New-


2016 style middleware.

1.11 LTS 1.11 Last version to support Python


LTS 2.7.Supported until at least April 2020

274
2.0 Dec First Python 3-only release,
2017 Simplified URL routing syntax,
Mobile friendly admin.

Popularity
Django is widely accepted and used by various well-known sites such as:

o Instagram
o Mozilla
o Disqus
o Pinterest
o Bitbucket
o The Washington Times

Features of Django
o Rapid Development
o Secure
o Scalable
o Fully loaded
o Versatile
o Open Source
o Vast and Supported Community

Rapid Development
Django was designed with the intention to make a framework which takes less time to build web application.
The project implementation phase is a very time taken but Django creates it rapidly.

Secure
Django takes security seriously and helps developers to avoid many common security mistakes, such as SQL
injection, cross-site scripting, cross-site request forgery etc. Its user authentication system provides a secure
way to manage user accounts and passwords.

Scalable
Django is scalable in nature and has ability to quickly and flexibly switch from small to large scale application
project.

275
Fully loaded
Django includes various helping task modules and libraries which can be used to handle common Web
development tasks. Django takes care of user authentication, content administration, site maps, RSS feeds etc.

Versatile
Django is versatile in nature which allows it to build applications for different-different domains. Now a days,
Companies are using Django to build various types of applications like: content management systems, social
networks sites or scientific computing platforms etc.

Open Source
Django is an open source web application framework. It is publicly available without cost. It can be downloaded
with source code from the public repository. Open source reduces the total cost of the application development.

Vast and Supported Community


Django is an one of the most popular web framework. It has widely supportive community and channels to
share and connect.



• Sign In
• Home
• Saved Videos
• Courses

Data Structures and Algorithms
ML & Data Science
Web Development
Languages
Interview Corner
CS Subjects
Jobs
Practice
Contests
• GBlog
• Puzzles
• What's New ?
Change Language
• 90% Refund @Courses
• Free Python 3 Tutorial
• Data Types
• Control Flow
• Functions
• List
• String
276
• Set
• Tuple
• Dictionary
• Oops
• Exception Handling
• Python Programs
• Python Projects
• Python Interview Questions
• Python MCQ
• NumPy
• Pandas
• Python Database
• Data Science With Python
• Machine Learning with Python
• Django
• Flask
• R

Related Articles
• Explore Our Geeks Community
• Share Your Experience
• Python Itertools
• Python - Itertools.count()
• Python - Itertools.cycle()
• Python - itertools.repeat()
• Python - Itertools.accumulate()
• Python - Itertools.chain()
• Python - Itertools.chain.from_iterable()
• Python - Itertools.compress()
• Python - Itertools.dropwhile()
• Python - Itertools.filterfalse()
• Python - Itertools.islice()
• Python - Itertools.starmap()
• Python - Itertools.takewhile()
• Python - Itertools.tee()
• Python - Itertools.zip_longest()
• Python - Itertools.Product()
• Python - Itertools.Permutations()
• Python - Itertools.Combinations_with_replacement()
• Coding for EveryoneCourse
Python Itertools
Read
Courses
Practice
Jobs
••

277
Python’s Itertool is a module that provides various functions that work on iterators to produce
complex iterators. This module works as a fast, memory-efficient tool that is used either by
themselves or in combination to form iterator algebra.
For example, let’s suppose there are two lists and you want to multiply their elements. There
can be several ways of achieving this. One can be using the naive approach i.e by iterating
through the elements of both the list simultaneously and multiplying them. And another
approach can be using the map function i.e by passing the mul operator as the first parameter
to the map function and Lists as the second and third parameter to this function. Let’s see the
time taken by each approach.

• Python3

# Python program to demonstrate


# iterator module

import operator
import time

# Defining lists
L1 = [1, 2, 3]
L2 = [2, 3, 4]

# Starting time before map


# function
t1 = time.time()

# Calculating result
a, b, c = map(operator.mul, L1, L2)

# Ending time after map


# function
t2 = time.time()

# Time taken by map function


print("Result:", a, b, c)
print("Time taken by map function: %.6f" % (t2 - t1))

# Starting time before naive


# method
t1 = time.time()

# Calculating result using for loop


print("Result:", end=" ")
for i in range(3):
print(L1[i] * L2[i], end=" ")

# Ending time after naive


# method

278
t2 = time.time()
print("\nTime taken by for loop: %.6f" % (t2 - t1))

Output:
Result: 2 6 12
Time taken by map function: 0.000005
Result: 2 6 12
Time taken by for loop: 0.000014

• Python

from itertools import count

for number in count(start=1, step=2):


if number > 10:
break
print(number) # print statement

output :

In the above example, it can be seen that the time taken by the map function is approximately
half than the time taken by for loop. This shows that itertools are fast, memory-efficient tools.
Different types of iterators provided by this module are:
• Infinite iterators
• Combinatoric iterators
• Terminating iterators

Infinite iterators

Iterator in Python is any Python type that can be used with a ‘for in loop’. Python lists, tuples,
dictionaries, and sets are all examples of inbuilt iterators. But it is not necessary that an iterator
object has to exhaust, sometimes it can be infinite. Such types of iterators are known
as Infinite iterators.
Python provides three types of infinite iterators:
count(start, step): This iterator starts printing from the “start” number and prints
infinitely. If steps are mentioned, the numbers are skipped else step is 1 by default. See the
below example for its use with for in loop.

279
Example:
• Python3

# Python program to demonstrate


# infinite iterators

import itertools

# for in loop
for i in itertools.count(5, 5):
if i == 35:
break
else:
print(i, end=" ")

Output:
5 10 15 20 25 30
cycle(iterable): This iterator prints all values in order from the passed container. It
restarts printing from the beginning again when all elements are printed in a cyclic
manner.
Example 1:
• Python3

# Python program to demonstrate


# infinite iterators

import itertools

count = 0

# for in loop
for i in itertools.cycle('AB'):
if count > 7:
break
else:
print(i, end=" ")
count += 1

Output:
ABABABAB
Example 2: Using the next function.
• Python3

# Python program to demonstrate


# infinite iterators

280
import itertools

l = ['Geeks', 'for', 'Geeks']

# defining iterator
iterators = itertools.cycle(l)

# for in loop
for i in range(6):

# Using next function


print(next(iterators), end=" ")

Combinatoric iterators
Output:
Geeks for Geeks Geeks for Geeks
repeat(val, num): This iterator repeatedly prints the passed value an infinite number of times.
If the optional keyword num is mentioned, then it repeatedly prints num number of times.
Example:
• Python3

# Python code to demonstrate the working of


# repeat()

# importing "itertools" for iterator operations


import itertools

# using repeat() to repeatedly print number


print("Printing the numbers repeatedly : ")
print(list(itertools.repeat(25, 4)))

Output:
Printing the numbers repeatedly :
[25, 25, 25, 25]

Combinatoric iterators

The recursive generators that are used to simplify combinatorial constructs such as
permutations, combinations, and Cartesian products are called combinatoric iterators.
In Python there are 4 combinatoric iterators:
Product(): This tool computes the cartesian product of input iterables. To compute the
product of an iterable with itself, we use the optional repeat keyword argument to specify the
number of repetitions. The output of this function is tuples in sorted order.
Example:
• Python3

281
# import the product function from itertools module
from itertools import product

print("The cartesian product using repeat:")


print(list(product([1, 2], repeat=2)))
print()

print("The cartesian product of the containers:")


print(list(product(['geeks', 'for', 'geeks'], '2')))
print()

print("The cartesian product of the containers:")


print(list(product('AB', [3, 4])))

Output:
The cartesian product using repeat:
[(1, 1), (1, 2), (2, 1), (2, 2)]

The cartesian product of the containers:


[('geeks', '2'), ('for', '2'), ('geeks', '2')]

The cartesian product of the containers:


[('A', 3), ('A', 4), ('B', 3), ('B', 4)]
Permutations(): Permutations() as the name speaks for itself is used to generate all possible
permutations of an iterable. All elements are treated as unique based on their position and
not their values. This function takes an iterable and group_size, if the value of group_size is
not specified or is equal to None then the value of group_size becomes the length of the
iterable.
Example:
• Python3

# import the product function from itertools module


from itertools import permutations

print("All the permutations of the given list is:")


print(list(permutations([1, 'geeks'], 2)))
print()

print("All the permutations of the given string is:")


print(list(permutations('AB')))
print()

print("All the permutations of the given container is:")


print(list(permutations(range(3), 2)))

282
Output:
All the permutations of the given list is:
[(1, 'geeks'), ('geeks', 1)]

All the permutations of the given string is:


[('A', 'B'), ('B', 'A')]

All the permutations of the given container is:


[(0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1)]
Combinations(): This iterator prints all the possible combinations(without replacement) of
the container passed in arguments in the specified group size in sorted order.
Example:
• Python3

# import combinations from itertools module

from itertools import combinations

print ("All the combination of list in sorted order(without replacement) is:")


print(list(combinations(['A', 2], 2)))
print()

print ("All the combination of string in sorted order(without replacement) is:")


print(list(combinations('AB', 2)))
print()

print ("All the combination of list in sorted order(without replacement) is:")


print(list(combinations(range(2), 1)))

Output:
All the combination of list in sorted order(without replacement) is:
[('A', 2)]

All the combination of string in sorted order(without replacement) is:


[('A', 'B')]

All the combination of list in sorted order(without replacement) is:


[(0, ), (1, )]
Combinations_with_replacement(): This function returns a subsequence of length n from
the elements of the iterable where n is the argument that the function takes determining the
length of the subsequences generated by the function. Individual elements may repeat
itself in combinations_with_replacement function.
283
Example:
• Python3

# import combinations from itertools module

from itertools import combinations_with_replacement

print("All the combination of string in sorted order(with replacement) is:")


print(list(combinations_with_replacement("AB", 2)))
print()

print("All the combination of list in sorted order(with replacement) is:")


print(list(combinations_with_replacement([1, 2], 2)))
print()

print("All the combination of container in sorted order(with replacement) is:")


print(list(combinations_with_replacement(range(2), 1)))

Output:
All the combination of string in sorted order(with replacement) is:
[('A', 'A'), ('A', 'B'), ('B', 'B')]

All the combination of list in sorted order(with replacement) is:


[(1, 1), (1, 2), (2, 2)]

All the combination of container in sorted order(with replacement) is:


[(0, ), (1, )]

Terminating iterators

Terminating iterators are used to work on the short input sequences and produce the output
based on the functionality of the method used.
Different types of terminating iterators are:
accumulate(iter, func): This iterator takes two arguments, iterable target and the function
which would be followed at each iteration of value in target. If no function is passed, addition
takes place by default. If the input iterable is empty, the output iterable will also be empty.
Example:
• Python3

# Python code to demonstrate the working of


# accumulate()

284
import itertools
import operator

# initializing list 1
li1 = [1, 4, 5, 7]

# using accumulate()
# prints the successive summation of elements
print("The sum after each iteration is : ", end="")
print(list(itertools.accumulate(li1)))

# using accumulate()
# prints the successive multiplication of elements
print("The product after each iteration is : ", end="")
print(list(itertools.accumulate(li1, operator.mul)))

# using accumulate()
# prints the successive summation of elements
print("The sum after each iteration is : ", end="")
print(list(itertools.accumulate(li1)))

# using accumulate()
# prints the successive multiplication of elements
print("The product after each iteration is : ", end="")
print(list(itertools.accumulate(li1, operator.mul)))

Output:
The sum after each iteration is : [1, 5, 10, 17]
The product after each iteration is : [1, 4, 20, 140]
The sum after each iteration is : [1, 5, 10, 17]
The product after each iteration is : [1, 4, 20, 140
chain(iter1, iter2..): This function is used to print all the values in iterable targets one after
another mentioned in its arguments.
Example:
• Python3

# Python code to demonstrate the working of


# and chain()

import itertools

# initializing list 1
li1 = [1, 4, 5, 7]

# initializing list 2
li2 = [1, 6, 5, 9]

285
# initializing list 3
li3 = [8, 10, 5, 4]

# using chain() to print all elements of lists


print("All values in mentioned chain are : ", end="")
print(list(itertools.chain(li1, li2, li3)))

Output:
All values in mentioned chain are : [1, 4, 5, 7, 1, 6, 5, 9, 8, 10, 5, 4]
• chain.from_iterable(): This function is implemented similarly as a chain() but
the argument here is a list of lists or any other iterable container.
Example:
• Python3

# Python code to demonstrate the working of


# chain.from_iterable()

import itertools

# initializing list 1
li1 = [1, 4, 5, 7]

# initializing list 2
li2 = [1, 6, 5, 9]

# initializing list 3
li3 = [8, 10, 5, 4]

# initializing list of list


li4 = [li1, li2, li3]

# using chain.from_iterable() to print all elements of lists


print ("All values in mentioned chain are : ", end ="")
print (list(itertools.chain.from_iterable(li4)))

Output:
All values in mentioned chain are : [1, 4, 5, 7, 1, 6, 5, 9, 8, 10, 5, 4]
compress(iter, selector): This iterator selectively picks the values to print from the passed
container according to the boolean list value passed as other arguments. The arguments
corresponding to boolean true are printed else all are skipped.
Example:
• Python3

286
# Python code to demonstrate the working of
# and compress()

import itertools

# using compress() selectively print data values


print("The compressed values in string are : ", end="")
print(list(itertools.compress('GEEKSFORGEEKS', [
1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0])))

Output:
The compressed values in string are : ['G', 'F', 'G']
• dropwhile(func, seq): This iterator starts printing the characters only after the
func. in argument returns false for the first time.
Example:
• Python3

# Python code to demonstrate the working of


# dropwhile()

import itertools

# initializing list
li = [2, 4, 5, 7, 8]

# using dropwhile() to start displaying after condition is false


print ("The values after condition returns false : ", end ="")
print (list(itertools.dropwhile(lambda x : x % 2 == 0, li)))

Output:
The values after condition returns false : [5, 7, 8]
filterfalse(func, seq): As the name suggests, this iterator prints only values that return false
for the passed function.
Example:
• Python3

# Python code to demonstrate the working of


# filterfalse()

import itertools

287
# initializing list
li = [2, 4, 5, 7, 8]

# using filterfalse() to print false values


print ("The values that return false to function are : ", end ="")
print (list(itertools.filterfalse(lambda x : x % 2 == 0, li)))

Output:
The values that return false to function are : [5, 7]
islice(iterable, start, stop, step): This iterator selectively prints the values mentioned in its
iterable container passed as argument. This iterator takes 4 arguments, iterable container,
starting pos., ending position and step.
Example:
• Python3

# Python code to demonstrate the working of


# islice()

import itertools

# initializing list
li = [2, 4, 5, 7, 8, 10, 20]

# using islice() to slice the list acc. to need


# starts printing from 2nd index till 6th skipping 2
print ("The sliced list values are : ", end ="")
print (list(itertools.islice(li, 1, 6, 2)))

Output:
The sliced list values are : [4, 7, 10]
starmap(func., tuple list): This iterator takes a function and tuple list as argument and returns
the value according to the function from each tuple of the list.
Example:
• Python3

# Python code to demonstrate the working of


# starmap()

import itertools

# initializing tuple list


li = [ (1, 10, 5), (8, 4, 1), (5, 4, 9), (11, 10, 1) ]

288
# using starmap() for selection value acc. to function
# selects min of all tuple values
print ("The values acc. to function are : ", end ="")
print (list(itertools.starmap(min, li)))

Output:
The values acc. to function are : [1, 1, 4, 1]
• takewhile(func, iterable): This iterator is the opposite of dropwhile(), it prints
the values till the function returns false for 1st time.
Example:
• Python3

# Python code to demonstrate the working of


# takewhile()

import itertools

# initializing list
li = [2, 4, 6, 7, 8, 10, 20]

# using takewhile() to print values till condition is false.


print ("The list values till 1st false value are : ", end ="")
print (list(itertools.takewhile(lambda x : x % 2 == 0, li )))

Output:
The list values till 1st false value are : [2, 4, 6]
• tee(iterator, count):- This iterator splits the container into a number of
iterators mentioned in the argument.
Example:
• Python3

# Python code to demonstrate the working of


# tee()

import itertools

# initializing list
li = [2, 4, 6, 7, 8, 10, 20]

# storing list in iterator


iti = iter(li)

# using tee() to make a list of iterators


# makes list of 3 iterators having same values.

289
it = itertools.tee(iti, 3)

# printing the values of iterators


print("The iterators are : ")
for i in range(0, 3):
print(list(it[i]))

Output:
The iterators are :
[2, 4, 6, 7, 8, 10, 20]
[2, 4, 6, 7, 8, 10, 20]
[2, 4, 6, 7, 8, 10, 20]
zip_longest( iterable1, iterable2, fillval): This iterator prints the values of iterables
alternatively in sequence. If one of the iterables is printed fully, the remaining values are filled
by the values assigned to fillvalue.
Example:
• Python3

# Python code to demonstrate the working of


# zip_longest()

import itertools

# using zip_longest() to combine two iterables.


print("The combined values of iterables is : ")
print(*(itertools.zip_longest('GesoGes', 'ekfrek', fillvalue='_')))

Output:
The combined values of iterables is :
('G', 'e') ('e', 'k') ('s', 'f') ('o', 'r') ('G', 'e') ('e', 'k') ('s', '_')

SciPy
SciPy in Python is an open-source library used for solving mathematical, scientific, engineering, and
technical problems. It allows users to manipulate the data and visualize the data using a wide range of high-
level Python commands. SciPy is built on the Python NumPy extention. SciPy is also pronounced as “Sigh
Pi.”

Sub-packages of SciPy:
• File input/output – scipy.io
• Special Function – scipy.special
• Linear Algebra Operation – scipy.linalg

290
• Interpolation – scipy.interpolate
• Optimization and fit – scipy.optimize
• Statistics and random numbers – scipy.stats
• Numerical Integration – scipy.integrate
• Fast Fourier transforms – scipy.fftpack
• Signal Processing – scipy.signal
• Image manipulation – scipy.ndimage

Why use SciPy


• SciPy contains varieties of sub packages which help to solve the most common issue related to
Scientific Computation.
• SciPy package in Python is the most used Scientific library only second to GNU Scientific Library for
C/C++ or Matlab’s.
• Easy to use and understand as well as fast computational power.
• It can operate on an array of NumPy library.

Numpy VS SciPy
Numpy
• Numpy is written in C and use for mathematical or numeric calculation.
• It is faster than other Python Libraries
• Numpy is the most useful library for Data Science to perform basic calculations.
• Numpy contains nothing but array data type which performs the most basic operation like sorting,
shaping, indexing, etc.
SciPy
• SciPy is built in top of the NumPy
• SciPy module in Python is a fully-featured version of Linear Algebra while Numpy contains only a
few features.
• Most new Data Science features are available in Scipy rather than Numpy.

SciPy – Installation and Environment Setup


You can also install SciPy in Windows via pip
Python3 -m pip install --user numpy scipy
Install Scipy on Linux
sudo apt-get install python-scipy python-numpy
Install SciPy in Mac
sudo port install py35-scipy py35-numpy

291
Before we start learning SciPy Python, you need to know basic functionality as well as different types of an
array of NumPy
The standard way to import SciPy modules and Numpy:
from scipy import special #same for other modules
import numpy as np
File Input / Output package
Scipy, I/O package, has a wide range of functions for work with different files format which are Matlab, Arff,
Wave, Matrix Market, IDL, NetCDF, TXT, CSV and binary format.
Let us take one file format Python SciPy example as which are regularly used in MatLab:
import numpy as np
from scipy import io as sio
array = np.ones((4, 4))
sio.savemat('example.mat', {'ar': array})
data = sio.loadmat(‘example.mat', struct_as_record=True)
data['ar']
Output:
array([[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.]])
Code Explanation
• Line 1 & 2: Import the essential SciPy library in Python with I/O package and Numpy.
• Line 3: Create 4 x 4, dimensional one’s array
• Line 4: Store array in example.mat file.
• Line 5: Get data from example.mat file
• Line 6: Print output.
Special Function package
• scipy.special package contains numerous functions of mathematical physics.
• SciPy special function includes Cubic Root, Exponential, Log sum Exponential, Lambert, Permutation
and Combinations, Gamma, Bessel, hypergeometric, Kelvin, beta, parabolic cylinder, Relative Error
Exponential, etc..
• For one line description all of these function, type in Python console:
help(scipy.special)
Output:
292
NAME
scipy.special

DESCRIPTION
========================================
Special functions (:mod:`scipy.special`)
========================================

.. module:: scipy.special

Nearly all of the functions below are universal functions and follow
broadcasting and automatic array-looping rules. Exceptions are noted.
Cubic Root Function
Cubic Root function finds the cube root of values.
Syntax:
scipy.special.cbrt(x)
Example:
from scipy.special import cbrt
#Find cubic root of 27 & 64 using cbrt() function
cb = cbrt([27, 64])
#print value of cb
print(cb)
Output: array([3., 4.])
Exponential Function:
Exponential function computes the 10**x element-wise.
Example:
from scipy.special import exp10
#define exp10 function and pass value in its
exp = exp10([1,10])
print(exp)
Output: [1.e+01 1.e+10]
Permutations & Combinations

293
SciPy also gives functionality to calculate Permutations and Combinations.
Combinations – scipy.special.comb(N,k)
Example:
from scipy.special import comb
#find combinations of 5, 2 values using comb(N, k)
com = comb(5, 2, exact = False, repetition=True)
print(com)
Output: 15.0
Permutations –
scipy.special.perm(N,k)
Example:
from scipy.special import perm
#find permutation of 5, 2 using perm (N, k) function
per = perm(5, 2, exact = True)
print(per)
Output: 20
Log Sum Exponential Function
Log Sum Exponential computes the log of sum exponential input element.
Syntax :
scipy.special.logsumexp(x)
Bessel Function
Nth integer order calculation function
Syntax :
scipy.special.jn()
Linear Algebra with SciPy
• Linear Algebra of SciPy is an implementation of BLAS and ATLAS LAPACK libraries.
• Performance of Linear Algebra is very fast compared to BLAS and LAPACK.
• Linear algebra routine accepts two-dimensional array object and output is also a two-dimensional
array.
Now let’s do some test with scipy.linalg,
Calculating determinant of a two-dimensional matrix,
from scipy import linalg
import numpy as np
294
#define square matrix
two_d_array = np.array([ [4,5], [3,2] ])
#pass values to det() function
linalg.det( two_d_array )
Output: -7.0
Inverse Matrix –
scipy.linalg.inv()
Inverse Matrix of Scipy calculates the inverse of any square matrix.
Let’s see,
from scipy import linalg
import numpy as np
# define square matrix
two_d_array = np.array([ [4,5], [3,2] ])
#pass value to function inv()
linalg.inv( two_d_array )
Output:
array( [[-0.28571429, 0.71428571],
[ 0.42857143, -0.57142857]] )
Eigenvalues and Eigenvector
scipy.linalg.eig()
• The most common problem in linear algebra is eigenvalues and eigenvector which can be easily solved
using eig()function.
• Now lets we find the Eigenvalue of (X) and correspond eigenvector of a two-dimensional square
matrix.
Example
from scipy import linalg
import numpy as np
#define two dimensional array
arr = np.array([[5,4],[6,3]])
#pass value into function
eg_val, eg_vect = linalg.eig(arr)
#get eigenvalues
print(eg_val)

295
#get eigenvectors
print(eg_vect)
Output:
[ 9.+0.j -1.+0.j] #eigenvalues
[ [ 0.70710678 -0.5547002 ] #eigenvectors
[ 0.70710678 0.83205029] ]
Discrete Fourier Transform – scipy.fftpack
• DFT is a mathematical technique which is used in converting spatial data into frequency data.
• FFT (Fast Fourier Transformation) is an algorithm for computing DFT
• FFT is applied to a multidimensional array.
• Frequency defines the number of signal or wavelength in particular time period.

Package Name Description

scipy.io File input/output

scipy.special Special Function

scipy.linalg Linear Algebra Operation

scipy.interpolate Interpolation

scipy.optimize Optimization and fit

scipy.stats Statistics and random numbers

scipy.integrate Numerical Integration

scipy.fftpack Fast Fourier transforms

scipy.signal Signal Processing

scipy.ndimage Image manipulation –

296
Matplotlib (Python Plotting Library)

Human minds are more adaptive for the visual representation of data rather than textual data. We can easily understand

things when they are visualized. It is better to represent the data through the graph where we can analyze the data more

efficiently and make the specific decision to data analysis. Before learning the matplotlib, we need to understand data

visualization and why data visualization is important.

Data Visualization

Graphics provides an excellent approach for exploring the data, which is essential for presenting results. Data visualization is

a new term. It expresses the idea that involves more than just representing data in the graphical form (instead of using

textual form).

This can be very helpful when discovering and getting to know a dataset and can help with classifying patterns, corrupt data, o
much more. With a little domain knowledge, data visualizations can be used to express and demonstrate key relationships in p
The static does indeed focus on quantitative description and estimations of data. It provides an important set of tools for

gaining a qualitative understanding.

There are five key plots that are used for data visualization.

297
There are five phases which are essential to make the decision for the organization:

Visualize: We analyze the raw data, which means it makes complex data more accessible, understandable, and more

usable. Tabular data representation is used where the user will look up a specific measurement, while the chart of several

types is used to show patterns or relationships in the data for one or more variables.

Analysis: Data analysis is defined as cleaning, inspecting, transforming, and modelling data to derive useful information.

Whenever we make a decision for the business or in daily life, is by past experience. What will happen to choose a

particular decision, it is nothing but analyzing our past. That may be affected in the future, so the proper analysis is

necessary for better decisions for any business or organization.

Document Insight: Document insight is the process where the useful data or information is organized in the document in

the standard format.

Transform Data Set: Standard data is used to make the decision more effectively.

Why need data visualization?

298
Data visualization can perform below tasks:

It identifies areas that need improvement and attention.

It clarifies the factors.

It helps to understand which product to place where.

Predict sales volumes.

Benefit of Data Visualization


Here are some benefits of the data visualization, which helps to make an effective decision for the organizations or business:

1. Building ways of absorbing information


Data visualization allows users to receive vast amounts of information regarding operational and business conditions. It

helps decision-makers to see the relationship between multi-dimensional data sets. It offers new ways to analyses data

through the use of maps, fever charts, and other rich graphical representations.

Visual data discovery is more likely to find the information that the organization needs and then end up with being more

productive than other competitive companies.

2. Visualize relationship and patterns in Businesses


The crucial advantage of data visualization is that it is essential to find the correlation between operating conditions and

business performance in today's highly competitive business environment.

299
The ability to make these types of correlations enables the executives to identify the root cause of the problem and act

quickly to resolve it.

Suppose a food company is looking their monthly customer data, and the data is presented with bar charts, which shows

that the company's score has dropped by five points in the previous months in that particular region; the data suggest that

there's a problem with customer satisfaction in this area.

3. Take action on the emerging trends faster


Data visualization allows the decision-maker to grasp shifts in customer behavior and market conditions across multiple

data sets more efficiently.

Having an idea about the customer's sentiments and other data discloses an emerging opportunity for the company

to act on new business opportunities ahead of their according competitor.

4. Geological based Visualization


Geo-spatial visualization is occurred due to many websites providing web-services, attracting visitor's interest. These types

of websites are required to take benefit of location-specific information, which is already present in the customer details.

Matplotlib is a Python library which is defined as a multi-platform data visualization library built on Numpy array. It can be

used in python scripts, shell, web application, and other graphical user interface toolkit.
ADVERTISEMENT

The John D. Hunter originally conceived the matplotlib in 2002. It has an active development community and is distributed

under a BSD-style license. Its first version was released in 2003, and the latest version 3.1.1 is released on 1 July 2019.

Matplotlib 2.0.x supports Python versions 2.7 to 3.6 till 23 June 2007. Python3 support started with

Matplotlib 1.2. Matplotlib 1.4 is the last version that supports Python 2.6.

There are various toolkits available that are used to enhance the functionality of the matplotlib. Some of these tools are

downloaded separately, others can be shifted with the matplotlib source code but have external dependencies.

Bashmap: It is a map plotting toolkit with several map projections, coastlines, and political boundaries.

Cartopy: It is a mapping library consisting of object-oriented map projection definitions, and arbitrary point, line, polygon,

300
and image transformation abilities.

Excel tools: Matplotlib provides the facility to utilities for exchanging data with Microsoft Excel.

Mplot3d: It is used for 3D plots.

Natgrid: It is an interface to the Natgrid library for irregular gridding of the spaced data.

Matplotlib Architecture
There are three different layers in the architecture of the matplotlib which are the following:

Backend Layer

Artist layer

Scripting layer

Backend layer
The backend layer is the bottom layer of the figure, which consists of the implementation of the various functions that are

necessary for plotting. There are three essential classes from the backend layer FigureCanvas(The surface on which the

figure will be drawn), Renderer(The class that takes care of the drawing on the surface), and Event(It handle the mouse

and keyboard events).

Artist Layer
The artist layer is the second layer in the architecture. It is responsible for the various plotting functions, like axis, which

coordinates on how to use the renderer on the figure canvas.

Scripting layer
The scripting layer is the topmost layer on which most of our code will run. The methods in the scripting layer, almost

automatically take care of the other layers, and all we need to care about is the current state (figure & subplot).

The General Concept of Matplotlib


A Matplotlib figure can be categorized into various parts as below:

301
Figure: It is a whole figure which may hold one or more axes (plots). We can think of a Figure as a canvas that holds plots.

Axes: A Figure can contain several Axes. It consists of two or three (in the case of 3D) Axis objects. Each Axes is comprised

of a title, an x-label, and a y-label.

Axis: Axises are the number of line like objects and responsible for generating the graph limits.

Artist: An artist is the all which we see on the graph like Text objects, Line2D objects, and collection objects. Most Artists

are tied to Axes.

Installing Matplotlib
Before start working with the Matplotlib or its plotting functions first, it needs to be installed. The installation of matplotlib

is dependent on the distribution that is installed on your computer. These installation methods are following:

Use the Anaconda distribution of Python


The easiest way to install Matplotlib is to download the Anaconda distribution of Python. Matplotlib is pre-installed in the

anaconda distribution No further installation steps are necessary.

Visit the official site of Anaconda and click on the Download Button

302
Choose download according to your Python interpreter configuration.

Install Matplotlib using with Anaconda Prompt

303
Matplotlib can be installed using with the Anaconda Prompt by typing command. To install matplotlib, open Anaconda

Prompt and type the following command:

conda install matplotlib

Install Matplotlib with pip


ADVERTISEMENT

The python package manager pip is also used to install matplotlib. Open the command prompt window, and type the

following command:

pip install matplotlib

Verify the Installation


To verify that matplotlib is installed properly or not, type the following command includes calling .__version __ in the

terminal.

import matplotlib

matplotlib.__version__

'3.1.1'

Basic Example of plotting Graph


Here is the basic example of generating a simple graph; the program is following:

from matplotlib import pyplot as plt

#ploting our canvas

plt.plot([1,2,3],[4,5,1])

#display the graph

plt.show()

Output:

304
It takes only three lines to plot a simple graph using the Python matplotlib. We can add titles, labels to our chart which are

created by Python matplotlib library to make it more meaningful. The example is the following:

from matplotlib import pyplot as plt

x = [5, 2, 7]

y = [1, 10, 4]

plt.plot(x, y)

plt.title('Line graph')

plt.ylabel('Y axis')

plt.xlabel('X axis')

plt.show()

Output:

The graph is more understandable from the previous graph.

Working with Pyplot


305
The matplotlib.pyplot is the collection command style functions that make matplotlib feel like working with MATLAB. The

pyplot functions are used to make some changes to figure such as create a figure, creates a plotting area in a figure, plots

some lines in a plotting area, decorates the plot including labels, etc.

It is good to use when we want to plot something quickly without instantiating any figure or Axes.

While working with matplotlib.pyplot, some states are stored across function calls so that it keeps track of the things like

current figure and plotting area, and these plotting functions are directed to the current axes.

The pyplot module provide the plot() function which is frequently use to plot a graph. Let's have a look on the simple

example:

from matplotlib import pyplot as plt

plt.plot([1,2,3,4,5])

plt.ylabel("y axis")

plt.xlabel('x axis')

plt.show()

Output:

In the above program, it plots the graph x-axis ranges from 0-4 and the y-axis from 1-5. If we provide a single list to the

plot(), matplotlib assumes it is a sequence of y values, and automatically generates the x values. Since we know that python

index starts at 0, the default x vector has the same length as y but starts at 0. Hence the x data are [0, 1, 2, 3, 4].

We can pass the arbitrary number of arguments to the plot(). For example, to plot x versus y, we can do this following way:

from matplotlib import pyplot as plt

plt.plot([1,2,3,4,5],[1,4,9,16,25])

plt.ylabel("y axis")

plt.xlabel('x axis')

plt.show()

306
Output:

Formatting the style of the plot


There is an optional third argument, which is a format string that indicates the color and line type of the plot. The default

format string is 'b-'which is the solid blue as you can observe in the above plotted graph. Let's consider the following

example where we plot the graph with the red circle.

from matplotlib import pyplot as plt

plt.plot([1, 2, 3, 4,5], [1, 4, 9, 16,25], 'ro')

plt.axis([0, 6, 0, 20])

plt.show()

Output:

Example format String

'b' Using for the blue marker with default shape.

'ro' Red circle

307
'-g' Green solid line

'--' A dashed line with the default color

'^k:' Black triangle up markers connected by a dotted line

The matplotlib supports the following color abbreviation:

Character Color

'b' Blue

'g' Green

'r' Red

'c' Cyan

'm' Magenta

'y' Yellow

'k' Black

'w' White

Plotting with categorical variables


Matplotlib allows us to pass categorical variables directly to many plotting functions: consider the following example

from matplotlib import pyplot

names = ['Abhishek', 'Himanshu', 'Devansh']

marks= [87,50,98]

plt.figure(figsize=(9,3))

plt.subplot(131)

plt.bar(names, marks)

plt.subplot(132)

plt.scatter(names, marks)

308
plt.subplot(133)

plt.plot(names, marks)

plt.suptitle('Categorical Plotting')

plt.show()

Output:

In the above program, we have plotted the categorical graph using the subplot() function.

Let's a have a look on the subplot() function.

What is subplot()
The Matplotlib subplot() function is defined as to plot two or more plots in one figure. We can use
this method to separate two graphs which plotted in the same axis Matplotlib supports all kinds of subplots, including

2x1 vertical, 2x1 horizontal, or a 2x2 grid.

It accepts the three arguments: they are nrows, ncols, and index. It denote the number of rows, number of columns and

the index.

The subplot() function can be called in the following way:

subplot(nrows,ncols,index,**kwargs)

subplot(pos,**kwargs)

subplot(ax)

Parameters:
*args:
Three separate integers or three-digit integer describes the position of the subplot. If the three integers are nrows, ncols,

and index in order, the subplot will take the index position on a grid with nrows row and ncol column.

The argument pos are a three-digit integer, where the first digit is denoted the number of rows, the second digit denoted

the number of columns, and the third represents the index of the subplot. For example, subplot (1, 3, 2) is the same as the

subplot (132).

Note: Passed integer must be less than 10.

309
**kwargs
The subplot() function also accepts the keyword arguments for the returned axes base class.

Consider the following example:

Creating different types of graph


1. Line graph
The line graph is one of charts which shows information as a series of the line. The graph is plotted by the plot() function.

The line graph is simple to plot; let's consider the following example:

from matplotlib import pyplot as plt

x = [4,8,9]

y = [10,12,15]

plt.plot(x,y)

plt.title("Line graph")

plt.ylabel('Y axis')

plt.xlabel('X axis')

plt.show()

Output:

We can customize the graph by importing the style module. The style module will be built into a matplotlib installation.

It contains the various functions to make the plot more attractive. In the below program, we are using the style module:

from matplotlib import pyplot as plt

310
from matplotlib import style

style.use('ggplot')

x = [16, 8, 10]

y = [8, 16, 6]

x2 = [8, 15, 11]

y2 = [6, 15, 7]

plt.plot(x, y, 'r', label='line one', linewidth=5)

plt.plot(x2, y2, 'm', label='line two', linewidth=5)

plt.title('Epic Info')

fig = plt.figure()

plt.ylabel('Y axis')

plt.xlabel('X axis')

plt.legend()

plt.grid(True, color='k')

plt.show()

Output:

In Matplotlib, the figure (an instance of class plt.Figure) can be supposed of as a single container that consists of all the

objects denoting axes, graphics, text, and labels.

Example-3
import numpy as np

import matplotlib.pyplot as plt

311
fig = plt.figure()

ax = plt.axes()

x = np.linspace(0, 10, 1000)

ax.plot(x, np.sin(x))

Output:

The matplotlib provides the fill_between() function which is used to fill area around the lines based on the user defined

logic.

Example-4
import numpy as np

import matplotlib.pyplot as plt

fig = plt.figure()

ax = plt.axes()

x = np.linspace(0, 10, 1000)

ax.plot(x, np.sin(x))

import matplotlib.pyplot as plt

import numpy as np

x = np.arange(0.0, 2, 0.01)

312
y1 = np.sin(2 * np.pi * x)

y2 = 1.2 * np.sin(4 * np.pi * x)

fig, ax = plt.subplots(1, sharex=True)

ax.plot(x, y1, x, y2, color='black')

ax.fill_between(x, y1, y2, where=y2 >= y1, facecolor='blue', interpolate=True)

ax.fill_between(x, y1, y2, where=y2 <= y1, facecolor='red', interpolate=True)

ax.set_title('fill between where')

Output:

2. Bar graphs
Bar graphs are one of the most common types of graphs and are used to show data associated with the categorical variables.
provides a bar() to make bar graphs which accepts arguments such as: categorical variables, their value and color.

from matplotlib import pyplot as plt

players = ['Virat','Rohit','Shikhar','Hardik']

runs = [51,87,45,67]

plt.bar(players,runs,color = 'green')

plt.title('Score Card')

plt.xlabel('Players')

plt.ylabel('Runs')

plt.show()

Output:

313
Another function barh() is used to make horizontal bar graphs. It accepts xerr or yerr as arguments (in case of vertical

graphs) to depict the variance in our data as follows:

from matplotlib import pyplot as plt

players = ['Virat','Rohit','Shikhar','Hardik']

runs = [51,87,45,67]

plt.barh(players,runs, color = 'green')

plt.title('Score Card')

plt.xlabel('Players')

plt.ylabel('Runs')

plt.show()

Output:

Let's have a look on the other example using the style() function:

from matplotlib import pyplot as plt

from matplotlib import style

314
style.use('ggplot')

x = [5,8,10]

y = [12,16,6]

x2 = [6,9,11]

y2 = [7,15,7]

plt.bar(x, y, color = 'y', align='center')

plt.bar(x2, y2, color='c', align='center')

plt.title('Information')

plt.ylabel('Y axis')

plt.xlabel('X axis')

Output:

Similarly to vertical stack, the bar graph together by using the bottom argument and define the bar graph, which we want

to stack below and its value.

from matplotlib import pyplot as plt

import numpy as np

315
countries = ['USA', 'India', 'China', 'Russia', 'Germany']

bronzes = np.array([38, 17, 26, 19, 15])

silvers = np.array([37, 23, 18, 18, 10])

golds = np.array([46, 27, 26, 19, 17])

ind = [x for x, _ in enumerate(countries)]

plt.bar(ind, golds, width=0.5, label='golds', color='gold', bottom=silvers+bronzes)

plt.bar(ind, silvers, width=0.5, label='silvers', color='silver', bottom=bronzes)

plt.bar(ind, bronzes, width=0.5, label='bronzes', color='#CD853F')

plt.xticks(ind, countries)

plt.ylabel("Medals")

plt.xlabel("Countries")

plt.legend(loc="upper right")

plt.title("2019 Olympics Top Scorers")

Output:

3. Pie Chart
A pie chart is a circular graph that is broken down in the segment or slices of pie. It is generally used to represent the

percentage or proportional data where each slice of pie represents a particular category. Let's have a look at the below

example:

from matplotlib import pyplot as plt

316
# Pie chart, where the slices will be ordered and plotted counter-clockwise:

Players = 'Rohit', 'Virat', 'Shikhar', 'Yuvraj'

Runs = [45, 30, 15, 10]

explode = (0.1, 0, 0, 0) # it "explode" the 1st slice

fig1, ax1 = plt.subplots()

ax1.pie(Runs, explode=explode, labels=Players, autopct='%1.1f%%',

shadow=True, startangle=90)

ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.

plt.show()

Output:

4. Histogram
First, we need to understand the difference between the bar graph and histogram. A histogram is used for the distribution,

whereas a bar chart is used to compare different entities. A histogram is a type of bar plot that shows the frequency of a

number of values compared to a set of values ranges.

For example we take the data of the different age group of the people and plot a histogram with respect to the bin. Now,

bin represents the range of values that are divided into series of intervals. Bins are generally created of the same size.

from matplotlib import pyplot as plt

from matplotlib import pyplot as plt

population_age = [21,53,60,49,25,27,30,42,40,1,2,102,95,8,15,105,70,65,55,70,75,60,52,44,43,42,45]

bins = [0,10,20,30,40,50,60,70,80,90,100]

plt.hist(population_age, bins, histtype='bar', rwidth=0.8)

plt.xlabel('age groups')

317
plt.ylabel('Number of people')

plt.title('Histogram')

plt.show()

Output:

Let's consider the another example of plotting histogram:

from matplotlib import pyplot as plt

# Importing Numpy Library

import numpy as np

plt.style.use('fivethirtyeight')

mu = 50

sigma = 7

x = np.random.normal(mu, sigma, size=200)

fig, ax = plt.subplots()

ax.hist(x, 20)

ax.set_title('Historgram')

ax.set_xlabel('bin range')

ax.set_ylabel('frequency')

fig.tight_layout()

plt.show()

318
Output:

5. Scatter plot
The scatter plots are mostly used for comparing variables when we need to define how much one variable is affected by

another variable. The data is displayed as a collection of points. Each point has the value of one variable, which defines the

position on the horizontal axes, and the value of other variable represents the position on the vertical axis.

Let's consider the following simple example:

Example-1:
from matplotlib import pyplot as plt

from matplotlib import style

style.use('ggplot')

x = [5,7,10]

y = [18,10,6]

x2 = [6,9,11]

y2 = [7,14,17]

plt.scatter(x, y)

plt.scatter(x2, y2, color='g')

plt.title('Epic Info')

plt.ylabel('Y axis')

319
plt.xlabel('X axis')

plt.show()

Output:

Example-2
import matplotlib.pyplot as plt

x = [2, 2.5, 3, 3.5, 4.5, 4.7, 5.0]

y = [7.5, 8, 8.5, 9, 9.5, 10, 10.5]

x1 = [9, 8.5, 9, 9.5, 10, 10.5, 12]

y1 = [3, 3.5, 4.7, 4, 4.5, 5, 5.2]

plt.scatter(x, y, label='high income low saving', color='g')

plt.scatter(x1, y1, label='low income high savings', color='r')

plt.xlabel('saving*100')

plt.ylabel('income*1000')

plt.title('Scatter Plot')

plt.legend()

plt.show()

Output:

320
6. 3D graph plot
Matplotlib was initially developed with only two-dimension plot. Its 1.0 release was built with some

f three-dimensional plotting utilities on top of two-dimension display, and the result is a convenient set of tools for 3D data

visualization.

Three-dimension plots can be created by importing the mplot3d toolkit, include with the main Matplotlib installation:

from mpl_toolkits import mplot3d

When this module is imported in the program, three-dimension axes can be created by passing the keyword projection='3d'
normal axes creation routines:

Let's see the simple 3D plot

Example-1:
from mpltoolkits import mplot3d

import numpy as np

import matplotlib.pyplot as plt

fig = plt.figure()

ax = plt.axes(projection='3d')_

Output:

321
Example-2:
from mpl_toolkits import mplot3d

import numpy as np

import matplotlib.pyplot as plt

height = np.array([100,110,87,85,65,80,96,75,42,59,54,63,95,71,86])

weight = np.array([105,123,84,85,78,95,69,42,87,91,63,83,75,41,80])

scatter(height,weight)

fig = plt.figure()

ax = plt.axes(projection='3d')

# This is used to plot 3D scatter

ax.scatter3D(height,weight)

plt.title("3D Scatter Plot")

plt.xlabel("Height")

plt.ylabel("Weight")

plt.title("3D Scatter Plot")

plt.xlabel("Height")

plt.ylabel("Weight")

plt.show()

Output:

322
Note: We can use the plot3D () to plot simple 3D line graph.

Example-3
import matplotlib as mpl

from mpl_toolkits.mplot3d import Axes3D

import numpy as np

import matplotlib.pyplot as plt

mpl.rcParams['legend.fontsize'] = 10

fig = plt.figure()

ax = fig.gca(projection='3d')

theta1 = np.linspace(-4 * np.pi, 4 * np.pi, 100)

z = np.linspace(-2, 2, 100)

r = z**2 + 1

x = r * np.sin(theta1)

y = r * np.cos(theta1)

ax.plot3D(x, y, z, label='parametric curve', color = 'red')

ax.legend()

plt.show()

Output:

323
Important functions of Matplotlib
Functions Description

plot(x-axis value, y-axis-values) It is used to plot a simple line graph with x-axis value against the y-axis
values. show() It is used to display the graph.

title("string") It is used to set the title of the plotted graph as specified by the string.

xlabel("string") It is used to set the label for the x-axis as specified by the string.

ylabel("string") It is used to set the label for y-axis as specified by the string.

figure() It is used to control a figure level attributes.

subplots(nrows,ncol,index) It is used to add a subplot to recent figure.

subtitle("string") It adds a common title to the plotted graph specified by the string.

subplots(nrows,ncols,figsize) It provides the simple way to create subplot, in a single call and returns a
tuple of a figure and number of axes.

set_title("string") It is an axes level method which is used to set the title of the subplots.

bar(categorical variables, values, It is used to create a vertical bar graph.


color)

barh(categorical variables, values, It is used to create horizontal bar graphs.


color)

324
legend(loc) It is used to make a legend of the graph.

xtricks(index, categorical variables) It is used to set or get the current tick locations labels of the x-axis.

pie(value, categorical variables) It is used to create a pie chart.

hist(value, number of bins) It is used to create a histogram.

xlim(start value, end value) It is used to set the limit of values of the x-axis.

ylim(start value, end value) It is used to set the limit of values of the y-axis.

scatter(x-axis values, y-axis values) It is used to plots a scatter plot with x-axis value against the y-axis values.

axes() It is used to add axes to the recent figure.

set_xlabel("string") It is an axes level method which is used to set the x-label of the plot
specified as a string.

set_ylabel("string") It is used to set the y-label of the plot specified as a string.

scatter3D(x-axis values, y-axis It is used to plot a three-dimension scatter plot with x- axis value against
values) the y-axis.

plot3D(x-axis values, y-axis values) It is used to plots a three-dimension line graph with x- axis values against
y-axis values.

325

You might also like