DVP 1
DVP 1
DVP 1
Enter a number : 2
fn(2) = 1
In [6]: def binary_to_decimal(binary):
decimal = 0
power = 0
while binary != 0:
last_digit = binary % 10
decimal += last_digit * (2 ** power)
binary //= 10
power += 1
return decimal
def octal_to_hexadecimal(octal):
decimal = 0
power = 0
while octal != 0:
last_digit = octal % 10
decimal += last_digit * (8 ** power)
octal //= 10
power += 1
hexadecimal = ""
hex_digits = "0123456789ABCDEF"
while decimal != 0:
remainder = decimal % 16
hexadecimal = hex_digits[remainder] + hexadecimal
decimal //= 16
return hexadecimal
# Binary to Decimal conversion
binary_number = input("Enter a binary number: ")
decimal_number = binary_to_decimal(int(binary_number))
print("Decimal equivalent:", decimal_number)
# Octal to Hexadecimal conversion
octal_number = input("Enter an octal number: ")
hexadecimal_number = octal_to_hexadecimal(int(octal_number))
print("Hexadecimal equivalent:", hexadecimal_number)
Enter String 1
dhanush
Enter String 2
dhanas
Similarity between two said strings:
0.7142857142857143
In [10]: import matplotlib.pyplot as plt
# Sample data
categories = ['Category A', 'Category B', 'Category C', 'Category D']
values = [25, 50, 75, 100]
# Create a bar plot
plt.bar(categories, values, color='blue')
# Add labels and title
plt.xlabel('Categories')
plt.ylabel('Values')
plt.title('Bar Plot Example')
# Show the plot
plt.show()
In [11]: import matplotlib.pyplot as plt
# Sample data
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = [10, 15, 13, 17, 20, 22, 25, 30, 28, 35]
# Create a scatter plot
plt.scatter(x, y, color='blue', marker='o', label='Scatter Points', alpha=0.6)
# Add labels and title
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Scatter Plot Example')
# Show the plot
plt.legend()
plt.grid(True)
plt.show()
In [12]: import matplotlib.pyplot as plt
# Sample data
data = [10, 15, 10, 20, 25, 30, 25, 30, 30, 35, 40, 45, 45, 45, 50]
# Create a histogram plot
plt.hist(data, bins=5, color='blue', edgecolor='black')
# Add labels and title
plt.xlabel('Value Bins')
plt.ylabel('Frequency')
plt.title('Histogram Plot Example')
# Show the plot
plt.grid(True)
plt.show()
In [13]: import matplotlib.pyplot as plt
# Sample data
labels = ['Category A', 'Category B', 'Category C', 'Category D']
sizes = [30, 45, 15, 10]
colors = ['lightblue', 'lightcoral', 'lightgreen', 'lightsalmon']
explode = (0.1, 0, 0, 0) # Explode the first slice (Category A)
# Create a pie chart
plt.pie(sizes, explode=explode, labels=labels, colors=colors,
autopct='%1.1f%%', shadow=True, startangle=140)
# Add a title
plt.title('Pie Chart Example')
# Show the plot
plt.show()
In [15]: import matplotlib.pyplot as plt
# Sample data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# Create a linear plot
plt.plot(x, y, marker='o', color='blue')
# Add labels and title
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Linear Plot Example')
# Show the plot
plt.grid(True)
plt.show()
In [16]: import matplotlib.pyplot as plt
# Sample data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# Create a linear plot with custom line formatting
plt.plot(x, y, marker='o', color='blue', linestyle='--', linewidth=2,
markersize=8, label='Line Example')
# Add labels and title
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Linear Plot with Line Formatting')
# Show a legend
plt.legend()
# Show the plot
plt.grid(True)
plt.show()
In [17]: import seaborn as sns
import matplotlib.pyplot as plt
# Load a built-in dataset from Seaborn
tips = sns.load_dataset("tips")
# Set Seaborn style and color palette
sns.set(style="whitegrid", palette="Set1")
# Create a customized scatter plot
plt.figure(figsize=(8, 6))
sns.scatterplot(x="total_bill", y="tip", data=tips, hue="time", style="sex",
s=100, palette="Set2")
plt.title("Customized Scatter Plot")
plt.xlabel("Total Bill")
plt.ylabel("Tip")
plt.legend(title="Time of Day")
plt.show()
# Create a customized histogram
plt.figure(figsize=(8, 6))
sns.histplot(tips["total_bill"], bins=20, kde=True, color="skyblue",
line_kws={"color": "red"})
plt.title("Customized Histogram")
plt.xlabel("Total Bill")
plt.ylabel("Frequency")
plt.grid(True)
plt.show()
# Create a customized box plot
plt.figure(figsize=(8, 6))
sns.boxplot(x="day", y="total_bill", data=tips, palette="husl")
plt.title("Customized Box Plot")
plt.xlabel("Day")
plt.ylabel("Total Bill")
plt.show()
In [25]: import plotly.graph_objects as go
import numpy as np
# Create a grid of x and y values
x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
X, Y = np.meshgrid(x, y)
# Define a 3D function (e.g., a surface)
Z = np.tan(np.sqrt(X**2 + Y**2))
# Create a 3D surface plot using Plotly
fig = go.Figure(data=go.Surface(z=Z, x=x, y=y))
fig.update_layout(scene=dict(zaxis_title="Z-axis", yaxis_title="Y-axis",
xaxis_title="X-axis"),
title="3D Surface Plot")
# Show the plot
fig.show()
3D Surface Plot
In [28]: import plotly.graph_objects as go
import pandas as pd
# Sample time series data
data = {
'Date': pd.date_range(start='2023-01-01', periods=30, freq='D'),
'Value': [10, 15, 12, 18, 22, 24, 30, 28, 35, 40, 45, 48, 52, 50, 60, 58, 65,
70, 75, 80, 78, 85, 90, 95, 100, 95, 105, 110, 115, 120]
}
df = pd.DataFrame(data)
# Create a time series line plot
fig = go.Figure(data=go.Scatter(x=df['Date'], y=df['Value'],
mode='lines+markers', name='Time Series'))
# Set axis labels and plot title
fig.update_layout(xaxis_title='Date', yaxis_title='Value', title='Time Series Plot')
# Show the plot
fig.show()
Time Series Plot
120
100
80
Value
60
40
20
Chicago
In [ ]: