0% found this document useful (0 votes)
25 views

Python 036-FINAL

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Python 036-FINAL

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

230493131036 Python For DataScience

PRACTICAL SET: 1
AIM: Basics Of Python

Practical: 1.1
AIM: Write a python program to create a simple arithmetic application including
operations(addition, subtraction, multiplication, division, modulus, exponent, integer
division.

Input:
n1=int(input('Enter the number 1:'));
n2=int(input('Enter the number 2:'));
Add=n1+n2;
print('Addition:',Add);
Sub=n1-n2;
print('Subtraction:',Sub);
Mul=n1*n2;
print('Multiplication:',Mul);
Div=n1/n2;
print('Divison:',Div);

Output:

SNPITRC 1
230493131036 Python For DataScience

Practical: 1.2
AIM: Write a python program to convert numbers from octal, binary and hexadecimal
systems into decimal number system.

Input:
n1=input('Enter the number 1 in binary:');
print('decimal value',int(n1,2));
n2=input('Enter the number 2 in octal:');
print('decimal value',int(n2,8));
n3=input('Enter the number 3 in hexadecimal:');
print('decimal value',int(n3,16));

Output:

SNPITRC 2
230493131036 Python For DataScience

Practical: 1.3
AIM: Write a python program to convert numbers from decimal number system into
octal, binary and hexadecimal system.

Input:
a=int(input("Enter the decimal number you want to convert:"))
print("Binary number is",bin(a))
print("Octal number is",oct(a))
print("Hexadecimal number is",hex(a))

Output:

SNPITRC 3
230493131036 Python For DataScience

Practical: 1.4
AIM: Write a python program to check whether the given number is a palindrome or
not.

Input:
no = int(input("Enter the Integer= \n"))
sum = 0
temp = no
while(no>0):
r=no%10
sum=(sum*10)+r
no = int(no / 10)
if(temp==sum):
print("Given Number Is Palindrome")
else:
print("Given Number Is Not Palindrome!!!")

Output:

SNPITRC 4
230493131036 Python For DataScience

Practical: 1.5
AIM: Write a python program to calculate area of a triangle.

Input:
b=float(input("Enter The Value Of Base = \n"))
h=float(input("Enter The Value Of Height = \n"))
Area = (b*h)/2
print("The Area Of Triangle Is = ",Area)

Output:

SNPITRC 5
230493131036 Python For DataScience

Practical: 1.6
AIM: Write a python program to display maximum of given 3 Numbers.

Input:
a=int(input("Enter The Number 1 = \n"))
b=int(input("Enter The Number 2 = \n"))
c=int(input("Enter The Number 3 = \n"))
if (a >= b) and (a >= c):
largest = a
elif (b >= a) and (b >=c):
largest = b
else:
largest = c
print("The Largest Number Is =", largest)

Output:

SNPITRC 6
230493131036 Python For DataScience

Practical: 1.7
AIM: Write a python program to find those numbers which are divisible by 3 and
multiple of 5 within 500 numbers.

Input:

for x in range(1,501) :
if(x % 3==0 and x % 5 == 0):
print(x,end = ' ')

Output:

SNPITRC 7
230493131036 Python For DataScience

Practical: 1.8
AIM: Write a python program to draw kite pattern using for loop.

Input:
r= int (input ("enter the val for size of kite= "))
for x in range ( r,0,-1):
print( " " * x, "* " * (r-x))
for x in range ( 0,r,1):
print( " " * x, "* " * (r-x))
for x in range ( r-1,int(r-2)-1,-1):
print( " " * x, "* " * (r-x))

Output:

SNPITRC 8
230493131036 Python For DataScience

PRACTICAL SET: 2
Practical: 2.1
AIM: Write a python program to print numbers from 1 to 50. For multiple of 4 print
name instead of number and for multiple of 5 print father name. For the numbers which
are multiple of both 4 and 5 print surname

Input:
for i in range(1,51):
b=i
if(i % 4 == 0):
b="KASHISH"
if(i % 5 == 0):
b="VIMALBHAI"
if(i % 4 == 0 and i % 5 ==0):
b="TAILOR"
print(b)

Output:

SNPITRC 9
230493131036 Python For DataScience

Practical: 2.2
AIM: Write a python program to find numbers between 500 and 800 when each digit of
number is ODD and the number should be printed in sequence separated by comma.

Input:
item=[]
for i in range(500,801):
s=str(i)
if(int(s[0])%2!=0) and (int(s[1])%2!=0) and (int(s[2])%2!=0):
item.append(s)
print(", " .join(item))

Output:

SNPITRC 10
230493131036 Python For DataScience

Practical: 2.3
AIM: Write a python program which accept a sequence of 4 digit binary numbers
separated by comma and also print the numbers which are divisible by 3 in sequence
separated by comma.

Input:
item=[]
print("Enter the 4 bit binary sequence sperated by comma ")
number = [x for x in input().split(",") ]
for p in number:
x=int(p,2)
if (x % 3 ==0):
item.append(p)
print(", " .join(item))

Output:

SNPITRC 11
230493131036 Python For DataScience

Practical: 2.4
AIM: Write a python program to display Fibonacci sequence up to nth term using
recursive functions

Input:
def recurr_fib(n) :
if n<=1:
return n
else :
return(recurr_fib(n-1)+recurr_fib(n-2))
num = int(input("Enter the Range of Fib :"))

if num<=0:
print("Enter a positive Number")
else :
for i in range (num) :
print(recurr_fib(i))

Output:

SNPITRC 12
230493131036 Python For DataScience

Practical: 2.5
AIM: Write a python program that accept a string and calculate the number of
uppercase and lowercase letter.

Input:
str=input("Enter the string to check the lower and upper values \n")
b=0
m=0
for i in str:
if (i<='z' and i>='a'):
b=b+1
if( i>='A' and i<='Z'):
m=m+1

print("THE Lower CASE VLAUES ARE ",b)

print("THE Upper CASE VLAUES ARE ",m)

Output:

Practical: 2.6
SNPITRC 13
230493131036 Python For DataScience

AIM: Write a python program to search a number in array using sequential search.

Input:

Import numpy as nd
def seq_search(array,num) :
pos=0
found=False
while pos<len(array) and not found:
if (array[pos]==num):
found=True
else:
pos=pos+1
return found,pos+1
array1=nd.random.randint(50,size=(10))
print(array1)
number=int(input("Enter the number you want to search "))
print(seq_search(array1,number) )

Output:

SNPITRC 14
230493131036 Python For DataScience

Practical: 2.7
AIM: Write a python program to sort elements of array.

Input:

size = int(input("enter size of an array : "))


arr = []
for i in range(size):
element = int(input("enter element in array :"))
arr.append(element)
arr.sort()
print("sorted array in ascending order is : ",arr)
print("sorted array in descending order is : ",arr[::-1])

Output:

SNPITRC 15
230493131036 Python For DataScience

Practical: 2.8
AIM: Write a python program to input two matrix and perform the following given
operation.

Input:

Output:

SNPITRC 16
230493131036 Python For DataScience

PRACTICAL SET: 3
AIM 3.1: a) Read student data from given excel file sheet named as “5CSE” into
appropriate pandas data structure.
b) Fill missing values in columns “Subject” and “Batch” using forward fill method.
c) Fill value “Jay Patel” in “Mentor” column for students having “Enrollment”
column value from “200860131001” to “200860131029” and “Pal Patel” for
remaining students.
d) Add a new column “City” in existing student data and fill that column with
residential city of student.
e) Count total number of students subject-wise and batch-wise.

 INPUT:

(a) import pandas as pd


df = pd.read_csv('5CSE.csv')
print(df)

(b) import pandas as pd


df = pd.read_csv('5CSE.csv') df.drop("Unnamed: 0",axis=1,inplace=True)
df["Subject"].fillna(method="ffill",inplace=True)
df["Batch"].fillna(method="ffill",inplace=True)
print(df)

(c) import pandas as pd


df = pd.read_csv('5CSE.csv') def value(x):
if(x<=200860131029):
return "Jay Patel" else:
return "Pal Patel"
df["Mentor"]=df.apply(lambda x:value(x["Enrollment"]),axis=1)
print(df)

(d) import pandas as pd


df = pd.read_csv('5CSE.csv')
df['City']=["Vapi","Silvassa","Vapi","Bhilad","Bhilad","Vapi","Vapi","Vapi","Vapi","V
api","Vapi",
"Vapi","Vapi","Vapi","Silvassa","Vapi","Vapi","Vapi","Vapi","Udvada","Vapi","Vapi",
"Vapi","Va
pi","Vapi","Vapi","Silvassa","Pardi","Vapi","Bhilad","Vapi","Vapi","Silvassa","Vapi","
Vapi","Vapi ","Vapi","Vapi","Vapi","Vapi","Bhilad","Vapi"]
print(df)

SNPITRC 17
230493131036 Python For DataScience

(e) import pandas as pd


df = pd.read_csv('5CSE.csv')
print(df.groupby(["Subject"]).size().reset_index(name='Number of Students'))
print(df.groupby(["Batch"]).size().reset_index(name='Number of Students'))

OUTPUT (a):

SNPITRC 18
230493131036 Python For DataScience

OUTPUT (b):

SNPITRC 19
230493131036 Python For DataScience

OUTPUT ( C) :

SNPITRC 20
230493131036 Python For DataScience

OUTPUT (d):

SNPITRC 21
230493131036 Python For DataScience

OUTPUT (e):

SNPITRC 22
230493131036 Python For DataScience

Practical: 3.2:
AIM:
a) Read data from given csv file. Delete rows having missing values.
b) Calculate average price of cars having four and six cylinder engines.
c) Find out cheapest and most expensive car details.
d) Find out convertible and sedan car details having maximum engine horsepower.
e) Find average sedan car price.
f) Count total number of cars per company.
g) Find each company’s highest car price.

 INPUT:

(a) import pandas as pd import numpy as np


df=pd.read_csv("Automobile_data_Miss.csv")
print(df)
df.dropna(axis='rows')

(b) a1=df["num-of- doors"]=='four'


a_1=df[a1]["price"].mean()
a2=df["num-of- doors"]=='two'
a_2=df[a2]["price"].mea n()
avg=(a_1+a_2)/2
print(avg)

(c) print("Detail of cheapest car")


print(df[df.price == df.price.min()])
print("Detail of expensive car")
print(df[df.price == df.price.max()])

(d) print('maximum hoursepower of convertible car is\n\n',df1)


print('\n\nmaximum hoursepower of sedan car is\n\n',df2)

(e) print(df[df["body-style"]=="sedan"].agg({"price":'mean'}))
print(df['make'].value_counts())

SNPITRC 23
230493131036 Python For DataScience

 OUTPUT (a):

 OUTPUT (b):

13191.900236674213

 OUTPUT (c):

SNPITRC 24
230493131036 Python For DataScience

 OUTPUT (d):

 OUTPUT (e):

SNPITRC 25
230493131036 Python For DataScience

 OUTPUT (f):

PRACTICAL SET: 4
SNPITRC 26
230493131036 Python For DataScience

AIM 4.1: Plot gender-wise share of overall voters with legend and suitable labels.
(Pie chart).

from pandas import DataFrame, read_csv import matplotlib.pyplot


as plt import pandas as pd
result_df=pd.read_csv('Votes 2019.csv') result_df x=result_df["Female "].sum()
print("Sum of Female voters = ",x) y=result_df["Male"].sum() print("Sum of Male voters = ",y)
z=result_df["Other "].sum() print("Sum of Other voters = ",z) sizes=[x,y,z]
labels=["Female","Male","Other"] colors=['palegreen','lightskyblue','red'] explode=(0,0.1,0)
plt.pie(sizes,explode=explode,labels=labels,colors=colors,startangle=90,autopct="%.1f%% ",
shado w=True)
plt.legend(labels,loc=2) plt.show()

OUTPUT:

SNPITRC 27
230493131036 Python For DataScience

AIM 4.2: Indian states are divided into six administrative zones: Central, East,
North, Northeast, South and Western. Plot six bar chart into single figure to visualize
total voters with suitable chart title.

zone=['Southern','Southern','North East','North
East','East','North','Central','Western','Western','Western','Western','North','North','North','East
','South
ern','Southern','Southern','Central','Western','North','North East','North East','North
East','North','East','Southern','North','North','North East','Southern','Southern','North
East','North','North','East'] result_df['Zone']=zone result_df.head()
result_df.to_csv("Votes_2019.csv")
y=result_df['Zone']fig=plt.figure() ax=fig.add_axes([0,3,1,1])
l1=ax.bar(y,result_df['TotalVoters']) ax.set_title("TotalVotersvsZone")
ax.set_ylabel("TotalVoters",size=12) ax.set_xlabel("Administrative Zones",size=12)
plt.show()

OUTPUT:

SNPITRC 28
230493131036 Python For DataScience

AIM 4.3: Plot zone-wise share of total voters with legend and suitable labels. (Pie
chart)

xx=result_df['Total Voters'].groupby(result_df['Zone']).sum() print(xx)


sizes=[xx["Central"],xx["East"],xx["North"],xx["North East"],xx["Southern"],xx["Western"]]
labels=["Central","East","North","Northeast","South","West"]
colors=['c','gold','yellow','chartreuse','lightgreen','tomato']
plt.pie(sizes,colors=colors,labels=labels,autopct="%.1f%%") plt.legend(labels,loc=4)
plt.show()

OUTPUT:

SNPITRC 29
230493131036 Python For DataScience

AIM 4.4: Plot horizontal bar chart for states vs total actual votes with suitable labels.

fig=plt.figure() ax=fig.add_axes([0,3,2,3])
ax.barh(y=result_df['State Name'],width=result_df['Total Actual
Votes']) plt.title("States vs Total actual voters") plt.ylabel("State
name") plt.xlabel("Total Actual Voters") plt.show()

OUTPUT:

SNPITRC 30
230493131036 Python For DataScience

AIM 4.5: Plot type-wise share (EVM and Postal) with legend and suitable labels for
each administrative zone into single figure. (Pie chart)

import numpy as np fig=plt.figure() ax=fig.subplots(2,3)


xx=result_df['EVM Vote'].groupby(result_df['Zone']).sum() yy=result_df['Postal
Vote'].groupby(result_df['Zone']).sum()
ax[0][0].pie([xx['East'],yy['East']],autopct='%.2f%%') ax[0][0].set_title('East Zone')
ax[0][1].pie([xx['North'],yy['North']],autopct='%.2f%%') ax[0][1].set_title('North Zone')
ax[0][2].pie([xx['Western'],yy['Western']],autopct='%.2f%%') ax[0][2].set_title('Western
Zone')
ax[1][0].pie([xx['Southern'],yy['Southern']],autopct='%.2f%%') ax[1][0].set_title('Southern
Zone') ax[1][1].pie([xx['Central'],yy['Central']],autopct='%.2f%%') ax[1][1].set_title('Central
Zone')
ax[1][2].pie([xx['North East'],yy['North East']],autopct='%.2f%%') ax[1][2].set_title('Noth-
East Zone') plt.legend(["EVM vote","Postal Votes"])
plt.show()

OUTPUT:

SNPITRC 31
230493131036 Python For DataScience

AIM 4.6: Plot vote deficits (Total actual votes – Total voters) for each states using line
chart.

result_df.loc[:,"New"]=result_df.loc[:,"Total Actual Votes"].subtract(result_df.loc[:,"Total


Voters"]) result_df fig =plt.figure() ax = fig.add_axes([0,3,2,3])
ax.plot(result_df['State Name'],result_df['New'],color='purple',marker='o') plt.title("State vs
Total Actual Votes - Total voters",fontsize=14)
plt.xticks(rotation=90)plt.xlabel("Statename",fontsize=14) plt.ylabel("Total Actual Votes -
Total Voters",fontsize=14)
plt.show()

OUTPUT:

SNPITRC 32
230493131036 Python For DataScience

AIM 4.7: Plot horizontal bar chart for states vs male, female and other votes (grouping
of bars) with legend and suitable title.

fig = plt.figure() ax = fig.add_axes([0,3,2,3])


pos=list(range(len(result_df["Female "]))) width=0.25
ax.barh(pos,result_df['Female '],width,alpha=0.5,color='purple',label=result_df['State
Name'][0])
ax.barh([p + width for p in pos],
result_df['Male'],width,alpha=0.5,color='mediumpurple',label=result_df['State Name'][1])
ax.barh([p + width*2 for p in pos],
result_df['Other '],width,alpha=0.5,color='pink',label=result_df['State Name'][2])
ax.set_title('State vs male, female, other voters') ax.set_yticks([p + 1.5*width for p in pos])
ax.set_yticklabels(result_df['State Name']) plt.ylim(min(pos)- width,max(pos)+width*4)
plt.xlim([0,max(result_df['Female ']+result_df['Male']+result_df['Other '])])
plt.legend(['Female','Male','Other '],loc='upper right')
plt.grid()
plt.show()
OUTPUT:

SNPITRC 33
230493131036 Python For DataScience

PRACTICAL SET 5
AIM 5.1: Load iris dataset from sklearn library given iris.csv file into appropriate
data structure of pandas.

%matplotlib inline
from sklearn.datasets import load_iris iris = load_iris() import pandas as pd import numpy as
np
print('Your pandas version is: %s' % pd. version ) print('Your NumPy version is %s' % np.
version ) from sklearn.datasets import load_iris iris = load_iris() iris_nparray = iris.data
iris_dataframe = pd.DataFrame(iris.data, columns=iris.feature_names) iris_dataframe['group']
= pd.Series([iris.target_names[k] for k in iris.target], dtype="category")
iris_dataframe

OUTPUT:

SNPITRC 34
230493131036 Python For DataScience

AIM 5.2: Perform Descriptive Statistics for Numeric Data, Measuring central
tendency, Measuring variance and range.

a) print(iris_dataframe.mean(numeric_only=True))
b) print(iris_dataframe.median(numeric_only=True))
c) print(iris_dataframe.std())
d) print(iris_dataframe.max(numeric_only=True) - iris_dataframe.min(numeric_only=True))
e) print(iris_dataframe.quantile([0,.25,.50,.75,1]))

OUTPUT (a):

OUTPUT (b):

OUTPUT (c):

SNPITRC 35
230493131036 Python For DataScience

OUTPUT (d):

OUTPUT (e):

SNPITRC 36
230493131036 Python For DataScience

AIM 5.3: To Working with percentiles and defining measures of normality.

INPUT (a):
from scipy.stats import kurtosis, kurtosistest variable = iris_dataframe['petal length (cm)'] k =
kurtosis(variable)
zscore, pvalue = kurtosistest(variable)
print('Kurtosis %0.3f z-score %0.3f p-value %0.3f' % (k, zscore, pvalue))

INPUT (b):

from scipy.stats import skew, skewtest variable = iris_dataframe['petal length (cm)'] s =


skew(variable)
zscore, pvalue = skewtest(variable)
print('Skewness %0.3f z-score %0.3f p-value %0.3f' % (s, zscore, pvalue))

OUTPUT (a):

OUTPUT (b):

SNPITRC 37
230493131036 Python For DataScience

AIM 5.4: To Counting for Categorical Data, Understanding frequencies, Creating


contingency tables.

INPUT (a):
pcts = [0, .25, .5, .75,1] iris_binned = pd.concat([pd.qcut(iris_dataframe.iloc[:,0], pcts,
precision=1), pd.qcut(iris_dataframe.iloc[:,1], pcts, precision=1),
pd.qcut(iris_dataframe.iloc[:,2], pcts, precision=1), pd.qcut(iris_dataframe.iloc[:,3], pcts,
precision=1)], join='outer', axis = 1)
print(iris_dataframe['group'].value_counts())

INPUT (b):
print(iris_binned['petal length (cm)'].value_counts())

INPUT (c):
print(pd.crosstab(iris_dataframe['group'], iris_binned['petal length (cm)']))

OUTPUT (a):

OUTPUT (b):

SNPITRC 38
230493131036 Python For DataScience

OUTPUT (C):

SNPITRC 39
230493131036 Python For DataScience

AIM 5.5: To Creating Applied Visualization for EDA like boxplots.

INPUT (a):
boxplots = iris_dataframe.boxplot(fontsize=9)

INPUT (b):
import matplotlib.pyplot as plt
boxplots = iris_dataframe.boxplot(column='petal length (cm)', by='group', fontsize=10)
plt.suptitle("") plt.show()

OUTPUT (a):

OUTPUT (b):

SNPITRC 40
230493131036 Python For DataScience

PRACTICAL SET 6
AIM 6.1: Prepare a Pie charts by taking suitable data as reference.

import matplotlib.pyplot as plt


values = [2, 7, 5, 10, 6, 4]
colors = ['b', 'g', 'r', 'c', 'm', 'y']
labels = ['A', 'B', 'C', 'D', 'E', 'F']
explode = (0, 0.2, 0, 0, 0, 0)
plt.pie(values, colors=colors, labels=labels, explode=explode, autopct='%1.1f%%',
counterclock=False, shadow=True)
plt.title('Values')
plt.show()

OUTPUT:

SNPITRC 41
230493131036 Python For DataScience

AIM 6.2: Prepare a Barcharts charts by taking suitable reference.

import matplotlib.pyplot as plt


values = [4, 9, 3, 10, 5, 8]
widths =[0.7, 0.8, 0.7, 0.7, 0.7, 0.7]
colors = ['b', 'r', 'b', 'b', 'b', 'b']
plt.bar(range(0, 6), values, width=widths, color=colors, align='center')
plt.show()

OUTPUT:

SNPITRC 42
230493131036 Python For DataScience

AIM 6.3: Prepare a Histograms by taking suitable reference.

import numpy as np
import matplotlib.pyplot as plt
x = 20 * np.random.randn(10000)
plt.hist(x, 25, range=(-50, 50), histtype='stepfilled', align='mid', color='g', label='Test Data')
plt.legend()
plt.title('Step Filled Histogram')
plt.show()

OUTPUT:

SNPITRC 43
230493131036 Python For DataScience

AIM 6.4: Prepare a Boxplots by taking suitable reference.

import numpy as np
import matplotlib.pyplot as plt
spread = 100 * np.random.rand(100) center= np.ones(50) * 50 flier_high = 100 *
np.random.rand(10) + 100 flier_low = -100* np.random.rand(10)
data = np.concatenate((spread, center, flier_high, flier_low)) plt.boxplot(data, sym='gx',
widths=.75, notch=True)
plt.show()

OUTPUT:

SNPITRC 44
230493131036 Python For DataScience

AIM 6.5: Prepare a Scatterplots by taking suitable reference.

import numpy as np import matplotlib.pyplot as


plt x1 = 5 * np.random.rand(40) x2 = 5 * np.random.rand(40) + 25 x3 = 25 *
np.random.rand(20) x = np.concatenate((x1, x2, x3)) y1 = 5 * np.random.rand(40) y2 = 5 *
np.random.rand(40) + 25 y3 = 25 * np.random.rand(20) y = np.concatenate((y1, y2, y3))
plt.scatter(x, y, s=[100], marker='^', c='m')
plt.show()

OUTPUT:

SNPITRC 45
230493131036 Python For DataScience

AIM 6.6: Prepare a Time Series by taking suitable reference.\

import pandas as pd import matplotlib.pyplot as plt


import datetime as dt import numpy as np start_date = dt.datetime(2022, 10, 20) end_date =
dt.datetime(2022, 11, 5) daterange = pd.date_range(start_date, end_date) sales =
(np.random.rand(len(daterange)) * 50).astype(int) df = pd.DataFrame(sales, index=daterange,
columns=['Sales']) df.loc['Oct 4 2022':'Nov 04 2022'].plot()
plt.ylim(0,50)
plt.xlabel('Sales Date')
plt.ylabel('Sale Value')
plt.title('Plotting Time')
plt.show()

OUTPUT:

SNPITRC 46

You might also like