Python Lab Manual
Python Lab Manual
1A) Write A Python Program to Find the Best Of Two Test Average Marks
Program:
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)
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)
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
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.")