You are on page 1of 7

SAL COLLEGE OF ENGINEERING

COMPUTER ENGINNERING
Python for Data Science [3150713] – LIST OF PRACTICALS

Practical Set No. Title of the practical


1 Write a Python Program to Convert Celsius to Fahrenheit and vice –a-versa.
Write a program in python to swap two variables without using temporary
variable.
a=5
b=10
temp=a
a=b
b=temp
2 OR
x = int(input(“Enter value of x”))
y = int(input(“Enter value of y”))

x=x+y
y=x-y
x=x-y
print("After Swapping: x =", x, " y =", y)
Write a Python Program to Convert Decimal to Binary, Octal and Hexadecimal.

dec = int(input(“Enter decimal number”))


3 print("The decimal value of", dec, "is:")
print(bin(dec), "in binary.")
print(oct(dec), "in octal.")
print(hex(dec), "in hexadecimal.")
4 Write a Python program to perform following operation on given string input:
a) Count Number of Vowel in given string

string=raw_input("Enter string:")
vowels=0
for i in string:
if(i=='a' or i=='e' or i=='i' or i=='o' or i=='u' or i=='A' or i=='E' or i=='I'
or i=='O' or i=='U'):
vowels=vowels+1
print("Number of vowels are:")
print(vowels)

b) Count Length of string (donot use len() )

str = input("Enter a string: ")


counter = 0
for s in str:
counter = counter+1
print("Length of the input string is:", counter)
SAL COLLEGE OF ENGINEERING
COMPUTER ENGINNERING
Python for Data Science [3150713] – LIST OF PRACTICALS

c) Reverse string
Method 1:
def reverse(s):
str = ""
for i in s:
str = i + str
return str

s = input("Enter a string: ")

print ("The original string is : ",end="")


print (s)

print ("The reversed string(using loops) is : ",end="")


print (reverse(s))

Method 2:
def reverse(string):
string = string[::-1]
return string

s = input("Enter a string: ")

print ("The original string is : ",end="")


print (s)

print ("The reversed string(using extended slice syntax) is : ",end="")


print (reverse(s))

d) Find and replace operation

string = "python for Data Science"

print(string.replace("python", "Python"))

e) check whether string entered is a palindrome or not

def isPalindrome(s):
return s == s[::-1]

# Driver code
s = input("Enter a string: ")
ans = isPalindrome(s)

if ans:
print("Yes")
else:
print("No")
Write a program in python to implement Fibonacci series up to user entered
5
number. (Use recursive Function)
SAL COLLEGE OF ENGINEERING
COMPUTER ENGINNERING
Python for Data Science [3150713] – LIST OF PRACTICALS

Define a procedure histogram() that takes a list of integers and prints a histogram
to the screen. For example, histogram([4, 9, 7]) should print the following:
****
*********
*******

def histogram( items ):


for n in items:
6
output = ''”
times = n
while( times > 0 ):
output += '*'
times = times - 1
print(output)

histogram([4, 9, 7])
Write a Python program to convert a Panda module Series to Python list and it's
type.
import pandas as pd
ds = pd.Series([2, 4, 6, 8, 10])
print("Pandas Series and type")
7
print(ds)
print(type(ds))
print("Convert Pandas Series to Python list")
print(ds.tolist())
print(type(ds.tolist()))
Write a Python program to add, subtract, multiple and divide two Pandas Series.
import pandas as pd
ds1 = pd.Series([2, 4, 6, 8, 10])
ds2 = pd.Series([1, 3, 5, 7, 9])
ds = ds1 + ds2
print("Add two Series:")
print(ds)
print("Subtract two Series:")
8
ds = ds1 - ds2
print(ds)
print("Multiply two Series:")
ds = ds1 * ds2
print(ds)
print("Divide Series1 by Series2:")
ds = ds1 / ds2
print(ds)
9 Write a Python program to convert a dictionary to a Pandas series.
import pandas as pd
d1 = {'a': 100, 'b': 200, 'c':300, 'd':400, 'e':800}
print("Original dictionary:")
print(d1)
new_series = pd.Series(d1)
print("Converted series:")
SAL COLLEGE OF ENGINEERING
COMPUTER ENGINNERING
Python for Data Science [3150713] – LIST OF PRACTICALS

print(new_series)
Write a Pandas program to convert a NumPy array to a Pandas series.
import numpy as np
import pandas as pd
np_array = np.array([10, 20, 30, 40, 50])
10 print("NumPy array:")
print(np_array)
new_series = pd.Series(np_array)
print("Converted Pandas series:")
print(new_series)
Write a Pandas program to convert Series of lists to one Series.

One Series
Original Series of list 0 Red
0 [Red, Green, White] 1 Green
1 [Red, Black] 2 White
2 [Yellow] 3 Red
dtype: object 4 Black
5 Yellow
11 import pandas as pd
s = pd.Series([
['Red', 'Green', 'White'],
['Red', 'Black'],
['Yellow']])
print("Original Series of list")
print(s)
s = s.apply(pd.Series).stack().reset_index(drop=True)
print("One Series")
print(s)
Write a Python program to draw a line with suitable label in the x axis, y axis and
a title.
import matplotlib.pyplot as plt
X = range(1, 50)
Y = [value * 3 for value in X]
print("Values of X:")
print(*range(1,50))
print("Values of Y (thrice of X):")
print(Y)
12 # Plot lines and/or markers to the Axes.
plt.plot(X, Y)
# Set the x axis label of the current axis.
plt.xlabel('x - axis')
# Set the y axis label of the current axis.
plt.ylabel('y - axis')
# Set a title
plt.title('Draw a line.')
# Display the figure.
plt.show()
13 Write a Python program to plot two or more lines on same plot with suitable
SAL COLLEGE OF ENGINEERING
COMPUTER ENGINNERING
Python for Data Science [3150713] – LIST OF PRACTICALS

legends of each line.

import matplotlib.pyplot as plt


# line 1 points
x1 = [10,20,30]
y1 = [20,40,10]
# plotting the line 1 points
plt.plot(x1, y1, label = "line 1")
# line 2 points
x2 = [10,20,30]
y2 = [40,10,30]
# plotting the line 2 points
plt.plot(x2, y2, label = "line 2")
plt.xlabel('x - axis')
# Set the y axis label of the current axis.
plt.ylabel('y - axis')
# Set a title of the current axes.
plt.title('Two or more lines on same plot with suitable legends ')
# show a legend on the plot
plt.legend()
# Display a figure.
plt.show()
14 Write a Python programming to display a bar chart of the popularity of
programming Languages.
Sample data:
Programming languages: Java, Python, PHP, JavaScript, C#, C++
Popularity: 22.2, 17.6, 8.8, 8, 7.7, 6.7
The code snippet gives the output shown in the following screenshot:
SAL COLLEGE OF ENGINEERING
COMPUTER ENGINNERING
Python for Data Science [3150713] – LIST OF PRACTICALS

import matplotlib.pyplot as plt


x = ['Java', 'Python', 'PHP', 'JavaScript', 'C#', 'C++']
popularity = [22.2, 17.6, 8.8, 8, 7.7, 6.7]
x_pos = [i for i, _ in enumerate(x)]
plt.bar(x_pos, popularity, color='blue')
plt.xlabel("Languages")
plt.ylabel("Popularity")
plt.title("PopularitY of Programming Language\n" + "Worldwide, Oct 2017
compared to a year ago")
plt.xticks(x_pos, x)
# Turn on the grid
plt.minorticks_on()
plt.grid(which='major', linestyle='-', linewidth='0.5', color='red')
# Customize the minor grid
plt.grid(which='minor', linestyle=':', linewidth='0.5', color='black')
plt.show()
15 Write a Python programming to create a pie chart of the popularity of
programming Languages.
Sample data:
Programming languages: Java, Python, PHP, JavaScript, C#, C++
Popularity: 22.2, 17.6, 8.8, 8, 7.7, 6.7
The code snippet gives the output shown in the following screenshot:
SAL COLLEGE OF ENGINEERING
COMPUTER ENGINNERING
Python for Data Science [3150713] – LIST OF PRACTICALS

import matplotlib.pyplot as plt


# Data to plot
languages = 'Java', 'Python', 'PHP', 'JavaScript', 'C#', 'C++'
popuratity = [22.2, 17.6, 8.8, 8, 7.7, 6.7]
colors = ["#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#9467bd", "#8c564b"]
# explode 1st slice
explode = (0.1, 0, 0, 0,0,0)
# Plot
plt.pie(popuratity, explode=explode, labels=languages, colors=colors,
autopct='%1.1f%%', shadow=True, startangle=140)

plt.axis('equal')
plt.show()

Subject Teacher Subject Coordinator HOD

You might also like