Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
23 views

Unit 4 Python

This document provides an overview of Python's NumPy library, detailing its functionalities, advantages over Python lists, and various operations like array creation, reshaping, and mathematical functions. It also introduces the Pandas library, highlighting its purpose for working with relational data and its development history. The document includes practical examples and code snippets to illustrate the concepts discussed.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Unit 4 Python

This document provides an overview of Python's NumPy library, detailing its functionalities, advantages over Python lists, and various operations like array creation, reshaping, and mathematical functions. It also introduces the Pandas library, highlighting its purpose for working with relational data and its development history. The document includes practical examples and code snippets to illustrate the concepts discussed.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

lOMoARcPSD|50078205

Python UNIT 4 - good

Python programming (Anna University)

Scan to open on Studocu

Studocu is not sponsored or endorsed by any college or university


Downloaded by Anthonyammal M (anthonyammal191979@gmail.com)
lOMoARcPSD|50078205

MC4103 PYTHON PROGRAMMING UNIT 4 PACKAGE AND FRAMEWORKS

What are NumPy Arrays?

NumPy is a Python package that stands for ‘Numerical Python’. It is the core library for
scientific computing, which contains a powerful n-dimensional array object.

Where is NumPy used?

Python NumPy arrays provide tools for integrating C, C++, etc. It is also useful in linear
algebra, random number capability etc. NumPy array can also be used as an efficient multi-
dimensional container for generic data. Now, let me tell you what exactly is a Python NumPy
array.

Python NumPy Array: Numpy array is a powerful N-dimensional array object which is in the
form of rows and columns. We can initialize NumPy arrays from nested Python lists and access
it elements. In order to perform these NumPy operations, the next question which will come in
your mind is:

How do I install NumPy?

To install Python NumPy, go to your command prompt and type “pip install numpy”. Once the
installation is completed, go to your IDE (For example: PyCharm) and simply import it by
typing: “import numpy as np”

Moving ahead in python numpy tutorial, let us understand what exactly is a multi-dimensional
numPy array.

Here, I have different elements that are stored in their respective memory locations. It is said
to be two dimensional because it has rows as well as columns. In the above image, we have 3
columns and 4 rows available.

How do I start NumPy?

Let us see how it is implemented in PyCharm:

Single-dimensional Numpy Array:

Prepared By.E.Janakiraman.MCA,MPhil,. | AP/APEC DEPT.OF MCA

Downloaded by Anthonyammal M (anthonyammal191979@gmail.com)


lOMoARcPSD|50078205

MC4103 PYTHON PROGRAMMING UNIT 4 PACKAGE AND FRAMEWORKS

import numpy as
np
a=np.array([1,2,3])
print(a)
Output – [1 2 3]

Multi-dimensional Array:

a=np.array([(1,2,3),(4,5,6)])
print(a)
O/P – [[ 1 2 3]
[4 5 6]]

Many of you must be wondering that why do we use python NumPy if we already have Python
list? So, let us understand with some examples in this python NumPy tutorial.

Python NumPy Array v/s List

Why NumPy is used in Python?

We use python NumPy array instead of a list because of the below three reasons:

1. Less Memory
2. Fast
3. Convenient

The very first reason to choose python NumPy array is that it occupies less memory as
compared to list. Then, it is pretty fast in terms of execution and at the same time, it is very
convenient to work with NumPy. So these are the major advantages that Python NumPy array
has over list. Don’t worry, I am going to prove the above points one by one practically in
PyCharm. Consider the below example:

import numpy as np

import time
import sys
S= range(1000)
print(sys.getsizeof(5)*len(S))

D= np.arange(1000)
print(D.size*D.itemsize)
O/P – 14000

4000

Prepared By.E.Janakiraman.MCA,MPhil,. | AP/APEC DEPT.OF MCA

Downloaded by Anthonyammal M (anthonyammal191979@gmail.com)


lOMoARcPSD|50078205

MC4103 PYTHON PROGRAMMING UNIT 4 PACKAGE AND FRAMEWORKS

The above output shows that the memory allocated by list (denoted by S) is 14000 whereas the
memory allocated by the NumPy array is just 4000. From this, you can conclude that there is
a major difference between the two and this makes Python NumPy array as the preferred choice
over list.

Next, let’s talk how python NumPy array is faster and more convenient when compared to list.

import time
import sys

SIZE = 1000000

L1= range(SIZE)
L2= range(SIZE)
A1= np.arange(SIZE)
A2=np.arange(SIZE)

start= time.time()
result=[(x,y) for x,y in zip(L1,L2)]
print((time.time()-start)*1000)

start=time.time()
result= A1+A2
print((time.time()-start)*1000)

O/P – 380.9998035430908

49.99995231628418

In the above code, we have defined two lists and two numpy arrays. Then, we have compared
the time taken in order to find the sum of lists and sum of numpy arrays both. If you see the
output of the above program, there is a significant change in the two values. List took 380ms
whereas the numpy array took almost 49ms. Hence, numpy array is faster than list. Now, if you
noticed we had run a ‘for’ loop for a list which returns the concatenation of both the lists
whereas for numpy arrays, we have just added the two array by simply printing A1+A2. That’s
why working with numpy is much easier and convenient when compared to the lists.

Therefore, the above examples proves the point as to why you should go for python numpy
array rather than a list!

Moving forward in python numpy tutorial, let’s focus on some of its operations.

Prepared By.E.Janakiraman.MCA,MPhil,. | AP/APEC DEPT.OF MCA

Downloaded by Anthonyammal M (anthonyammal191979@gmail.com)


lOMoARcPSD|50078205

MC4103 PYTHON PROGRAMMING UNIT 4 PACKAGE AND FRAMEWORKS

You may go through this recording of Python NumPy tutorial where our instructor has
explained the topics in a detailed manner with examples that will help you to

Python NumPy Operations

Ndim


You can find the dimension of the array, whether it is a two-dimensional array or a
single dimensional array. So, let us see this practically how we can find the dimensions.
In the below code, with the help of ‘ndim’ function, I can find whether the array is of
single dimension or multi dimension.

import numpy as np
a = np.array([(1,2,3),(4,5,6)])
print(a.ndim)
Output – 2

Since the output is 2, it is a two-dimensional array (multi dimension).

itemsize:


You can calculate the byte size of each element. In the below code, I have defined a
single dimensional array and with the help of ‘itemsize’ function, we can find the size
of each element.

import numpy as np
a = np.array([(1,2,3)])
print(a.itemsize)

Output – 4

So every element occupies 4 byte in the above numpy array.

Prepared By.E.Janakiraman.MCA,MPhil,. | AP/APEC DEPT.OF MCA

Downloaded by Anthonyammal M (anthonyammal191979@gmail.com)


lOMoARcPSD|50078205

MC4103 PYTHON PROGRAMMING UNIT 4 PACKAGE AND FRAMEWORKS

dtype:


You can find the data type of the elements that are stored in an array. So, if you want
to know the data type of a particular element, you can use ‘dtype’ function which will
print the datatype along with the size. In the below code, I have defined an array
where I have used the same function.

import numpy as np
a = np.array([(1,2,3)])
print(a.dtype)

Output – int32

As you can see, the data type of the array is integer 32 bits. Similarly, you can find the size and
shape of the array using ‘size’ and ‘shape’ function respectively.

import numpy as np
a = np.array([(1,2,3,4,5,6)])
print(a.size)
print(a.shape)
Output – 6 (1,6)

Next, let us move forward and see what are the other operations that you can perform with
python numpy module. We can also perform reshape as well as slicing operation using python
numpy operation. But, what exactly is reshape and slicing? So let me explain this one by one
in this python numpy tutorial.

• reshape:
Reshape is when you change the number of rows and columns which gives a new view
to an object. Now, let us take an example to reshape the below array:

Prepared By.E.Janakiraman.MCA,MPhil,. | AP/APEC DEPT.OF MCA

Downloaded by Anthonyammal M (anthonyammal191979@gmail.com)


lOMoARcPSD|50078205

MC4103 PYTHON PROGRAMMING UNIT 4 PACKAGE AND FRAMEWORKS

As you can see in the above image, we have 3 columns and 2 rows which has converted
into 2 columns and 3 rows. Let me show you practically how it’s done.

import numpy as np
a = np.array([(8,9,10),(11,12,13)])
print(a)
a=a.reshape(3,2)
print(a)
Output – [[ 8 9 10] [11 12 13]]

[[ 8 9] [10 11] [12 13]]

• linspace
This is another operation in python numpy which returns evenly spaced numbers over
a specified interval. Consider the below example:

import numpy as np
a=np.linspace(1,3,10)
print(a)
• Output – [ 1. 1.22222222 1.44444444 1.66666667 1.88888889 2.11111111
2.33333333 2.55555556 2.77777778 3. ]
• As you can see in the result, it has printed 10 values between 1 to 3.
• max/ min
Next, we have some more operations in numpy such as to find the minimum, maximum
as well the sum of the numpy array. Let’s go ahead in python numpy tutorial and
execute it practically.

1import numpy as np
2
3a= np.array([1,2,3])
4print(a.min())
5print(a.max())
6print(a.sum())
• Output – 1 3 6

Prepared By.E.Janakiraman.MCA,MPhil,. | AP/APEC DEPT.OF MCA

Downloaded by Anthonyammal M (anthonyammal191979@gmail.com)


lOMoARcPSD|50078205

MC4103 PYTHON PROGRAMMING UNIT 4 PACKAGE AND FRAMEWORKS

• You must be finding these pretty basic, but with the help of this knowledge you can
perform a lot bigger tasks as well. Now, lets understand the concept of axis in python
numpy.




As you can see in the figure, we have a numpy array 2*3. Here the rows are called as
axis 1 and the columns are called as axis 0. Now you must be wondering what is the
use of these axis?
• Suppose you want to calculate the sum of all the columns, then you can make use of
axis. Let me show you practically, how you can implement axis in your PyCharm:

1a= np.array([(1,2,3),(3,4,5)])
2print(a.sum(axis=0))
• Output – [4 6 8]
• Therefore, the sum of all the columns are added where 1+3=4, 2+4=6 and 3+5=8.
Similarly, if you replace the axis by 1, then it will print [6 12] where all the rows get
added.
• Square Root & Standard Deviation

There are various mathematical functions that can be performed using python numpy.
You can find the square root, standard deviation of the array. So, let’s implement
these operations:

1import numpy as np
2a=np.array([(1,2,3),(3,4,5,)])
3print(np.sqrt(a))
4print(np.std(a))
Output – [[ 1. 1.41421356 1.73205081]
[ 1.73205081 2. 2.23606798]]
1.29099444874

As you can see the output above, the square root of all the elements are printed. Also,
the standard deviation is printed for the above array i.e how much each element varies
from the mean value of the python numpy array.

Prepared By.E.Janakiraman.MCA,MPhil,. | AP/APEC DEPT.OF MCA

Downloaded by Anthonyammal M (anthonyammal191979@gmail.com)


lOMoARcPSD|50078205

MC4103 PYTHON PROGRAMMING UNIT 4 PACKAGE AND FRAMEWORKS

• Addition Operation

You can perform more operations on numpy array i.e addition,


subtraction,multiplication and division of the two matrices. Let me go ahead in python
numpy tutorial, and show it to you practically:

1import numpy as np
2x= np.array([(1,2,3),(3,4,5)])
3y= np.array([(1,2,3),(3,4,5)])
4print(x+y)
• Output – [[ 2 4 6] [ 6 8 10]]
• This is extremely simple! Right? Similarly, we can perform other operations such as
subtraction, multiplication and division. Consider the below example:

1import numpy as np
2x= np.array([(1,2,3),(3,4,5)])
3y= np.array([(1,2,3),(3,4,5)])
4print(x-y)
5print(x*y)
6print(x/y)
• Output – [[0 0 0] [0 0 0]]
[[ 1 4 9] [ 9 16 25]]
[[ 1. 1. 1.] [ 1. 1. 1.]]
• Vertical & Horizontal Stacking
Next, if you want to concatenate two arrays and not just add them, you
• ravel
There is one more operation where you can convert one numpy array into a single
column i.e ravel. Let me show how it is implemented practically:

import numpy as np
x= np.array([(1,2,3),(3,4,5)])
print(x.ravel())
• Output – [ 1 2 3 3 4 5]
• Let’s move forward in python numpy tutorial, and look at some of its special
functions.

Introduction to Pandas in Python


Pandas is an open-source library that is made mainly for working with relational or
labeled data both easily and intuitively. It provides various data structures and operations for
manipulating numerical data and time series. This library is built on top of the NumPy
library. Pandas is fast and it has high performance & productivity for users.
History
Pandas were initially developed by Wes McKinney in 2008 while he was working at AQR
Capital Management. He convinced the AQR to allow him to open source the Pandas.
Another AQR employee, Chang She, joined as the second major contributor to the library in

Prepared By.E.Janakiraman.MCA,MPhil,. | AP/APEC DEPT.OF MCA

Downloaded by Anthonyammal M (anthonyammal191979@gmail.com)


lOMoARcPSD|50078205

MC4103 PYTHON PROGRAMMING UNIT 4 PACKAGE AND FRAMEWORKS

2012. Over time many versions of pandas have been released. The latest version of the
pandas is 1.3.4
Advantages
• Fast and efficient for manipulating and analyzing data.
• Data from different file objects can be loaded.
• Easy handling of missing data (represented as NaN) in floating point as well as
non-floating point data
• Size mutability: columns can be inserted and deleted from DataFrame and higher
dimensional objects
• Data set merging and joining.
• Flexible reshaping and pivoting of data sets
• Provides time-series functionality.
• Powerful group by functionality for performing split-apply-combine operations on
data sets.
Getting Started
After the pandas have been installed into the system, you need to import the library. This
module is generally imported as:
import pandas as pd
Here, pd is referred to as an alias to the Pandas. However, it is not necessary to import the
library using the alias, it just helps in writing less amount code every time a method or
property is called.
Pandas generally provide two data structures for manipulating data, They are:
• Series
• DataFrame

Series:

Pandas Series is a one-dimensional labelled array capable of holding data of any type
(integer, string, float, python objects, etc.). The axis labels are collectively called indexes.
Pandas Series is nothing but a column in an excel sheet. Labels need not be unique but must
be a hashable type. The object supports both integer and label-based indexing and provides a
host of methods for performing operations involving the index.
A pandas Series can be created using the following constructor −
pandas.Series( data, index, dtype, copy)

Prepared By.E.Janakiraman.MCA,MPhil,. | AP/APEC DEPT.OF MCA

Downloaded by Anthonyammal M (anthonyammal191979@gmail.com)


lOMoARcPSD|50078205

MC4103 PYTHON PROGRAMMING UNIT 4 PACKAGE AND FRAMEWORKS

In the real world, a Pandas Series will be created by loading the datasets from existing
storage, storage can be SQL Database, CSV file, an Excel file. Pandas Series can be created
from the lists, dictionary, and from a scalar value etc.
Example:

import pandas as pd
import numpy as np
# Creating empty series
ser = pd.Series()

print(ser)

# simple array
data = np.array(['g', 'e', 'e', 'k', 's'])

ser = pd.Series(data)
print(ser)

Output:
Series([], dtype: float64)

Prepared By.E.Janakiraman.MCA,MPhil,. | AP/APEC DEPT.OF MCA

Downloaded by Anthonyammal M (anthonyammal191979@gmail.com)


lOMoARcPSD|50078205

MC4103 PYTHON PROGRAMMING UNIT 4 PACKAGE AND FRAMEWORKS

0 g
1 e
2 e
3 k
4 s
dtype: object
Example 2

#import the pandas library and aliasing as pd


import pandas as pd
import numpy as np
data = np.array(['a','b','c','d'])
s = pd.Series(data,index=[100,101,102,103])
print s
Its output is as follows −
100 a
101 b
102 c
103 d
dtype: object

DataFrame

Pandas DataFrame is a two-dimensional size-mutable, potentially heterogeneous tabular data


structure with labeled axes (rows and columns). A Data frame is a two-dimensional data
structure, i.e., data is aligned in a tabular fashion in rows and columns. Pandas DataFrame
consists of three principal components, the data, rows, and columns.
A pandas DataFrame can be created using the following constructor −
pandas.DataFrame( data, index, columns, dtype, copy)

Prepared By.E.Janakiraman.MCA,MPhil,. | AP/APEC DEPT.OF MCA

Downloaded by Anthonyammal M (anthonyammal191979@gmail.com)


lOMoARcPSD|50078205

MC4103 PYTHON PROGRAMMING UNIT 4 PACKAGE AND FRAMEWORKS

Creating a DataFrame:
In the real world, a Pandas DataFrame will be created by loading the datasets from existing
storage, storage can be SQL Database, CSV file, an Excel file. Pandas DataFrame can be
created from the lists, dictionary, and from a list of dictionaries, etc.
A pandas DataFrame can be created using various inputs like −

• Lists
• dict
• Series
• Numpy ndarrays
• Another DataFrame

Example:

import pandas as p
# Calling DataFrame constructor
df = pd.DataFrame()
print(df)
# list of strings
lst = ['python', 'programming', 'is', 'easy',
'to', 'undertanding', 'students']

Prepared By.E.Janakiraman.MCA,MPhil,. | AP/APEC DEPT.OF MCA

Downloaded by Anthonyammal M (anthonyammal191979@gmail.com)


lOMoARcPSD|50078205

MC4103 PYTHON PROGRAMMING UNIT 4 PACKAGE AND FRAMEWORKS

# Calling DataFrame constructor on list


df = pd.DataFrame(lst)
print(df)

Output:
Empty DataFrame
Columns: []
Index: []
0
0 python
1 language
2 is
3 easy
4 to
5 understanding
6 students

Example 2
import pandas as pd
data = [['Alex',10],['Bob',12],['Clarke',13]]
df = pd.DataFrame(data,columns=['Name','Age'])
print df
Its output is as follows –

Name Age
0 Alex 10
1 Bob 12
2 Clarke 13
panel
A panel is a 3D container of data. The term Panel data is derived from econometrics and is
partially responsible for the name pandas − pan(el)-da(ta)-s.
The names for the 3 axes are intended to give some semantic meaning to describing operations
involving panel data. They are −
• items − axis 0, each item corresponds to a DataFrame contained inside.

Prepared By.E.Janakiraman.MCA,MPhil,. | AP/APEC DEPT.OF MCA

Downloaded by Anthonyammal M (anthonyammal191979@gmail.com)


lOMoARcPSD|50078205

MC4103 PYTHON PROGRAMMING UNIT 4 PACKAGE AND FRAMEWORKS

• major_axis − axis 1, it is the index (rows) of each of the DataFrames.


• minor_axis − axis 2, it is the columns of each of the DataFrames.
pandas.Panel()
A Panel can be created using the following constructor −
pandas.Panel(data, items, major_axis, minor_axis, dtype, copy)

Python | Introduction to Matplotlib

Matplotlib is an amazing visualization library in Python for 2D plots of arrays.


Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed
to work with the broader SciPy stack. It was introduced by John Hunter in the year 2002.
One of the greatest benefits of visualization is that it allows us visual access to huge amounts
of data in easily digestible visuals. Matplotlib consists of several plots like line, bar, scatter,
histogram etc.
Installation :
Windows, Linux and macOS distributions have matplotlib and most of its dependencies as
wheel packages. Run the following command to install matplotlib package :

python -mpip install -U matplotlib

Importing matplotlib :

from matplotlib import pyplot as plt


or
import matplotlib.pyplot as plt

Basic plots in Matplotlib :


Matplotlib comes with a wide variety of plots. Plots helps to understand trends,
patterns, and to make correlations. They’re typically instruments for reasoning about
quantitative information. Some of the sample plots are covered here.
Line plot :

# importing matplotlib module


from matplotlib import pyplot as plt
# x-axis values
x = [5, 2, 9, 4, 7]
# Y-axis values

Prepared By.E.Janakiraman.MCA,MPhil,. | AP/APEC DEPT.OF MCA

Downloaded by Anthonyammal M (anthonyammal191979@gmail.com)


lOMoARcPSD|50078205

MC4103 PYTHON PROGRAMMING UNIT 4 PACKAGE AND FRAMEWORKS

y = [10, 5, 8, 4, 2]

# Function to plot
plt.plot(x,y)
# function to show the plot
plt.show()

Output :

Bar plot :

# importing matplotlib module


from matplotlib import pyplot as plt
# x-axis values
x = [5, 2, 9, 4, 7]
# Y-axis values
y = [10, 5, 8, 4, 2]
# Function to plot the bar
plt.bar(x,y)
# function to show the plot
plt.show()

Prepared By.E.Janakiraman.MCA,MPhil,. | AP/APEC DEPT.OF MCA

Downloaded by Anthonyammal M (anthonyammal191979@gmail.com)


lOMoARcPSD|50078205

MC4103 PYTHON PROGRAMMING UNIT 4 PACKAGE AND FRAMEWORKS

Output :

Histogram :

# importing matplotlib module


from matplotlib import pyplot as plt
# Y-axis values
y = [10, 5, 8, 4, 2]
# Function to plot histogram
plt.hist(y)
# Function to show the plot
plt.show()

Output :

Prepared By.E.Janakiraman.MCA,MPhil,. | AP/APEC DEPT.OF MCA

Downloaded by Anthonyammal M (anthonyammal191979@gmail.com)


lOMoARcPSD|50078205

MC4103 PYTHON PROGRAMMING UNIT 4 PACKAGE AND FRAMEWORKS

Scatter Plot :

# importing matplotlib module


from matplotlib import pyplot as plt
# x-axis values
x = [5, 2, 9, 4, 7]
# Y-axis values
y = [10, 5, 8, 4, 2]
# Function to plot scatter
plt.scatter(x, y)
# function to show the plot
plt.show()

Output :

Python Plotly
Python Plotly Library is an open-source library that can be used for data visualization
and understanding data simply and easily. Plotly supports various types of plots like line
charts, scatter plots, histograms, cox plots, etc. So you all must be wondering why Plotly over
other visualization tools or libraries? Here’s the answer –
• Plotly has hover tool capabilities that allow us to detect any outliers or anomalies
in a large number of data points.
• It is visually attractive that can be accepted by a wide range of audiences.
• It allows us for the endless customization of our graphs that makes our plot more
meaningful and understandable for others.

Prepared By.E.Janakiraman.MCA,MPhil,. | AP/APEC DEPT.OF MCA

Downloaded by Anthonyammal M (anthonyammal191979@gmail.com)


lOMoARcPSD|50078205

MC4103 PYTHON PROGRAMMING UNIT 4 PACKAGE AND FRAMEWORKS

Installation
Plotly does not come built-in with Python. To install it type the below command in
the terminal.
pip install plotly
Package Structure of Plotly
There are three main modules in Plotly. They are:
• plotly.plotly
• plotly.graph.objects
• plotly.tools

plotly.plotly acts as the interface between the local machine and Plotly. It contains functions
that require a response from Plotly’s server.
plotly.graph_objects module contains the objects (Figure, layout, data, and the definition of
the plots like scatter plot, line chart) that are responsible for creating the plots. The Figure
can be represented either as dict or instances of plotly.graph_objects.Figure and these are
serialized as JSON before it gets passed to plotly.js. Consider the below example for better
understanding.

Example:
• Python3

import plotly.express as px

# Creating the Figure instance


fig = px.line(x=[1,2, 3], y=[1, 2, 3])

# printing the figure instance


print(fig)

Output:

Prepared By.E.Janakiraman.MCA,MPhil,. | AP/APEC DEPT.OF MCA

Downloaded by Anthonyammal M (anthonyammal191979@gmail.com)


lOMoARcPSD|50078205

MC4103 PYTHON PROGRAMMING UNIT 4 PACKAGE AND FRAMEWORKS

Figures are represented as trees where the root node has three top layer attributes
– data, layout, and frames and the named nodes called ‘attributes’. Consider the above
example, layout.legend is a nested dictionary where the legend is the key inside the
dictionary whose value is also a dictionary.
plotly.tools module contains various tools in the forms of the functions that can enhance
the Plotly experience.

Creating Different Types of Charts


With plotly we can create more than 40 charts and every plot can be created using the
plotly.express and plotly.graph_objects class. Let’s see some commonly used charts with the
help of Plotly.

Line Chart

Line plot in Plotly is much accessible and illustrious annexation to plotly which manage a
variety of types of data and assemble easy-to-style statistic. With px.line each data position
is represented as a vertex (which location is given by the x and y columns) of a polyline
mark in 2D space.
Example:

import plotly.express as px

# using the iris dataset


df = px.data.iris()

# plotting the line chart


fig = px.line(df, x="species", y="petal_width")

# showing the plot


fig.show()

Output:

Prepared By.E.Janakiraman.MCA,MPhil,. | AP/APEC DEPT.OF MCA

Downloaded by Anthonyammal M (anthonyammal191979@gmail.com)


lOMoARcPSD|50078205

MC4103 PYTHON PROGRAMMING UNIT 4 PACKAGE AND FRAMEWORKS

Bar Chart

A bar chart is a pictorial representation of data that presents categorical data with
rectangular bars with heights or lengths proportional to the values that they represent. In other
words, it is the pictorial representation of dataset. These data sets contain the numerical
values of variables that represent the length or height.
Example:

import plotly.express as px

# using the iris dataset


df = px.data.iris()

# plotting the bar chart


fig = px.bar(df, x="sepal_width", y="sepal_length")

# showing the plot


fig.show()

Output:

Prepared By.E.Janakiraman.MCA,MPhil,. | AP/APEC DEPT.OF MCA

Downloaded by Anthonyammal M (anthonyammal191979@gmail.com)


lOMoARcPSD|50078205

MC4103 PYTHON PROGRAMMING UNIT 4 PACKAGE AND FRAMEWORKS

Python Frameworks
Python is one of the most lucrative programming languages that is used as
the main coding language by more than 80% of the developers. It is one of
the fastest-growing programming languages that is embedded with extensive
libraries and frameworks to fuel up different processes. Popular companies like
Oracle, Amazon, Instagram, Google, and many more are using Python in different
ways. Python is the home of most trending technologies like Artificial Intelligence
and Machine Learning processes.

Some of the amazing features of Python are – easy to learn and code, facilitates
object-oriented programming, platform independence, enhanced performance,
and security. It is used in different industries like Finance, Healthcare, Education,
Banking, Electronics, and many more.
It is embedded with a lot of frameworks for web development, data science,
artificial intelligence, and a lot more

1. Django

It is a high-level Python framework that facilitates the concise design and rapid
development. As per the latest survey, Django is the 4th most wanted framework
for web development. It offers some distinguishing features like ORM, URL
routing, database schema migration which makes Django really fast. Some of the
popular sites that use Django are Instagram, Mozilla, Pinterest, National
Geographic, and many more. So this is one of the in-demand frameworks of
Python that you must learn in 2022.
It offers amazing features like:
• Ready-made components to use for rapid development
• It is embedded with enhanced security features
• Highly scalable and extremely versatile

Prepared By.E.Janakiraman.MCA,MPhil,. | AP/APEC DEPT.OF MCA

Downloaded by Anthonyammal M (anthonyammal191979@gmail.com)


lOMoARcPSD|50078205

MC4103 PYTHON PROGRAMMING UNIT 4 PACKAGE AND FRAMEWORKS

• It has thousands of additional packages available.



Why Django Framework ?
• Excellent documentation and high scalability.
• Used by Top MNCs and Companies, such as Instagram, Disqus,
Spotify, Youtube, Bitbucket, Dropbox, etc. and the list is never-
ending.
• Easiest Framework to learn, rapid development and Batteries fully
included.
• The last but not least reason to learn Django is Python, Python has
huge library and features such as Web Scrapping, Machine
Learning, Image Processing, Scientific Computing, etc. One can
integrate it all this with web application and do lots and lots of
advance stuff.
Django architecture
Django is based on MVT (Model-View-Template) architecture. MVT is
a software design pattern for developing a web application.
MVT Structure has the following three parts –
Model: Model is going to act as the interface of your data. It is responsible
for maintaining data. It is the logical data structure behind the entire
application and is represented by a database (generally relational databases
such as MySql, Postgres).
View: The View is the user interface — what you see in your browser when
you render a website. It is represented by HTML/CSS/Javascript and Jinja
files.
Template: A template consists of static parts of the desired HTML output as
well as some special syntax describing how dynamic content will be inserted.

Flask – (Creating first simple application)


Building a webpage using python.
There are many modules or frameworks which allow building your webpage
using python like a bottle, Django, flask, etc. But the real popular ones are Flask
and Django. Django is easy to use as compared to Flask but Flask provides you
the versatility to program with.
To understand what Flask is you have to understand few general terms.
1. WSGI Web Server Gateway Interface (WSGI) has been adopted as a
standard for Python web application development. WSGI is a
specification for a universal interface between the web server and the
web applications.
2. Werkzeug It is a WSGI toolkit, which implements requests, response
objects, and other utility functions. This enables building a web

Prepared By.E.Janakiraman.MCA,MPhil,. | AP/APEC DEPT.OF MCA

Downloaded by Anthonyammal M (anthonyammal191979@gmail.com)


lOMoARcPSD|50078205

MC4103 PYTHON PROGRAMMING UNIT 4 PACKAGE AND FRAMEWORKS

framework on top of it. The Flask framework uses Werkzeug as one of


its bases.
3. jinja2 jinja2 is a popular templating engine for Python. A web
templating system combines a template with a certain data source to
render dynamic web pages.

Flask:
Flask is a web application framework written in Python. Flask is based on the
Werkzeug WSGI toolkit and Jinja2 template engine. Both are Pocco projects.
Installation:
We will require two package to setup your environment. virtualenv for a user to
create multiple Python environments side-by-side. Thereby, it can avoid
compatibility issues between the different versions of the libraries and the next
will be Flask itself.

• virtualenv
pip install virtualenv
• Flask
pip install Flask

After completing the installation of the package, let’s get our hands on the code.

Prepared By.E.Janakiraman.MCA,MPhil,. | AP/APEC DEPT.OF MCA

Downloaded by Anthonyammal M (anthonyammal191979@gmail.com)


lOMoARcPSD|50078205

MC4103 PYTHON PROGRAMMING UNIT 4 PACKAGE AND FRAMEWORKS

Save it in a file and then run the script we will be getting an output like this.

Then go to the url given there you will seeing your first webpage displaying hello
world there on your local server.
Digging further into the context, the route() decorator in Flask is used to bind
URL to a function. Now to extend this functionality our small web app is also
equipped with another method add_url_rule() which is a function of an

Prepared By.E.Janakiraman.MCA,MPhil,. | AP/APEC DEPT.OF MCA

Downloaded by Anthonyammal M (anthonyammal191979@gmail.com)


lOMoARcPSD|50078205

MC4103 PYTHON PROGRAMMING UNIT 4 PACKAGE AND FRAMEWORKS

application object is also available to bind a URL with a function as in the above
example, route() is used.

Example:
def gfg():
return ‘geeksforgeeks’
app.add_url_rule(‘/’, ‘g2g’, gfg)
Output:
geeksforgeeks
You can also add variables in your webapp, well you might be thinking how it’ll
help you, it’ll help you to build a URL dynamically. So lets figure it out with an
example.

from flask import Flask

app = Flask(__name__)

@app.route('/hello/<name>')

def hello_name(name):

return 'Hello %s!' % name

if __name__ == '__main__':

app.run()

And go to the URL http://127.0.0.1:5000/hello/geeksforgeeks it’ll give you the


following output.

Prepared By.E.Janakiraman.MCA,MPhil,. | AP/APEC DEPT.OF MCA

Downloaded by Anthonyammal M (anthonyammal191979@gmail.com)


lOMoARcPSD|50078205

MC4103 PYTHON PROGRAMMING UNIT 4 PACKAGE AND FRAMEWORKS

We can a
.

>163lso use HTTP methods in Flask lets see how to do that


HTTP protocol is the foundation of data communication in world wide web.
Different methods of data retrieval from specified URL are defined in this
protocol. The methods are described down below.
GET : Sends data in simple or unencrypted form to the server.
HEAD : Sends data in simple or unencrypted form to the server without body.
HEAD : Sends form data to the server. Data is not cached.
PUT : Replaces target resource with the updated content.
DELETE : Deletes target resource provided as URL.

Prepared By.E.Janakiraman.MCA,MPhil,. | AP/APEC DEPT.OF MCA

Downloaded by Anthonyammal M (anthonyammal191979@gmail.com)


lOMoARcPSD|50078205

MC4103 PYTHON PROGRAMMING UNIT 4 PACKAGE AND FRAMEWORKS

Prepared By.E.Janakiraman.MCA,MPhil,. | AP/APEC DEPT.OF MCA

Downloaded by Anthonyammal M (anthonyammal191979@gmail.com)

You might also like