Python 036-FINAL
Python 036-FINAL
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
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:
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:
SNPITRC 17
230493131036 Python For DataScience
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:
(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).
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)
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)
OUTPUT:
SNPITRC 31
230493131036 Python For DataScience
AIM 4.6: Plot vote deficits (Total actual votes – Total voters) for each states using line
chart.
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.
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
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):
OUTPUT (a):
OUTPUT (b):
SNPITRC 37
230493131036 Python For DataScience
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
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.
OUTPUT:
SNPITRC 41
230493131036 Python For DataScience
OUTPUT:
SNPITRC 42
230493131036 Python For DataScience
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
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
OUTPUT:
SNPITRC 45
230493131036 Python For DataScience
OUTPUT:
SNPITRC 46