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

Assignment Sol

sol

Uploaded by

yuhiwala
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Assignment Sol

sol

Uploaded by

yuhiwala
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

TABLE OF CONTENTS

S.No Name of the Exercise Page.No


Python Programs
1. Creating a menu driven program to perform arithmetic operations. 3
2. Creating a python program to display Fibonacci series 5
3. Creating a menu driven program to find factorial and sum of list of numbers using 7
function.
4. Creating a python program to implement returning value(s) from function. 9
5. Creating a python program to implement mathematical functions. 10
6. Creating a python program to generate random number between 1 to 6 12
7. Creating a python program to read a text file line by line and display each word 14
separated by '#'.
8. Creating a python program to read a text file and display the number of 15
vowels/consonants/lower case/ upper case characters.
9. Creating python program to display short words from a text file 17
10. Creating a python program to copy particular lines of a text file into another text 18
file.
11. Creating a python program to create and search records in binary file. 20
12. Creating a python program to create and update/modify records in binary file. 22
13. Creating a python program to create and search employee’s record in csv file. 24
14. Creating a python program to implement stack operations (list). 26
15. Creating a python program to implement stack operations (dictionary). 30
Python – SQL connectivity programs
16. Creating a python program to integrate mysql with python (creating database 32
and table)
17. Creating a python program to integrate mysql with python (inserting records and 34
displaying records)
18. Creating a python program to integrate mysql with python (searching and 36
displaying records)
19. Creating a python program to integrate mysql with python (updating records) 37
SQL Queries
20. SQL PRACTICAL – 1 39
21. SQL PRACTICAL – 2 40
22. SQL PRACTICAL – 3 42
23. SQL PRACTICAL – 4 44
24. SQL PRACTICAL - 5 46

2
EX.NO: 1
DATE:

CREATING A MENU DRIVEN PROGRAM TO PERFORM ARITHMETIC


OPERATIONS

AIM:

To write a menu driven Python Program to perform Arithmetic operations (+,-*,/)


based on the user’s choice.

SOURCE CODE:

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

3
SAMPLE OUTPUT:

Python Program Executed Output:

***************************************************************************************

4
EX.NO: 2
DATE:
CREATING A PYTHON PROGRAM TO DISPLAY FIBONACCI SERIES

AIM:
To write a Python Program to display Fibonacci Series up to ‘n’ numbers.

SOURCE CODE:

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

5
SAMPLE OUTPUT:
Python Executed Program Output:

*****************************************************************************************

6
EX.NO: 3

DATE:

CREATING A MENU DRIVEN PROGRAM TO FIND FACTORIAL AND SUM OF LIST OF


NUMBERS USING FUNCTION.

AIM:

To write a menu driven Python Program to find Factorial and sum of list of numbers
using function.

SOURCE CODE:

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

7
SAMPLE OUTPUT:

Python Executed Program Output:

************************************************************************************************

8
EX.NO: 4

DATE:

CREATING A PYTHON PROGRAM TO IMPLEMENT RETURNING VALUE(S)


FROM FUNCTION

AIM:

To Write a Python program to define the function Check(no1,no2) that take two numbers and
Returns the number that has minimum ones digit.

Source Code:

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

Sample Output:

**********************************************************************************

9
EX.NO: 5

DATE:

CREATING A PYTHON PROGRAM TO IMPLEMENT MATHEMATICAL FUNCTIONS

AIM:
To write a Python program to implement python mathematical functions to find:
(i) To find Square of a Number.
(ii) To find Log of a Number(i.e. Log10)
(iii) To find Quad of a Number

SOURCE CODE:

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

10
SAMPLE OUTPUT:
Python Executed Program Output:

**************************************************************************************

11
EX.NO: 6

DATE:

CREATING A PYTHON PROGRAM TO GENERATE RANDOM NUMBER


BETWEEN 1 TO 6

AIM:

To write a 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.

12
SAMPLE OUTPUT:
Python Executed Output Program:

**************************************************************************************

13
EX.NO: 7
DATE:

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 a text file "Story.txt" line by line and display
each word separated by '#'.

SOURCE CODE:

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

SAMPLE OUTPUT:
Story.txt:

14
EX.NO: 8

DATE:

CREATING A PYTHON PROGRAM TO READ A TEXT FILE AND DISPLAY THE


NUMBER OF VOWELS/CONSONANTS/LOWER CASE/ UPPER CASE CHARACTERS.
AIM:

To write a Python Program to read a text file "Story.txt" and displays the number of
Vowels/ Consonants/ Lowercase / Uppercase/characters in the file.

SOURCE CODE:

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

15
SAMPLE OUTPUT:
Story.txt:

***************************************************************************************

16
EX.NO: 9
DATE:

CREATING PYTHON PROGRAM TO DISPLAY SHORT WORDS FROM A TEXT FILE

AIM:
To Write a method Disp() in Python, to read the lines from poem.txt and display those
words which are less than 5 characters.

Source Code:

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

Sample Output:

Poem.txt:

Python Executed Program Output:

17
EX.NO: 10

DATE:

CREATING A PYTHON PROGRAM TO COPY PARTICULAR LINES OF A TEXT FILE


INTO AN ANOTHER TEXT FILE

AIM:

To write a python program to read lines from a text file "Sample.txt" and copy those lines
into another file which are starting with an alphabet 'a' or 'A'.

SOURCE CODE:

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

18
SAMPLE OUTPUT:

Python Executed Program output:


Sample.txt:

Python Executed Program Output:

New.txt:

*****************************************************************************************

19
EX.NO: 11
DATE:

CREATING A PYTHON PROGRAM TO CREATE AND SEARCH RECORDS IN


BINARY FILE

AIM:

To write a Python Program to Create a binary file with roll number and name.
Search for a given roll number and display the name, if not found display appropriate
message.

SOURCE CODE:

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

20
SAMPLE OUPUT:

PYTHON PROGRAM EXECUTED OUTPUT:

*******************************************************************************************

21
EX.NO: 12
DATE:

CREATING A PYTHON PROGRAM TO CREATE AND UPDATE/MODIFY RECORDS IN


BINARY FILE
AIM:

To write a Python Program to Create a binary file with roll number, name, mark
and update/modify the mark for a given roll number.

SOURCE CODE:

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

22
SAMPLE OUTPUT:
PYTHON PROGRAM EXECUTED OUTPUT:

**********************************************************************************

23
EX.NO: 13
DATE:

CREATING A PYTHON PROGRAM TO CREATE AND SEARCH EMPLOYEE’S RECORD


IN CSV FILE.

AIM:

To write a Python program Create a CSV file to store Empno, Name, Salary and search
any Empno and display Name, Salary and if not found display appropriate message.
SOURCE CODE:

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

24
SAMPLE OUTPUT:
PYTHON PROGRAM EXECUTED OUTPUT:

******************************************************************************

25
EX.NO: 14
DATE:

CREATING A PYTHON PROGRAM TO IMPLEMENT STACK OPERATIONS(LIST)


AIM:
To write a Python program to implement Stack using a list data-structure, to perform

the following operations:

(i) To Push an object containing Doc_ID and Doc_name of doctors who specialize
in "ENT" to the stack.
(ii) (ii) To Pop the objects from the stack and display them.
(iii)
(iv) (iii) To display the elements of the stack (after performing PUSH or POP)

SOURCE CODE:

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

27
SAMPLE OUTPUT:
Python Program Executed Output:

28
****************************************************************************************

29
EX.NO: 15
DATE:

CREATING A PYTHON PROGRAM TO IMPLEMENT STACK OPERATIONS(Dictionary)

AIM:
To Write a program, with separate user-defined functions to perform the following
operations:
(i) Create a function Push(Stk,D) Where Stack 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 value (marks) is greater than 70.
(ii) Create a Function Pop(Stk) , where Stk is a Stack implemented by a list of student names.
The function returns the value deleted from the stack.
(iii) To display the elements of the stack (after performing PUSH or POP).
Source Code:

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

Sample Output:

*********************************************************************************

31
EX.NO: 16
DATE:
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 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.

32
Sample Output:

*******************************************************************************************************

33
EX.NO: 17
DATE:
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


Emp table and display the records.

SOURCE CODE:

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

34
SAMPLE OUTPUT:

Python Executed Program Output:

***************************************************************************************************************************** *************************************************

35
EX.NO: 18

DATE:
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:

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

SAMPLE OUTPUT:

Python Executed Program Output:

36
EX.NO: 19
DATE:

CREATING A PYTHON PROGRAM TO INTEGRATE MYSQL WITH PYTHON


(UPDATING RECORDS)
AIM:

To write a Python Program to integrate MYSQL with Python to search an 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:

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

37
SAMPLE OUTPUT:

Python Executed Program Output:

**************************************************************************************************************************************************

38
Here are the SQL answers for each question based on the provided `Product` table:

---

1. **Create the `Product` table as shown above.**


```sql
CREATE TABLE Product (
ProductID INT,
ProductName VARCHAR(50),
Category VARCHAR(50),
Price DECIMAL(10, 2),
Stock INT,
SupplierID INT
);

2. **Retrieve the names of all products priced between 200 and 500.**

SELECT ProductName
FROM Product
WHERE Price BETWEEN 200 AND 500;

3. **Display the total `Stock` available for each `Category`.**

SELECT Category, SUM(Stock) AS TotalStock


FROM Product
GROUP BY Category;

4. **Delete products that are out of stock (i.e., `Stock` = 0).**

DELETE FROM Product


WHERE Stock = 0;

5. **Update the `Price` of all `Furniture` items by reducing it by 10%.**


UPDATE Product
SET Price = Price * 0.9
WHERE Category = 'Furniture';
Here are the SQL answers for each question based on the provided `Customer` table:

1. **List all unique cities where customers are located.**

SELECT DISTINCT City


FROM Customer;

2. **Retrieve the `CustomerName` and `LoyaltyPoints` for customers aged between 25 and 40.**

SELECT CustomerName, LoyaltyPoints


FROM Customer
WHERE Age BETWEEN 25 AND 40;

3. **Find the average `LoyaltyPoints` of customers from New York.**


SELECT AVG(LoyaltyPoints) AS AverageLoyaltyPoints
FROM Customer
WHERE City = 'New York';

4. **Update the `LoyaltyPoints` by adding 100 points for all customers aged 30 and above.**
UPDATE Customer
SET LoyaltyPoints = LoyaltyPoints + 100
WHERE Age >= 30;

5. **Delete records of customers with less than 600 `LoyaltyPoints`.**


DELETE FROM Customer
WHERE LoyaltyPoints < 600;
Here are the SQL answers for each question based on the provided `Sales` table:

1. **Display the total sales (`TotalAmount`) for each `ProductID`.**

SELECT ProductID, SUM(TotalAmount) AS TotalSales

FROM Sales

GROUP BY ProductID;

2. **Find the total quantity sold for each month in 2023.**

SELECT EXTRACT(YEAR FROM SaleDate) AS Year, EXTRACT(MONTH FROM SaleDate) AS Month,


SUM(QuantitySold) AS TotalQuantity

FROM Sales

WHERE EXTRACT(YEAR FROM SaleDate) = 2023

GROUP BY Year, Month;

3. **Retrieve sales records where the `QuantitySold` is greater than 2.**

SELECT *

FROM Sales

WHERE QuantitySold > 2;

4. **Delete sales records where `TotalAmount` is less than 500.**

DELETE FROM Sales

WHERE TotalAmount < 500;

5. **Retrieve the maximum `TotalAmount` for each `ProductID`.**

SELECT ProductID, MAX(TotalAmount) AS MaxTotalAmount

FROM Sales

GROUP BY ProductID;
Here are the SQL answers for each question based on the provided `Supplier` table:

1. **List all suppliers located in New York.**

SELECT *

FROM Supplier

WHERE City = 'New York';

2. **Count the number of suppliers from each city.**

SELECT City, COUNT(*) AS SupplierCount

FROM Supplier

GROUP BY City;

3. **Retrieve supplier names that have "Tech" in their `SupplierName`.**

SELECT SupplierName

FROM Supplier

WHERE SupplierName LIKE '%Tech%';

4. **Update the `ContactNumber` for the supplier with `SupplierID` 103 to "555-9999".**

UPDATE Supplier

SET ContactNumber = '555-9999'

5. **Delete suppliers from cities where there are more than one supplier.**

DELETE FROM Supplier

WHERE City IN (

SELECT City

FROM Supplier

GROUP BY City

HAVING COUNT(*) > 1

);
Here are the SQL answers for each question based on the provided `Employee` table:

1. **Add a new column `Experience` (in years) to the `Employee` table.**

ALTER TABLE Employee

ADD COLUMN Experience INT;

2. **Increase the `Salary` by 5% for employees in the `HR` department.**

UPDATE Employee

SET Salary = Salary * 1.05

WHERE Department = 'HR';

3. **Retrieve names of employees who earn more than 60000 and are in the `IT` department.**

SELECT Name

FROM Employee

WHERE Salary > 60000 AND Department = 'IT';

4. **Display the `Department` and the total `Salary` paid to employees in each department.**

SELECT Department, SUM(Salary) AS TotalSalary

FROM Employee

GROUP BY Department

5. **Delete records of employees who joined before `2019`.**

DELETE FROM Employee

WHERE JoinDate < '2019-01-01';

You might also like