0% found this document useful (0 votes)
4 views50 pages

Python File

The document outlines a series of Python programs focused on user-defined functions, file handling, and database operations using SQL. It includes various tasks such as creating functions for arithmetic operations, reading files, and manipulating data in databases. Additionally, it provides examples of SQL commands for creating databases, inserting records, and querying data from tables.

Uploaded by

Rishabh Gupta
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)
4 views50 pages

Python File

The document outlines a series of Python programs focused on user-defined functions, file handling, and database operations using SQL. It includes various tasks such as creating functions for arithmetic operations, reading files, and manipulating data in databases. Additionally, it provides examples of SQL commands for creating databases, inserting records, and querying data from tables.

Uploaded by

Rishabh Gupta
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

Python

Practical
Program
s
1
Menu
driven
Functions

2
Program-1
WAP to make a user defined function to pass a message.
INPUT:

OUTPUT:

3
Program-2
WAP to create a user defined function to perform all arithmetic operators
(menu driven)
INPUT:

4
OUTPUT:

Program-3
WAP to create a user defined to calculate the area of different shapes
(menu driven)
INPUT:

5
OUTPUT:

6
7
User
defined
functions

8
Program-4
WAP to create a user defined function that shows a parameter or
argument.
INPUT:

OUTPUT:

9
Program-5
WAP to create a user defined function to pass a string.
INPUT:

OUTPUT:

10
Program-6
WAP to create a user defined function to pass a list.
INPUT:

OUTPUT:

11
Program-7
WAP to create a user defined function to pass a tuple.
INPUT:

OUTPUT:

12
Program-8
WAP to create a user defined function to pass a dictionary.
INPUT:

OUTPUT:

13
Text
files

14
Program-9
WAP to create a user defined function “Read ()” to read the content of file
‘[Link]’.
INPUT:

OUTPUT:

15
Program-10
WAP to create a user defined function “display_count ()” and read that
text of the file which begins with ‘a’, ‘A’, ‘h’, ‘H.’
INPUT:

OUTPUT:

16
Program-11
WAF “File_long ()” that accepts a file name and report the longest line.
INPUT:

OUTPUT:

17
Program-12
WAF “count_h ()” that displays the number of lines starting with h/H in
the file.
INPUT:

OUTPUT:

18
Program-13
WAP to create user defined function to read the content of file “[Link]”
line by line.
INPUT:

OUTPUT:

19
Program-14
WAP to create a user defined function with name “Remove ()” to remove
all the content from the file.
INPUT:

OUTPUT:

20
Program-15
WAP to remove a file.
INPUT:

OUTPUT:

21
Binary
files

22
Program-16
WAP to create a user defined function to store data and read five records
of employees in a list and store it in a binary file named “[Link]”.
INPUT:

OUTPUT:

23
Program-17
Write a menu-driven program to perform all the basic operations using
dictionary on student binary file such as inserting, reading, updating,
searching and deleting a record.
INPUT:

24
25
OUTPUT:

26
27
28
Csv
files

29
Program-18
WAP in python that defines and calls the following user-defined functions:
a) add (): to accept and add data of an employee to a csv file
“[Link]”. Each record consists of a list with field elements [Fid,
Fname, Fprice] to store furniture id, furniture name, and furniture
price respectively.
b) search (): to display the records of the furniture whose price is more
than 10000.
INPUT:

30
OUTPUT:

31
Program-19
WAP that defines and calls the following user-defined functions
i. add (): To accept and add data to a CSV file [Link]. Each record
consists of a list with field elements as admno, sname and per not
store admission number, student name and percentage marks
respectively.
ii. search (): To display the records of the students whose percentage is
more than 75.
INPUT:

32
OUTPUT:

33
Program-20
WAP to create a stack at run time to add five elements as list. Also remove
the last inserted element from the stack. Then display the stack as per the
principle. Use user-defined functions to solve the concept.
INPUT:

OUTPUT:

34
Structured
Query
Language

TABLE – 1
35
(Employees)

1. How to create a database?


Ans:
mysql> CREATE DATABASE CompanyDB;
Query OK, 1 row affected (0.01 sec)

2. Use a database.
Ans:
mysql> USE CompanyDB;

36
Database changed

3. How to create table?


Ans:
mysql> CREATE TABLE Employees (
-> EmpID INT PRIMARY KEY AUTO_INCREMENT,
-> FirstName VARCHAR(50),
-> LastName VARCHAR(50),
-> Age INT,
-> Salary DECIMAL(10,2),
-> DeptID INT
-> );
Query OK, 0 rows affected (0.04 sec)

mysql> -- Departments table


mysql> CREATE TABLE Departments (
-> DeptID INT PRIMARY KEY AUTO_INCREMENT,
-> DeptName VARCHAR(50),
-> Location VARCHAR(50)
-> );
Query OK, 0 rows affected (0.03 sec)

4. How to describe a database?


Ans:
mysql> DESC Departments;

37
+----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+----------------+
| DeptID | int | NO | PRI | NULL | auto_increment |
| DeptName | varchar(50) | YES | | NULL | |
| Location | varchar(50) | YES | | NULL | |
+----------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

mysql> DESC Employees;


+-----------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------------+------+-----+---------+----------------+
| EmpID | int | NO | PRI | NULL | auto_increment |
| FirstName | varchar(50) | YES | | NULL | |
| LastName | varchar(50) | YES | | NULL | |
| Age | int | YES | | NULL | |
| Salary | decimal(10,2) | YES | | NULL | |
| DeptID | int | YES | | NULL | |
+-----------+---------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)

5. How to insert values


Ans:
mysql> -- Insert into Departments

38
mysql> INSERT INTO Departments (DeptName, Location)
-> VALUES ('HR', 'New Delhi'),
-> ('IT', 'Bengaluru'),
-> ('Finance', 'Haryana');
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0

mysql> -- Insert into Employees


mysql> INSERT INTO Employees (FirstName, LastName, Age, Salary,
DeptID)
-> VALUES ('John', 'Doe', 30, 50000, 1),
-> ('Suresh', 'Gupta', 28, 60000, 2),
-> ('Yash', 'Kumar', 40, 80000, 3),
-> ('Viraaj', 'Chachra', 32, 90000, 4);
Query OK, 4 rows affected (0.01 sec)
Records: 4 Duplicates: 0 Warnings: 0

6. Show tables with records.


Ans:
mysql> SELECT * FROM Employees;
+-------+-----------+----------+------+----------+--------+
| EmpID | FirstName | LastName | Age | Salary | DeptID |
+-------+-----------+----------+------+----------+--------+
| 1 | John | Doe | 30 | 50000.00 | 1|
| 2 | Suresh | Gupta | 28 | 60000.00 | 2|
| 3 | Yash | Kumar | 40 | 80000.00 | 3|

39
| 4 | Viraaj | Chachra | 32 | 90000.00 | 4|
+-------+-----------+----------+------+----------+--------+
4 rows in set (0.00 sec)

mysql> SELECT * FROM Departments;


+--------+----------+-----------+
| DeptID | DeptName | Location |
+--------+----------+-----------+
| 1 | HR | New Delhi |
| 2 | IT | Bengaluru |
| 3 | Finance | Haryana |
+--------+----------+-----------+
3 rows in set (0.00 sec)

7. Show Employees older than 30.


Ans:
mysql> SELECT * FROM Employees WHERE Age > 30;
+-------+-----------+----------+------+----------+--------+
| EmpID | FirstName | LastName | Age | Salary | DeptID |
+-------+-----------+----------+------+----------+--------+
| 3 | Yash | Kumar | 40 | 80000.00 | 3|
| 4 | Viraaj | Chachra | 32 | 90000.00 | 4|
+-------+-----------+----------+------+----------+--------+
2 rows in set (0.00 sec)

8. Show Employees in IT department.

40
Ans:
mysql> SELECT * FROM Employees WHERE DeptID = 2;
+-------+-----------+----------+------+----------+--------+
| EmpID | FirstName | LastName | Age | Salary | DeptID |
+-------+-----------+----------+------+----------+--------+
| 2 | Suresh | Gupta | 28 | 60000.00 | 2|
+-------+-----------+----------+------+----------+--------+
1 row in set (0.00 sec)

9. Add a column.
Ans:
mysql> ALTER TABLE Employees ADD Email VARCHAR(100);
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0

[Link] a column.
Ans:
mysql> ALTER TABLE Employees DROP COLUMN Email;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0

[Link] existing column name.


Ans:
mysql> ALTER TABLE Employees CHANGE Age EmployeeAge INT;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0

41
[Link] column type.
Ans:
mysql> ALTER TABLE Employees MODIFY Salary DECIMAL(12,2);
Query OK, 4 rows affected (0.07 sec)
Records: 4 Duplicates: 0 Warnings: 0

[Link] data in table.


Ans:
mysql> UPDATE Employees
-> SET Salary = 55000
-> WHERE FirstName = 'John' AND LastName = 'Doe';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0

[Link] a record.
Ans:
mysql> DELETE FROM Employees WHERE FirstName = 'Yash' AND
LastName = 'Kumar';
Query OK, 1 row affected (0.01 sec)

[Link] two tables using join query.


Ans:
mysql> -- Inner Join Employees with Departments
mysql> SELECT [Link], [Link], [Link], [Link],
[Link], [Link]
42
-> FROM Employees e
-> JOIN Departments d ON [Link] = [Link];
+-------+-----------+----------+----------+----------+-----------+
| EmpID | FirstName | LastName | Salary | DeptName | Location |
+-------+-----------+----------+----------+----------+-----------+
| 1 | John | Doe | 55000.00 | HR | New Delhi |
| 2 | Suresh | Gupta | 60000.00 | IT | Bengaluru |
+-------+-----------+----------+----------+----------+-----------+
2 rows in set (0.00 sec)

mysql> -- Left Join


mysql> SELECT [Link], [Link], [Link]
-> FROM Employees e
-> LEFT JOIN Departments d ON [Link] = [Link];
+-----------+----------+----------+
| FirstName | LastName | DeptName |
+-----------+----------+----------+
| John | Doe | HR |
| Suresh | Gupta | IT |
| Viraaj | Chachra | NULL |
+-----------+----------+----------+
3 rows in set (0.00 sec)

TABLE –2
(Library)
43
1. How to create a database?
Ans:
mysql> CREATE DATABASE Library;
Query OK, 1 row affected (0.01 sec)

2. Use a database.
Ans:
mysql> USE Library;
Database changed

3. How to create table?


Ans:
mysql> USE Library;

44
Database changed
mysql> -- Create Books table
mysql> CREATE TABLE Books (
-> BookID INTEGER PRIMARY KEY,
-> Title TEXT NOT NULL,
-> Author TEXT,
-> Genre TEXT,
-> Price REAL
-> );
Query OK, 0 rows affected (0.04 sec)

mysql> -- Create Publishers table


mysql> CREATE TABLE Publishers (
-> PublisherID INTEGER PRIMARY KEY,
-> Name TEXT NOT NULL,
-> Country TEXT
-> );
Query OK, 0 rows affected (0.03 sec)

4. How to describe a database?


Ans:
mysql> DESC Books;
+--------+--------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+--------+------+-----+---------+-------+
| BookID | int | NO | PRI | NULL | |

45
| Title | text | NO | | NULL | |
| Author | text | YES | | NULL | |
| Genre | text | YES | | NULL | |
| Price | double | YES | | NULL | |
+--------+--------+------+-----+---------+-------+
5 rows in set (0.01 sec)

mysql> DESC Publishers;


+-------------+------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------+------+-----+---------+-------+
| PublisherID | int | NO | PRI | NULL | |
| Name | text | NO | | NULL | |
| Country | text | YES | | NULL | |
+-------------+------+------+-----+---------+-------+
3 rows in set (0.00 sec)

5. How to insert values


Ans:
mysql> -- Insert into Books
mysql> INSERT INTO Books (BookID, Title, Author, Genre, Price)
VALUES
-> (1, 'The Alchemist', 'Paulo Coelho', 'Fiction', 254),
-> (2, 'Sapiens', 'Yuval Noah Harari', 'History', 459),
-> (3, 'Clean Code', 'Robert C. Martin', 'Programming', 750);
Query OK, 3 rows affected (0.01 sec)

46
Records: 3 Duplicates: 0 Warnings: 0

mysql> -- Insert into Publishers


mysql> INSERT INTO Publishers (PublisherID, Name, Country)
VALUES
-> (1, 'HarperCollins', 'USA'),
-> (2, 'Penguin Random House', 'UK');
Query OK, 2 rows affected (0.01 sec)

6. Show tables with records.


Ans:
mysql> SELECT * FROM Books;
+--------+---------------+-------------------+-------------+-------+
| BookID | Title | Author | Genre | Price |
+--------+---------------+-------------------+-------------+-------+
| 1 | The Alchemist | Paulo Coelho | Fiction | 254 |
| 2 | Sapiens | Yuval Noah Harari | History | 459 |
| 3 | Clean Code | Robert C. Martin | Programming | 750 |
+--------+---------------+-------------------+-------------+-------+
3 rows in set (0.00 sec)

mysql> SELECT * FROM Publishers;


+-------------+----------------------+---------+
| PublisherID | Name | Country |
+-------------+----------------------+---------+

47
| 1 | HarperCollins | USA |
| 2 | Penguin Random House | UK |
+-------------+----------------------+---------+
2 rows in set (0.00 sec)

7. Show books with price is more than 300.


Ans:
mysql> SELECT * FROM Books WHERE Price > 300;
+--------+------------+-------------------+-------------+-------+
| BookID | Title | Author | Genre | Price |
+--------+------------+-------------------+-------------+-------+
| 2 | Sapiens | Yuval Noah Harari | History | 459 |
| 3 | Clean Code | Robert C. Martin | Programming | 750 |
+--------+------------+-------------------+-------------+-------+
2 rows in set (0.00 sec)

8. Show books by a specific author.


Ans:
mysql> SELECT * FROM Books WHERE Author = 'Paulo Coelho';
+--------+---------------+--------------+---------+-------+
| BookID | Title | Author | Genre | Price |
+--------+---------------+--------------+---------+-------+
| 1 | The Alchemist | Paulo Coelho | Fiction | 254 |
+--------+---------------+--------------+---------+-------+
1 row in set (0.00 sec)

48
9. Add a column.
Ans:
mysql> ALTER TABLE Books ADD COLUMN PublisherID
INTEGER;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0

[Link] a column.
Ans:
mysql> ALTER TABLE LibraryBooks DROP COLUMN Genre;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0

[Link] existing table name.


Ans:
mysql> ALTER TABLE Books RENAME TO LibraryBooks;
Query OK, 0 rows affected (0.02 sec)

[Link] column type.


Ans:
mysql> ALTER TABLE LibraryBooks MODIFY COLUMN Price
INTEGER;
Query OK, 2 rows affected (0.07 sec)
Records: 2 Duplicates: 0 Warnings: 0

[Link] data in table.


Ans:
49
mysql> UPDATE LibraryBooks SET PublisherID = 1 WHERE Title =
'The Alchemist';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0

[Link] a record.
Ans:
mysql> DELETE FROM LibraryBooks WHERE Title = 'Sapiens';
Query OK, 1 row affected (0.01 sec)

[Link] two tables using join query.


Ans:
mysql> SELECT
-> [Link],
-> [Link],
-> [Link] AS Publisher,
-> [Link]
-> FROM LibraryBooks
-> JOIN Publishers ON [Link] =
[Link];
+---------------+--------------+---------------+---------+
| Title | Author | Publisher | Country |
+---------------+--------------+---------------+---------+
| The Alchemist | Paulo Coelho | HarperCollins | USA |
+---------------+--------------+---------------+---------+
1 row in set (0.00 sec)

50

You might also like