0% found this document useful (0 votes)
159 views

Class - XII: Computer Science Practical File

The document provides instructions for a Computer Science practical file for Class 12. It includes 16 programs to be written in Python with descriptions and expected output for each. Students are instructed to copy the content as provided, write the output neatly, and cover their file with paper before submission. An index with program names and space for teacher signatures is also included.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
159 views

Class - XII: Computer Science Practical File

The document provides instructions for a Computer Science practical file for Class 12. It includes 16 programs to be written in Python with descriptions and expected output for each. Students are instructed to copy the content as provided, write the output neatly, and cover their file with paper before submission. An index with program names and space for teacher signatures is also included.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

(2023-24)

COMPUTER SCIENCE PRACTICAL FILE

Class - XII
Name : _________________________________
Section : ________________________________
Roll No. : ________________________________

Instructions to Follow

1. All content must be copied as it in sequence, there should not be any type
Of cutting.
2 Output must be done neatly on blank page.
3. Use only Blue pen. You can use Black pen only for Output.
4. Cover your files with white paper before the submission.
INDEX
TEACHER SIGN.
S. No. PROGRAM NAME DATE
Write a Python Program to Check Prime Number.
1.
Write a Python Program to Print the Fibonacci
2.
Sequence
Write a Python Program to Find ASCII value of a
3.
Character
4. Write a Python program to print the sum of all
Clements in an array

Write a program reverse a string in Python


5.
Write a program Python Program to check if a
6.
Number is Positive, Negative or Zero
Write a program Python Program to Find LCM
7.
Write a program How to append element in the list
8.
9. Write a Python program How to Calculate the Area
of the Circle using Python
Write a Python Program to Make a Simple
10.
Calculator
11. Write a Python Program to Read a text file line by
line and display
12. Write a Python Program How to Remove an
Element from a List in Python
Write a program to remove all the lines that contain
13.
the character 'a' in this file and write other lines
into another file.

Python program to create a binary file with roll


14.
number, name and marks. Input a roll number and
update the marks
15. Write a python program to Count and display the number
of vowels consonants, uppercase, lowercase characters in
string

16. Program to connect with database and store record


of employee and display records.
17. Program to connect with database and search
employee number in table employee and display
record, if empno not found display appropriate
message.
18. Program to connect with database and delete the
record of entered employee number.

19. Program to connect with database and store record


of employee and display records.

20.
Create a table ‘Coach’ as per schema given below
21. Insert and display following data in Coach Table
(made in practical 1) using SQL statements
22. List all the coaches with their date of appointment
in descending order using SQL statements. (Refer
coach table of practical 2)
Write a query to display report showing coach
23.
name, Pay, age and bonus (15% of pay) for all
coaches (Refer coach table
Of practical 2)

Write a query to delete all record from table coach


24.
(Refer coach table of practical 2)

Write SQL command to display name of weekday of


25.
Your date of birth.
Write SQL command to count number of characters
in your school

For the given table ‘Hospital’ write SQL command


26.
to display name all patient admitted in month of
May.
Program 1. Write a Python Program to Check Prime Number.

1. # A default function for Prime checking conditions


2. def PrimeChecker(a):
3. # Checking that given number is more than 1
4. if a > 1:
5. # Iterating over the given number with for loop
6. for j in range(2, int(a/2) + 1):
7. # If the given number is divisible or not
8. if (a % j) == 0:
9. print(a, "is not a prime number")
10. break
11. # Else it is a prime number
12. else:
13. print(a, "is a prime number")
14. # If the given number is 1
15. else:
16. print(a, "is not a prime number")
17. # Taking an input number from the user
18. a = int(input("Enter an input number:"))
19. # Printing result
20. PrimeChecker(a)

Output:

Enter an input number:17


17 is a prime number
Program 2. Write a Python Program to Print the Fibonacci sequence

1. n_terms = int(input ("How many terms the user wants to print? "))
2.
3. # First two terms
4. n_1 = 0
5. n_2 = 1
6. count = 0
7.
8. # Now, we will check if the number of terms is valid or not
9. if n_terms <= 0:
10. print ("Please enter a positive integer, the given number is not valid")
11. # if there is only one term, it will return n_1
12. elif n_terms == 1:
13. print ("The Fibonacci sequence of the numbers up to", n_terms, ": ")
14. print(n_1)
15. # Then we will generate Fibonacci sequence of number
16. else:
17. print ("The fibonacci sequence of the numbers is:")
18. while count < n_terms:
19. print(n_1)
20. nth = n_1 + n_2
21. # At last, we will update values
22. n_1 = n_2
23. n_2 = nth
24. count += 1

Output:
How many terms the user wants to print? 13
The Fibonacci sequence of the numbers is:
0
1
1
2
3
5
8
13
21
34
55
89
144
Program 3. Write a Python Program to Find ASCII value of a character

1. print ("Please enter the String: ", end = "")


2. string = input()
3. string_length = len(string)
4. for K in string:
5. ASCII = ord(K)
6. print (K, "\t", ASCII)

Output:
Please enter the String:
"JavaTpoint#
" 34
J 74
a 97
v 118
a 97
T 84
p 112
o 111
i 105
n 110
t 116
# 35

Program 4. Write a Python program to print the sum of all elements in


An array
1. #Initialize array
2. arr = [1, 2, 3, 4, 5];
3. sum = 0;
4.
5. #Loop through the array to calculate sum of elements
6. for i in range(0, len(arr)):
7. sum = sum + arr[i];
8.
9. print("Sum of all the elements of an array: " + str(sum));

Output:
Sum of all the elements of an array: 15
Program 5. Write a program reverse a string in Python

1. # Reverse string
2. # Using a while loop
3.
4. str = "JavaTpoint" # string variable
5. print ("The original string is : ",str)
6. reverse_String = "" # Empty String
7. count = len(str) # Find length of a string and save in count variable
8. while count > 0:
9. reverse_String += str[ count - 1 ] # save the value of str[count-1] in reverseString
10. count = count - 1 # decrement index
11. print ("The reversed string using a while loop is : ",reverse_String)# reversed string

Output:
('The original string is : ', 'JavaTpoint')
('The reversed string using a while loop is : ', 'tniopTavaJ')

Program 6. Write a program Python Program to check if a Number is Positive,


Negative or Zero
1. # Default function to run if else condition
2. def NumberCheck(a):
3. # Checking if the number is positive
4. if a > 0:
5. print("Number given by you is Positive")
6. # Checking if the number is negative
7. elif a < 0:
8. print("Number given by you is Negative")
9. # Else the number is zero
10. else:
11. print("Number given by you is zero")
12. # Taking number from user
13. a = float(input("Enter a number as input value: "))
14. # Printing result
15. NumberCheck(a)

Output:
Enter a number as input value: -6
Number given by you is Negative
Program 7. Write a program Python Program to Find LCM

1. # defining a function to calculate LCM


2. def calculate_lcm(x, y):
3. # selecting the greater number
4. if x > y:
5. greater = x
6. else:
7. greater = y
8. while(True):
9. if((greater % x == 0) and (greater % y == 0)):
10. lcm = greater
11. break
12. greater += 1
13. return lcm
14.
15. # taking input from users
16. num1 = int(input("Enter first number: "))
17. num2 = int(input("Enter second number: "))
18. # printing the result for the users
19. print("The L.C.M. of", num1,"and", num2,"is", calculate_lcm(num1, num2))

Output:
Enter first number: 3
Enter second number: 4
The L.C.M. of 3 and 4 is 12
Program 8. Write a program How to append element in the list

1. names = ["Joseph", "Peter", "Cook", "Tim"]

2. print('Current names List is:', names)

3. new_name = input("Please enter a name:\n")


4. names.append(new_name) # Using the append() function
5. print('Updated name List is:', names)

Output:
Current names List is: ['Joseph', 'Peter', 'Cook', 'Tim']
Please enter a name:
Devansh
Updated name List is: ['Joseph', 'Peter', 'Cook', 'Tim',
'Devansh']

Program 9. Write a Python program How to Calculate the Area of the Circle using
Python

1. import math as M
2. Radius = float (input ("Please enter the radius of the given circle: "))
3. area_of_the_circle = M.pi* Radius * Radius
4. print (" The area of the given circle is: ", area_of_the_circle)

Output:

Please enter the radius of the given circle: 3


The area of the given circle is: 28.274333882308138
Program 10. Write a Python Program to Make a Simple Calculator.

1. def add(P, Q):


2. # This function is used for adding two numbers
3. return P + Q
4. def subtract(P, Q):
5. # This function is used for subtracting two numbers
6. return P - Q
7. def multiply(P, Q):
8. # This function is used for multiplying two numbers
9. return P * Q
10. def divide(P, Q):
11. # This function is used for dividing two numbers
12. return P / Q
13. # Now we will take inputs from the user
14. print ("Please select the operation.")
15. print ("a. Add")
16. print ("b. Subtract")
17. print ("c. Multiply")
18. print ("d. Divide")
19.
20. choice = input("Please enter choice (a/ b/ c/ d): ")
21.
22. num_1 = int (input ("Please enter the first number: "))
23. num_2 = int (input ("Please enter the second number: "))
24.
25. if choice == 'a':
26. print (num_1, " + ", num_2, " = ", add(num_1, num_2))
27.
28. elif choice == 'b':
29. print (num_1, " - ", num_2, " = ", subtract(num_1, num_2))
30.
31. elif choice == 'c':
32. print (num1, " * ", num2, " = ", multiply(num1, num2))
33. elif choice == 'd':
34. print (num_1, " / ", num_2, " = ", divide(num_1, num_2))
35. else:
36. print ("This is an invalid input")
Output:
Please select the operation.
a. Add
b. Subtract
c. Multiply
d. Divide
Please enter choice (a/ b/ c/ d): b
Please enter the first number: 12
Please enter the second number: 11
12 - 11 = 1

Program 11. Write a Python Program to Read a text file line by line and display

each word separated by a '#' in Python


filein = open("mydoc.txt",'r')
line =" "
while line:
line = filein.readline()
#print(line)
for w in line:
if w == ' ':
print('#',end = '')
else:
print(w,end = '')
filein.close()

#Prepared by Latest Tech Updates--

filein = open("Mydoc.txt",'r')
for line in filein:
word= line .split()
for w in word:
print(w + '#',end ='')
print()
filein.close()
Sample Output
Program 12. Write a Python Program How to Remove an Element from a List in
Python

1. # Python program to show how to use the pop() function


2.
3. lst = ["Python", "Remove", "Elements", "List", "Tutorial"]
4. print("Initial List is :", lst)
5.
6. # using pop() function to remove element at index 2 of the list
7. element = lst.pop(2)
8. print("Element that is popped :", element)
9. print("List after deleting the element", lst)

output

Initial List is : ['Python', 'Remove', 'Elements', 'List',


'Tutorial']
Element that is popped : Elements
List after deleting the element ['Python', 'Remove', 'List',
'Tutorial']

Program 13 Write a program to remove all the lines that contain the character 'a'
in this file and write other lines into another file.

myfile = open("Answer.txt", "r")


newfile = open("nswer.txt", "w")
line = ""
while line:
line = myfile.readline()
if 'a' not in line:
newfile.write(line)

myfile.close()
newfile.close()
print("Newly created file contains")
print("------------")
newfile = open("nswer.txt","r")
line = ""
while line:
line = newfile.readline()
print(line)
newfile.close()

output

Newly created file contains


------------
Program 14. Python program to create a binary file with roll number, name and
marks. Input a roll number and update the marks

import pickle
#creating the file and writing the data
f=open("records.dat", "wb")
#list index 0 = roll number
#list index 1 = name
#list index 2 = marks
pickle.dump([1, "Wakil", 90], f)
pickle.dump([2, "Tanish", 80], f)
pickle.dump([3, "Priyashi", 90], f)
pickle.dump([4, "Kanupriya", 80], f)
pickle.dump([5, "Ashutosh", 85], f)
f.close()

#opeining the file to read contents


f=open("records.dat", "rb")
roll=int(input("Enter the Roll Number: "))
marks=float(input("Enter the updated marks: "))
List = [ ] #empty list
flag = False #turns to True if record is found
while True:
try:
record=pickle.load(f)
List.append(record)
except EOFError:
break
f.close()

#reopening the file to update the marks


f=open("records.dat", "wb")
for rec in List:
if rec[0]==roll:
rec[2] = marks
pickle.dump(rec, f)
print("Record updated successfully")
flag = True
else:
pickle.dump(rec,f)
f.close()
if flag==False:
print("This roll number does not exist")
Output

Enter the Roll Number: 3


Enter the updated marks: 95
Record updated successfully
Program 15. Write a python program to Count and display the number of vowels
consonants, uppercase, lowercase characters in string

s = input("Enter any string :")


vowel = consonent = uppercase = lowercase= 0
for i in s:
if(i == 'a' or i == 'e' or i == 'i' or i == 'o' or i == 'u'or i == 'A' or i
== 'E' or i == 'I' or i == 'O' or i == 'U'):
vowel = vowel +1
else:
consonent = consonent + 1
if i.isupper() :
uppercase = uppercase + 1

if i.islower():
lowercase = lowercase + 1

print("Total number of vowel:",vowel)


print("Total number of consonent:",consonent)
print("Total number of uppercase letter:",uppercase)
print("Total number of lowercase letter:",lowercase)

Output:
>>> %Run 'count vowelconsonet.py'
Enter any string :python
Total number of vowel: 1
Total number of consonent: 5
Total number of uppercase letter: 0
Total number of lowercase letter: 6

>>> %Run 'count vowelconsonet.py'


Enter any string :Sumedh
Total number of vowel: 2
Total number of consonent: 4
Total number of uppercase letter: 1
Total number of lowercase letter: 5
Program 16 Program to connect with database and store record of employee and
display records.
Program 17 Program to connect with database and search employee number in
table employee and display record, if empno not found display appropriate
message.
Program 18 Program to connect with database and delete the record of entered
employee number.
Program 19. Program to connect with database and store record of employee and
display records.
Program 20.

Create a table ‘Coach’ as per schema given below:

Coach
Field name Data type Attributes
Coachid Int Primary key
Coachname Varchar(20) Not Null
Age Int Default value 30
Sport Varchar(20) Not null
Dateofapp Date
Pay Float
Gender Char(1) Not null

Solution:

SQL Statement:

Create table coach

Coachid int primary key,

Coachnamevarchar(20) not null,

Age int default 30,

Sport varchar(20) not null,

Dateofapp date,

Pay float,

Gender char(1) not null);


Program 21.

Insert and display following data in Coach Table (made in practical 1)

using SQL statements.

Coachid Coachname Age Sport Dateofapp Pay Gender


1 Karan 35 Karate 27/03/19 10000 M
2 Ravina 34 Karate 20/01/20 12000 F
3 Kamal 34 Squash 19/02/20 20000 M
4 Tarun 33 Basketball 01/01/20 15000 M
5 Sumeru 36 Swimming 12/01/20 7500 M
6 Anjani 36 Swimming 24/02/20 8000 F
7 Shamima 37 Squash 20/02/20 22000 F
8 Soumya 30 Karate 22/02/20 11000 F
Program 22. List all the coaches with their date of appointment in descending
order

using SQL statements. (Refer coach table of practical 2)

Solution:

SQL Statement:

Select * From Coach Order By dateofapp DESC;


Program 23. Write a query to display report showing coachname,

Pay, age and bonus (15% of pay) for all coaches (Refer coach table

Of practical 2)

Solution:

SQL Statement:

Select coachname, pay,age,pay*15/100 as ‘bonus’ from coach;

Program 24. Write a query to delete all record from table coach

(Refer coach table of practical 2)

Solution:

SQL Statement:

Delete from coach;


Program 25. Write SQL command to display name of weekday of

Your date of birth.

Write SQL command to count number of characters in your school

Name.
Solution:

SQL Statement:

Select dayname(‘2019/06/22’);

Select length(‘techtipnow computers’);


Program 26. For the given table ‘Hospital’ write SQL command to display

name all patient admitted in month of May.

PID DEPT PNAME GENDER ADMITDATE FEES


AP/PT/001 Rahil Khan M 21/04/2019 ENT 250
AP/PT/002 Jitendal Pal M 12/05/2019 Cardio 400
AP/PT/003 SumanLakra F 19/05/2019 Cardio 400
AP/PT/004 Chandumal M 24/06/2019 Neuro 600
Jain
Solution:

SQL Statement:

Select * from hospital where MONTHNAME(admitdate) = ‘May’;

You might also like