PRACTICAL FILE (Final)
PRACTICAL FILE (Final)
COMPUTER SCIENCE
MADE BY:
list=[]
s=0
n=int(input("Enter number of numbers to be entered:"))
for i in range(n):
a=int(input("Enter number:"))
list.append(a)
s=s+a
print("The mean of the entered list is=",s/n)
list=[]
n=int(input("Enter number of numbers to be entered:"))
for i in range(n):
a=int(input("Enter number:"))
list.append(a)
x=min(list)
print("The minimum element of the entered list is=",x)
list=[]
a=int(input("Enter marks in English:"))
b=int(input("Enter marks in Maths:"))
c=int(input("Enter marks in Physics:"))
d=int(input("Enter marks in Chemistry:"))
e=int(input("Enter marks in Computer Science:"))
list.append([a,b,c,d,e])
print("Total Marks=",a+b+c+d+e)
print("Percentage=",((a+b+c+d+e)/500)*100)
list=[]
n=int(input("Enter length of list:"))
for i in range(n):
x=eval(input("Enter a string or a number to be entered:"))
if(i%2!=0):
a=x*2
list.append(a)
else:
list.append(x)
print("The list is=",list)
list=[]
c=0
n=int(input("Enter length of list:"))
for i in range(n):
x=input("Enter element:")
list.append(x)
print("The list is=",list)
a=input("Choose an element from the above list:")
for j in list:
if(j==a):
c=c+1
print("The frequency of element'",a,"'in the entered list is=",c)
list=[]
n=int(input("Enter the length of your list:"))
for i in range(n):
x=eval(input("Enter element:"))
list.append(x)
print("Your list is=",list)
if (len(list)>0):
last_element=list.pop()
list.insert(0,last_element)
print("After shifting the list is=",list)
list=[3,25,13,6,35,8,14,45]
i=0
while (i<len(list)-1):
if (list[i]%5!=0):
for j in range(i+1,len(list)):
if list[j]%5==0:
list[i],list[j]=list[j],list[i]
break
i=i+1
print("The modified list is=",list)
class_teachers={}
n=int(input("Enter the number of classes:"))
for i in range (n):
class_name=input("Enter the name of the class "+str(i+1)+" :")
teacher_name=input("Enter the name of the class teacher for "+class_name+" :")
class_teachers[class_name]=teacher_name
print("Classes and their class teachers:")
for class_name,teacher_name in class_teachers.items():
print("Class:",class_name,", Class Teacher's name:",teacher_name)
search_class=input("Enter the name of the class to find its class teacher:")
if search_class in class_teachers:
print("The Class Teacher of",search_class,"is",class_teachers[search_class],".")
else:
print("No information found for the class",search_class,".")
students={}
n=int(input("Enter the number of students:"))
for i in range (n):
name=input("Enter the name of the student "+str(i+1)+" :")
percentage=float(input("Enter the percentage of "+name+" :"))
students[name]=percentage
print("Original dictionary of students and their percentages:")
for name,percentage in students.items():
print("Student Name:",name,", Percentage:",percentage)
student_to_delete=input("Enter the name of the student to delete:")
if student_to_delete in students:
del students[student_to_delete]
print(student_to_delete,"has been deleted.")
else:
print("No student found with the name",student_to_delete)
print("The dictionary after deletion:")
for name,percentage in students.items():
print("Student Name:",name,", Percentage:",percentage)
input_string=input("Enter a string:")
sum_of_digits=0
for char in input_string:
if char.isdigit():
sum_of_digits+=int(char)
print("Sum of digits in the string:",sum_of_digits)
list1=[int(x) for x in input("Enter elements for the first list separated by spaces:").split()]
list2=[int(x) for x in input("Enter elements for the second list separated by spaces:").split()]
print("The first entered list is:",list1)
print("The second entered list is:",list2)
list2.extend(list1)
print("Second list after appending:",list2)
First and last 5 elements: [1, 4, 9, 16, 25, 676, 729, 784, 841, 900]
# Ques-22: Write a Python program to get unique values from a list.
import pickle
students=[]
n=int(input("Enter the number of students: "))
for _ in range(n):
name = input("Enter student's name: ")
roll_no = int(input("Enter roll number: "))
marks = float(input("Enter marks: "))
students.append({'name': name, 'roll_no': roll_no, 'marks': marks})
with open('students.dat', 'wb') as file:
pickle.dump(students, file)
def search_student(roll_no):
with open('students.dat', 'rb') as file:
students = pickle.load(file)
for student in students:
if student['roll_no'] == roll_no:
return student
return None
roll_no=int(input("Enter roll number to search: "))
student=search_student(roll_no)
if student:
print(f"Student Found: Name = {student['name']}, Marks = {student['marks']}")
else:
print("Student not found!")
import pickle
students=[]
n=int(input("Enter the number of students: "))
for _ in range(n):
name = input("Enter student's name: ")
roll_no = int(input("Enter roll number: "))
marks = float(input("Enter marks: "))
students.append({'name': name, 'roll_no': roll_no, 'marks': marks})
with open('students.dat', 'wb') as file:
pickle.dump(students, file)
for student in students:
student['marks']+=5
with open('students.dat','wb') as file:
pickle.dump(students,file)
print("Marks of all students have been increased by 5.")
for student in students:
print(f"Name:{student['name']},Roll No:{student['roll_no']},Updated Marks:{student['marks']}")
Employee Details:
ID: 001, Name: Dhruv, Department: Tech, Salary: 5000000.0
ID: 002, Name: Saksham, Department: Peon, Salary: 8000.0
# Ques-29: Create code to ask the user to enter department name then count total number of
# employees and total salary paid to a department from the above data file employee.
import mysql.connector
connection=mysql.connector.connect(
host="localhost",
user="root",
password="123",
database="school"
)
book_id=input("Enter Book ID:")
book_name=input("Enter Book Name:")
author=input("Enter Author Name:")
booked=input("Enter Booked status (1 for Yes, 0 for No):")
cursor=connection.cursor()
cursor.execute(
"INSERT INTO library (book_id, book_name, author, booked) VALUES (%s, %s, %s, %s)",
(book_id, book_name, author, booked)
)
connection.commit()
print("Record added successfully.")
connection.close()
print("Connection closed.")
import mysql.connector
connection=mysql.connector.connect(
host="localhost",
user="root",
password="123",
database="school"
)
book_id=input("Enter the Book ID to update:")
new_status=input("Enter new status (1 for booked, 0 for available):")
cursor=connection.cursor()
cursor.execute(
"UPDATE library SET booked = %s WHERE book_id=%s",
(new_status, book_id)
)
connection.commit()
print("Book status updated successfully.")
connection.close()
print("Connection closed.")
import mysql.connector
connection=mysql.connector.connect(
host="localhost",
user="root",
password="123",
database="school"
)
book_id=input("Enter the Book ID to delete:")
cursor=connection.cursor()
cursor.execute(
"DELETE FROM library WHERE book_id=%s",
(book_id,)
)
connection.commit()
print("Book record deleted successfully.")
connection.close()
print("Connection closed.")
import mysql.connector
connection = mysql.connector.connect(
host="localhost",
user="your_username",
password="your_password",
database="school"
)
cursor=connection.cursor()
cursor.execute("SELECT * FROM library")
records=cursor.fetchall()
print("Book Details:")
for row in records:
print(row)
connection.close()
print("Connection closed.")
Book Details:
(101, 'Python Programming', 'John Doe', Booked)
(102, 'Data Science Basics', 'Jane Smith', Unbooked)
(103, 'Machine Learning', 'Alice Brown', Booked)
(104, 'Deep Learning', 'Bob Clark', Unbooked)
Connection closed.
# Ques-35: Create MySql connectivity program to search the details of a book from the library
# table of database school on the basis of Book_Id.
import mysql.connector
connection=mysql.connector.connect(
host="localhost",
user="your_username",
password="your_password",
database="school"
)
book_id=input("Enter the Book ID to search:")
cursor=connection.cursor()
cursor.execute("SELECT * FROM library WHERE book_id = %s",(book_id,))
record=cursor.fetchone()
if record:
print("Book Details:")
print(f"Book ID: {record[0]}")
print(f"Book Name: {record[1]}")
print(f"Author: {record[2]}")
print(f"Booked Status: {'Booked' if record[3] == 1 else 'Available'}")
else:
print("No book found with that Book ID.")
connection.close()
print("Connection closed.")