Compuooter
Compuooter
BAHRAIN
AISSCE (2023-24)
CBSE (2023-2024)
BONAFIDE CERTIFICATE
This is to certify that the record work done by
Teacher’s signature
BAHRAIN
PYTHON
PROGRAMMING
Program 1
Write a program that reads a string from the keyboard and displays:
*The number of uppercase letters in the string
*The number of lowercase letters in the string
*The number of digits in the string
*The number of whitespace characters in the string
Source Code
str1 = input("Enter the string: ")
lower = upper = digit = space = 0
for ch in str1:
if ch.isupper():
upper += 1
elif ch.islower():
lower += 1
elif ch.isdigit():
digit += 1
elif ch == ' ':
space += 1
print('The number of uppercase letters:', upper)
print('The number of lowercase letters:', lower)
print('The number of digits:', digit)
print('The number of whitespace characters:', space)
Output
Program 2
Write a Python program that accepts a string from user. Your program should create a new string by
shifting one position to left. For example, if the user enters the string “examination 2023” then new
string would be “xamination 2023e”
Source Code
Output
Program 3
Write a program in python that accepts a string to setup a password.
Your entered password must meet the following requirements:
● The password must be at least eight characters long.
● It must contain at least one uppercase letter.
● It must contain at least one lowercase letter.
● It must contain at least one numeric digit.
● Your program should perform this validation
Source
Output
Program 4
Write a program that rotates the element of a list so that the element at the first index moves to the
second index, the element in the second index moves to the third index, etc., and the element in the last
index moves to the first index.
Source Code
list1=[]
size=int(input('How many elements you want to enter? '))
for i in range(size):
data = input()
list1.append(data)
temp = list1[size-1]
for i in range(size-1,0,-1):
list1[i] = list1[i-1]
list1[0] = temp
print('list after shifting', list1)
Output
Program 5
Write a program that accepts a string and change its lowercase vowels to
special character as given below:
a#
e@
i$
o%
u!
Source Code
Output
Program 6
Write a program that keeps name and birthday in a dictionary as key-value pairs. The program should
display a menu that lets the user search for a person’s birthday, add a new name and birthday, change an
existing birthday, and delete an existing name and birthday.
Source Code
birthday={}
num= int(input("Enter the number of birthdays "))
for i in range(num):
name=input("Enter name")
date=input("Enter birthday")
birthday[name]=date
print("This is your birthday dictionary:",birthday)
search=input("Whose birthday did you forget? Enter your name to view birthday.")
if search in birthday:
print("{}'s birthday is on {}.".format(search,birthday[search]))
else:
print("Person cannot be found. Please add in dictionary.")
nameadd= input("Enter new name.")
dateadd= input("Enter new date.")
birthday[nameadd]=dateadd
print("This is your birthday dictionary:",birthday)
namechange= input("Enter name of the person with the wrong birthday.")
datechange= input("Enter the correct date.")
birthday[namechange]=datechange
print("This is your birthday dictionary:",birthday)
delete=input("No longer friends? Whose birthday do you want to delete?")
del birthday[delete]print("This is your birthday dictionary:",birthday)
Output
Program 7
Program 7
Write the definition of a function Alter(A, N) in python, which should change all the multiples of 5 in the
list to 5 and rest of the elements as 0.
Source Code
Output
def Alter(A,N):
for i in range(N):
if(A[i]%5==0):
A[i]=5
else:
A[i]=0
print("New list", A)
count=0
A=[]
elements=int(input('Enter number of elements:'))
while count<elements:
element=int(input('Enter element:'))
A.append(element)
count+=1
print('original list:',A)
N=len(A)
Alter(A,N)
Output
Program 8
Write a code in python for a function void Convert ( T, N) , which repositions all the elements of array by
shifting each of them to next position and shifting last element to first position.
Source code
def Convert (T, N):
for i in range(N):
t=T[N-1]
T[N-1]=T[i]
T[i]=t
print("LIst after conversion", T)
T=[1,2,3,4,5,6,7,8,9,0]
N=len(T)
Convert(T,N)
Output
Program 9
Create a function showEmployee() in such a way that it should accept employee name, and it’s salary
and display both, and if the salary Is missing in function call it should show it as 9000
Source Code
def showEmployee(name,salary=9000):
print("employee name",name)
print("salary of employee",salary)
n=input("enter employee name")
s=eval(input("enter employee's salary"))
showEmployee(n,s)
Output
Program 10
Write a program using a function which accept two integers as an argument and return its sum. Call this
function and print the results in main()
Source code
def main(a,b)
return a+b
Output
Program 11
Write a program to find sum of digits of a given number using function Digitsum( ) that takes a number
and returns its digit sum.
Source Code
def Digitsum(n):
sum = 0
for i in str(n)
sum = sum + int(i)
return sum
int1=int(input("Enter the number:"))
print(Digitsum(int1))
Output
Program 12
Write a program to create a function Div7and10( ) that takes a 10 elements numeric tuple and return the
sum of elements which are divisible by 7 and 10.
Source Code
def Div7and10():
x=[]
for i in range(10):
num=int(input("Enter the number:"))
append(num)
num1=0
for i in x:
if i%7==0 and i%10==0:
num1+=i
else:
continue
print(num1)
Div7and10()
Output
Program 13
Program 13
Write a function, is vowel that returns the value true if a given character is a vowel, and otherwise
returns false. Write another function main, in main() function accept a string from user and count
number of
vowels in that string.
Source code
def is_vowel(letter):
if letter in 'aeiouAEIOU':
return True
else:
return False
def main():
count = 0
string = input('Enter a text: ')
for ch in string:
if(is_vowel(ch)):
count += 1
Output
Program 14
A text file named “matter.txt” contains some text, which needs to be displayed such that every next
character is separated by a symbol “#”. Write a function definition for hash display () in Python that
would
display the entire content of the file matter.txt in the desired format.
Example :If the file matter.txt has the following content stored in it :THE WORLD IS ROUND The function
hash_display() should display the following content :T#H#E# #W#O#R#L#D# #I#S# #R#O#U#N#D#
def hash_display():
file=open("matter.txt","r")
str1= file.read()
print("Original text:",str1)
print("Edited text:")
for i in str1:
print(i,end="#")
file.close()
hash_display()
Output