Cs Practical - Merged
Cs Practical - Merged
JANAKPURI ND-58
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.
Practical - 1
AIM : Write a program to input a list of numbers and find the minimum
and maximum value along with its index.
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.
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.
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
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:
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.
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
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.
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
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)
11
Date:
Practical - 9
AIM :Write a program to read and display text file content line by line
with each word separated by #
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.
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.
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.
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”
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.
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:
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.
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:
OUTPUT:
20
Date:
Practical - 16
AIM : Write a program to generate a random number between 1-6,
simulating a dice.
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
22
Date:
Practical - 18
AIM : Write the SQL command/query based on Creation, Insertion,
Deletion & Pattern Matching.
INPUT :
1.Write the command to create the table STUDENT.
OUTPUT :
23
Date:
OUTPUT :
OUTPUT :
OUTPUT :
24
Date:
OUTPUT :
25
Date:
Practical - 19
AIM : Write the SQL command/query based on Alter, Update, Deletion
and Sorting with reference to the STUDENT table.
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:
OUTPUT :
6.Write a command to delete the data of students who have
Medical Stream.
OUTPUT :
28
Date:
OUTPUT :
OUTPUT :
29
Date:
Practical - 20
AIM : Write the SQL command/query based on Aggregate Functions
with reference to the STUDENT table.
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 :
OUTPUT :
30
Date:
OUTPUT :
OUTPUT :
31
Date:
Practical - 21
AIM : Write the SQL command/query based on Group by and having
clause with reference to the STUDENT table.
INPUT :
1.Write a command to count the number of students in each
Stream.
OUTPUT :
OUTPUT :
32
Date:
OUTPUT :
OUTPUT :
33
Date:
Practical - 22
AIM : . Write the SQL command/query based on Single Row functions
with reference to the STUDENT table.
INPUT :
1.Write a command to extract the first 2 characters from the Name
column.
OUTPUT :
34
Date:
OUTPUT :
4.Write command to concatenate Name, Class and section of all
students who are in class 12.
OUTPUT :
35
Date:
36
Date:
Practical - 23
AIM : Write the SQL command/query based on Joins (Cartesian / Equi
/ Natural).
INPUT :
OUTPUT :
37
Date:
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