Python lab manual
Python lab manual
BCS358D
2023-24
PART A
1. a) Write a python program to find the best of two test average marks out 01
of three test’s marks accepted from the user.
b) Develop a Python program to check whether a given number is
palindrome or not and also count the number of occurrences of each
digit in the input number.
2. a) Defined as a function F as Fn = Fn-1 + Fn-2. Write a Python program 04
which accepts a value for N (where N >0) as input and pass this value to
the function. Display suitable error message if the condition for input
value is not followed.
b) Develop a python program to convert binary to decimal, octal to
hexadecimal using functions.
3. a) Write a Python program that accepts a sentence and find the number of 06
words, digits, uppercase letters and lowercase letters.
b) Write a Python program to find the string similarity between two given
strings.
Sample Output: Sample Output:
Original string: Original string:
Python Exercises Python Exercises
Python Exercises Python Exercises
Similarity between two said strings: Similarity between two said
strings:1.0 0.967741935483871
4. a) Write a Python program to Demonstrate how to Draw a Bar Plot 09
using Matplotlib.
b) Write a Python program to Demonstrate how to Draw a Scatter Plot
using Matplotlib
5. a) Write a Python program to Demonstrate how to Draw a Histogram 11
Plot using Matplotlib.
b) Write a Python program to Demonstrate how to Draw a Pie Chart
using Matplotlib.
6. a) Write a Python program to illustrate Linear Plotting using Matplotlib. 13
b) Write a Python program to illustrate liner plotting with line
formatting using Matplotlib.
Program 1 :
a) Write a python program to find the best of two test average marks out of three test’s marks
accepted from the user.
Output:
b) Develop a Python program to check whether a given number is palindrome or not and also
count the number of occurrences of each digit in the input number.
def is_palindrome(number):
# Convert the number to a string for easy comparison
number_str = str(number)
def count_digit_occurrences(number):
# Initialize a dictionary to store digit occurrences
digit_occurrences = {str(i): 0 for i in range(10)}
return digit_occurrences
Output:
Program 2 :
a) Defined as a function F as Fn = Fn-1 + Fn-2. Write a Python program which accepts a value
for N (where N >0) as input and pass this value to the function. Display suitable error message
if the condition for input value is not followed.
def F(N):
if N <= 0:
print("Error: Number of terms must be a positive integer.")
return
elif N == 1:
return [0]
elif N == 2:
return [0, 1]
else:
f_n = [0, 1] # fn = [fn-1,fn-2]
# Call the Fibonacci function and display the result or error message
result = F(n)
if result is not None:
print(f"The numbers till N are: {result}")
Output:
def binary_to_decimal(binary):
decimal = int(binary, 2)
return decimal
def octal_to_hexadecimal(octal):
decimal = int(octal, 8)
hexadecimal = hex(decimal).upper()[2:]
return hexadecimal
# Example usage:
binary_number = "101010"
decimal_result = binary_to_decimal(binary_number)
print(f"Binary {binary_number} is equivalent to Decimal {decimal_result}")
octal_number = "52"
hexadecimal_result = octal_to_hexadecimal(octal_number)
print(f"Octal {octal_number} is equivalent to Hexadecimal {hexadecimal_result}")
Output:
Program 3 :
a) Write a Python program that accepts a sentence and find the number of words, digits,
uppercase letters and lowercase letters.
def analyze_sentence(sentence):
word_count = len(sentence.split())
digit_count = sum(c.isdigit() for c in sentence)
uppercase_count = sum(c.isupper() for c in sentence)
lowercase_count = sum(c.islower() for c in sentence)
# Example usage:
Output :
b) Write a Python program to find the string similarity between two given strings.
return dp[m][n]
# Example usage:
string1 = "python"
string2 = "program"
Output:
Program 4:
a) Write a Python program to Demonstrate how to Draw a Bar Plot using Matplotlib.
Output:
b) Write a Python program to Demonstrate how to Draw a Scatter Plot using Matplotlib
Output:
Program 5 :
a) Write a Python program to Demonstrate how to Draw a Histogram Plot using Matplotlib.
# Create a histogram
plt.hist(data, bins=30, alpha=0.7, color='blue', edgecolor='black')
Output:
b) Write a Python program to Demonstrate how to Draw a Pie Chart using Matplotlib
# Data to plot
labels = 'Python', 'Java', 'C++', 'JavaScript'
sizes = [45, 30, 15, 10] # These are percentages
colors = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue']
explode = (0.1, 0, 0, 0) # explode 1st slice
# Adding a title
plt.title('Programming Languages Usage')
output:
Program 6 :
a) Write a Python program to illustrate Linear Plotting using Matplotlib
Output:
b) Write a Python program to illustrate liner plotting with line formatting using Matplotlib.
# Adding a legend
plt.legend()
Output:
Program 7 :
Write a Python program which explains uses of customizing seaborn plots with Aesthetic
functions.
# Customize legend
plt.legend(title='Species', loc='upper right')
# Add grid
plt.grid(True)
Output:
Program 8 :
Write a Python program to explain working with bokeh line graph using Annotations and
Legends.
# Sample data
x = [1, 2, 3, 4, 5]
y1 = [2, 3, 5, 7, 11]
y2 = [1, 4, 9, 16, 25]
# Create a figure
p = figure(title='Bokeh Line Graph with Annotations and Legends', x_axis_label='X-axis',
y_axis_label='Y-axis')
# Add annotations
title = Title(text="Line Graph with Annotations and Legends", text_font_size="14pt")
p.title = title
# Add legend
p.legend.location = "top_left"
p.legend.click_policy="hide"
Output:
a) Write a Python program for plotting different types of plots using Bokeh.
# Scatter Plot
output_file("scatter_plot.html")
x = [1, 2, 3, 4, 5]
y = [6, 7, 2, 4, 6]
# Line Plot
output_notebook()
x = [1, 2, 3, 4, 5]
y = [6, 7, 2, 4, 6]
# Bar Plot
output_notebook()
# Grid Plot
output_file("grid_plot.html")
x = [1, 2, 3, 4, 5]
y1 = [6, 7, 2, 4, 6]
y2 = [2, 3, 1, 5, 8]
Output:
Program 9:
Write a Python program to draw 3D Plots using Plotly Libraries.
import plotly.graph_objects as go
# Generate sample data
import numpy as np
np.random.seed(42)
x = np.random.rand(100)
y = np.random.rand(100)
z = np.random.rand(100)
Output:
Program 10 :
Write a Python program to draw Time Series using Plotly Libraries.
import plotly.graph_objects as go
from datetime import datetime
# Sample data
dates = [
datetime(2023, 1, 1),
datetime(2023, 2, 1),
datetime(2023, 3, 1),
datetime(2023, 4, 1),
datetime(2023, 5, 1)
]
values = [10, 14, 18, 24, 30]
# Create a trace
trace = go.Scatter(x=dates, y=values, mode='lines+markers')
# Create layout
layout = go.Layout(
title='Time Series Plot',
xaxis=dict(title='Date'),
yaxis=dict(title='Value')
)
# Create figure
fig = go.Figure(data=[trace], layout=layout)
Output:
import plotly.express as px
Output: