Professional Documents
Culture Documents
CS Practical
CS Practical
COMPUTER SCIENCE
7. Write a program to input your name and print each character of your
name with a single space in one line using a while loop.
8. Write a program to check whether the word ‘CBSE’ comes after or before
‘BOARD EXAM’ in a string.
9. Write a program to enter your name and print it the same amount of
times as the number of characters it has excluding spaces.
10. Write a program to enter a string and display it without vowel characters.
11. Write a program to enter a string and then display it in reverse order.
13. Write a program to enter a string and replace all punctuation characters
with spaces.
15. Write a programme to perform linear search in the given list of numbers.
Display the indices of the number found and the frequency of its
occurrence. Also display the message “Number not found” ,if the number
does not exist in the list .
16. Write a programme to delete all even numbers from a numeric list.
17. Write a program to find largest and second largest in a given list of
positive integers .Use loops and list functions for the same .
18. Write a program to sort the elements in the list in both ascending and
descending order. The list contains (i) positive integers (ii) Strings . Use
loops and list functions for the same.
19.
20.
21.
22. Create list of tuples containing employee name , age , mobile number of
employee . Data types to be used are string and integer. Take details of
five employees from the user and display the same.
23. Write a program to enter ‘N’ numbers in a tuple Num and find the sum
,maximum and minimum values in the tuple.
24. Write a program that inputs student’s name and total marks scored
together in a tuple. Input details for five students. Display the name of
the student scoring the highest marks.
25. Write a program to enter the PAN Number, Name and annual salary and
calculate tax for all employees working in a company.
26. Create an application to enter a year and print the total number of days in
that year after considering the leap year which has 29 days in February.
When you enter the year, it should be a 4-digit number and no character
entry in the year.The program should store the days and the respective
months in an array called Months. Also, print the days and months in a
tabular form.
27. Write a program to input ‘N’ number of students Name in a tuple and print
the tuple in ascending order of name.
28. Write a program to count the frequency of words in a given line of text
using a dictionary.
29. Write a python code that takes a value and checks whether the given
value is part of a given dictionary or not. If it is, it should print the
corresponding key otherwise print an error message.
30. Create a dictionary whose keys are month names and whose values are
the number of days in the month.
(a) Ask the user to enter a month name and use the dictionary to tell how
many days are in the month.
(b) Print out all the months with 31 days.
(c) Print out all the keys in alphabetical order.
31. Print the name of the student who scored the most marks, given a
dictionary containing the names of students and their marks.
For and While Loops:
5=8
f=1
if 1% 2:
5 += (x**1)/f
else:
= (x**1)/f
f = (2*1)*(2*1+1)
# With loops
c=0
for i in s:
c+=1
print(“The number of characters in the given string is”, c)
Q5. Write a program to count the total number of vowel characters in
an input string.
# With loops
i=0
for c in s:
if c in 'aeiou':
i += 1
print(“The number of vowels in the given string is”, i)
# Taking a string as the input from the user not using .lower() as ‘h’s are not
included
s = input("Enter the string in which the number of ‘H’s are to be found: ")
#With loops
c=0
for i in s:
if i==’H’:
c+=1
print(“The number of ‘H’s in the string is”, c)
Q7. Write a program to input your name and print each character of
your name with a single space in one line using a while loop.
i=0
while i<len(n):
if n[i] != ‘ ‘:
print(n[i], end=” “)
i+=1
Q8. Write a program to check whether the word ‘CBSE’ comes after or
before ‘BOARD EXAM’ in a string.
if fc<fb:
print(“The word ‘CBSE’ appears before ‘BOARD EXAM’ in the string)
elif fc>lb:
print(“The word ‘CBSE’ appears after ‘BOARD EXAM’ in the string)
else:
print(“The word ‘CBSE’ appears after the first occurrence of ‘BOARD
EXAM’ but before its last occurrence”)
Q9. Write a program to enter your name and print it the same amount
of times as the number of characters it has excluding spaces.
# With if statement
for i in n:
if i != “ “:
print(n)
print()
# With slicing
print(s[::-1])
# With loops
c=0
c=0
for i in s:
if c%2=1:
print(i, end=””)
c+=1
# With slicing
print(“The string with alternate characters printed in either ways is: “)
print(s[::2])
print(s[1::2])
Q13. Write a program to enter a string and replace all punctuation
characters with spaces.
import string
L = string.punctuation
for t in s:
print(ord(t), end=” “)
Lists:
Q15):Write a programme to perform linear search in the given list of
numbers.
Display the indices of the number found and the frequency of its
occurrence. Also display the message “Number not found” ,if the
number
does not exist in the list .
nums=[]
indices = []
frequency = 0
if frequency > 0:
print(f"Number {target} found at indices: {indices}")
print(f"Frequency of occurrence: {frequency}")
else:
print("Number not found")
def largest(list):
greatest=0
for r in list:
if r>greatest:
greatest=r
return greatest
list2=[1,2,5,12,13,90,99,99]
x=largest(list2)
print(largest(list2))
y =list2
for r in y:
if r==x:
y.remove(r)
y.remove(x)
print(largest(y))
list4 = [1, 2, 3, 4, 5, 6]
print("Original list:", list4)
if len(list4) % 2 != 0:
print("List length must be even for swapping even and odd positions.")
else:
# Swap elements at even and odd positions
for i in range(0, len(list4), 2):
list4[i], list4[i + 1] = list4[i + 1], list4[i]
list1=[2,4,2,6,3,5,6,2,1,7,32,5,2,5,2]
for r in list1:
if list1.count(r)>1:
list1[list1.index(r)]=0
Q21)W.A.P to display a list in reverse order. Use loops and list functions for
the same
list2.reverse()
print(list2)
#using loops
list3=[]
for r in range(len(list2),0,-1):
list3.append(list2[r-1])
print(list3)
Tuples:
Q22. Create a list of tuples containing employee name , age , mobile
number of employee . Data types to be used are string and integer.
Take details of five employees from the user and display the same.
highest_marks = 0
highest_scorer=""
Q25.
Employees=tuple()
Temp=tuple()
#Inputs
for i in range(20):
PAN=input("Enter PAN Number of employee:")
name=input("Enter your name:")
ann=input("Enter the annual salary:")
Temp=Temp+((PAN, name, ann),)
for t in Temp:
if int(t[2])<=250000:
tax=0
elif int(t[2])<=500000:
tax=int(t[2])*5/100
elif int(t[2])<=1000000:
tax=int(t[2])*20/100
else:
tax=int(t[2])*30/100
if int(t[2])<=5000000:
sur=0
elif int(t[2])<=10000000:
sur=tax*10/100
else:
sur=tax*15/100
cess=tax*4/100
total =tax+sur+cess
Employees=Employees+((int(t[0]),t[1],int(t[2]),tax,cess,sur,total),)
#Output table
print("Pan Number \tName \tAnnual Salary \tTax\tCess\tSurcharge\tTotal
Tax")
for j in Employees:
for z in j:
print(z, end="\t")
Q26.
#Input
year=int(input("Enter the year:"))
if year%4==0:
leap=True
if year%100==0:
leap=False
if year%400==0:
leap=True
Year=((1,'January', 31),
(2,'February',n),(3,'March',31),(4,'April',30),(5,'May','31'),(6,'June',30),(7,'Jul
y',31),(8,'August',31),(9,'September',30),(10,'October',31),(11,'November',
30),(12,'December',31))
#Output Sequence
if leap:
print('The year', year, 'is a leap year')
else:
print('The year', year, 'is not a leap year')
for i in Year:
for j in i:
print(j, end="\t")
print()
dic = {}
for i in L:
if i not in dic:
dic[i] = 0
dic[i] += 1
for k in dic:
print(k, “:”, dic[k])
Q29. Write a python code that takes a value and checks whether the
given value is part of a given dictionary or not. If it is, it should print
the corresponding key otherwise print an error message.
isP = False
L = []
for k in dic:
if dic[k]==val:
L.append(k)
isP = True
if not isP:
print(“Error, given value is not in the dictionary”)
else:
print(“Given value is in the dictionary and corresponds to the key(s): “,
end=””)
for i in L:
print(i, end=” “)
Q30. Create a dictionary whose keys are month names and whose
values are the number of days in the month.
(a) Ask the user to enter a month name and use the dictionary to tell
how many days are in the month.
(b) Print out all the months with 31 days.
(c) Print out all the keys in alphabetical order.
keys = list(months)
keys.sort()
Q31. Print the name of the student who scored the most marks, given a
dictionary containing the names of students and their marks.
mark = -1
L = []
for stu in marks:
if marks[stu]>mark:
mark = marks[stu]
if len(L)==1:
print(L[0], “has gotten the highest marks at”, marks[L[0]])
elif len(L)>1:
print(“The highest marks are”, marks[L[0]], “and the students who got
that are: “, end=””)
for stu in L:
print(stu, end=” “)