PYTHON LAB MANUAL
1A) Write A Python Program to Find the Best Of Two Test Average Marks
Out Of Three Test’s Marks Accepted from The User.
Program:
marks1=int(input("Enter Test 1 Marks:"))
marks2=int(input("Enter Test 2 Marks:"))
marks3=int(input("Enter Test 3 Marks:"))
minimum=min(marks1,marks2,marks3)
sumofbest2=marks1+marks2+marks3-minimum
avgofbest2=sumofbest2/2
print("Avarage of best Two :",avgofbest2)
1b) 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.
Program :
n=input("enter number")
temp=n
rev=0
while(n>0):
dig=n%10
rev=rev*10+dig
n=n//10
print("the reverse number is :",rev)
if(temp==rev):
print("the number is palindrome:")
else:
print("the number is not a palindrome:")
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.
Program:
def fibonacci(n):
fib_series = []
a, b = 0, 1
for _ in range(n):
fib_series.append(a)
a, b=b, a+b
return fib_series
num_terms = 5
result = fibonacci(num_terms)
print(f"the fibonaccie series up to {num_terms} terms:")
print(result)
2b) Develop a python program to convert binary to decimal, octal to
hexadecimal using functions.
Program:
def BinToDec(b):
return int(b,2)
print("enter the binary number:",end="")
bnum=input()
dnum=BinToDec(bnum)
print("\nEquivalent Decimal value=",dnum)
def OctTohex(o):
return hex(int(o,8))
print("enter octal number:",end="")
onum=input()
hnum=OctTohex(onum)
print("\n equuivalent Hexadecimal Value=",hnum[2:].upper())
3a) Write a Python program that accepts a sentence and find the number of words,
digits, uppercase letters and lowercase letters.
Program:
s = input("Enter a sentence: ")
w, d, u, l, m = 0, 0, 0, 0, 0
l_w = s.split()
w = len(l_w)
for c in s:
if c.isdigit():
d=d+1
elif c.isupper():
u=u+1
elif c.islower():
l=l+1
elif c.isspace():
m = m+1
print ("No of Words: ", w)
print ("No of Digits: ", d)
print ("No of Uppercase letters: ", u)
print ("No of Lowercase letters: ", l)
print("no of space is:",m)
3b) Write a Python program to find the string similarity between two given Strings
Program:
str1 = input("Enter First String:\n")
str2 = input("Enter Second String\n")
if len(str2) < len(str1):
short = len(str2)
long = len(str1)
else:
short = len(str1)
long = len(str2)
matchCnt = 0
for i in range(short):
if str1[i] == str2[i]:
matchCnt += 1
print("Similarity between two said String:")
print(matchCnt/ long)
4a) Write a python program to implement insertion sort and merge sort using lists
program:
def insertion_sort(alist):
for i in range(1, len(alist)):
temp = alist[i]
j=i-1
while (j >= 0 and temp < alist[j]):
alist[j + 1] = alist[j]
j=j-1
alist[j + 1] = temp
alist = input('Enter The List of Numbers:').split()
alist = [int(x) for x in alist]
insertion_sort(alist)
print('Sorted List: ', end='')
print(alist)
Merge Sort Program
Program:
def mergesort(list1):
if len(list1) > 1:
mid = len(list1) // 2
left = list1[:mid]
right = list1[mid:]
mergesort(left)
mergesort(right)
i=0
j=0
k=0
while i < len(left) and j < len(right):
if left[i] < right[j]:
list1[k] = left[i]
i=i+1
k=k+1
else:
list1[k] = right[j]
j=j+1
k=k+1
while i < len(left):
list1[k] = left[i]
i=i+1
k=k+1
while j < len(right):
list1[k] = right[j]
j=j+1
k=k+1
list1 = input('enter the list of values to be sorted: ').split()
list1 = [int(x) for x in list1]
mergesort(list1)
print(list1)
5a) Write a program to convert roman numbers in to integer values using
dictionaries.
Program:
class sol_Roman:
def roman_to_integerNo(self, s):
roman_no = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
integer_no = 0
for i in range(len(s)):
if i > 0 and roman_no[s[i]] > roman_no[s[i - 1]]:
integer_no += roman_no[s[i]] - 2 * roman_no[s[i - 1]]
else:
integer_no += roman_no[s[i]]
return integer_no
print("Roman Numerical to Integer is:",
sol_Roman().roman_to_integerNo(input("Enter the Roman Numerals: ")))
5b)Write a function called isphonenumber () to recognize a pattern 415-555- 4242
without using regular expression and also write the code to recognize the same pattern
using regular expression.
def isphonenumber(s):
if len(s) != 12:
return False
for i in range(12):
if i in (3, 7):
if s[i] != '_':
return False
else:
if not s[i].isdigit():
return False
return True
phone_number = '415_555_4242'
if isphonenumber(phone_number):
print(f"{phone_number} is a valid phone number.")
else:
print(f"{phone_number} is not a valid phone number.")