0% found this document useful (0 votes)
13 views10 pages

DVP 2

Uploaded by

Worldly
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)
13 views10 pages

DVP 2

Uploaded by

Worldly
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/ 10

2/28/24, 7:46 PM Untitled1 - Jupyter Notebook

In [ ]: #1st a

In [13]: a=float(input("enter the marks of 1st subject:"))


b=float(input("enter the marks of 2nd subject:"))
c=float(input("enter the marks of 3rd subject:"))

ab=(a+b)/2.0
bc=(b+c)/2.0
ca=(a+c)/2.0

print("best of two test average marks is:")
if(ab>bc and ab>ca):
print("ab")
if(bc>ab and bc>ca):
print("bc")
if(ca>bc and ca>ab):
print("ca")

enter the marks of 1st subject:33


enter the marks of 2nd subject:43
enter the marks of 3rd subject:55
best of two test average marks is:
bc

In [ ]: #1.b

In [7]: num=int(input("enter a number:"))


num_str=str(num)
Is_palindrome=num_str==num_str[::-1]
digit_counts={}
for digit in num_str:
if digit in digit_counts:
digit_counts[digit]+=1
else:
digit_counts[digit]=1
print("Is palindrome:",Is_palindrome)
print("digit counts:",digit_counts)

enter a number:123454321
Is palindrome: True
digit counts: {'1': 2, '2': 2, '3': 2, '4': 2, '5': 1}

In [ ]: #2.a

localhost:8888/notebooks/Untitled1.ipynb?kernel_name=python3 1/10
2/28/24, 7:46 PM Untitled1 - Jupyter Notebook

In [8]: def fn(n):


if n==1:
return 0
elif n==2:
return 1
else:
return fn(n-1)+fn(n-2)
num=int(input("enter a number:"))
if num>0:
print("fn(",num,")=",fn(num),sep="")
else:
print("error in input")

enter a number:5
fn(5)=3

In [ ]: #2.b

In [12]: def binary_to_decimal(binary):


return int(binary,2)

def octal_to_hexadecimal(octal):
decimal=int(octal,8)
hexadecimal=hex(decimal)[2:]
return hexadecimal
binary=input("ennter a binary number:")
octal=input("enter a octal number:")

print("decimal equivalent of binary number:",binary_to_decimal(binary))
print("hexadecimal equivalent of octal number:",octal_to_hexadecimal(octal))

ennter a binary number:10110


enter a octal number:4563
decimal equivalent of binary number: 22
hexadecimal equivalent of octal number: 973

In [ ]: #3.b

localhost:8888/notebooks/Untitled1.ipynb?kernel_name=python3 2/10
2/28/24, 7:46 PM Untitled1 - Jupyter Notebook

In [18]: str1=input("enter string1\n")


str2=input("enter string\n")
if len(str2)>len(str1):
short=len(str1)
long=len(str2)
else:
short=len(str2)
long=len(str1)
matchCnt=0
for i in range(short):
if str1[i]==str2[i]:
matchCnt+=1
print("similarity between two said string is:")
print(matchCnt/long)

enter string1
rayyan
enter string
rayyan jafri
similarity between two said string is:
0.5

In [ ]: #3.a

In [20]: sentence=input("enter a sentence:")


wordlist=sentence.split(" ")
print("the sentence has",len(wordlist),"words")
digCnt=upCnt=loCnt=0
for ch in sentence:
if '0'<=ch<='9':
digCnt+=1
elif 'A'<=ch<='Z':
upCnt+=1
elif 'a'<=ch<='z':
loCnt+=1
print("this sentence has:",digCnt,"digits",upCnt,"uppercase letters",loCnt,"low

enter a sentence:Md Rayyan Jafri


the sentence has 1 words
this sentence has: 0 digits 3 uppercase letters 10 lower case letters

In [ ]: #4.a

localhost:8888/notebooks/Untitled1.ipynb?kernel_name=python3 3/10
2/28/24, 7:46 PM Untitled1 - Jupyter Notebook

In [22]: import numpy as np


import matplotlib.pyplot as plt
data={'c':20,'c++':15,'java':30,'python':35}
courses=list(data.keys())
values=list(data.values())

fig=plt.figure(figsize=(10,5))
plt.bar(courses,values,color='maroon',width=0.4)
plt.xlabel("courses offered")
plt.ylabel("no of students enrolled")
plt.title("students enrolled in diffferent courses")
plt.show()

In [ ]: #4.b

localhost:8888/notebooks/Untitled1.ipynb?kernel_name=python3 4/10
2/28/24, 7:46 PM Untitled1 - Jupyter Notebook

In [24]: import matplotlib.pyplot as plt


x_axis_values=[6,7,12,14,16,9,8,5,3,2,1,13]
y_axis_values=[98,96,95,102,112,99,95,86,88,87,85.77,71]
plt.scatter(x_axis_values,y_axis_values)
plt.show()

In [ ]: #5.a

localhost:8888/notebooks/Untitled1.ipynb?kernel_name=python3 5/10
2/28/24, 7:46 PM Untitled1 - Jupyter Notebook

In [26]: import matplotlib.pyplot as plt


import numpy as np
data=np.random.randn(1000)
plt.hist(data,bins=30,color='skyblue',edgecolor='black')
plt.xlabel("values")
plt.ylabel("frequency")
plt.title("Basic histogram")
plt.show()

In [ ]: #5.b

localhost:8888/notebooks/Untitled1.ipynb?kernel_name=python3 6/10
2/28/24, 7:46 PM Untitled1 - Jupyter Notebook

In [36]: import matplotlib.pyplot as plt


set_data=[20,25,50]
plt.pie(set_data)
plt.show()

In [ ]: #6.a

localhost:8888/notebooks/Untitled1.ipynb?kernel_name=python3 7/10
2/28/24, 7:46 PM Untitled1 - Jupyter Notebook

In [42]: import matplotlib.pyplot as plt


import numpy as np
x=np.array([1,2,3,4])
y=x*2
plt.plot(x,y)
plt.show()

In [ ]: #6.b

localhost:8888/notebooks/Untitled1.ipynb?kernel_name=python3 8/10
2/28/24, 7:46 PM Untitled1 - Jupyter Notebook

In [46]: import matplotlib.pyplot as plt


x=[1,2,3,4,5]
y1=[2,3,5,7,11]
y2=[1,4,6,8,10]

plt.plot(x,y1,'bo-',label='line1')
plt.plot(x,y2,'rs--',label='line2')

plt.xlabel('x-axis')
plt.ylabel('y-axis')
plt.title("liner plot using line formatting")
plt.legend()
plt.show()

In [ ]: #7

localhost:8888/notebooks/Untitled1.ipynb?kernel_name=python3 9/10
2/28/24, 7:46 PM Untitled1 - Jupyter Notebook

In [50]: import seaborn as sns


import matplotlib.pyplot as plt
tips=sns.load_dataset("tips")
sns.set_style("darkgrid")
sns.boxplot(x="day",y="total_bill",hue="sex",data=tips,palette="Set2")
plt.title("boxplot with hue")
plt.xlabel("day of the week")
plt.ylabel("total_bill")
plt.legend(title="Sex",loc="upper left")
plt.show()

In [ ]: ​

localhost:8888/notebooks/Untitled1.ipynb?kernel_name=python3 10/10

You might also like