Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

BOARD PRACTICAL FILE:

COMPUTER SCIENCE

KOTHARI INTERNATIONAL SCHOOL


(Session 2023-2024)

Name: RUDRR VYNAYAK BHATT


Grade: 11B2
Sr. No. Programs Pg. No. Signature

1. Find result of series: 1!+2!+3!+..................+n!

2. Find result of series: 1!+2!+3!+4!+......+n!

3. Find result of series: x - x²/3! + x³/5! - x⁴/7!..... upto n terms.

4. Write a program to count the total number of characters in an input string.

5. Write a program to count the total number of vowel characters in an input


string.

6. Write a program to check the number of ‘H’ present in a string.

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.

12. Write a program to enter a string and print alternate characters.

13. Write a program to enter a string and replace all punctuation characters
with spaces.

14. Write a program to print the ASCII code of an entered word.

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:

Q1:Find result of series: 1!+2!+3!+..................+n!

n=int(input("Enter limit for series:"))


fact=1
s=0
i=1
#Using while loop
while i<=n:
factorial=factorial*i
sum=sum+factorial
i+=1
print("Sum of series is", sum)

Q2:Find result of series: 1!+2!+3!+4!+......+n! (while loop)

n=int(input("Enter limit for series:"))


sum=0
i=1
#Using while loop
while i<=n:
sum+=i**2
i+=1
print("Sum of series is",sum)
Q3:Find result of series: x - x²/3! + x³/5! - x⁴/7!..... upto n terms.

#Taking 'n as the limit for series and the value of x

x = int(input("Enter the value of the variable in the series: "))

n int(input("Enter the limit for the sum of the series: "))

5=8

f=1

for i in range(1, n+1):

if 1% 2:

5 += (x**1)/f

else:

= (x**1)/f

f = (2*1)*(2*1+1)

print("The sum of the series x-x²/31+x²/51-x^/7!... is", s)


Strings:

Q4. Write a program to count the total number of characters in an


input string.

# Taking a string as the input from the user


s = input(“Enter the string whose characters are to be counted: “)

# With the len() function


print(“The number of characters in the given string is”, len(s))

# 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.

# Taking a string as the input from the user


s = input("Enter the string whose vowels are to be counted: ").lower()

# With the .count() function


print(“The number of vowels in the given string is”, s.count('a') + s.count('e')
+ s.count('i')+ s.count('o')+ s.count('u'))

# 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)

Q6. Write a program to check the number of ‘H’ present in a string.

# 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 the .count() function


print(“The number of ‘H’s in the string is”, s.count(‘H’))

#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.

# Asking the user’s name and taking it as the input


n = input(“Enter your name: “)

print(“The name with spaces between each letter is: “)

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.

# Taking a string as the input from the user


s = input(“Enter a string that has ‘CBSE’ and ‘BOARD EXAM’ as substrings
(Not case sensitive): “).lower()

# Getting indices of the positions of these substrings


fc = s.find(‘cbse’)
fb = s.find(‘board exam’)
lb = s.rfind(‘board exam’)
cc = s.count(‘cbse’)
cb = s.count(‘board exam’)

# Splitting into different cases with differing numbers of the substrings


if cc==0 and cb==0:
print(“Both the words ‘CBSE’ and ‘BOARD EXAM’ do not exist in the
string”)
quit()
elif cc==0:
print(“The word ‘CBSE’ does not exist in the string”)
quit()
elif cb==0:
print(“The word ‘BOARD EXAM’ does not exist in the string”)
quit()

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.

# Asking the user’s name and taking it as the input


n = input(“Enter your name: “)

# With if statement
for i in n:
if i != “ “:
print(n)
print()

# With the len() and .count() functions


for i in range(len(n)-n.count(‘ ‘):
print(n)

Q10. Write a program to enter a string and display it without vowel


characters.

# Taking a string as the input from the user


s = input(“Enter a string: “)

print(“The string without vowels is: “)


for t in s:
if t not in “aeiou”:
print(t, end=””)

Q11. Write a program to enter a string and then display it in reverse


order.

# Taking a string as the input from the user


s = input(“Enter a string: “)

print(“The string in reverse order is: “)


# With loops
for i in range(len(s)-1,-1,-1):
print(s[i], end="")

# With slicing
print(s[::-1])

Q12. Write a program to enter a string and print alternate characters.

# Taking a string as the input from the user


s = input(“Enter a string: “)

# With loops
c=0

print(“The string with alternate characters printed in either ways is: “)


for i in s:
if c%2=0:
print(i, end=””)
c+=1

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

# Taking a string as the input from the user


s = input(“Enter a string: “)

print(“The string with the punctuations replaced by spaces is: “)


for i in s:
if i not in L:
print(i, end="")
else:
print(end=" ")

Q14. Write a program to print the ASCII code of an entered word.

# Taking a string as the input from the user


s = input(“Enter a word: “)

print(“The ASCII code of the given word/string is: “)

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

for i, num in (nums):


if num == target:
indices.append(i)
frequency += 1

if frequency > 0:
print(f"Number {target} found at indices: {indices}")
print(f"Frequency of occurrence: {frequency}")
else:
print("Number not found")

q16)Write a program to find largest and secondlargest in a given list


of positive
integers .Use loops and list functions for the same .

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))

Q17:Write a program to delete all even numbers from a numeric list.

#To remove all even elements from list L1


L1=[1,2,3,4,5,6,7,8]
i=0
#Using for loop
for i in L1:
#Providing condition for even elements
if i%2==0:
L1.remove(i)
print("List with even numbers deleted:",L1)

Q18):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
#Using list functions to sort list L1 in ascending and descending order
L=[]
integers=[x for x in L if isinstance(x,int)]
strings=[x for x in L if isinstance(x,str)]
integers_asc=integers.sort()
integers_desc=integers.sort(reverse=True)
strings_asc=strings.sort()
strings_desc=strings.sort(reverse=True)
L_asc=integers_asc.extend(strings_asc)
L_desc=integers_desc.extend(string_desc)
print(L_asc)
print(L_desc)

Q19) Write a program to swap even position elements with odd


positions.

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]

print("List after swapping", list4)

Q20) W.A.P programme to replace all duplicate elements of a list with


a ‘0’.:

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

#using list function


list2=[1,3,5,2,5,3,5]

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.

# Initialize an empty list to store the employee details


employee_data = []

# Take details of five employees from the user


for i in range(1, 6):
name = input("Enter the name of employee " + str(i) + ": ")
age = int(input("Enter the age of employee " + str(i) + ": "))
mobile_number = int(input("Enter the mobile number of employee " +
str(i) + ": "))

# Store the employee details as a tuple in the employee_data list


employee_data.append((name, age, mobile_number))

# Display the employee details


print("\nEmployee Details:")
for employee in employee_data:
print("Name: {}, Age: {}, Mobile Number: {}".format(employee[0],
employee[1], employee[2]))
Q.23) Write a program to enter ‘N’ numbers in a tuple Num and find
the sum ,maximum and minimum values in the tuple.

# Take the number of elements from the user


n = int(input("Enter the number of elements in the list: "))

# Initialize an empty list to store the numbers


num = []

# Take the numbers from the user


for i in range(n):
num.append(int(input(f"Enter element {i+1}: ")))

# Calculate the sum, maximum, and minimum values in the list


total = sum(num)
maximum = max(num)
minimum = min(num)

# Convert the list to a tuple


Num = tuple(num)

# Display the results


print("\nThe sum of the numbers in the tuple is:", total)
print("The maximum value in the tuple is:", maximum)
print("The minimum value in the tuple is:", minimum)
print("The tuple of numbers is:", Num)

Q24.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.

# Initialize an empty list to store student details


student_details = []
# Take input for details of five students
for i in range(5):
name = input("Enter the name of student:")
marks = int(input("Enter marks:"))
student_details.append((name, marks))

highest_marks = 0
highest_scorer=""

# Find the student with the highest marks


for name,marks in student_details:
if marks>highest_marks:
highest_marks=marks
highest_scorer=name

# Display the name of the student scoring the highest marks


print(f"Student with the highest marks:{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),)

#Evaluating taxes etc.


tax=int()
cess=int()
sur=int()
total=int()

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:"))

#Evaluating year type


leap=False

if year%4==0:
leap=True
if year%100==0:
leap=False
if year%400==0:
leap=True

#Creating your tuple


if leap:
n=29
else:
n=28

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')

print("Calendar year:", year)


print("Month No.\t Month Name\t Days")

for i in Year:
for j in i:
print(j, end="\t")
print()

Q27.Write a program to input ‘N’ number of students Name in a tuple


and print the tuple in ascending order of name .

# Input the value


N = int(input("Enter the number of students : "))

# Initialize an empty list to store student names


student_names = []
# Take input for N student names
for i in range(N):
name = input("Enter the name of student " + str(i + 1) + ": ")
student_names.append(name)

# Convert the list to a tuple


names_tuple = tuple(student_names)
sorted_names = tuple(sorted(names_tuple))

# Display the sorted tuple


print("\nStudent Names in Ascending Order:")
for name in sorted_names:
print(name)
Dictionaries:

Q28. Write a program to count the frequency of words in a given line


of text using a dictionary.

# Taking a string as the input from the user


s = input(“Enter a sentence: “)
L = s.split()

dic = {}

for i in L:
if i not in dic:
dic[i] = 0
dic[i] += 1

print(“The frequency of each word is: “)

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.

# Creating a user defined dictionary and other inputs


dic = {}
while True:
pair = input(“Enter paired keys and values (seperated by ;)(QUIT to
stop): “)
if pair.lower()==”stop” or pair.lower()==”quit”:
break
else:
temp = pair.split(‘;’)
dic[eval(temp[0])] = eval(temp[1])

val = eval(input(“Enter the value to be searched for: “))

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.

# Creating the preset dictionary


months = {"January":31, "February":28, "March":31, "April":30, "May":31,
"June":30, "July":31, "August":31, "September":30, "October":31,
"November":30", "December":31}

# Taking the month name from the user


mon = input(“Enter a month: “).capitalize()

if mon not in months:


print(“Error,”, mon, “is not a month”)
else:
print(“The month of”, mon, “has”, months[mon], “days”)

print(“All the months with 31 days are: “)


for m in months:
if months[m]==31:
print(m)

keys = list(months)
keys.sort()

print(“The months in alphabetical order are: “)


for i in keys:
print(i)

Q31. Print the name of the student who scored the most marks, given a
dictionary containing the names of students and their marks.

# Creating a user defined dictionary


marks = {}
while True:
pair = input(“Enter the student’s name and marks (seperated by
;)(QUIT to stop): “)
if pair.lower()==”stop” or pair.lower()==”quit”:
break
else:
temp = pair.split(‘;’)
marks[eval(temp[0])] = eval(temp[1])

mark = -1
L = []
for stu in marks:
if marks[stu]>mark:
mark = marks[stu]

for stu in marks:


if marks[stu]==mark:
L.append(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=” “)

You might also like