dvpy lab exam
dvpy lab exam
dvpy lab exam
program 1.b
program 2.a
def f(n):
if n==1:
return 0
if n==2:
return 1
return f(n-1)+f(n-2)
n=int(input("enter a number:"))
print("f(",n,")=",f(n))
enter a number:5
f( 5 )= 3
prgram 2.b
def b_d(bstr):
dnum=0
bstr=bstr[::-1]
for i in range(len(bstr)):
if bstr[i]=='1':
dnum+=2**i
return dnum
def o_h(ostr):
ostr=int(ostr,8)
return hex(ostr)[2:]
while True:
print("1.Binary to Decimal")
print("2.octal to Hexadecimal")
print("3.to exit")
c=int(input("enter your choice:"))
if c==1:
bstr=input("enter a binary number:")
print("decimal=",b_d(bstr))
elif c==2:
ostr=input("enter a octal number:")
print("hexadecimal=",o_h(ostr))
elif c==3:
break
else:
print("invalid choice")
1.Binary to Decimal
2.octal to Hexadecimal
3.to exit
enter your choice:1
enter a binary number:100101
decimal= 37
1.Binary to Decimal
2.octal to Hexadecimal
3.to exit
enter your choice:2
enter a octal number:2356
hexadecimal= 4ee
1.Binary to Decimal
2.octal to Hexadecimal
3.to exit
enter your choice:3
program 3.a
s=input("enter a sentence:")
wl=len(s.split())
dc=uc=lc=0
for w in s:
if '0'<=w<='9':
dc+=1
elif 'A'<=w<='Z':
uc+=1
elif 'a'<='z':
lc+=1
print("count",wl)
print("digits=",dc)
print("upper case letters=",uc)
print("lower case letters=",lc)
program 3.b
program 4.a
import matplotlib.pyplot as p
c=['cat1','cat2','cat3','cat4']
v=[23,45,67,89]
p.bar(c,v,color='red')
p.xlabel("categories")
p.ylabel("values")
p.title("bar plot")
p.show()
program 4.b
import matplotlib.pyplot as p
x=[1,2,3,4,5,6,7,8]
y=[8,7,6,5,4,3,2,1]
p.scatter(x,y,color='yellow',marker='o',alpha=0.5,label='scatter
points')
p.xlabel("x-axis")
p.ylabel("y-axis")
p.title("scatter plot")
p.legend()
p.show()
program 5.a
import matplotlib.pyplot as p
d=[1,2,12,13,1,42,135,78,34,56,89,87,54,23,56,99,9,6,5,90]
p.hist(d,bins=5,color='green',edgecolor='black')
p.xlabel("data")
p.ylabel("frequency")
p.title("histogram")
p.grid(True)
p.show()
program 5.b
import matplotlib.pyplot as p
l=['cat1','cat2','cat3','cat4']
v=[15,15,45,25]#total should be 100
p.pie(v,labels=l,autopct='%1.1f%%')
p.show()
program 6.a
import matplotlib.pyplot as p
x=[1,2,3,4,5,6,7,8]
y=[8,7,6,5,4,3,2,1]
p.figure(figsize=(10,5))
p.plot(x,y,marker='o',linestyle='-',color='red')
p.xlabel("x-axis")
p.ylabel("y-axis")
p.title("line plot")
p.grid(True)
p.show()
program 6.b
import matplotlib.pyplot as p
x=[1,2,3,4,5,6,7,8]
y1=[8,7,6,5,4,3,2,1]
y2=[2,4,6,8,10,12,14,16]
p.figure(figsize=(10,5))
p.plot(x,y1,marker='o',linestyle='-',color='red',label='line1')
p.plot(x,y2,marker='s',linestyle='-',color='blue',label='line2')
p.xlabel("x-axis")
p.ylabel("y-axis")
p.title("line plot")
p.legend()
p.grid(True)
p.show()
program 7
import seaborn as s
import matplotlib.pyplot as p
a=[1,2,3,4,5]
b=[9,8,7,6,5]
s.set(style='whitegrid')
p.figure(figsize=(8,6))
s.scatterplot(x=a,y=b,color='red',marker='s',s=100,label='scatter
plot')
p.xlabel("x-axis")
p.ylabel("y-axis")
p.title("customised using seaborn")
p.legend()
p.show()
program 8.a
x = [1, 2, 3, 4, 5]
y1, y2 = [2, 4, 6, 8, 10], [1, 3, 5, 7, 9]
program 8.b
program 9
import plotly.graph_objects as go
import numpy as np
x =np.linspace(-5, 5, 100)
y =np.linspace(-5, 5, 100)
X, Y=np.meshgrid(x, y)
Z =np.sin(np.sqrt(X**2 + Y**2))
fig.show()
program 10.a
import plotly.graph_objects as go
import pandas as pd
data = {
'Date': pd.date_range(start='2023-01-01', periods=10, freq='D'),
'Value': [10, 12, 15, 20, 18, 22, 25, 28, 30, 32] }
df = pd.DataFrame(data)
fig = go.Figure()
fig.add_trace(go.Scatter(x=df['Date'], y=df['Value'],
mode='lines+markers', name='Time Series'))
fig.update_layout(
title="Time Series Line Chart",
xaxis_title="Date",
yaxis_title="Value",
xaxis=dict(
rangeselector=dict(
buttons=list([
dict(count=7, label="1w", step="day", stepmode="backward"),
dict(count=1, label="1m", step="month", stepmode="backward"),
dict(count=6, label="6m", step="month", stepmode="backward"),
dict(step="all")]
)),rangeslider=dict(visible=True),type="date")
)
fig.show()
program 10.b
import plotly.graph_objects as go
data = {
'State': ['Delhi', 'Maharashtra', 'Tamil Nadu', 'Karnataka',
'Kerala'],
'Latitude': [28.6139, 19.7515, 11.1271, 15.3173, 10.8505],
'Longitude': [77.2090, 75.7139, 78.6569, 75.7139, 76.2711],
'Value': [100, 200, 300, 400, 500]
}
fig = go.Figure()
for i in range(len(data['State'])):
fig.add_trace(go.Scattergeo(
lon=[data['Longitude'][i]], lat=[data['Latitude'][i]],
text=f"{data['State'][i]}<br>Value: {data['Value'][i]}",
mode='markers',
marker=dict(size=10, opacity=0.7, color=data['Value'][i],
colorscale='Viridis',
colorbar=dict(title='Value')),
name=data['State'][i]
))
fig.update_geos(scope='asia', showcoastlines=True,
coastlinecolor="Black", showland=True, landcolor="lightgray")
fig.update_layout(title='Indian States Map')
fig.show()