0% found this document useful (0 votes)
2 views

Document (1)

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

Document (1)

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

SHRI DRONACHARYA RAMESH

CHAND VIDYAWATI CONVENT

SCHOOL
Computer Science Practical File
2024-2025

Submitted by:
Name – Khushi Nagar
Class – XII-A
CBSE Roll Number –

Under the guidance of:


PRAMOD SHARMA,PGT (CS)
Certificate

This is to certify that Khushi Nagar class:


XII-A of SHRI DRONACHARYA RAMESH
CHAND VIDYAWATI CONVENT SCHOOL
has done this Practical File under my
supervision .She has taken interest and
shown at most sincerity in completion of
this Practical File.
I certify this Practical File up to my
expectations and as per guidelines
issued by CBSE,NEW DELHI.

Internal Examiner External


Examiner

Principal
ACKNOWLEDGMENTS
It is with pleasure that I acknowledge my
sincere gratitude to our teacher ,Mr PRAMOD
SHARMA,PGT (CS) who taught and undertook
the responsibility of teaching the subject
computer science . I have been greatly
benefited from his classes.
I am especially indebted to our principal MRS
GARGI GHOSH KANSABANIK who has always
been a source of encouragement and support
and without whose inspiration this project
would not have been a successful l would like
to place on record heartfelt thanks to her.
Finally , I would like to express my sincere
appreciation for all the other students for my
batch their friendship and the fine time that
we all shared together.

INDEX
Python programs
1. Creating a python program display fibonacci series ?
2. Creating a menu drive and program to find factorial and sum list of
numbers using function?

3.Creating a python program to generate random numbers between 1 to


6?

4. Creating a python program to read a text file line by line and display
each word separated by ‘#’?

5. Read a text file and display the number of vowels/consonants/lower


case/uppercase characters and other than characters and digit in the file?

6. Creating python program to copy particular lines of a text file into


another text file?

7. Creating a python program to implement stack operations (Dictionary)?

Python SQL connectivity programs

8. Creating a python program to integrate MYSQL with Python (Creating


database and table)?

9. Creating a python program to integrate MYSQL with Python (Inserting


records and displaying records)?

10.Creating a python program to integrate MYSQL with Python (Searching


and Displaying records)?

11.Creating a python program to integrate MYSQL with Python (Updating


records)?

SQL QUERIES
12. SQL COMMANDS EXERCISE – 1

13. SQL COMMANDS EXERCISE – 2

14. SQL COMMANDS EXERCISE – 3

15. SQL COMMANDS EXERCISE – 4

16.SQL COMMANDS EXERCISE – 5

Exp-1

Creating a Python program display fibonacci series?

Aim:

To write Python program to display fibonacci series up to ‘n’ numbers?

Source code:

Def fibonacci(n):
Fib_series = [0, 1]

While len(fib_series) < n:

Fib_series.append(fib_series[-1] + fib_series[-2])

Return fib_series

N = int(input(“Enter the number of terms: “))

Print(“Fibonacci Series up to”, n, “terms:”)

Print(fibonacci(n))

Result:

Thus, the above Python Program has been executed and the output is
verified successfully.

Sample output:

Python Program executed output:

Exp-2:

Creating a menu drive and program to find factorial and sum list of
numbers using function?

Aim:

To write Python program to define the function check (no1,no2) that take two
numbers and returns the number that has minimum ones digit.

Source code:

Def check(no1, no2):

“””Return the number with the minimum ones digit”””

Return min(no1, no2, key=lambda x: x % 10)


Def main():

No1 = int(input(“Enter the first number: “))

No2 = int(input(“Enter the second number: “))

Print(“Number with minimum ones digit:”, check(no1, no2))

If __name__ == “__main__”:

Main()

Result:

Thus, the above Python program has been executed and the output is
verified successfully.

Sample output:

Exp–3

Creating a python program to generate random numbers between 1


to 6?

Aim:

To write Python program to generate random number between 1 to 6 to


simulate the dice.

Source code:
Result:

Thus, the above Python program has been executed and the output is
verified successfully.

Sample output:

Python executed program output:

Exp-4:

Creating a python program to read a text file line by line and display
each word separated by ‘#’?

Aim:

To write a python program to read text file “Story.tx.t” line by line and display
each word separated by ‘#’.

Source code:

Def read_file(filename):

“””Read a text file line by line and display each word separated by ‘#’”””

Try:

With open(filename, ‘r’) as file:

For line in file:

Words = line.split()

Print(‘#’.join(words))
Except FileNotFoundError:

Print(f”File ‘{filename}’ not found.”)

Def main():

Filename = “Story.txt”

Print(f”Reading file ‘{filename}’...”)

Read_file(filename)

If __name__ == “__main__”:

Main()

Result:

Thus the above Python program has been executed and the output is verified
successfully.

Sample Output:

Python executed program output:


Exp-5:

Read a text file and display the number of vowels/consonants/lower


case/uppercase characters and other than characters and digit in
the file?

Aim:

To write a method to display in python, to read the lines from poem.txt and
display those words which are less than 5 characters.

Source code:

Def read_short_words(filename):

“””Read a text file and display words with less than 5 characters”””

Try:

With open(filename, ‘r’) as file:

For line in file:

Words = line.split()

For word in words:

If len(word) < 5:

Print(word)

Except File Not Found Error:

Print(f”File ‘{filename}’ not found.”)

Def main():
Filename = “poem.txt”

Print(f”Reading file ‘{filename}’...”)

Read_short_words(filename)

If __name__ == “__main__”:

Main()

Result:

Thus , the above Python program has been executed and the output is
verified successfully.

Sample output:

Poem.txt:

Python executed program output:

The following words are lesser than 5 character rays of on the sky.
Exp-6:

Creating python program to copy particular lines of a text file into


another text file?

Aim:

To write a python program to read line from text file ‘Sample.txt’ and copy
those lines into another file which are starting with alphabets ‘a’ and ‘A’.

Source code:

Def copy_lines(filename):

“””Read a text file and copy lines starting with ‘a’ or ‘A’ to another file”””

Try:

With open(filename, ‘r’) as file:

Lines = file.readlines()

With open(‘output.txt’, ‘w’) as output_file:

For line in lines:

If line.strip().lower().startswith(‘a’):

Output_file.write(line)

Except FileNotFoundError:

Print(f”File ‘{filename}’ not found.”)

Def main():

Filename = “Sample.txt”

Print(f”Reading file ‘{filename}’...”)

Copy_lines(filename)

Print(“Lines copied to output.txt”)

If __name__ == “__main__”:

Main()

Result:

This , the above python program has been executed and the output is
verified successfully.
Sample output:

Python executed program output:

Sample.txt

Exp-7:

Creating a Python program to implement stack operations


(Dictionary)

Aim:

To write a program with separate user defined function to perform the


following operations
(i) To create a function push [Stk.D] Where are stacks is an empty
list and D is dictionary of items from this dictionary. Push the
keys (name of the student) into a stack , where the
corresponding values (marks) is greater than 70.
(ii) To create function pop (Stk) where (Stk) is a stack implemented
by a list of (student names) the function returns the items
deleted from the stack.

(iii) To display the elements of the stack (after performing PUSH or POP).

Source code:

Result:

Thus , the above Python program has been executed and the output is
verified successfully.

Sample output:

Python program executed output:


Exp-8:

Creating a Python program to integrate MYSQL with Python


(Creating database and table).

Aim:

To write a Python program to integrate MYSQL with Python to create a


database and table to store the details of employees .
Source code:

Result:

Thus , the above Python program has been executed and the output is
verified successfully.

Sample output:

Python program executed output:


Exp-9:

Creating a python program to integrate MYSQL with Python


(Inserting records and displaying records)?

Aim:

To write a Python program to integrate MYSQL with Python by inserting


records to Emptable and display the records.

Source code:
Import mysql.connector

From mysql.connector import Error

# MySQL connection parameters

Username = ‘your_username’

Password = ‘your_password’

Host = ‘localhost’

Database = ‘your_database’

# Create a connection to the MySQL database

Def create_connection():

Try:

Connection = mysql.connector.connect(

Host=host,

User=username,

Password=password,

Database=database

Return connection

Except Error as e:

Print(f”Error connecting to database: {e}”)

Return None

# Insert records into the emptable table

Def insert_records(connection):

Try:

Cursor = connection.cursor()
Query = “””

INSERT INTO emptable (name, age, department, salary)

VALUES (%s, %s, %s, %s);

“””

Records = [

(‘John Doe’, 30, ‘Sales’, 50000.00),

(‘Jane Doe’, 25, ‘Marketing’, 60000.00),

(‘Bob Smith’, 40, ‘IT’, 70000.00),

(‘Alice Johnson’, 35, ‘HR’, 80000.00)

Cursor.executemany(query, records)

Connection.commit()

Print(“Records inserted successfully”)

Except Error as e:

Print(f”Error inserting records: {e}”)

# Retrieve all records from the emptable table

Def retrieve_records(connection):

Try:

Cursor = connection.cursor()

Query = “SELECT * FROM emptable;”

Cursor.execute(query)

Rows = cursor.fetchall()

For row in rows:

Print(row)

Except Error as e:

Print(f”Error retrieving records: {e}”)


# Main program

Connection = create_connection()

If connection:

Insert_records(connection)

Retrieve_records(connection)

Connection.close()

Result:

Thus , the above Python program has been executed and output is verified
successfully.

Sample output:

Python programs executed output:


Exp-10:

Creating a python program to integrate MYSQL with Python


(Searching and Displaying records)?

Aim:

To write a Python program to integrate MYSQL with Python to search an


employee using EMPID and display the record if present in already existing
table EMP , if not display the appropriate message.

Source code:

Import mysql.connector

From mysql.connector import Error

# MySQL connection parameters

Username = ‘your_username’

Password = ‘your_password’

Host = ‘localhost’

Database = ‘your_database’

# Create a connection to the MySQL database

Def create_connection():
Try:

Connection = mysql.connector.connect(

Host=host,

User=username,

Password=password,

Database=database

Return connection

Except Error as e:

Print(f”Error connecting to database: {e}”)

Return None

# Search an employee by EMPID

Def search_employee(connection, emp_id):

Try:

Cursor = connection.cursor()

Query = “SELECT * FROM EMP WHERE EMPID = %s;”

Cursor.execute(query, (emp_id,))

Row = cursor.fetchone()

If row:

Print(“Employee found:”)

Print(row)

Else:

Print(“Employee not found”)

Except Error as e:

Print(f”Error searching employee: {e}”)


# Main program

Connection = create_connection()

If connection:

Emp_id = input(“Enter EMPID to search: “)

Search_employee(connection, emp_id)

Connection.close()

Result:

Thus , the above Python program has been executed and the output is
verified successfully.

Sample output:

Python programs executed output:


Exp-11:

Creating a Python program to integrate MYSQL with Python


(Updating records).

Aim:

To write a Python program to integrate MYSQL with Python to search and


Employee using EMPID and update the salary of an employee if present in
already existing table EMP , if not display the appropriate message .

Source code:

Import mysql.connector

From mysql.connector import Error

# MySQL connection parameters

Username = ‘your_username’

Password = ‘your_password’

Host = ‘localhost’

Database = ‘your_database’

# Create a connection to the MySQL database

Def create_connection():

Try:

Connection = mysql.connector.connect(

Host=host,

User=username,

Password=password,

Database=database

Return connection

Except Error as e:
Print(f”Error connecting to database: {e}”)

Return None

# Search an employee by EMPID and update their salary

Def update_employee_salary(connection, emp_id, new_salary):

Try:

Cursor = connection.cursor()

Query = “SELECT * FROM EMP WHERE EMPID = %s;”

Cursor.execute(query, (emp_id,))

Row = cursor.fetchone()

If row:

Query = “UPDATE EMP SET SALARY = %s WHERE EMPID = %s;”

Cursor.execute(query, (new_salary, emp_id))

Connection.commit()

Print(“Employee salary updated successfully”)

Else:

Print(“Employee not found”)

Except Error as e:

Print(f”Error updating employee salary: {e}”)

# Main program

Connection = create_connection()

If connection:

Emp_id = input(“Enter EMPID to update salary: “)

New_salary = float(input(“Enter new salary: “))

Update_employee_salary(connection, emp_id, new_salary)

Connection.close()
Result:

Thus , the above Python program has been executed and the output is
verified successfully.

Sample output:

Python program executed output:


Exp-12:

SQL COMMANDS EXERCISE – 1

Aim:

To write queries for the following questions based on the given table:

(a) Write queries of create a new database in the name of “STUDENTS”.


CREATE DATABASE STUDENTS;
(b) Write a query to open the database “STUDENTS”.
USES STUDENTS;
(c) Write a query to create the above table called “STUDENT”.
CREATE TABLE STUDENTS(ROLL NO INT PRIMARY KEY,
NAME VARCHAR(10), GENDER VARCHAR(3),
AGE INT, DEPT VARCHAR (15),DOA DATE,FEES INT);
(d) Write a query to list all the existing database names.

SHOW DATABASES;

(e) Write a query to list all the tables that exist in the current database.

SHOW TABLES;

OUTPUT:
Exp-13:

SQL COMMANDS EXERCISE – 2

Aim:

To write queries for the following questions based on the given table:

(a) Create a query to insert all the rows of above table into Infotable.

INSERTINTOSTUVALUES(l,’Arun’,’M’,24,’COMPUTER’,’l997-Ol-10’,120);
INSERTINTOSTUVALUES(2,’Ankit’,’M’,2l,’HISTORY’,’l998-O3-24’,2OO);
INSERTINTOSTUVALUES(3,’Anu’,’F’,2O,’HINDI’,’l996-l2-l2’,3OO);
INSERTINTOSTUVALUES(4,’Bala’,’M’,l9,NULL,’l999-O7-Ol’,4OO);
INSERTINTOSTUVALUES(5,’Charan’,’M’,l8,’HINDI’,’l997-O6-27’,25O);
INSERTINTOSTUVALUES(6,’Deepa’,’F’,l9,’HISTORY’,’l997-O6-27’,3OO);

INSERTINTOSTUVALUES(7,’Dinesh’,’M’,22,’COMPUTER’,’l997-O2-25’,2lO);

INSERTINTOSTUVALUES(8,’Usha’,’F’,23,NULL,’l997-O7-3l’,2OO);

(b) Write a query to display all the details of employee from the above
table STUDENTS.
SELECT*FROM STUDENTS;
OUTPUT:
(c) Write a query to display Rollno, Name and Department of the
student from STUDENTS Table.
SELECT ROLLNO,NAME,DEPT FROM STUDENTS;
(d) Write a query to select distinct department from STUDENTS table.
SELECT DISTINCT (DEPT) FROM STUDENTS;
OUTPUT:

(e) To show all information about students of history department.


SELECT*FROM STUDENTS WHERE DEPT=’HISTORY’;
OUTPUT:

Exp-14:

SQL COMMANDS EXERCISES - 3

Aim:

To write queries for the following question based on the given table:

(a) Write a query to list name of female students in Hindi Departments.


SELECT NAME FROM STUDENTS WHERE DEPT=’HINDI’ AND GENDER
‘F’

OUTPUT:

(b) Write a query to list name of the students whose ages are between
18 to 20.
SELECT NAMES FROM STUDENT WHERE AGE BETWEEN 18 AND 20;
OUTPUT:
(c) Write a query to display the name of the student whose name is

starting with ‘A’.


SELECT NAME FROM STUDENT WHERE NAME LIKE ‘A%’.
OUTPUT:
(d) Write a query to list the name of those students whose name have

second alphabet ‘n’ in their names.

SELECT NAME FROM STUDENTS WHERE NAME LIKE ‘N%’.


OUTPUT:

Exp-15:

SQP COMMANDS EXERCISES – 4

Aim:

To write queries for the following question based on the given table:

(a) Write a query to delete the details of roll number is 8.

DELETE FROM STUDENTS WHERE ROLL NUMBER =8;

OUTPUT (AFTER DELETING):

(b) Write a query to change the fees of student to 170 whose roll
number is 1 , if the existing fees is less than 130.
UPDATE STUDENT SET FEES=170 WHERE ROLL NO=1 AND
FEES<130;
(c) Write a query to add a new column Area of type varchar in table

STUDENTS.
ALTER TABLE STUDENT ADD AREA VARCHAR (20);
OUTPUT:

(d) Write a query to display name of all student whose area contains
null.

SELECT NAME FROM STUDENT WHERE AREA IS NULL.

OUTPUT:

(e) Write a query to delete Area column from the table STUDENTS.
ALTER TABLE STUDENTS DROP AREA;
OUTPUT:

(f) Write a query to delete table from database.


DROP TABLE STUDENTS;
OUTPUT:

Exp-16:

SQL COMMANDS EXERCISES – 5

Aim:

To write queries for the following question based on the given table:

(a) To display the average price of all the uniform of Raymond company
from table cost.

SELECT AVG(PRICE) FROM COST WHERE COMPANY=’RAYMOND’;


OUTPUT:
(b) To display details of the all the uniform in the uniform table in
descending order of Stock date.
SELECT * FROM UNIFORM ORDER BY STACK DATE DESC;
OUTPUT:

(c) To display Max price and min price of each company.

SELECT COMPANY,MAX(PRICE),MIN(PRICE) FROM COST GROUP BY


COMPANY;

OUTPUT:

(d) To display the company where the number of uniform sizes more
than 2.
SELECT COMPANY,COUNT(*) FROM COST GROUP BY COMPANY
HAVING COUNT(*)>2;
OUTPUT:

(e) To display the Ucode,Uname,Ucolor,size,Company of tables uniform


and cost.

SELECT UCODE,UNAME,UCOLOUR,SIZE ,COMPANY FROM UNIFORM,UCOSTC


WHEREU. UCODE=C.UCODE;

You might also like