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

python

The document contains multiple Python functions for various tasks including calculating averages of test scores, checking if a number is a palindrome, generating Fibonacci sequences, converting binary and octal to decimal and hexadecimal, analyzing sentences for character types, and measuring string similarity. It also includes visualizations using matplotlib for correlation between hours and marks, distribution of sepal lengths from the Iris dataset, and a pie chart of fruit distribution. Each function is designed to take user input and display results accordingly.

Uploaded by

Akhila K T
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

python

The document contains multiple Python functions for various tasks including calculating averages of test scores, checking if a number is a palindrome, generating Fibonacci sequences, converting binary and octal to decimal and hexadecimal, analyzing sentences for character types, and measuring string similarity. It also includes visualizations using matplotlib for correlation between hours and marks, distribution of sepal lengths from the Iris dataset, and a pie chart of fruit distribution. Each function is designed to take user input and display results accordingly.

Uploaded by

Akhila K T
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

def average():

test1=float(input("Enter marks for test1 "))


test2=float(input("Enter marks for test2 "))
test3=float(input("Enter marks for test3 "))

marks=[test1, test2, test3]


marks.sort(reverse=True)
best_avg=(marks[0] + marks[1])/ 2
print(f"the average of 2 tests {best_avg: .2f}")
average()

def palindrome():
num=input("Enter a number ")

if num==num[::-1]:
print(f"the {num} is palindrome")
else:
print(f"the {num} is not palindrome")

print("Digit Occurances")
for digit in set(num):
print(f"{digit}: {num.count(digit)}")
palindrome()

def fibonacci(n):
if n <= 0:
print("Error the number should be greater tahn 0")
return
fib=[0, 1]
for i in range(2,n):
fib.append(fib[-1]+fib[-2])
print(f"the fibonacci sequence is {n} sequence {fib[:n]}")
try:
N=int(input("Enter a positive integer "))
fibonacci(N)
except ValueError:
print("invalid input")
2b)
def binary_to_decimal(binary):
return int(binary,2)

def ocatl_to_hexadecimal(octal):
decimal=int(octal,8)
return hex(decimal)[2:]

binary=input("Enter a binary number")


print(f" Decimal number is: {binary_to_decimal(binary)}")

octal=input("Enter a octal number")


print(f" Hexadecimal number is :{ocatl_to_hexadecimal(octal)} ")

3a)
def analyze_sentence():
sentence=input("Enter a sentence")
words=sentence.split()
digits=sum(c.isdigit() for c in sentence)
uppercase=sum(c.isupper() for c in sentence)
lowercase=sum(c.islower() for c in sentence)

print(f" Number of words: {len(words)} ")


print(f" Number of digits: {digits} ")
print(f" Number of uppercase: {uppercase} ")
print(f"Number of lowercase: {lowercase} ")
analyze_sentence()
3b)
from difflib import SequenceMatcher
def string_similarity(s1,s2):
similarity=SequenceMatcher(None,s1,s2).ratio()
print(f" The similarity between two strings '{s1}' and '{s2}' :{similarity: .2f} ")

str1=input("Enter the first string ")


str2=input("Enter the second string ")
string_similarity(str1, str2)
4a)
import matplotlib.pyplot as plt
hours = [5,5.5,6,6.5,7,7.5,8,8.5,9,9.5,10, 10.5,11, 11.5,12,12.5,13,13.5,14]
marks = [60,62,65,66, 68, 70, 71,68,66,65,64,62,60,58,45,43,42,40,35]
plt.scatter (hours, marks)
plt.title("Hours to Marks Correlation")
plt.xlabel("Hours")
plt.ylabel("Marks")
plt.show()
4b)
import matplotlib.pyplot as plt
hours = [5,5.5,6,6.5,7,7.5,8,8.5,9,9.5,10, 10.5,11, 11.5,12,12.5,13,13.5,14]
marks = [60,62,65,66, 68, 70, 71,68,66,65,64,62,60,58,45,43,42,40,35]
plt.scatter (hours, marks)
plt.title("Hours to Marks Correlation")
plt.xlabel("Hours")
plt.ylabel("Marks")
plt.show()
5a)
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris

iris = load_iris()
sepal_lengths = iris.data[:, 0]

plt.hist(sepal_lengths, bins=15, color='green', edgecolor='black', alpha=0.8)


plt.title('Distribution of Sepal Lengths (Iris Dataset)')
plt.xlabel('Sepal Length (cm)')
plt.ylabel('Frequency')
plt.show()
5b)
import matplotlib.pyplot as plt
import numpy as np

y = np.array([35, 25, 25, 15])


mylabels = ["Apples", "Bananas", "Cherries", "Dates"]
myexplode = [0.2, 0, 0, 0]

plt.pie(y, labels = mylabels, explode = myexplode, shadow = True, autopct='%.2f')


plt.show()

You might also like