cspratical
cspratical
cspratical
Teacher’s Signature
try:
with open(file_name, "r") as file:
content = file.read()
for char in content:
if char in vowels:
vowel_count += 1
elif char in consonants:
consonant_count += 1
if char.isupper():
uppercase_count += 1
if char.islower():
lowercase_count += 1
print(f"Vowels: {vowel_count}")
print(f"Consonants: {consonant_count}")
print(f"Uppercase: {uppercase_count}")
print(f"Lowercase: {lowercase_count}")
except FileNotFoundError:
print("File not found. Please check the file name.")
try:
with open(input_file, "r") as infile, open(output_file, "w") as outfile:
for line in infile:
if "a" not in line: # Skip lines containing "a"
outfile.write(line)
except FileNotFoundError:
print("File not found. Please check the file name.")
students = {}
if search_admission_no in students:
print("\nStudent Details:")
for key, value in students[search_admission_no].items():
print(f"{key}: {value}")
else:
print("Admission number not found!")
OUTPUT:
Enter the number of students: 4
Student Details:
Roll Number: 2
Name: AYUSH RAJ
Percentage: 89.4
Program 5:Program to create binary file to store Roll no and
Name, Search any Roll no and display name if
Roll no found otherwise “Roll no not found”
# Main program
file_name = "students.dat"
if found:
with open(filename, "wb") as file:
for record in temp_records:
pickle.dump(record, file)
print("Marks updated successfully.")
else:
print("Roll number not found.")
# Main program
file_name = "students.dat"
if choice == 1:
create_file(file_name)
elif choice == 2:
try:
update_marks(file_name)
except FileNotFoundError:
print("File not found. Please create the file first.")
else:
print("Invalid choice.")
OUTPUT:
Creating file: Updating marks:
1. Create File 1. Create File
2. Update Marks 2. Update Marks
Enter your choice: 1 Enter your choice: 2
Enter the number of students: 4 Enter Roll Number to update marks: 4
Enter Roll Number: 1 Enter new marks for ARPIT KR. MANDAL: 446
Enter Name: PRIYANSHU KUMAR Marks updated successfully.
Enter Marks: 460
Enter Roll Number: 2
Enter Name: AYUSH RAJ
Enter Marks: 447
Enter Roll Number: 3
Enter Name: AYUSH KESHRI
Enter Marks: 447
Enter Roll Number: 4
Enter Name: ARPIT KR. MANDAL
Enter Marks: 447
Data successfully written to the file.
Program 7: Program to generate random number 1-6, simulating
a dice.
def roll_dice():
return random.randint(1, 6)
while True:
print("=" * 55)
print("*********************** Rolling Dice ************************")
print("=" * 55)
dice_value = roll_dice()
print(f"\nYeh aya: {dice_value}", end=" ")
if dice_value == 6:
print("...... Wah bhai wah!!!! 🎉")
else:
print("...... Kya gunda banega re tu!")
while True:
print("\n1. Push\n2. Pop\n3. Peek\n4. Display\n5. Exit")
choice = int(input("Enter your choice: "))
if choice == 1:
element = input("Enter element to push: ")
stack.push(element)
elif choice == 2:
stack.pop()
elif choice == 3:
stack.peek()
elif choice == 4:
stack.display()
elif choice == 5:
print("Exiting program. Goodbye!")
break
else:
print("Invalid choice. Please try again.")
OUTPUT:
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 1
Enter element to push: 10
10 pushed onto stack.
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 1
Enter element to push: 20
20 pushed onto stack.
1. Push 1. Push
2. Pop 2. Pop
3. Peek 3. Peek
4. Display 4. Display
5. Exit 5. Exit
Enter your choice: 1 Enter your choice: 4
Enter element to push: 30 Stack elements (top to bottom):
30 pushed onto stack. 30
20
10
1. Push
2. Pop
3. Peek 1. Push
4. Display 2. Pop
5. Exit 3. Peek
Enter your choice: 1 4. Display
Enter element to push: 40 5. Exit
40 pushed onto stack. Enter your choice: 5
Exiting program. Goodbye!
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 2
40 popped from stack.
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 3
Top of the stack is: 30
Program 9: Create a CSV file by entering user-id and
password, read and search the password for
given user- id.
# Main program
file_name = "user_data.csv"
if choice == 1:
create_csv_file(file_name)
elif choice == 2:
try:
search_password(file_name)
except FileNotFoundError:
print("File not found. Please create the file first.")
else:
print("Invalid choice.")
OUTPUT:
Creating CSV file:
1. Create CSV File
2. Search Password
Enter your choice: 1
Enter the number of users: 2
Enter User ID: ayushkeshri
Enter Password: ayush@keshri
Enter User ID: ayushraj
Enter Password: takliya
CSV file created successfully.
Searching for password:
1. Create CSV File
2. Search Password
Enter your choice: 2
Enter User ID to search: ayushraj
Password for User ID 'ayushraj' is: takliya
Program 10: To write SQL-Queries for the following
Questions based on the given table
e) Write a Query to List all the tables that exists in the current
database.
ANSWER- mysql> SHOW TABLES;
+--------------------+
| Tables_in_students |
+--------------------+
| Info |
+--------------------+
f) Write a Query to insert all the rows of above table into Info table.
ANSWER-
INSERT INTO Info Values(1,’Arun’,’M’,24,’COMPUTER’,’1997-01-10’,120);
INSERT INTO Info Values(2,’Ankit’,’M’,21,’HISTORY’,’1998-03-24’,200));
INSERT INTO Info Values(3,’Anu’,’F’,20,’HINDI’,’1996-12-12’,300);
INSERT INTO Info Values(4,’Bala’,’M’,19,’NULL’,’1999-07-01’,400);
INSERT INTO Info Values(5,’Charan’,’M’,18,’HINDI’,’1997-09-05’,250);
INSERT INTO Info Values(6,’Deepa’,’F’,19,’HISTORY’,’1997-06-27’,300);
INSERT INTO Info Values(7,’Dinesh’,’M’,22,’COMPUTER’,’1997-02-25’,210);
INSERT INTO Info Values(8,’Usha’,’F’,23,’NULL’,’1997-07-31’,200);
g) Write a Query to display all the details of the Employees from the
above table ‘Info’.
ANSWER-
mysql> SELECT * FROM Info;
OUTPUT:
+---------+--------+--------+------+----------+------------+------+
| Roll_no | Name | Gender | Age | Dept | DOA | Fees |
+---------+--------+--------+------+----------+------------+------+
| 1 | Arun | M | 24 | COMPUTER | 1997-01-10 | 120 |
| 2 | Ankit | M | 21 | HISTORY | 1998-03-24 | 200 |
| 3 | Anu | F | 20 | HINDI | 1996-12-12 | 300 |
| 4 | Bala | M | 19 | NULL | 1999-07-01 | 400 |
| 5 | Charan | M | 18 | HINDI | 1997-09-05 | 250 |
| 6 | Deepa | F | 19 | HISTORY | 1997-06-27 | 300 |
| 7 | Dinesh | M | 22 | COMPUTER | 1997-02-25 | 210 |
| 8 | Usha | F | 23 | NULL | 1997-07-31 | 200 |
+---------+--------+--------+------+----------+------------+------+
a) To display the total Unit price of all the products whose Dcode as 102.
ANSWER-
mysql> SELECT SUM(UnitPrice) FROM STOCK GROUP BY Dcode HAVING Dcode=102;
OUTPUT:
+----------------+
| SUM(UnitPrice) |
+----------------+
| 33.00 |
+----------------+
# Connect to MySQL
con = mycon.connect(
host='localhost',
user='root',
password=”admin"
)
cur = con.cursor()
# Menu-driven program
choice = None
while choice != 0:
print("1. ADD RECORD")
print("2. DISPLAY RECORD")
print("0. EXIT")
choice = int(input("Enter Choice: "))
if choice == 1:
# Add employee record
e = int(input("Enter Employee Number: "))
n = input("Enter Name: ")
d = input("Enter Department: ")
s = int(input("Enter Salary: "))
query = "INSERT INTO employee VALUES({}, '{}', '{}', {})".format(e, n, d, s)
cur.execute(query)
con.commit()
print("## Data Saved ##")
elif choice == 2:
# Display employee records
query = "SELECT * FROM employee"
cur.execute(query)
result = cur.fetchall()
print("%10s" % "EMPNO", "%20s" % "NAME", "%15s" % "DEPARTMENT", "%10s" % "SALARY")
for row in result:
print("%10s" % row[0], "%20s" % row[1], "%15s" % row[2], "%10s" % row[3])
elif choice == 0:
# Exit the program
con.close()
print("## Bye!! ##")
else:
print("Invalid Choice! Please try again.")
OUTPUT:
1. ADD RECORD
2. DISPLAY RECORD
0. EXIT
Enter Choice: 1
Enter Employee Number: 1
Enter Name: AYUSH KESHRI
Enter Department: HR
Enter Salary: 50000
## Data Saved ##
1. ADD RECORD
2. DISPLAY RECORD
0. EXIT
Enter Choice: 2
EMPNO NAME DEPARTMENT SALARY
1 AYUSH KESHRI HR 50000
1. ADD RECORD
2. DISPLAY RECORD
0. EXIT
Enter Choice: 0
## Bye!! ##
Program 14: Program to connect with database and search
employee number in table employee and display
record, if empno not found display appropriate
message.
PYTHON SOURCE CODE:
import mysql.connector as mycon
if result:
print("\n## Employee Found ##")
print("EmpNo:", result[0])
print("Name:", result[1])
print("Department:", result[2])
print("Salary:", result[3])
else:
print("\n## Employee Not Found ##")
# Menu-driven program
choice = None
while choice != 0:
print("\n1. SEARCH EMPLOYEE")
print("0. EXIT")
choice = int(input("Enter Choice: "))
if choice == 1:
search_employee()
elif choice == 0:
print("## Bye!! ##")
con.close()
else:
print("Invalid Choice! Please try again.")
OUTPUT:
1. SEARCH EMPLOYEE
0. EXIT
Enter Choice: 1
Enter Employee Number to Search: 1
## Employee Found ##
EmpNo: 1
Name: AYUSH KESHRI
Department: HR
Salary: 50000
1. SEARCH EMPLOYEE
0. EXIT
Enter Choice: 0
## Bye!! ##
Program 15: Perform all the operations (Insert, Update, Delete,
Display) with reference to table ‘students’ through
MySQL-Python connectivity.
def update_record():
roll_no = int(input("Enter Roll Number to Update: "))
new_name = input("Enter New Name: ")
new_age = int(input("Enter New Age: "))
new_class = input("Enter New Class: ")
query = "UPDATE student SET name = %s, age = %s, class = %s WHERE roll_no = %s"
values = (new_name, new_age, new_class, roll_no)
cur.execute(query, values)
con.commit()
if cur.rowcount > 0:
print("## Record Updated Successfully ##")
else:
print("## Record Not Found ##")
def delete_record():
roll_no = int(input("Enter Roll Number to Delete: "))
query = "DELETE FROM student WHERE roll_no = %s"
cur.execute(query, (roll_no,))
con.commit()
if cur.rowcount > 0:
print("## Record Deleted Successfully ##")
else:
print("## Record Not Found ##")
def display_records():
query = "SELECT * FROM student"
cur.execute(query)
results = cur.fetchall()
if results:
print("\n%10s %20s %10s %10s" % ("ROLL_NO", "NAME", "AGE", "CLASS"))
print("-" * 50)
for row in results:
print("%10s %20s %10s %10s" % (row[0], row[1], row[2], row[3]))
else:
print("## No Records Found ##")
# Menu-driven program
choice = None
while choice != 0:
print("\n1. INSERT RECORD")
print("2. UPDATE RECORD")
print("3. DELETE RECORD")
print("4. DISPLAY RECORDS")
print("0. EXIT")
choice = int(input("Enter Choice: "))
if choice == 1:
insert_record()
elif choice == 2:
update_record()
elif choice == 3:
delete_record()
elif choice == 4:
display_records()
elif choice == 0:
print("## Bye!! ##")
con.close()
else:
print("Invalid Choice! Please try again.")
OUTPUT:
1. INSERT RECORD
2. UPDATE RECORD
3. DELETE RECORD
4. DISPLAY RECORDS
0. EXIT
Enter Choice: 1
Enter Roll Number: 1
Enter Name: PRIYANSHU KUMAR
Enter Age: 19
Enter Class: XII
## Record Inserted Successfully ##
1. INSERT RECORD
2. UPDATE RECORD
3. DELETE RECORD
4. DISPLAY RECORDS
0. EXIT
Enter Choice: 1
Enter Roll Number: 2
Enter Name: AYUSH RAJ
Enter Age: 18
Enter Class: XII
## Record Inserted Successfully ##
1. INSERT RECORD
2. UPDATE RECORD
3. DELETE RECORD
4. DISPLAY RECORDS
0. EXIT
Enter Choice: 2
Enter Roll Number to Update: 1
Enter New Name: PRIYANSHU KUMAR
Enter New Age: 18
Enter New Class: XII
## Record Updated Successfully ##
1. INSERT RECORD
2. UPDATE RECORD
3. DELETE RECORD
4. DISPLAY RECORDS
0. EXIT
Enter Choice: 3
Enter Roll Number to Delete: 2
## Record Deleted Successfully ##
1. INSERT RECORD
2. UPDATE RECORD
3. DELETE RECORD
4. DISPLAY RECORDS
0. EXIT
Enter Choice: 4
1. INSERT RECORD
2. UPDATE RECORD
3. DELETE RECORD
4. DISPLAY RECORDS
0. EXIT
Enter Choice: 0
## Bye!! ##