0% found this document useful (0 votes)
96 views16 pages

Ip Lab Manual (Python) 2019-20

The document provides a practical syllabus for a course on Python programming. It includes 3 units: 1. Lab tests - Students will complete Python programs for data handling and SQL queries, graded on logic, documentation and code quality. 2. Report file and viva - Students will submit a report file with at least 21 Python programs, including 4 using SQL and 1 implementing a web server. They will also participate in a viva voce exam. 3. Project and viva - Students will complete a project using concepts from the course and participate in a project viva voce exam. The document also provides examples of programs and concepts students may implement, including aggregating data from SQL queries,

Uploaded by

Amber Das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views16 pages

Ip Lab Manual (Python) 2019-20

The document provides a practical syllabus for a course on Python programming. It includes 3 units: 1. Lab tests - Students will complete Python programs for data handling and SQL queries, graded on logic, documentation and code quality. 2. Report file and viva - Students will submit a report file with at least 21 Python programs, including 4 using SQL and 1 implementing a web server. They will also participate in a viva voce exam. 3. Project and viva - Students will complete a project using concepts from the course and participate in a project viva voce exam. The document also provides examples of programs and concepts students may implement, including aggregating data from SQL queries,

Uploaded by

Amber Das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

5.

Practical Syllabus

Sl.No. Unit Name Marks


1. Lab Test (10 marks)
Python programs for data handling (60% logic + 20% 7
documentation + 20% code quality)
Small Python program that sends a SQL query to a database and 3
displays the result. A stub program can be provided.
2. Report File + viva(9 marks)
7
Report file: Minimum 21 Python programs. Out of this at least 4
programs should send SQL commands to a database, and retrieve the
result; at least 1 program should implement the web server to write
user data to a CSV file.
Viva voce based on the report file 2
Project + viva (11 marks)
3. Project (that uses most of the concepts that have been learnt) 8
Project viva voce 3

5.1. Data Management: SQL+web-server


Find the min, max, sum, and average of the marks in a student marks table.
Find the total number of customers from each country in the table (customer ID, customer Name,
country) using group by.
Write a SQL query to order the (student ID, marks) table in descending order of the marks.
Integrate SQL with Python by importing MYSQL dB
Write a Django based web server to parse a user request (POST), and write it to a CSV file.

5.2. Data handling using Python libraries


Use map functions to convert all negative numbers in a Data Frame to the mean of all the numbers.
Consider a Data Frame, where each row contains the item category, item name, and expenditure.
Group the rows by the category, and print the total expenditure per category.
Given a Series, print all the elements that are above the 75th percentile.
Given a day’s worth of stock market data, aggregate it. Print the highest, lowest, and closing prices
of each stock.
Given sample data, plot a linear regression line.
Take data from government web sites, aggregate and summarize it. Then plot it using different
plotting functions of the PyPlot library.

5.3. Basic Software Engineering

Business use-case diagrams for an airline ticket booking system, train reservation system, stock
exchange
Collaboratively write a program and manage the code with a version control system (GIT)
PRACTICAL FILE –INFORMATICS PRACTICES (065)
LIST OF PRACTICALS (2019-20) CLASS-XII
Programming Language: Python

Find the min, max, sum, and average of the marks in a student marks table.

Aggregate functions:
These functions are used to do operations from the values of the column and a single value is returned.
1. AVG()
2. COUNT()
3. FIRST()
4. LAST()
5. MAX()
6. MIN()
7. SUM()

Students-Table

Aggregate Functions

MAX(): The MAX() function returns the maximum value of the selected column.
Syntax:
SELECT MAX(column_name) FROM table_name;
Queries:

Fetching maximum marks among students from the Students table.


SELECT MAX(MARKS) AS MaxMarks FROM Students;
Output:

MaxMarks
95
Fetching max age among students from the Students table.
SELECT MAX(AGE) AS MaxAge FROM Students;
Output:

MaxAge
21
MIN(): The MIN() function returns the minimum value of the selected column.
Syntax:
SELECT MIN(column_name) FROM table_name;
Queries:
Fetching minimum marks among students from the Students table.
SELECT MIN(MARKS) AS MinMarks FROM Students;
Output:

MinMarks
50
Fetching minimum age among students from the Students table.
SELECT MIN(AGE) AS MinAge FROM Students;
Output:

MinAge
18
SUM (): The SUM () function returns the sum of all the values of the selected column.
Syntax:
SELECT SUM (column_name) FROM table_name;
Queries:

Fetching summation of total marks among students from the Students table.
SELECT SUM(MARKS) AS TotalMarks FROM Students;
Output:

TotalMarks
400
Fetching summation of total age among students from the Students table.
SELECT SUM(AGE) AS TotalAge FROM Students;
Output:

TotalAge
97

Find the total number of customers from each country in the table (customer ID,
customer Name, country) using group by.

Custom CustomerName Contact Address City PostalCod Country


erID Name e

1 Alfreds Maria Obere Str. 57 Berlin 12209 Germany


Futterkiste Anders

2 Ana Trujillo Ana Avda. de la México 05021 Mexico


Emparedados y Trujillo Constitución D.F.
helados 2222
3 Antonio Moreno Antonio Mataderos 2312 México 05023 Mexico
Taquería Moreno D.F.

4 Around the Horn Thomas 120 Hanover Sq. London WA1 1DP UK
Hardy

5 Berglunds Christina Berguvsvägen 8 Luleå S-958 22 Sweden


snabbköp Berglund

6 Blauer See Hanna Forsterstr. 57 Mannhei 68306 Germany


Delikatessen Moos m

7 Blondel père et Frédériq 24, place Kléber Strasbour 67000 France


fils ue g
Citeaux

8 Bólido Comidas Martín C/ Araquil, 67 Madrid 28023 Spain


preparadas Sommer

9 Bon app' Laurence 12, rue des Marseille 13008 France


Lebihans Bouchers

10 Bottom-Dollar Elizabeth 23 Tsawassen Tsawasse T2F 8M4 Canada


Marketse Lincoln Blvd. n

SELECT COUNT(CustomerID), Country


FROM Customers
GROUP BY Country
ORDER BY COUNT(CustomerID) DESC;

COUNT(CustomerID) Country

2 Germany

2 France

1 Canada

1 UK

1 Spain

2 Mexico

1 Sweden
Write a SQL query to order the (student ID, marks) table in descending order of the
marks.

Select * From Table Order by student_ID desc , marks desc ;

SELECT * FROM `student` ORDER BY class desc, mark desc

id name class mark

2 Max Ruin Three 85

3 Arnold Three 55

1 John Deo Four 75

4 Krish Star Four 60

5 John Mike Four 60

6 Alex John Four 55

Integrate SQL with Python by importing MYSQL dB

Install MySQL Connector Library for Python


For Python 2.7 or lower install using pip as:

pip install mysql-connector


For Python 3 or higher version install using pip3 as:

pip3 install mysql-connector

Test the MySQL Database connection with Python


To test database connection here we use pre-installed MySQL connector and pass credentials into
connect() function like host, username and password.

Syntax to access MySQL with Python:

import mysql.connector
db_connection = mysql.connector.connect
(
host="hostname",
user="username",
passwd="password"
)

Example,

import mysql.connector
db_connection = mysql.connector.connect(
host="localhost",
user="root",
passwd="root"
)
print(db_connection)

Output:

<mysql.connector.connection.MySQLConnection object at 0x000002338A4C6B00>

Write a Django based web server to parse a user request (POST), and write it to a
CSV file.

Using the Python CSV library¶


Python comes with a CSV library, csv. The key to using it with Django is that the csv module’s CSV-
creation capability acts on file-like objects, and Django’s HttpResponse objects are file-like objects.

Here’s an example:

import csv
from django.http import HttpResponse

def some_view(request):
# Create the HttpResponse object with the appropriate CSV header.
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="somefilename.csv"'

writer = csv.writer(response)
writer.writerow(['First row', 'Foo', 'Bar', 'Baz'])
writer.writerow(['Second row', 'A', 'B', 'C', '"Testing"', "Here's a quote"])

return response
The code and comments should be self-explanatory, but a few things deserve a mention:
The response gets a special MIME type, text/csv. This tells browsers that the document is a CSV file, rather
than an HTML file. If you leave this off, browsers will probably interpret the output as HTML, which will
result in ugly, scary gobbledygook in the browser window.The response gets an additional Content-
Disposition header, which contains the name of the CSV file. This filename is arbitrary; call it whatever you
want. It’ll be used by browsers in the “Save as…” dialog, etc.You can hook into the CSV-generation API by
passing response as the first argument to csv.writer. The csv.writer function expects a file-like object, and
HttpResponse objects fit the bill. For each row in your CSV file, call writer.writerow, passing it an iterable.
The CSV module takes care of quoting for you, so you don’t have to worry about escaping strings with
quotes or commas in them. Pass writerow() your raw strings, and it’ll do the right thing.

Use map functions to convert all negative numbers in a Data Frame to the mean of
all the numbers.

In [1]: import pandas as pd

In [2]: df = pd.DataFrame({'a': [0, -1, 2], 'b': [-3, 2, 1]})

In [3]: df
Out[3]:
a b
0 0 -3
1 -1 2
2 2 1

In [4]: df[df < 0] = 0

In [5]: df
Out[5]:
a b
0 0 0
1 0 2
2 2 1
Consider a Data Frame, where each row contains the item category, item name, and
expenditure. Group the rows by the category, and print the total expenditure per
category.

Pandas GroupBy
Groupby is a pretty simple concept. We can create a grouping of categories and apply a function to the
categories. It’s a simple concept but it’s an extremely valuable technique that’s widely used in data science.
In real data science projects, you’ll be dealing with large amounts of data and trying things over and over,
so for efficiency, we use Groupby concept. Groupby concept is really important because it’s ability to
aggregate data efficiently, both in performance and the amount code is magnificent.

# importing pandas module


import pandas as pd

# Define a dictionary containing employee data


data1 = {'Name':['Jai', 'Anuj', 'Jai', 'Princi',
'Gaurav', 'Anuj', 'Princi', 'Abhi'],
'Age':[27, 24, 22, 32,
33, 36, 27, 32],
'Address':['Nagpur', 'Kanpur', 'Allahabad', 'Kannuaj',
'Jaunpur', 'Kanpur', 'Allahabad', 'Aligarh'],
'Qualification':['Msc', 'MA', 'MCA', 'Phd',
'B.Tech', 'B.com', 'Msc', 'MA']}

# Convert the dictionary into DataFrame


df = pd.DataFrame(data1)

print(df)

Address Age Name Qualification


0 Nagpur 27 Jai Msc
1 Kanpur 24 Anuj MA
2 Allahabad 22 Jai MCA
3 Kannuaj 32 Princi Phd
4 Jaunpur 33 Gaurav B.Tech
5 Kanpur 36 Anuj B.com
6 Allahabad 27 Princi Msc
7 Aligarh 32 Abhi MA
Given a Series, print all the elements that are above the 75th percentile.
Grouping data with one key:
In order to group data with one key, we pass only one key as an argument in groupby function.

numpy.percentile() in python
numpy.percentile()function used to compute the nth precentile of the given data (array elements) along
the specified axis.

Syntax : numpy.percentile(arr, n, axis=None, out=None)


Parameters :
arr :input array.
n : percentile value.
axis : axis along which we want to calculate the percentile value. Otherwise, it will consider arr to be
flattened(works on all the axis). axis = 0 means along the column and axis = 1 means working along the
row.
out :Different array in which we want to place the result. The array must have same dimensions as
expected output.

Return :nth Percentile of the array (a scalar value if axis is none)or array with percentile values along
specified axis.

Code #1 : Working

# Python Program illustrating


# numpy.percentile() method

import numpy as np

# 1D array
arr = [20, 2, 7, 1, 34]
print("arr : ", arr)
print("50th percentile of arr : ", np.percentile(arr, 50))
print("25th percentile of arr : ", np.percentile(arr, 25))
print("75th percentile of arr : ", np.percentile(arr, 75))

Output :

arr : [20, 2, 7, 1, 34]


30th percentile of arr : 7.0
25th percentile of arr : 2.0
75th percentile of arr : 20.0
Given a day’s worth of stock market data, aggregate it. Print the highest, lowest, and
closing prices of each stock.

import datetime as dt
import matplotlib.pyplot as plt
from matplotlib import style
import pandas as pd
import pandas_datareader.data as web

style.use('ggplot')

start = dt.datetime(2015, 1, 1)
end = dt.datetime.now()
df = web.DataReader("TSLA", 'morningstar', start, end)
df.reset_index(inplace=True)
df.set_index("Date", inplace=True)
df = df.drop("Symbol", axis=1)

print(df.head())

Given sample data, plot a linear regression line.

Simple Linear Regression

Simple linear regression is an approach for predicting a response using a single feature.
It is assumed that the two variables are linearly related. Hence, we try to find a linear function that
predicts the response value(y) as accurately as possible as a function of the feature or independent
variable(x).
Let us consider a dataset where we have a value of response y for every feature x:
For generality, we define:
x as feature vector, i.e x = [x_1, x_2, …., x_n],
y as response vector, i.e y = [y_1, y_2, …., y_n]
for n observations (in above example, n=10).
A scatter plot of above dataset looks like:-

import numpy as np
import matplotlib.pyplot as plt

def estimate_coef(x, y):


# number of observations/points
n = np.size(x)

# mean of x and y vector


m_x, m_y = np.mean(x), np.mean(y)

# calculating cross-deviation and deviation about x


SS_xy = np.sum(y*x) - n*m_y*m_x
SS_xx = np.sum(x*x) - n*m_x*m_x

# calculating regression coefficients


b_1 = SS_xy / SS_xx
b_0 = m_y - b_1*m_x

return(b_0, b_1)

def plot_regression_line(x, y, b):


# plotting the actual points as scatter plot
plt.scatter(x, y, color = "m",
marker = "o", s = 30)

# predicted response vector


y_pred = b[0] + b[1]*x

# plotting the regression line


plt.plot(x, y_pred, color = "g")

# putting labels
plt.xlabel('x')
plt.ylabel('y')

# function to show plot


plt.show()

def main():
# observations
x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
y = np.array([1, 3, 2, 5, 7, 8, 8, 9, 10, 12])

# estimating coefficients
b = estimate_coef(x, y)
print("Estimated coefficients:\nb_0 = {} \
\nb_1 = {}".format(b[0], b[1]))

# plotting regression line


plot_regression_line(x, y, b)

if __name__ == "__main__":
main()

Output of above piece of code is:


Estimated coefficients:
b_0 = -0.0586206896552
b_1 = 1.45747126437
And graph obtained looks like this:

Business use-case diagrams for an airline ticket booking system, train reservation
system, stock exchange
Collaboratively write a program and manage the code with a version control system
(GIT)

What is Git?

By far, the most widely used modern version control system in the world today is Git. Git is a mature,
actively maintained open source project originally developed in 2005 by Linus Torvalds, the famous
creator of the Linux operating system kernel. A staggering number of software projects rely on Git for
version control, including commercial projects as well as open source. Developers who have worked with
Git are well represented in the pool of available software development talent and it works well on a wide
range of operating systems and IDEs (Integrated Development Environments).
Having a distributed architecture, Git is an example of a DVCS (hence Distributed Version Control System).
Rather than have only one single place for the full version history of the software as is common in once-
popular version control systems like CVS or Subversion (also known as SVN), in Git, every developer's
working copy of the code is also a repository that can contain the full history of all changes.In addition to
being distributed, Git has been designed with performance, security and flexibility in mind.

You might also like