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

Python Pr-Set3

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

Python Pr-Set3

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

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:

SNPITRC 13
230493131036 Python For DataScience

Practical: 2.6
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):

SNPITRC 26

You might also like