[go: up one dir, main page]

0% found this document useful (0 votes)
8 views51 pages

Cs Practical - Merged

This document is a practical file for Class XII-A Computer Science students at PM SHRI Kendriya Vidyalaya for the academic year 2024-25. It includes a list of programming tasks and SQL commands that students must complete, along with sample input and output for each task. The practical exercises cover various programming concepts using Python and database management using SQL.

Uploaded by

anirudhdas18may
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views51 pages

Cs Practical - Merged

This document is a practical file for Class XII-A Computer Science students at PM SHRI Kendriya Vidyalaya for the academic year 2024-25. It includes a list of programming tasks and SQL commands that students must complete, along with sample input and output for each task. The practical exercises cover various programming concepts using Python and database management using SQL.

Uploaded by

anirudhdas18may
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

PM SHRI KENDRIYA VIDYALAYA, C-2,

JANAKPURI ND-58

Academic Year: 2024-25


Practical File

Name: ​ ANIRUDH PARASHAR ​


Roll no.:
Class: ​​ ​ ​ XII-A
Subject: ​ ​ ​ Computer Science
Subject Code: ​​ 083
Project Guide: ​ Mr. RANJEET PGT, COMPUTER
SCIENCE, KENDRIYA VIDYALAYA,
JANAKPURI ​ NEW DELHI


INDEX
S.N Aim/Practical Date Page Sign
o. no
1 Write a program to input a list of numbers 1
and find the minimum and maximum value
along with its index.

2 Write a program to input a list and store the 2


positive and negative number from the list in
two separate lists.

3 WAP to count and display the number of 3


vowels, consonants, uppercase and
lowercase characters in a string
4 Write a program for a menu driven program 5
for finding whether a number is perfect,
Armstrong or a palindrome

5 Write a program to input a string and check 7


whether it is a palindrome string or not.

6 Write a menu driven program in python to 8


display Fibonacci series of a given number
and print factorial of a given number

7 Write a program to input a string and search 10


any word in that string/sentence.

8 Write a program to read the content of text file 11


and display the total number of consonants,
uppercase, vowels , lowercase and digits
characters

9 Write a program to read and display text file 12


content line by line with each word separated
by #

10 Write a program to read the content of a text 13


file line by line and write it to another file
except for the lines WHICH contains “a” letter
in it.

11 Create a dictionary with keys as course name 14


and fee as value. Write a program to push the
course name in the stack where the fee is
more than 10000.

12 Write a program to pass an integer list as 15


stack to a function and push only those
elements in the stack which are divisible by 7.
Also write a function to pop and display the
stack.

13 Write a program to create a binary file to store 16


Rollno and Name then Search any Rollno and
display name
14 Write a program to create a binary file to store 17
Roll number,Name and Marks and update
marks based on Roll number given by the
user

15 Write a program to create a CSV file and take 19


emp number, name, salary and store them.
Then search any emp number and display
name, salary.

16 Write a program to generate a random 21


number between 1-6, simulating a dice.

17 Write brief detail about SQL and its 22


commands
18 Write the SQL command/query based on 23
Creation, Insertion, Deletion & Pattern
Matching
19 Write the SQL command/query based on 26
Alter, Update, Deletion and Sorting
20 Write the SQL command/query based on 30
Aggregate Functions
21 Write the SQL command/query based on 32
Group by and having clause
22 Write the SQL command/query based on 34
Single Row functions.
23 Write the SQL command/query based on 35
Joins(Cartesian/Equi/Natural).
24 Program to connect with database and 39
store records of employees and display
records.
25 Program to connect with database and 41
search employee number in table
employee and display record, if emp
number not found display appropriate
message.
26 Program to connect with the database and 43
update the employee record of entered
emp number.
27 Program to connect with the database and 45
delete the record of the entered employee
number.
Date:

Practical - 1
AIM : Write a program to input a list of numbers and find the minimum
and maximum value along with its index.

Software Used : Python 3.8.10

INPUT :
a = eval(input("Enter a list of numbers:"))
for i in range(len(a)):
if a[i] == min(a):
print("Smallest number = ",a[i],"at index =",i)
elif a[i] == max(a):
print("Largest number = ",a[i],"at index =",i)

OUTPUT:

1
Date:

Practical - 2
AIM : Write a program to input a list and store the positive and
negative number from the list in two separate lists and print the same.

Software Used : Python 3.8.10

INPUT :
L=eval(input(“Enter a list of numbers:”))
p=[]
n=[]
for i in L:
if i >= 0:
p.append(i)
​ if i<0:
​ ​ n.append(i)
print("List of positive numbers:",p)
print("List of negative numbers:",n)

OUTPUT:

2
Date:

Practical - 3
AIM :Write a program to input a string and display the number of
vowels, consonants, uppercase, lowercase and digits present in the
string.

Software Used : Python 3.8.10

INPUT :
S=input(“Enter a string:”)
v,c,u,l,d=0,0,0,0,0
for i in S:
​ if i.isalnum():
​ ​ if i in “AEIOUaeiou”:
​ ​ ​ v+=1
​ ​ if i not in “AEIOUaeiou”:
​ ​ ​ c+=1
​ ​ if i.isupper():
​ ​ ​ u+=1
​ ​ if i.islower():
​ ​ ​ l+=1
​ ​ if i.isdigit():
​ ​ ​ d+=1
print("No. of vowels =",v)
print("No. of consonants =",c)
print("No. of uppercase letters =",u)
print("No. of lowercase letters =",l)
print("No. of digits =",d)

3
Date:

OUTPUT:

4
Date:

Practical - 4
AIM : Write a program for a menu driven program for finding whether
a number is a perfect number, an Armstrong number or a palindrome
number

Software Used : Python 3.8.10

INPUT :
n=int(input("enter the number:"))
print("Press 1: Perfect Number")
print("Press 2: Armstrong Number")
print("Press 3: Palindrome Number")
c=int(input("Enter the choice 1/2/3 : "))
s=0
if c==1:
for i in range(1,n):
if n%i==0:
s=s+i
if s==n:
print(n,"is a perfect number")
else:
print(n,"is not a perfect number")
if c==2:
q=str(n)
z=0
for i in q:
​ z+=int(i)**len(q)
if z==n:
​ print(n,”is an armstrong number!”)
else:

5
Date:

​ print(n,”isn’t an armstrong number!”)


if c==3:
​ n=str(n)
​ if n==n[::-1]:
​ ​ print(n,"is a palindrome number")
​ else:
​ ​ print(n,"is not a palindrome number")
else:
​ (“ERROR! Enter a valid option”)

OUTPUT:
Perfect number

Armstrong number

Palindrome number

6
Date:

Practical - 5
AIM : Write a program to input a string and check whether it is a
palindrome string or not.

Software Used : Python 3.8.10

INPUT :
S=input("Enter a String:")
if S==S[::-1]:
​ print(“it is a palindrome”)
else:
​ print(“it is not a palindrome”)

OUTPUT:

7
Date:

Practical - 6
AIM : Write a menu driven program in python to display Fibonacci
series of a given number and print factorial of a given number

Software Used : Python 3.8.10

INPUT :
print("press 1: for fibonacci series")
print("press 2: for the factorial of a number")
c=int(input("Enter the choice:"))
if c==1:
n=int(input("Enter the number of element for the series:"))
n1=0
n2=1
print(n1,end=" ")
print(n2,end=" ")
for i in range(3,n+1):
n3=n1+n2
print(n3,end=" ")
n1=n2
n2=n3
if c==2:
n=int(input("Enter the number"))
f=1
for i in range(1, n + 1):
f*=i
print("Factorial of",n,"is",f)
else:
​ print(“ERROR!”)
​ print(“Enter a valid option!”)

8
Date:

OUTPUT:

Fibonacci series:

Factorial:

9
Date:

Practical - 7
AIM : Write a program to input a string and search any word in that
string/sentence.

Software Used : Python 3.8.10

INPUT :
n=input(“Enter the string:”)
a=input(“Enter word to search:”)
if a in n:
​ print(“Word found”)

OUTPUT:

10
Date:

Practical - 8
AIM : Write a program to read the content of text file and display the
total number of consonants, uppercase, vowels , lowercase and digits
characters

Software Used : Python 3.8.10

INPUT :
f=open(“KaChow.txt”,’r’)
r=f.read()
v,c,u,l,d=0,0,0,0,0
for i in r:
​ if i.isalnum():
​ ​ if i in “AEIOUaeiou”:
​ ​ ​ v+=1
​ ​ if i not in “AEIOUaeiou”:
​ ​ ​ c+=1
​ ​ if i.isupper():
​ ​ ​ u+=1
​ ​ if i.islower():
​ ​ ​ l+=1
​ ​ if i.isdigit():
​ ​ ​ d+=1
print("No. of vowels =",v)
print("No. of consonants =",c)
print("No. of uppercase letters =",u)
print("No. of lowercase letters =",l)
print("No. of digits =",d)

OUTPUT: Text file Output: Python

11
Date:

Practical - 9
AIM :Write a program to read and display text file content line by line
with each word separated by #

Software Used : Python 3.8.10

INPUT :
f=open("MyText.txt",'r')
for i in f:
q=i.split()
a='#'.join(q)
print(a)

OUTPUT:
File

Python

12
Date:

Practical - 10
AIM : Write a program to read the content of a text file line by line and
write it to another file except for the lines WHICH contains “a” letter in
it.

Software Used : Python 3.8.10

INPUT :
f=open("MyText.txt",'r')
f1=open("NewFile.txt",'w')
d=f.readlines()
for i in d:
if 'a' not in i:
f1.write(i)
f.close()
f1.close()

OUTPUT:

13
Date:

Practical - 11
AIM : Create a dictionary with keys as course name and fee as value
after taking input from the user. Write a program to push the course
name in the stack where the fee is more than 10000. Pop and display
contents of the stack on the screen.

Software Used : Python 3.8.10

INPUT:​
K=eval(input("Enter keys:"))
V=eval(input("Enter values:"))
D={}
stack=[]
for i in range(len(K)):
D[K[i]]=V[i]
for i in D:
if D[i]>10000:
stack.append(i)
for i in range(len(stack)):
print(stack.pop())

OUTPUT:

14
Date:

Practical - 12
AIM : Write a program to pass an integer list as stack to a function
and push only those elements in the stack which are divisible by 7.
Also write a function to pop and display the stack.

Software Used : Python 3.8.10

INPUT :
l=[12,14,96,98,25,70]
stack=[]
def func(l):
for i in l:
if i%7==0:
stack.append(i)
return stack
func(l)
print(stack)
for i in range(len(stack)):
print(stack.pop())
if stack == []:
print("Stack is empty!")

OUTPUT:

15
Date:

Practical - 13
AIM : Write a program to create a binary file to store Roll number and
Name, after taking input from user, then Search any Roll number and
display name if Roll number found otherwise “Roll number not found”

Software Used : Python 3.8.10

INPUT :
import pickle
f=open(“MyFile.dat”,’wb’)
f1=open(“MyFile.dat”,’rb’)
V=eval(input(“Enter dictionary with structure ‘name’:roll number→”)
pickle.dump(V,f)
f.close()
r=int(input(“enter roll number to find:”)
a=pickle.load(f1)
for i in a:
​ if a[i]==r:
​ ​ print(“Name associated with roll number “,r,”:”,a[i])
else:
​ print(“Roll number not found!”)
f1.close()

OUTPUT:

16
Date:

Practical - 14
AIM : Write a program to create a binary file to store Roll
number,Name and Marks after taking input from the user and then
update marks based on Roll number given by the user.

Software Used : Python 3.8.10

INPUT :
import pickle
f=open("students.dat", "wb+")
students=[]
n=int(input("Enter the number of students:"))
for i in range(n):
roll=int(input("Enter roll no.:"))
name=input("Enter name:")
marks=float(input("Enter marks:"))
students.append({"roll": roll, "name": name, "marks": marks})
pickle.dump(students,f)
print("Student records added.")
f.seek(0)
students=pickle.load(f)
roll_update=int(input("Enter roll no. to search:"))
new_marks=float(input("Enter new marks:"))
flag=0
for i in students:
if i["roll"]==roll_update:
i["marks"]=new_marks
flag=1
print("Marks updated")
break
if flag==0:

17
Date:

print("Roll Number not found.")


else:
f.seek(0)
pickle.dump(students,f)
f.close()

OUTPUT:

18
Date:

Practical - 15
AIM : Write a program to create a CSV file and take emp
number,name,salary as input and store them in that file. Then search
any emp number and display name,salary and if not found appropriate
message.

Software Used : Python 3.8.10

INPUT :
import csv
f=open("Employee.csv",'w')
f1=open("Employee.csv",'r')
a=int(input("Enter no. of employees:"))
for i in range(a):
q=csv.writer(f)
e1=input("Enter employee number:")
e2=input("Enter name of employee:")
e3=input("Enter salary:")
q.writerow([e1,e2,e3])
else:
print("Data updated!")
f.close()
w=input("Enter employee no. to search:")
r=csv.reader(f1)
c=0
for j in r:
if j==[]:
continue
else:
if j[0]==w:
print("Employee found!")

19
Date:

print("Employee name:",j[1]," Salary:",j[2])


c=1
break
if c==0:
print("Incorrect Employee no. entered!!")
f1.close()

OUTPUT:

20
Date:

Practical - 16
AIM : Write a program to generate a random number between 1-6,
simulating a dice.

Software Used : Python 3.8.10

INPUT :
import random as r
dice=r.randint(1,6)
print("Number after dice throw:",dice)

OUTPUT:

21
Date:

Practical - 17
AIM : Write brief detail about SQL and its commands ​

SQL : The Structured Query Language (SQL) is a language that


enables you to create and operate on relational databases, which are
sets of related information stored in tables.

CLASSIFICATION OF SQL STATEMENTS :


SQL commands can be mainly divided into following categories:
1. Data Definition Language(DDL) Commands :
Commands that allow you to perform task, related to data
definition e.g; ​
​ • Creating, altering and dropping.
• Granting and revoking privileges and roles.
• Maintenance commands.

2. Data Manipulation Language(DML) Commands :


Commands that allow you to perform data manipulation e.g.,
retrieval, insertion, deletion and modification of data stored in a
database.

3. Transaction Control Language(TCL) Commands :


Commands that allow you to manage and control the
transactions e.g.,
• Making changes to database, permanent
• Undoing changes to database, permanent
• Creating savepoints
• Setting properties for current transactions

22
Date:

Practical - 18
AIM : Write the SQL command/query based on Creation, Insertion,
Deletion & Pattern Matching.

Software Used : MySQL 8.0

INPUT :
1.​Write the command to create the table STUDENT.

OUTPUT :

2.​Write the command to insert the values in the table STUDENT.

23
Date:

OUTPUT :

3.​Write the command to select distinct Streams in the table


STUDENT.

OUTPUT :

4.​Write the command to display details of all students whose name


starts with D.

OUTPUT :

24
Date:

5.​Write the command to display details of all students that have


the second character ‘i’ in their name.

​ ​ OUTPUT :

25
Date:

Practical - 19
AIM : Write the SQL command/query based on Alter, Update, Deletion
and Sorting with reference to the STUDENT table.

Software Used : MySQL 8.0

INPUT :
1.​Write a command to update marks of students having admission
number 8765 to 76.


​ OUTPUT :



2.​Write a command to view the structure of the table STUDENT.

26
Date:


​ OUTPUT :


3.​Write a command to display details of all students whose marks
fall between 80 and 100.


​ OUTPUT :


4.​Write a command to display the Name and Stream of all
students who have Nonmedical stream.


​ OUTPUT :

27
Date:

5.​Write a command to display Name, Stream and Marks of all


students in descending order of Marks.


​ OUTPUT :


6.​Write a command to delete the data of students who have
Medical Stream.


​ OUTPUT :

28
Date:

7.​Write a command to add a new column Grade in the table


STUDENT.

OUTPUT :

8.​Write a command to delete the column added in (7.).


​ OUTPUT :

29
Date:

Practical - 20
AIM : Write the SQL command/query based on Aggregate Functions
with reference to the STUDENT table.

Software Used : MySQL 8.0

INPUT :
1.​Write a command to compute the sum of marks of all students in
the STUDENT table.

​ OUTPUT :


2.​Write a command to display maximum Marks in the STUDENT
table.

OUTPUT :

3.​Write a command to display minimum Marks in the STUDENT


table.

OUTPUT :

30
Date:

4.​Write a command to display the average of Marks in the


STUDENT table.

OUTPUT :

5.​Write a command to display the total number of students in the


STUDENT table.

OUTPUT :

31
Date:

Practical - 21
AIM : Write the SQL command/query based on Group by and having
clause with reference to the STUDENT table.

Software Used : MySQL 8.0

INPUT :
1.​Write a command to count the number of students in each
Stream.

OUTPUT :

2.​Write a command to find maximum marks obtained by students


in each Stream.

OUTPUT :

3.​Write a command to display only those streams where the


number of students is greater than 2.

32
Date:


OUTPUT :

4.​Write a command to display that class who have total number of


students greater than 5.

OUTPUT :

33
Date:

Practical - 22
AIM : . Write the SQL command/query based on Single Row functions
with reference to the STUDENT table.

Software Used : SQL 8.0

INPUT :
1.​Write a command to extract the first 2 characters from the Name
column.

OUTPUT :

2.​Write a command to display the total number of characters in


each value of column Name.

OUTPUT :

34
Date:

3.​Write a command to display all the values in the stream column


in upper case letters.

​ OUTPUT :


4.​Write command to concatenate Name, Class and section of all
students who are in class 12.


OUTPUT :

35
Date:

5.​Write a command to extract the last three characters from the


Name column.

OUTPUT :

36
Date:

Practical - 23
AIM : Write the SQL command/query based on Joins (Cartesian / Equi
/ Natural).

Software Used : MySQL 8.0

INPUT :

1.​To display names of participants along with workshop titles for


only those workshops that have more than 2 speakers.

​ OUTPUT :

37
Date:

2.​To display ParticipantID, Participant’s name, WorkshopId for


workshops meant for Senior Managers and Junior Managers.

​ OUTPUT :


3.​To display WorkshopId, title , ParticipantId for only those
workshops that have fees in the range of 5000.00 to 8000.00.

​ OUTPUT :

38
Date:

Practical - 24
AIM : Program to connect with database and store records of
employees and display records.

Software Used :
●​ Python 3.8.10
●​ MySQL 8.0

INPUT :
import mysql.connector as mc
con=mc.connect(host="localhost",user="root",password="root",
database="me")
c=con.cursor()
c.execute("insert into Employee values({},'{}','{}',{})".format
(121,"Kartik singh","IT",40000))
c.execute("insert into Employee values({},'{}','{}',{})".format
(324,"Deepa kumari","HR",34000))
c.execute("insert into employee values({},'{}','{}',{})".format
(754,"Mahesh singh","MARKETING",23500))
c.execute("insert into employee values({},'{}','{}',{})".format
(432,"Anuj kumar","HR",35000))
c.execute("insert into employee values({},'{}','{}',{})".format
(234,"Aarti","IT", 45000))

con.commit()
print("Data inserted successfully")
c.execute("select * from employee")
f=c.fetchall()
for i in f:
print(i)
con.close()

39
Date:

OUTPUT :
●​ Python:

●​ MySQL:

40
Date:

Practical - 25
AIM : Program to connect with database and search employee
number in table employee and display record, if Empno not found
display appropriate message.

Software Used :
●​ Python 3.8.10
●​ MySQL 8.0

INPUT :
import mysql.connector as mc
con=mc.connect(host="localhost",user="root",password="root",
database="me")
c=con.cursor()
c.execute('select * from Employee;')
f=c.fetchall()
flag=0
n=int(input("Enter Employee No. to be searched:"))
for i in f:
if i[0]==n:
print("Employee Number:",i[0])
print("Name:",i[1])
print("Department:",i[2])
print("Salary:",i[3])
flag=1
if flag==0:
print("Employee No. not found")
con.close()

OUTPUT :

41
Date:

42
Date:

Practical - 26
AIM : . Program to connect with the database and update the
employee record of entered Empno.

Software Used :
●​ Python 3.8.10
●​ MySQL 8.0

INPUT :
import mysql.connector as mc
con=mc.connect(host="localhost",user="root",password="root",
database="me")
c=con.cursor()
c.execute('select * from Employee;')
f=c.fetchall()
flag=0
n=int(input("Enter Employee No. to be updated:"))
for i in f:
if i[0]==n:
print("Employee found")
name=input("Enter the Name:")
dept=input("Enter the Department:")
salary=int(input("enter the Salary:"))
c.execute("update Employee set Ename='{}', Dept='{}', Salary={}
where Empno={}".format(name,dept,salary,n)) ​
con.commit()
print("Employee record updated")
flag=1
if flag==0:
print("Employee No. not found")
con.close()

43
Date:

OUTPUT :
●​ Python

●​ MySQL

44
Date:

Practical - 27
AIM : Program to connect with database and delete the record of
entered employee number.

Software Used :
●​ Python 3.8.10
●​ MySQL 8.0

INPUT :
import mysql.connector as mc
con=mc.connect(host="localhost",user="root",password="root",
database="me")
c=con.cursor()
c.execute('select * from Employee;')
f=c.fetchall()
flag=0
n=int(input("Enter Employee No. to be deleted from the record:"))
for i in f:
if i[0]==n:
print("Employee found")
c.execute("delete from Employee where​
Empno={}".format(n))
con.commit()
print("Employee record updated")
flag=1
if flag==0:
print("Employee No. not found")
con.close()

OUTPUT :
●​ Python

45
Date:

●​ MySQL

46

You might also like