Computer Science (083) : Lab File
Computer Science (083) : Lab File
LAB FILE
SESSION: 2023-24
Write a program to input names of ‘n’ students and store them in tuple also input them
from the user and find if these students are present in the tuple or not.
name = tuple()
n = int(input("How many names do you want to enter?: "))
for i in range(0, n): num = input("> ")
name = name + (num,)
print("\nThe names entered in the tuple are:")
print(name)
search=input("\nEnter the name of the student you want to search? ")
if search in name:
print("The name",search,"is present in the tuple")
else:
print("The name",search,"is not found in the tuple")
Program #2
Write a program that reads a line and counts the number of lowercase, uppercase,
alphabets, and digits.
Program #6
Write a program to create a dictionary with the Roll.No, name, and marks of n students in
a class and display the names of students with marks above 75.
Write a program to create a 3rd dictionary from 2 dictionaries having same common keys
in a way such that the values of common keys.
A tuple stores marks of a student in 5 subjects, write a program to calculate the grade of
the student as per the following:
AVERAGE GRADE
>=75 ‘A’
60-74.9 ‘B’
50-59.9 ‘C’
<50 ‘D’
Write a program to input 2 lists and display the new elements from both the lists combined
along with its index.
Program #12
Write a program to input a string and print each individual word with its length.
def splitString (str):
str = str.split (' ')
for words in str:
print (words," (", len (words), ")")
str = "Hello World How are you?"
splitString(str)
Program #14
Write a program to input a string which has some digits, calculate the sum of the digits
present in the string.
def func(a):
k =0
for i in range(2,a):
if a%i == 0:
k += 1
else:
continue
if k == 0:
return 0
else:
return 1
x = int(input("enter the number: "))
if func(x)==0:
print("the number is prime")
else:
print("the number is composite")
Program #18
Write a program which takes n as input and generates a random number having n digits.
import random
def func(n):
k = ""
for i in range(1,n+1):
x = random.randint(0,9)
j = str(x)
k = k+j
t = int(k)
return t
a = int(input("enter the number: "))
print(func(a))
Program #19
def func(n):
a=0
b =1
if n == 1:
print(0)
elif n>0 :
for i in range(n):
c = a+b
print(a)
a=b
b =c
else:
print("error")
k = int(input("enter the number of terms: "))
func(k)
Program #20
def check(n):
if n>0:
print("positive")
elif n<0:
print("negative")
else:
print("zero")
k = int(input("enter the number: "))
check(k)
Program #22
def area(l,b):
ar = l * b
return ar
a = int(input("enter the length: "))
b = int(input("enter the breadth: "))
print(area(a,b))
Program #23
def factorial(a):
f=1
if a ==0:
f=1
elif a==1:
f=1
elif a<0:
f='invalid input'
else:
for i in range(1,a+1):
f=f*i
return f
y=int(input("Enter number: "))
print(factorial(y))
Program #24
a=open("/Users/admin/Desktop/text.txt",'r')
b=a.read(30)
print(b)
a.close()
Program #25
a=open("/Users/admin/Desktop/text.txt",'r')
b=a.read()
print(b)
a.close()
Program #26
Write a program which reads n bytes and then reads some more bytes from the last
position read.
a=open ("/Users/admin/Desktop/text.txt",'r')
x=int(input("Enter number of bytes you want to read: "))
b=a.read(x)
print(b)
c=a.read(10)
print(c)
a.close
Program #27
a=open("/Users/admin/Desktop/text.txt",'r')
b=a.readline()
print(b)
c=a.readline()
print(c)
d=a.readline()
print(d)
a.close()
Program #28
a = open(r"/Users/admin/Desktop/text.txt","r")
b =0
c = a.readlines()
d = len(c)
for i in range (d):
b =b +1
print (b)
a.close()
Program #29
a = open(r"/Users/admin/Desktop/text.txt","w")
b = int(input("Enter number of students required: "))
for i in range (b):
c = input("Enter Name: ")
a.write(c)
a.write("\n")
a.close()
Program #30
a = open(r"/Users/admin/Desktop/text.txt","r")
b =0
c = a.read()
d = len(c)
print ("Length of file in bytes: ",d)
a.close()
Program #31
Write a program to read a text file line by line and display each word separated by #.
a = open(r"/Users/admin/Desktop/text.txt","r")
while a:
b = a.readlines()
for i in b:
c = i.split()
for j in c:
print (j + "#",end="")
a.close()
Program #32
a = open(r"/Users/admin/Desktop/text.txt","r")
b = a.readlines()
print(b)
a.close()
Program #33
a = open(r"/Users/admin/Desktop/text.txt","r")
while a:
b = a.readline()
print (b,end="")
a.close()
Program #34
Write a program to create a CSV file to store student data (Roll.no., Name, and marks),
obtain data from user and write the file records.
import csv
def collect_student_data():
roll_no = int(input("Enter Roll Number: "))
name = input("Enter Name: ")
marks = float(input("Enter Marks: "))
return [roll_no, name, marks]
def write_to_csv_file(data):
with open('student_data.csv', 'a', newline='') as file:
writer = csv.writer(file)
writer.writerow(data)
while True:
student_data = collect_student_data()
write_to_csv_file(student_data)
choice = input("Do you want to enter data for another student? (y/n): ").lower()
if choice != 'y':
break
Program #35
try:
a = int(input("Enter your number: "))
b = int(input("Enter your number: "))
print (a/b)
except ZeroDivisionError:
print ("Don't divide by zero")
else:
print ("All Good")
finally:
print ("BYE BYE")
Program #36
create table new_employee (ECODE int (10),NAME char(20), DESIG varchar(29), SGRADE
varchar(20), DOJ date, DOB date);
insert into new_employee values(101,'Vikrant','Executive','S03','2003-03-23','1980-01-13');
insert into new_employee values(102,'Ravi','Head-IT','S02','2010-02-12','1987-07-22');
insert into new_employee values(103,'John Cena','Receptionist','S03','2009-6-24','1983-02-24');
insert into new_employee values(105,'Azhar Ansari','GM','S02','2009-08-11','1984-03-03');
insert into new_employee values(108,'Priyam Sen','CEO','S01','2004-12-19','1982-01-19');
select*from new_employee
Program #39
(ii) To display NAME and DESIG of those employees whose sgrade is either ‘S02’ or ‘S03’.
select name, desig, sgrade from new_employee where doj like '2009%';
(v) To display number of employee working in each SALGRADE from table EMPLOYEE
(iii) To increase the prize by 1000 for customers whose names start with S?
(iv) To select different values from column “CID”
(i) Display ename, sal, and sal added with comm from table employee
(ii) Write a query to display employee name, salary, and department number who are not
getting commission from table employee
(iii) List all department numbers in table employee
(v) List details of all clerks who have not been assigned departments as yet
Program #43
(ii) Write a query to display the name of the product which contains “Y” as the last
alphabet.
(iii) List the details of the product which contains 8 letters in its CAT
(iv) Write a query to display the name of product who is having “L” in its name.
(i) Display the names of the students who are getting a grade “C” either GAME or SUPW
(iii) Display the SUPW taken up by the students, whose name starts with “A”
Program #45