0% found this document useful (0 votes)
2 views

Manual Vtu

from VTU privided

Uploaded by

iqam bin yunus
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Manual Vtu

from VTU privided

Uploaded by

iqam bin yunus
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

lOMoARcPSD|40301617

Manual - VTU

introduction to python programming (Atria Institute of Technology )

Scan to open on Studocu

Studocu is not sponsored or endorsed by any college or university


Downloaded by iqam bin yunus ([email protected])
lOMoARcPSD|40301617

Data Visualization with Python

BCS358D

January 5, 2024

Downloaded by iqam bin yunus ([email protected])


lOMoARcPSD|40301617

Data Visualization with Python BCS358D

1 1a

"""
a) Write a python program to find the best of two test average
marks out 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.
"""
s = input('Enter a: ')
a = int(s)

s = input('Enter b: ')
b = int(s)

s = input('Enter c: ')
c = int(s)

max = a+b

if max < a+c:


max = a+c

if max < b+c:


max = b+c

avg = max/2
print(avg)

Computer Science and Engineering 2 of 26

Downloaded by iqam bin yunus ([email protected])


lOMoARcPSD|40301617

Data Visualization with Python BCS358D

2 1b

"""
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.
"""
s = #input('Enter number : ')

if s[:] == s[::-1]:
print('Palindrome')
else:
print('Not a palindrome')

d = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

for c in s:
i = int(c)
d[i] = d[i] + 1

print(d)

Computer Science and Engineering 3 of 26

Downloaded by iqam bin yunus ([email protected])


lOMoARcPSD|40301617

Data Visualization with Python BCS358D

3 2a

"""
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) :
a, b = 0, 1
for i in range(n) :
c = a+b
a, b = b, c
return c

s = input("Enter n: ")
n = int(s)

if n > 0:
rv = f(n)
print(rv)
else:
print("n = ", n, "Enter n > 1")

RUN

Enter n: 6
13

Computer Science and Engineering 4 of 26

Downloaded by iqam bin yunus ([email protected])


lOMoARcPSD|40301617

Data Visualization with Python BCS358D

4 2b

"""
Develop a python program to convert binary to decimal,
octal to hexadecimal using functions.
"""
s = input('Enter binary number: ')
n = int(s, 2)

print(n)

s = input('Enter octal number: ')


n = int(s, 8)

n = hex(n)
print(n)

RUN

Enter binary number: 1010


10
Enter octal number: 15
0xd

Computer Science and Engineering 5 of 26

Downloaded by iqam bin yunus ([email protected])


lOMoARcPSD|40301617

Data Visualization with Python BCS358D

5 3a

"""
a) Write a Python program that accepts a sentence and find the number
of words, digits, uppercase letters and lowercase letters.
"""

s = input("Enter s: ")
d = {}

for c in s:
if c.isspace(): # Assumption is words are separated by a single space.
d['W'] = d.get('W', 1) + 1

if c.isdigit():
d['D'] = d.get('D', 0) + 1

if c.isupper():
d['U'] = d.get('U', 0) + 1

if c.islower():
d['L'] = d.get('L', 0) + 1

print(d)

RUN

Enter s: I saw the Moon 10 days ago.


{'U': 2, 'W': 7, 'L': 16, 'D': 2}

Computer Science and Engineering 6 of 26

Downloaded by iqam bin yunus ([email protected])


lOMoARcPSD|40301617

Data Visualization with Python BCS358D

6 3b

"""
b) Write a Python program to find the string similarity between
two given strings
"""
# Longest contiguous matching subsequence (LCS)
import difflib

s1 = input("Enter s1: ")


s2 = input("Enter s2: ")

rv = difflib.SequenceMatcher(a=s1, b=s2)

print("Similarity Metric: ", rv.ratio())

RUN

Enter s1: Python Exercises


Enter s2: Python Exercises
Similarity Metric: 1.0

Enter s1: Python Exercises


Enter s2: Python Exercise
Similarity Metric: 0.967741935483871

Computer Science and Engineering 7 of 26

Downloaded by iqam bin yunus ([email protected])


lOMoARcPSD|40301617

Data Visualization with Python BCS358D

7 4a

"""
Write a Python program to Demonstrate how to Draw a Bar Plot
using Matplotlib
"""

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv('./src/lifespan.csv')

# Plot area
plt.figure(figsize=(10, 4))

# Title, X-axis and Y-axis labels


plt.title('Animal X Lifespan')
plt.xlabel('Animal')
plt.ylabel('Lifespan')

# Bar plot 1
plt.bar(df['Animal'], df['Lifespan'], width=.75, label="blue")

# legend
plt.legend(loc="best")

# Display
plt.show()

Computer Science and Engineering 8 of 26

Downloaded by iqam bin yunus ([email protected])


lOMoARcPSD|40301617

Data Visualization with Python BCS358D

RESULT

Computer Science and Engineering 9 of 26

Downloaded by iqam bin yunus ([email protected])


lOMoARcPSD|40301617

Data Visualization with Python BCS358D

8 4b

"""
Write a Python program to Demonstrate how to Draw a Scatter Plot
using Matplotlib.
"""

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv('./src/lifespan.csv')

# Plot area
plt.figure(figsize=(10, 4))

# Title, X-axis and Y-axis labels


plt.title('Animal X Lifespan')
plt.xlabel('Animal')
plt.ylabel('Lifespan')

plt.scatter(df['Animal'], df['Lifespan'], label="blue")

# legend
plt.legend(loc="best")

# Display
plt.show()

Computer Science and Engineering 10 of 26

Downloaded by iqam bin yunus ([email protected])


lOMoARcPSD|40301617

Data Visualization with Python BCS358D

RESULT

Computer Science and Engineering 11 of 26

Downloaded by iqam bin yunus ([email protected])


lOMoARcPSD|40301617

Data Visualization with Python BCS358D

9 5a

"""
Write a Python program to Demonstrate how to Draw a Scatter Plot
using Matplotlib.
"""

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv('./src/lifespan.csv')

# Plot area
plt.figure(figsize=(10, 4))

# Title, X-axis and Y-axis labels


plt.title('Animal X Lifespan')
plt.xlabel('Lifespan')
plt.ylabel('Animal')

# n[i] contains the number of values that lie within the


# interval with the boundaries bins [i] and bins [i + 1]:

n, bins, edges = plt.hist(df['Lifespan'], edgecolor='w', label="blue")


plt.xticks(bins)

# legend
plt.legend(loc="best")

# Display
plt.show()

Computer Science and Engineering 12 of 26

Downloaded by iqam bin yunus ([email protected])


lOMoARcPSD|40301617

Data Visualization with Python BCS358D

RESULT

Computer Science and Engineering 13 of 26

Downloaded by iqam bin yunus ([email protected])


lOMoARcPSD|40301617

Data Visualization with Python BCS358D

10 5b

"""
Write a Python program to Demonstrate how to Draw a Pie Chart
using Matplotlib.
"""

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv('./src/lifespan.csv')

# Plot area
plt.figure(figsize=(10, 4))

# Title, X-axis and Y-axis labels


plt.title('Animal X Lifespan')
plt.xlabel('Animal')
plt.ylabel('Lifespan')

# Pie plot; try explode, color, radius


plt.pie(df['Lifespan'], labels=df['Animal'], autopct="%0.1f%%")

# legend
plt.legend(loc="best")

# Display
plt.show()

Computer Science and Engineering 14 of 26

Downloaded by iqam bin yunus ([email protected])


lOMoARcPSD|40301617

Data Visualization with Python BCS358D

Computer Science and Engineering 15 of 26

Downloaded by iqam bin yunus ([email protected])


lOMoARcPSD|40301617

Data Visualization with Python BCS358D

11 6a

"""
Write a Python program to illustrate Linear Plotting using
Matplotlib.
"""

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv('./src/lifespan.csv')

# Plot area
plt.figure(figsize=(10, 4))

# Title, X-axis and Y-axis labels


plt.title('Animal X Lifespan')
plt.xlabel('Animal')
plt.ylabel('Lifespan')

# Linear plot; color, marker, line style


plt.plot(df['Animal'], df['Lifespan'], marker='o', color='blue')

# grid
plt.grid()

# legend
plt.legend(loc="best")

# Display
plt.show()

Computer Science and Engineering 16 of 26

Downloaded by iqam bin yunus ([email protected])


lOMoARcPSD|40301617

Data Visualization with Python BCS358D

Computer Science and Engineering 17 of 26

Downloaded by iqam bin yunus ([email protected])


lOMoARcPSD|40301617

Data Visualization with Python BCS358D

12 6b

"""
Write a Python program to illustrate Linear Plotting using
Matplotlib.
"""

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv('./src/lifespan.csv')

# Plot area
plt.figure(figsize=(10, 4))

# Title, X-axis and Y-axis labels


plt.title('Animal X Lifespan')
plt.xlabel('Animal')
plt.ylabel('Lifespan')

# Linear plot; color, marker, line style


plt.plot(df['Animal'], df['Lifespan'], marker='o', color='blue', linestyle='dashed'

# grid
plt.grid()

# legend
plt.legend(loc="best")

# Display
plt.show()

Computer Science and Engineering 18 of 26

Downloaded by iqam bin yunus ([email protected])


lOMoARcPSD|40301617

Data Visualization with Python BCS358D

Computer Science and Engineering 19 of 26

Downloaded by iqam bin yunus ([email protected])


lOMoARcPSD|40301617

Data Visualization with Python BCS358D

13 7

"""
Write a Python program which explains uses of customizing seaborn
plots with Aesthetic functions.
"""
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
sns.set(style='white')

df = pd.read_csv('./src/lifespan.csv')

# Plot area
# plt.figure(figsize=(10, 4)) not required

# Title, X-axis and Y-axis labels


plt.title('Animal X Lifespan')
# plt.xlabel('Animal') not required.
# plt.ylabel('Lifespan')

# set sthe theme


# Linear plot; color, marker, line style
sns.scatterplot(data=df, x='Animal', y='Lifespan')

sns.set(style="dark")
sns.despine()
# grid
# plt.grid() not required

# legend
plt.legend(loc="best")

Computer Science and Engineering 20 of 26

Downloaded by iqam bin yunus ([email protected])


lOMoARcPSD|40301617

Data Visualization with Python BCS358D

# Display
plt.show()

Computer Science and Engineering 21 of 26

Downloaded by iqam bin yunus ([email protected])


lOMoARcPSD|40301617

Data Visualization with Python BCS358D

14 8

Write a Python program to explain working with bokeh line graph using Annota-
tions and Legends. a) Write a Python program for plotting different types of plots
using Bokeh.

# pip install bokeh

import numpy as np
import pandas as pd
from bokeh.plotting import figure, show

df = pd.read_csv('./src/lifespan.csv')

plot = figure(title='Animal X Lifespan', x_axis_label='Animal', y_axis_label='Lifes


plot.line([nx for nx in range(len(df['Animal']))], df['Lifespan'], legend_label='1'

# Output plot
show(plot)

Computer Science and Engineering 22 of 26

Downloaded by iqam bin yunus ([email protected])


lOMoARcPSD|40301617

Data Visualization with Python BCS358D

Computer Science and Engineering 23 of 26

Downloaded by iqam bin yunus ([email protected])


lOMoARcPSD|40301617

Data Visualization with Python BCS358D

15 9

"""
Write a Paython program to draw 3D Plots using Plotly Libraries.
"""

import plotly.graph_objects as go

import numpy as np
import pandas as pd
from bokeh.plotting import figure, show

df = pd.read_csv('./src/lifespan.csv')

print(df)

fig = go.Figure(data=[go.Scatter3d(x=df['Animal'], y=df['Lifespan'], z=df['Weight']


fig.update_layout(scene=dict(xaxis_title='X', yaxis_title='Y', zaxis_title='Z'),
title='Animal X Lifespan X Weight')

fig.show()

Computer Science and Engineering 24 of 26

Downloaded by iqam bin yunus ([email protected])


lOMoARcPSD|40301617

Data Visualization with Python BCS358D

Animal Lifespan Weight


0 Human 122.5 62035.0
1 Dog 24.0 40000.0
2 Cat 30.0 3900.0
3 Alligator 77.0 15000.0
4 Hamster 3.9 105.0
5 Penguin 26.0 13036.8
6 Lion 27.0 175000.0
7 Shark 392.0 175000.0
8 Tortoise 177.0 20.5
9 Elephant 65.0 48000.0

Computer Science and Engineering 25 of 26

Downloaded by iqam bin yunus ([email protected])


lOMoARcPSD|40301617

Data Visualization with Python BCS358D

10 Sealion 35.7 18000.0


11 Fruitfly 0.3 50.0
12 Mouse 4.0 20.5
13 Giraffe 39.5 80000.0
14 Wildboar 27.0 13000.0

Computer Science and Engineering 26 of 26

Downloaded by iqam bin yunus ([email protected])

You might also like