Computer Science Practical Class 12
Computer Science Practical Class 12
COMPUTER
Practical
SCIENCEFILE
SUBMITTED TO: SUBMITTED BY:
MR AJAY TOMAR Rajdeep Mishra
PGT (Computer Class12 A
Science)
Certificate
Rajdeep Mishra
XII-A
Index
S.No. Programs Remark
1 Program 1:Write a Python Program to
add two numbers inputted by the user.
x=5
y = 10
x, y = y, x
print("x =", x)
print("y =", y)
Output:
Program 5 : Python program to check if year
is a leap year or not.
def factorial(N):
if (N == 1):
return 1
# Recursive Call
return N * factorial(N - 1)
# Function to find the sum of
# the series 1! - 2! + 3! - 4!
# + 5!... till the Nth term
def SeriesSum(N):
# Stores Required Sum
Sum = 0
# Loop to calculate factorial
# and sum them with their
# respective sign
for i in range(1, N + 1):
# Factorial of cur integer
fact = factorial(i);
# Stores the sign
sign = fact;
# If position is even sign
# must be negative
if (i % 2 == 0):
sign = sign * -1
# Adding value in sum
Sum += sign
# Return Answer
return Sum
# Driver Code
N = int(input(“Enter a number:”))
print(SeriesSum(N))
Output:
Program 14: Write a python program to find
sum of series.
1/1 + ½ + 1/3 …… 1/n
def sum(n):
i=1
s = 0.0
for i in range(1, n+1):
s = s + 1/i;
return s;
# Driver Code
n = int(input(“Enter a number:”
print("Sum is", round(sum(n), 6))
Output:
Program 15: Write a program to print the
following pattern .
1
22
333
for i in range(number):
print(" ")
Output:
Program 16: Write a program to make the
following sequence.
12
123
for i in range(1,n+1):
print(j, end="")
print()
Output:
Program 17: Write a python program to print
the following sequence .
11
111
….
def pypart(n):
# n in this case
print("1 ",end="")
print("\r")
pypart(n)
Output:
Program 18: Write a program to print the
following sequence.
1111
222
33
4…
b=0
b += 1
print('\r')
Output:
Program 19: Write a program to print
following pattern.
4444
333
22
1…
num = i
print("\r")
Output:
Program 20: Write a program to print
following pattern.
21
321…
print("")
Output:
Program 21: Write a program to print
following sequence.
**
…..
def pypart(n):
# n in this case
print("* ",end="")
print("\r")
pypart(n)
Output:
Program 22: Write a program to print full
pyramid using *.
k=0
print(end=" ")
while k!=(2*i-1):
k += 1
k=0
print()
Output:
Program 23: Write a python program of
connected inverted pyramid pattern of
numbers.
for l in range(i):
print('\n')
Output:
Program 24: Write a program to print even
number pyramid pattern.
LastEvenNumber = 2 * rows
evenNumber = LastEvenNumber
evenNumber = LastEvenNumber
for j in range(i):
evenNumber -= 2
print("\r")
Output:
Program 25: Write a program to print pattern
of horizontal tables.
print()
Output:
Program 26: Write a program to print
pyramid pattern of alternate numbers.
i=1
j=1
while j <= i:
j=j+1
i=i+1
print()
Output:
Program 27: Write a program to convert
lowercase string to uppercase and vice versa.
print(name.swapcase())
Output:
Program 28: Write a program to count
alphabets , special symbols and numbers in
given string.
for i in range(len(string)):
if(string[i].isalpha()):
alphabets = alphabets + 1
elif(string[i].isdigit()):
digits = digits + 1
else:
special = special + 1
matOne = []
for i in range(3):
matOne.append([])
for j in range(3):
num = int(input())
matOne[i].append(num)
matTwo = []
for i in range(3):
matTwo.append([])
for j in range(3):
num = int(input())
matTwo[i].append(num)
matThree = []
for i in range(3):
matThree.append([])
for j in range(3):
matThree[i].append(matOne[i][j]+matTwo[i]
[j])
for i in range(3):
for j in range(3):
print()
Output:
Program 30: Write a program to transpose a
matrix.
X = [[12,7],
[4 ,5],
[3 ,8]]
result = [[0,0,0],
[0,0,0]]
for i in range(len(X)):
for j in range(len(X[0])):
result[j][i] = X[i][j]
for r in result:
print(r)
Output:
Program 31: Write a python program to use
all basic stack functions( push , pop , quit).
class Stack:
def __init__(self):
self.items = []
def is_empty(self):
return self.items == []
self.items.append(data)
def pop(self):
return self.items.pop()
s = Stack()
while True:
print('push <value>')
print('pop')
print('quit')
operation = do[0].strip().lower()
if operation == 'push':
s.push(int(do[1]))
if s.is_empty():
print('Stack is empty.')
else:
break
Output:
Program 32: Write a program to count and
display vowles in a string.
print(len(final))
print(final)
# Driver Code
vowels = "AaEeIiOoUu"
Check_Vow(string, vowels);
Output:
Program 33: Write a program to sort list of
numbers using bubble sort.
def bubbleSort(arr):
n = len(arr)
swapped = False
for i in range(n-1):
swapped = True
if not swapped:
return
bubbleSort(arr)
def insertionSort(arr):
if n <= 1:
j = i-1
j -= 1
insertionSort(arr)
print(arr)
Output :
Program 35: Write a program to copy
contents of first file to other.
secondfile.write(line)
Output:
Program 36: Write a program to count
number of frequencies of a word in a text file
and display it.
word = "contents"
count = 0
for line in f:
words = line.split()
for i in words:
if(i==word):
count=count+1
def counting(filename):
vowel = 0
line = 0
character = 0
if alpha in vowels_list:
vowel += 1
character += 1
# Checking if the current character
line += 1
counting('F1.txt')
Output:
Program 38: Write a program to read contents
from a text file and show it line wise on
screen.
def read_file():
file = open("F2.txt","r")
print(line, end="")
file.close()
read_file()
Output:
Program 39: Aditi has used a text editing
software to type some text. After saving the
article as WORDS.TXT, she realised that she
has wrongly typed alphabet J in place of
alphabet I everywhere in the article.
Write a function definition for JTOI() in Python
that would display the corrected version of
entire content of the file WORDS.TXT with all
the alphabets "J" to be displayed as an
alphabet "I" on screen.
Note: Assuming that WORD.TXT does not
contain any J alphabet otherwise.
def JTOI():
file = open("words.txt","r")
data = file.read()
if letter == 'J':
print("I",end="")
else:
print(letter,end="")
file.close()
JTOI()
Output:
Program 40: Write a program to count
number of times A and M appear in file F2.txt.
def AMcount():
file = open('F2.txt','r')
data = file.read()
counta=0
countm=0
for letter in data:
counta += 1
countm += 1
file.close()
AMcount()
Output:
Program 41: Write a function in python to
read lines from file “POEM.txt” and display all
those words, which has two characters in it.
def TwoCharWord():
f = open('poem.txt')
count = 0
for line in f:
words = line.split()
for w in words:
if len(w)==2:
print(w,end=' ')
f.close()
Output:
Program 42: Write a function that counts and
display the number of 5 letter words in a text
file “Sample.txt”.
def count_words( ):
c=0
f = open("Sample.txt")
line = f.read()
word = line.split()
for w in word:
if len(w) == 5:
c += 1
print(c)
f.close()
Output:
Program 43: Write a function to display those
lines which start with the letter “G” from the
text file “MyNotes.txt”.
def count_lines( ):
c=0
f = open("MyNotes.txt")
line = f.readlines()
for w in line:
if w[0] == 'G':
print(w)
f.close()
Output :
Program 44: Write a user defined function
countwords() to display the total number of
words present in the file from a text file
“Quotes.Txt”.
def countwords():
s = open("Quotes.txt","r")
f = s.read()
z = f.split ()
count = 0
for i in z:
count = count + 1
f=open("abc.txt","r")
d=f.readlines()
count=len(d)
print(count)
f.close()
Output:
SQL Ques 1:
Display Output :
Ans:
i)
ii)
SQL Ques 2:
Write queries to :
Answers :
i) SELECT CNO , CNAME, TRAVELDATE FROM
TRAVEL ORDER BY CNO DESC;
SQL Ques 3:
Write queries to:
Answers :
ORDER BY NO;
Write queries to :
i) To display those company name which are
having prize less than 30000.
ii) To display the name of the companies in
reverse alphabetical order.
Answers :
i) SELECT NAME FROM COMPANY WHERE
COMPANY.CID=CUSTOMER. CID AND
PRICE < 30000;
ii) SELECT NAME FROM COMPANY ORDER BY
NAME DESC;
SQL Ques 9: