BCA Sem-3
Python Programming Lab Programs (23CA2307)
1.Write a program to get the list of even numbers upto a given number.
n=int(input("Enter number:"))
for i in range(n):
if(i%2==0):
print(i)
output:
Enter number:10
0
2
4
6
8
2.Write a program to get the number of vowels in the input string (No control flow allowed)
With control flow
String = input('Enter the string :')
count = 0
#to check for less conditions
#keep string in lowercase
String = String.lower()
for i in String:
if i == 'a' or i == 'e' or i == 'i' or i == 'o' or i == 'u':
#if True
count+=1
#check if any vowel found
if count == 0:
print('No vowels found')
else:
print('Total vowels are :' + str(count))
output:
Enter the string :alpa patel
Total vowels are :4
Without control flow
string = input('Enter the string :')
vowels = "aeiouAEIOU"
count = sum(string.count(vowel) for vowel in vowels)
print(count)
output:
Enter the string :Hello How R U?
4
1|Page
3.Write a python program to find the factorial of a number using recursion
def recur_factorial(n):
if n == 1:
return n
else:
return n*recur_factorial(n-1)
num=int(input("Enter Number to find Factorial:"))
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
print("The factorial of", num, "is", recur_factorial(num))
output:
Enter Number to find Factorial:5
The factorial of 5 is 120
4. Write a python program to find the best of two test average marks out of three test’s marks
accepted from the user.
m1 = int (input("Enter the marks in the first test: "))
m2 = int (input("Enter the marks in second test: "))
m3 = int (input("Enter the marks in third test: "))
if (m1 > m2):
if (m2 > m3):
total = m1 + m2
else:
total = m1 + m3
elif (m1 > m3):
total = m1 + m2
else:
total = m2 + m3
Avg = total / 2
print ("The average of the best two test marks is: ",Avg)
output:
Enter the marks in the first test: 20
Enter the marks in second test: 24
Enter the marks in third test: 16
The average of the best two test marks is: 22.0
2|Page
5. Write a Python program to find the string similarity between two given strings.
import difflib
def string_similarity(str1, str2):
result = difflib.SequenceMatcher(a=str1.lower(), b=str2.lower())
return result.ratio()
str1=str(input("Enter First String:"))
str2=str(input("Enter Second String:"))
print("Similarity between two said strings:")
print(string_similarity(str1, str2))
Output:
Enter First String:Good Morning
Enter Second String:Good Mornings
Similarity between two said strings:
0.96
6. Write a python program to implement insertion sort and merge sort using lists.
a) Insertion Sort
def insertion_sort(list1):
# Outer loop to traverse through 1 to len(list1)
for i in range(1, len(list1)):
value = list1[i]
# Move elements of list1[0..i-1], that are
# greater than value, to one position ahead
# of their current position
j=i-1
while j >= 0 and value < list1[j]:
list1[j + 1] = list1[j]
j -= 1
list1[j + 1] = value
return list1
list1 = [10, 5, 13, 8, 2]
print("The unsorted list is:", list1)
print("The sorted list1 is:", insertion_sort(list1))
output:
The unsorted list is: [10, 5, 13, 8, 2]
The sorted list1 is: [2, 5, 8, 10, 13]
b) Merge Sort:
def mergeSort(list1):
if len(list1)>1:
mid = len(list1)//2
lefthalf = list1[:mid]
righthalf = list1[mid:]
mergeSort(lefthalf)
mergeSort(righthalf)
3|Page
i=j=k=0
while i < len(lefthalf) and j < len(righthalf):
if lefthalf[i] < righthalf[j]:
list1[k]=lefthalf[i]
i=i+1
else:
list1[k]=righthalf[j]
j=j+1
k=k+1
while i < len(lefthalf):
list1[k]=lefthalf[i]
i=i+1
k=k+1
while j < len(righthalf):
list1[k]=righthalf[j]
j=j+1
k=k+1
list1 = [10, 5, 13, 8, 2]
print("Unsorted list=")
print(list1)
mergeSort(list1)
print("sorted list=")
print(list1)
output:
Unsorted list=
[10, 5, 13, 8, 2]
sorted list=
[2, 5, 8, 10, 13]
7. 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.
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 : 1221
Palindrome
1 appears 2 times
2 appears 2 times
4|Page
8. Write a program to get the binary form of a given number.
def DecimalToBinary(num):
if num >= 1:
DecimalToBinary(num // 2)
print(num % 2, end = '')
n=int(input("Enter Decimal number:"))
print("Binary of Given number is:")
DecimalToBinary(n)
Output:
Enter Decimal number:11
Binary of Given number is:
01011
9. Write a Python program that accepts a sentence and find the number of words, digits, uppercase
letters and lowercase letters.
s = input("Enter a sentence: ")
w, d, u, l = 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
print ("No of Words: ", w)
print ("No of Digits: ", d)
print ("No of Uppercase leWers: ", u)
print ("No of Lowercase leWers: ", l)
Output:
Enter a sentence: Hello 123 Good Morning
No of Words: 4
No of Digits: 3
No of Uppercase leWers: 3
No of Lowercase leWers: 13
5|Page
10.Write Python programs to print the following Patterns:
1)
n=5
alph = 65
for i in range(0, n):
print(" " * (n-i), end=" ")
for j in range(0, i+1):
print(chr(alph), end=" ")
alph += 1
alph = 65
print()
output:
A
AB
ABC
ABCD
ABCDE
6|Page
2)
rows = 5
i = rows
while i >= 1:
j = rows
while j > i:
# display space
print(' ', end=' ')
j -= 1
k=1
while k <= i:
print('*', end=' ')
k += 1
print()
i -= 1
output:
*****
****
***
**
*
3)
n=5
k=69
for i in range(n):
# print spaces
for j in range(i):
print(' ', end='')
# print alphabet
for j in range(2*(n-i)-1):
print(chr(k), end='')
k=k-1
print()
Output:
EEEEEEEEE
DDDDDDD
CCCCC
BBB
A
7|Page