Professional Documents
Culture Documents
PYTHON ListOfPracticals With Solution
PYTHON ListOfPracticals With Solution
COMPUTER ENGINNERING
Python for Data Science [3150713] – LIST OF PRACTICALS
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.
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)
c) Reverse string
Method 1:
def reverse(s):
str = ""
for i in s:
str = i + str
return str
Method 2:
def reverse(string):
string = string[::-1]
return string
print(string.replace("python", "Python"))
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:
****
*********
*******
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
plt.axis('equal')
plt.show()