PROGRAM 1
A) Write a python program to find the best of two test average
marks out of three test’s marks accepted from the user.
SOURCE CODE
m1 = int(input("Enter marks for test1 : "))
m2 = int(input("Enter marks for test2 : "))
m3 = int(input("Enter marks for test3 : "))
if m1 <= m2 and m1 <= m3:
avgMarks = (m2 + m3) / 2
elif m2 <= m1 and m2 <= m3:
avgMarks = (m1 + m3) / 2
elif m3 <= m1 and m2 <= m2:
avgMarks = (m1 + m2) / 2
print("Average of best two test marks out of three test’s marks is",
avgMarks)
OUTPUT
Enter marks for test1 : 12
Enter marks for test2 : 11
Enter marks for test3 : 10
Average of best two test marks out of three test’s marks is 11.5
Enter marks for test1 : 54
Enter marks for test2 : 76
Enter marks for test3 : 34
Average of best two test marks out of three test’s marks is 65
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.
SOURCE CODE
val = int(input("Enter a value : "))
str_val = str(val)
if str_val == str_val[::-1]:
print("Palindrome")
else:
print("Not Palindrome")
for i in range(10):
if str_val.count(str(i)) > 0:
print(str(i), "appears", str_val.count(str(i)), "times")
OUTPUT
Enter a value : 11
Palindrome
1 appears 2 times
Enter a value :12333
Not Palindrome
1 appears 1 times
2 appears 1 times
3 appears 3 times
PROGRAM 2
A) 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.
SOURCE CODE
def fn(n):
if n <= 2:
return n - 1
else:
return fn(n-1) + fn(n-2)
try:
num = int(input("Enter a number : "))
if num > 0:
print(f' fn({num}) = {fn(num)}')
else:
print("Input should be greater than 0")
except ValueError:
print("Try with numeric value")
OUTPUT
Enter a number : 6
fn(6) = 5
Enter a number : asc
Try with numeric value
Enter a number : -3
Input should be greater than 0
B) Develop a python program to convert binary to decimal, octal to hexadecimal
using functions.
SOURCE CODE
def bin2Dec(val):
rev = val[::-1]
dec = 0
i=0
for dig in rev:
dec += int(dig) * 2 ** i
i += 1
return dec
def oct2Hex(val):
rev = val[::-1]
dec = 0
i=0
for dig in rev:
dec += int(dig) * 8 ** i
i += 1
list = []
while dec != 0:
list.append(dec % 16)
dec = dec // 16
nl = []
for elem in list[::-1]:
if elem <= 9:
nl.append(str(elem))
else:
nl.append(chr(ord('A') + (elem - 10)))
hex = "".join(nl)
return hex
num1 = input("Enter a binary number : ")
print(bin2Dec(num1))
num2 = input("Enter a octal number : ")
print(oct2Hex(num2))
OUTPUT
Enter a binary number : 1010
Equivalent Decimal value is 10
Enter a octal number : 73
Equivalent hexadecimal value is 3B
Enter a binary number : 1110
Equivalent Decimal value = 14
Enter a octal number : 15
Equivalent hexadecimal value = D
PROGRAM 3
A) Write a Python program that accepts a sentence and find the number of
words,digits, uppercase letters and lowercase letters.
SOURCE CODE
sentence = input("enter a sentence: ")
wordList = sentence.split(" ")
print("this 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, "upper case letters",
loCnt, "lower case letters")
OUTPUT
enter a sentence :
John went to market
This sentence has 4 words
This sentence has 0 digits 1 upper case letters 16 lower case letters
B) Write a Python program to find the string similarity between two given
strings
SOURCE CODE
str1 = input("Enter String 1 \n")
str2 = input("Enter String 2 \n")
if len(str2) < len(str1):
short = len(str2)
long = len(str1)
else:
short = len(str1)
long = len(str2)
match_count = 0
for i in range(short):
if str1[i] == str2[i]:
match_count += 1
print("Similarity between two said strings: ")
print(match_count/long)
OUTPUT
Enter String 1
HAPPY
Enter String 2
GOOGLE
Similarity between two said strings:
0.0
Enter String 1 : SWEET
Enter String 2 : SWEET
Similarity between strings "SWEET" and "SWEET" is : 1.0
Enter String 1 : FACE
Enter String 2 : FACEBOOK
Similarity between strings "FACE" and "FACEBOOK" is :
0.6666666666666666