0% found this document useful (0 votes)
9 views5 pages

Computer Practical

The document provides a comprehensive set of practical exercises for Class XII students, including Python programming tasks such as checking for perfect numbers, Armstrong numbers, and file handling, as well as SQL queries for managing employee and product data. It includes detailed SQL commands for selecting, updating, and deleting records from tables, as well as examples of how to interact with a MySQL database using Python for data insertion, updating, and fetching. The exercises aim to enhance students' programming and database management skills.
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)
9 views5 pages

Computer Practical

The document provides a comprehensive set of practical exercises for Class XII students, including Python programming tasks such as checking for perfect numbers, Armstrong numbers, and file handling, as well as SQL queries for managing employee and product data. It includes detailed SQL commands for selecting, updating, and deleting records from tables, as well as examples of how to interact with a MySQL database using Python for data insertion, updating, and fetching. The exercises aim to enhance students' programming and database management skills.
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/ 5

COMPUTER PRACTICAL

CLASS – XII
1. Python Programs
a) Write a program to find whether an inputted number is perfect or not.
b) Write a Program to check if the entered number is Armstrong or not.
c) Write a Program to find factorial of the entered number.
d) Write a Program to enter the number of terms and to print the Fibonacci
Series.
e) Write a Program to enter the string and to check if it’s palindrome or not
using loop.
f) Write a Program to enter the numbers in a list using split () and to use
all the functions related to list
g) Write a Program to enter the numbers in a list using split () and to use
all the functions related to list
h) Write a Program to find factorial of entered number using user-defined
module fact().
i) Write a Program to read data from data file and show Data File Handling
related functions utility in python.
j) Write a Program to read data from data file in append mode and use
writeLines function utility in python.
k) Write a Program to read data from data file in read mode and count the
particular word occurrences in given string, number of times in python.
l) Write a Program to read data from data file in read mode and append the
words starting with letter ‘T’ in a given file in python.

2. SQL Queries
i) The employees table has the following structure:
Table: employees
id first_name last_name department salary
1 John Doe HR 50000
2 Jane Smith IT 60000
3 Mike Johnson Finance 55000
4 Emily Davis IT 62000
5 Sarah Wilson HR 52000

a) Select employees from the IT department:


SELECT * FROM employees WHERE department = 'IT';
b) Select employees with a salary greater than 55000:
SELECT * FROM employees WHERE salary > 55000;
c) Update an employee's department:
UPDATE employees
SET department = 'HR'
WHERE first_name = 'Mike' AND last_name = 'Johnson';
d) Find the highest salary:
SELECT MAX(salary) AS highest_salary FROM employees;
ii) The products table has the following structure:
Table: products
product_id product_name category price stock
1 Laptop Electronics 1200 30
2 Smartphone Electronics 800 50
3 Desk Furniture 200 20
4 Chair Furniture 150 25
5 Monitor Electronics 300 40

a) Select only product names and prices:


SELECT product_name, price FROM products;
b) Update stock for a specific product:
UPDATE products
SET stock = stock + 10
WHERE product_id = 2;
c) Delete all products in the Furniture category:
DELETE FROM products WHERE category = 'Furniture';
d) Group products by category and count them:
SELECT category, COUNT(*) AS product_count
FROM products
GROUP BY category;
iii) Table Structures
Table: employees
employee_id first_name last_name department_id salary

1 John Doe 1 50000

2 Jane Smith 2 60000

3 Mike Johnson 1 55000

4 Emily Davis 3 62000

5 Sarah Wilson 2 52000

Table: departments
department_id department_name

1 HR

2 IT

3 Finance
a) Select first and last names of all employees with their department name:
SELECT e.first_name, e.last_name, d.department_name
FROM employees e
JOIN departments d ON e.department_id = d.department_id;
b) Update an employee's salary:
UPDATE employees
SET salary = 63000
WHERE first_name = 'Emily' AND last_name = 'Davis';
c) Delete employees with a salary less than 52000:
DELETE FROM employees WHERE salary < 52000;
d) Find the highest salary per department:
SELECT d.department_name, MAX(e.salary) AS highest_salary
FROM employees e
JOIN departments d ON e.department_id = d.department_id
GROUP BY d.department_name;
3. Python with MYSQL
a. Insert data into mysql database using python
import mysql.connector

# Establishing a connection to the database


db_connection = mysql.connector.connect(
host="localhost",
user="your_username",
password="your_password",
database="your_database"
)

# Creating a cursor object


cursor = db_connection.cursor()

# SQL query for inserting data


insert_query = "INSERT INTO students (id, name, age) VALUES (%s,
%s, %s)"

# Data to insert
data_to_insert = (1, "John Doe", 22)

# Inserting single record


cursor.execute(insert_query, data_to_insert)

# Committing the transaction


db_connection.commit()

print(f"{cursor.rowcount} record inserted.")

# Close cursor and connection


cursor.close()
db_connection.close()
b. Update data into mysql database using python
import mysql.connector

# Establishing a connection to the database


db_connection = mysql.connector.connect(
host="localhost", # MySQL host (usually 'localhost')
user="your_username", # Your MySQL username
password="your_password", # Your MySQL password
database="your_database" # Your MySQL database name
)

# Creating a cursor object


cursor = db_connection.cursor()

# SQL query to update a record


update_query = "UPDATE students SET age = %s WHERE id = %s"

# Data to update (new age for student with id = 1)


new_data = (25, 1) # Update age to 25 where id is 1

# Executing the update query


cursor.execute(update_query, new_data)

# Committing the transaction


db_connection.commit()

# Printing the number of rows affected


print(f"{cursor.rowcount} record(s) updated.")

# Closing the cursor and connection


cursor.close()
db_connection.close()
c. Fetch data into mysql database using python
import mysql.connector

# Establishing a connection to the database


db_connection = mysql.connector.connect(
host="localhost",
user="your_username",
password="your_password",
database="your_database"
)

# Creating a cursor object


cursor = db_connection.cursor()

# SQL query to fetch data


select_query = "SELECT id, name, age FROM students"

# Executing the query


cursor.execute(select_query)

# Fetching all results


results = cursor.fetchall()

# Iterating over the results and printing each row


for row in results:
print(f"ID: {row[0]}, Name: {row[1]}, Age: {row[2]}")

# Closing the cursor and connection


cursor.close()
db_connection.close()

You might also like