COMPUTER SCIENCE NEW
lab manual
aissce (2023-24)
Submitted to: Submitted by:
MR. RAVENDRA SINGH students name
Class-xii
Roll NO.
Sr.No Name of the Experiment Page.
No
1
10
11
12
13
14
15
16
17
18
19
20
21
Program. 1 #write a program to print cubes of numbers in the range 15 to 20.
for i in range(15, 21):
print("cube of number", i, end="")
print("is", i**3)
Output::
Program. 2 #write a program that multiplies two integer number without using the * operator
using repeated addition.
n1= int(input("enter first number:"))
n2= int(input("enter second number:"))
product= 0
count= n1
while count>0:
count= count-1
product= product+n2
print("the product of", n1, "and", n2, "is", product)
Output::
Program.3 # Program that receives two numbers in a function and returns the results of all
arithmetic operations (+,-,*,%) on these numbers
def calc (x,y):
return x+y, x-y, x*y, x/y, x%y
#_main
num1=int(input("Enter number for x: "))
num2=int(input("Enter number for y: "))
add, sub,mult,div,mod=calc(num1, num2)
print("Sum of given numbers :", add)
print("Substraction of given numbers :", sub)
print("Product of given numbers :", mult)
print("Division of given numbers :", div)
print("Modeelo of given numbers :", mod)
Output::
Program.4 Write a function that takes amount-in-dollars and dollar-to-rupee conversion price; it
then returns the amount converted to rupees. Create the function in both void and non-void
forms.
def void_dollar_to_rupee(dollar):
r=dollar*76.22
print("Void function:$", dollar, "=",r,"Rs")
def non_void_dollar_to_rupee(dollar):
return dollar*76.22
d=float(input("Enter amount to convert:"))
void_dollar_to_rupee(d)
print("Calling non-void function:",d,"=",non_void_dollar_to_rupee(d),"Rs")
Output::
Program.5 Write a function that receives two numbers and generates a random number from
that range. Using function the main should be able to print three numbers randomly.
import random
def random_num(x,y):
return random.randint(x,y)
n1=int(input("Enter the first number:"))
n2=int(input("Enter the second number:"))
print("Random No.1:",random_num(n1,n2))
print("Random No.2:",random_num(n1,n2))
print("Random No.3:",random_num(n1,n2))
Output::
Program.6 . Write a python program using a function to print Fibonacci series up to n numbers
def fibo():
n=int(input("Enter the number:"))
a=0
b=1
temp=0
for i in range(0,n):
temp = a + b
b=a
a= temp
print(a, end=" ")
fibo()
Output::
Program.7. Read a file line by line and print it.
L = ["Boston\n", "Public\n", "School\n"]
# writing to file
file1 = open('myfile.txt', 'w')
file1.writelines(L)
file1.close()
# Using readlines()
file1 = open('myfile.txt', 'r')
Lines = file1.readlines()
count = 0
# Strips the newline character
for line in Lines:
count += 1
print("Line{}: {}".format(count, line.strip()))
Output::
Program.8 Read a text file and display the number of vowels /consonants / uppercase /
lowercase characters in the file.
str = input("Type the string: ")
vowel_count=0
consonants_count=0
vowel = set("aeiouAEIOU")
for alphabet in str:
if alphabet in vowel:
vowel_count=vowel_count +1
elif alphabet == chr(32):
consonants_count=consonants_count
else:
consonants_count=consonants_count+1
print("Number of Vowels in ",str," is :",vowel_count)
print("Number of Consonants in ",str," is :",consonants_count)
uppercase_count=0
lowercase_count=0
for elem in str:
if elem.isupper():
uppercase_count += 1
elif elem.islower():
lowercase_count += 1
print("Number of UPPER Case in ",str,"' is :",uppercase_count)
print("Number of lower case in ",str,"' is :",lowercase_count)
Output::
Program.9. Write a Program to enter the string and to check if it’s palindrome or not using loop.
def isPalindrome(input_str):
for i in range(0, len(input_str)):
return False
return True
input_str =input("Enter the string to check if it is a palindrome:")
print("Palindrome") if isPalindrome(input_str) else print("Not Palindrome")
Output::
Program.10. Write a python program using a function to print Fibonacci series up
to n numbers.
def fibo():
n=int(input("Enter the number:"))
a=0
b=1
temp=0
for i in range(0,n):
temp = a + b
b=a
a= temp
print(a, end=" ")
fibo()
Output::
Program.11. Python program to check if the number is an Armstrong number or not
num = int(input("Enter a number: "))
sum = 0
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
if num == sum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")
Output::
Program. 12. Write a random number generator that generates random numbers between 1 and 6
(simulates a dice).
import random
min = 1
max = 6
roll_again = "y"
while roll_again == "y" or roll_again == "Y":
print("Rolling the dice...")
val = random.randint (min, max)
print("You get... :", val)
roll_again = input("Roll the dice again? (y/n)...")
Output::
Program. 13. Create a CSV file by entering user-id and password, read and search the password
for given user id.
import csv
with open("user_info.csv", "w") as obj:
fileobj = csv.writer(obj)
fileobj.writerow(["User Id", "password"])
while(True):
user_id = input("enter id: ")
password = input("enter password: ")
record = [user_id, password]
fileobj.writerow(record)
x = input("press Y/y to continue and N/n to terminate the program\n")
if x in "Nn":
break
elif x in "Yy":
continue
with open("user_info.csv", "r") as obj2:
fileobj2 = csv.reader(obj2)
given = input("enter the user id to be searched\n")
for i in fileobj2:
next(fileobj2)
if i[0] == given:
print(i[1])
break
Output::
Program. 14 Program to create binary file to store Rollno,Name and Marksand update
marks of entered Rollno.
import pickle
student=[ ]
f=open('student.dat','wb')
ans='y'
while ans.lower()=='y':
roll = int(input("Enter Roll Number :"))
name= input("Enter Name :")
marks = int(input("Enter Marks :"))
student.append([roll,name,marks])
ans=input("Add More ?(Y)")
pickle.dump(student,f)
f.close()
f=open('student.dat','rb+')
student=[ ]
while True:
try:
student = pickle.load(f)
except EOFError:
break
ans='y'
while ans.lower()=='y':
found=False
r = int(input("Enter Roll number to update :"))
for s in student:
if s[0]==r:
print("## Name is :",s[1], " ##")
print("## Current Marks is :",s[2]," ##")
m =int(input("Enter new marks :"))
s[2]=m
print("## Record Updated ##")
found=True
break
if not found:
print("####Sorry! Roll number not found ####")
ans=input("Update more ?(Y):")
f.close()
Output::
Program. 15. Write a python program to implement a stack using a list data-structure.
def isempty(stk):
if stk==[]:
return True
else:
return False
def push(stk,item):
stk.append(item)
top=len(stk)-1
def pop(stk):
if isempty(stk):
return "underflow"
else:
item=stk.pop()
if len(stk)==0:
top=None
else:
top=len(stk)-1
return item
def peek(stk):
if isempty(stk):
return "underflow"
else:
top=len(stk)-1
return stk[top]
def display(stk):
if isempty(stk):
print('stack is empty')
else:
top=len(stk)-1
print(stk[top],'<-top')
for i in range(top-1,-1,-1):
print(stk[i])
def main():
stk=[]
top=None
while True:
print('''stack operation
1.push
2.pop
3.peek
4.display
5.exit''')
choice=int (input('enter choice:'))
if choice==1:
item=int(input('enter item:'))
push(stk,item)
elif choice==2:
item=pop(stk)
if item=="underflow":
print('stack is underflow')
else:
print('poped')
elif choice==3:
item=peek(stk)
if item=="underflow":
print('stack is underflow')
else:
print('top most item is:',item)
elif choice==4:
display(stk)
elif choice==5:
break
else:
print('invalid')
exit()
main()
Output::
Program. 16. Write a python program to implement to deletes all the records from EMPLOYEE
whose AGE is more than 20 .
import mysql.connector
#establishing the connection
conn = mysql.connector.connect(
user='root', password='password', host='127.0.0.1', database='mydb')
#Creating a cursor object using the cursor() method
cursor = conn.cursor()
#Retrieving single row
print("Contents of the table: ")
cursor.execute("SELECT * from EMPLOYEE")
print(cursor.fetchall())
#Preparing the query to delete records
sql = "DELETE FROM EMPLOYEE WHERE AGE > '%d'" % (25)
try:
# Execute the SQL command
cursor.execute(sql)
# Commit your changes in the database
conn.commit()
except:
# Roll back in case there is any error
conn.rollback()
#Retrieving data
print("Contents of the table after delete operation ")
cursor.execute("SELECT * from EMPLOYEE")
print(cursor.fetchall())
#Closing the connection
conn.close()
Output::
Contents of the table:
[('Krishna', 'Sharma', 22, 'M', 2000.0),
('Raj', 'Kandukuri', 23, 'M', 7000.0),
('Ramya', 'Ramapriya', 26, 'F', 5000.0),
('Mac', 'Mohan', 20, 'M', 2000.0),
('Ramya', 'Rama priya', 27, 'F', 9000.0)]
Contents of the table after delete operation:
[('Krishna', 'Sharma', 22, 'M', 2000.0),
('Raj', 'Kandukuri', 23, 'M', 7000.0),
('Mac', 'Mohan', 20, 'M', 2000.0)]
Program. 17. Create a student table and insert data. Implement the following SQL
commands on the student table:
i)- ALTER table to add new attributes / modify data type / drop attribute
ii)- UPDATE table to modify data
iii)- ORDER By to display data in ascending / descending order
iv)- DELETE to remove tuple(s)
v)- GROUP BY and find the min, max, sum, count and average
Solution:
#Creating table student
mysql> create table student
-> (ROLLNO INT NOT NULL PRIMARY KEY,
-> NAME CHAR(10),
-> TELUGU CHAR(10),
-> HINDI CHAR(10),
-> MATHS CHAR(10));
Query OK, 0 rows affected (1.38 sec)
#Inserting values into table
mysql> insert into student
-> values(101,"student1",50,51,52),
-> (102,"student2",60,61,62),
-> (103,"student3",70,71,72),
-> (104,"student4",80,81,82),
-> (105,"student5",90,91,92),
-> (106,"student6",40,41,42),
-> (107,"student7",63,64,65);
Query OK, 7 rows affected (0.24 sec)
#Adding new attribute computers
mysql> alter table student
-> add (computers char(10));
#Describing table
mysql> desc student;
+-----------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+----------+------+-----+---------+-------+
| ROLLNO | int | NO | PRI | NULL | |
| NAME | char(10) | YES | | NULL | |
| TELUGU | char(10) | YES | | NULL | |
| HINDI | char(10) | YES | | NULL | |
| MATHS | char(10) | YES | | NULL | |
| computers | char(10) | YES | | NULL | |
+-----------+----------+------+-----+---------+-------+
#Modifying the datatype
mysql> alter table student
-> modify column computers varchar(10);
mysql> desc student;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| ROLLNO | int | NO | PRI | NULL | |
| NAME | char(10) | YES | | NULL | |
| TELUGU | char(10) | YES | | NULL | |
| HINDI | char(10) | YES | | NULL | |
| MATHS | char(10) | YES | | NULL | |
| computers | varchar(10) | YES | | NULL | |
+-----------+-------------+------+-----+---------+-------+
#Droping a attribute
mysql> alter table student
-> drop column computers;
#Describing table
mysql> desc student;
+--------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+----------+------+-----+---------+-------+
| ROLLNO | int | NO | PRI | NULL | |
| NAME | char(10) | YES | | NULL | |
| TELUGU | char(10) | YES | | NULL | |
| HINDI | char(10) | YES | | NULL | |
| MATHS | char(10) | YES | | NULL | |
+--------+----------+------+-----+---------+-------+
#UPDATE DATA TO MODIFY DATA
#ACTUAL DATA
mysql> select *from student;
+--------+----------+--------+-------+-------+
| ROLLNO | NAME | TELUGU | HINDI | MATHS |
+--------+----------+--------+-------+-------+
| 101 | student1 | 50 | 51 | 52 |
| 102 | student2 | 60 | 61 | 62 |
| 103 | student3 | 70 | 71 | 72 |
| 104 | student4 | 80 | 81 | 82 |
| 105 | student5 | 90 | 91 | 92 |
| 106 | student6 | 40 | 41 | 42 |
| 107 | student7 | 63 | 64 | 65 |
+--------+----------+--------+-------+-------+
#UPDATE THE MARKS FOR ATTRIBUTE TELUGU FOR THE STUDENT101
mysql> UPDATE STUDENT
-> SET TELUGU=99
-> WHERE ROLLNO=101;
#DATA IN THE TABLE AFTER UPDATING
mysql> SELECT *FROM STUDENT;
+--------+----------+--------+-------+-------+
| ROLLNO | NAME | TELUGU | HINDI | MATHS |
+--------+----------+--------+-------+-------+
| 101 | student1 | 99 | 51 | 52 |
| 102 | student2 | 60 | 61 | 62 |
| 103 | student3 | 70 | 71 | 72 |
| 104 | student4 | 80 | 81 | 82 |
| 105 | student5 | 90 | 91 | 92 |
| 106 | student6 | 40 | 41 | 42 |
| 107 | student7 | 63 | 64 | 65 |
+--------+----------+--------+-------+-------+
#ORDER BY DESCENDING ORDER
mysql> SELECT *FROM STUDENT
-> ORDER BY HINDI DESC;
+--------+----------+--------+-------+-------+
| ROLLNO | NAME | TELUGU | HINDI | MATHS |
+--------+----------+--------+-------+-------+
| 105 | student5 | 90 | 91 | 92 |
| 104 | student4 | 80 | 81 | 82 |
| 103 | student3 | 70 | 71 | 72 |
| 107 | student7 | 63 | 64 | 65 |
| 102 | student2 | 60 | 61 | 62 |
| 101 | student1 | 99 | 51 | 52 |
| 106 | student6 | 40 | 41 | 42 |
+--------+----------+--------+-------+-------+
#ORDER BY ASCENDING ORDER
mysql> SELECT *FROM STUDENT
-> ORDER BY HINDI ASC;
+--------+----------+--------+-------+-------+
| ROLLNO | NAME | TELUGU | HINDI | MATHS |
+--------+----------+--------+-------+-------+
| 106 | student6 | 40 | 41 | 42 |
| 101 | student1 | 99 | 51 | 52 |
| 102 | student2 | 60 | 61 | 62 |
| 107 | student7 | 63 | 64 | 65 |
| 103 | student3 | 70 | 71 | 72 |
| 104 | student4 | 80 | 81 | 82 |
| 105 | student5 | 90 | 91 | 92 |
+--------+----------+--------+-------+-------+
#DELETING A TUPLE FROM THE TABLE
mysql> DELETE FROM STUDENT
-> WHERE ROLLNO=101;
mysql> SELECT *FROM STUDENT;
+--------+----------+--------+-------+-------+
| ROLLNO | NAME | TELUGU | HINDI | MATHS |
+--------+----------+--------+-------+-------+
| 102 | student2 | 60 | 61 | 62 |
| 103 | student3 | 70 | 71 | 72 |
| 104 | student4 | 80 | 81 | 82 |
| 105 | student5 | 90 | 91 | 92 |
| 106 | student6 | 40 | 41 | 42 |
| 107 | student7 | 63 | 64 | 65 |
+--------+----------+--------+-------+-------+
#ORDER BY BRANCH
#ACTUAL DATA
mysql> SELECT *FROM STUDENT;
+--------+--------+----------+--------+-------+-------+
| ROLLNO | BRANCH | NAME | TELUGU | HINDI | MATHS |
+--------+--------+----------+--------+-------+-------+
| 102 | MPC | student2 | 60 | 61 | 62 |
| 103 | BIPC | student3 | 70 | 71 | 72 |
| 104 | BIPC | student4 | 80 | 81 | 82 |
| 105 | BIPC | student5 | 90 | 91 | 92 |
| 106 | BIPC | student6 | 40 | 41 | 42 |
| 107 | MPC | student7 | 63 | 64 | 65 |
+--------+--------+----------+--------+-------+-------+
mysql> SELECT BRANCH,COUNT(*)
-> FROM STUDENT
-> GROUP BY BRANCH;
+--------+----------+
| BRANCH | COUNT(*) |
+--------+----------+
| MPC | 2 |
| BIPC | 4 |
+--------+----------+
#e min, max, sum, count and average
mysql> SELECT MIN(TELUGU) "TELUGU MIN MARKS"
-> FROM STUDENT;
+------------------+
| TELUGU MIN MARKS |
+------------------+
| 40 |
+------------------+
mysql> SELECT MAX(TELUGU) "TELUGU MAX MARKS"
-> FROM STUDENT;
+------------------+
| TELUGU MAX MARKS |
+------------------+
| 90 |
+------------------+
1 row in set (0.00 sec)
mysql> SELECT SUM(TELUGU) "TELUGU TOTAL MARKS"
-> FROM STUDENT;
+--------------------+
| TELUGU TOTAL MARKS |
+--------------------+
| 403 |
+--------------------+
1 row in set (0.00 sec)
mysql> SELECT COUNT(ROLLNO)
-> FROM STUDENT;
+---------------+
| COUNT(ROLLNO) |
+---------------+
| 6 |
+---------------+
1 row in set (0.01 sec)
mysql> SELECT AVG(TELUGU) "TELUGU AVG MARKS"
-> FROM STUDENT;
+-------------------+
| TELUGU AVG MARKS |
+-------------------+
| 67.16666666666667 |
+-------------------+