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

Python Programs for Data Manipulation

The document outlines various Python programs and SQL queries for tasks such as displaying series terms, calculating areas of shapes, checking for palindromes, reading and writing text files, and manipulating databases. Each program is defined with its purpose, code, and results indicating successful execution. Additionally, it includes SQL commands for creating, altering, and querying tables in a database, all verified successfully.
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)
43 views50 pages

Python Programs for Data Manipulation

The document outlines various Python programs and SQL queries for tasks such as displaying series terms, calculating areas of shapes, checking for palindromes, reading and writing text files, and manipulating databases. Each program is defined with its purpose, code, and results indicating successful execution. Additionally, it includes SQL commands for creating, altering, and querying tables in a database, all verified successfully.
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

1.

TO DISPLAY THE N TERMS OF THE SERIES

PROGRAM DEFINITION: A menu driven Python code to display n terms of following series
using functions
8, 7, 11, 12, 14, 17, 17, 22, ?
3, 12, 27, 48, 75, 108, ?

PROGRAM CODE

RESULT: The program was successfully executed and verified.


OUTPUT:
2. TO FIND THE AREA OF DIFFERENT SHAPES

PROGRAM DEFINITION: A menu driven Python program to calculate and return the
values of following shapes.
 Area of triangle
 Area of a circle
 Area of rectangle

PROGRAM CODE:

RESULT: The program was successfully executed and verified.


OUTPUT:
3. PALINDROME AND OCCURRENCE OF A CHARACTER

PROGRAM DEFINITION: A menu driven program in Python using user defined functions
to take a stringas input and
• Check if it is palindrome
• Count number of occurrences of a given character

PROGRAM CODE:

RESULT: The program was successfully executed and verified.


OUTPUT:
OUTPUT
OUTPUT
6. READING A TEXT FILE LINE BY LINE SEPARATED BY #

PROGRAM DEFINITION: To write a program in Python to read a text file line by line and
display each word separated by #

PROGRAM CODE:

RESULT: The program was successfully executed and verified.

TEXT FILE: [Link]

OUTPUT:
7. COUNT OF VOWELS, CONSONANTS, UPPER CASE, LOWER CASE IN A
TEXT FILE

PROGRAM DEFINITION: To read a text file and display the number of vowels, consonants,
uppercase, lowercase characters in the file.

PROGRAM CODE:

RESULT: The program was successfully executed and verified.

OUTPUT:
8. READING AND WRITING IN ANOTHER TEXT FILE

PROGRAM DEFINITION: To write a program in Python to remove all the lines that contain
the character ‘a’ in a file and write it to another file.

PROGRAM CODE:

RESULT: The program was successfully executed and verified.

TEXT FILE: [Link]

OUTPUT:

TEXT FILE: read_res.txt


9. UPDATING THE MARKS IN BINARY FILE

PROGRAM DEFINITION: To create a binary file with name, roll number and marks in
Python. Input a roll number and update the marks.

PROGRAM CODE:
RESULT: The program was successfully executed and verified.

OUTPUT:
10. WRITING STUDENT DATA IN CSV FILE

PROGRAM DEFINITION: To create a CSV file to store student data – Rollno, Name,
Marks. Obtain data from user and write 5 records into the file.
PROGRAM CODE:

RESULT: The program was successfully executed and verified.

OUTPUT:
CSV FILE: [Link]
11. BINARY FILE – STUDENT DATABASE

PROGRAM DEFINITION: A menu driven program in Python using Pickle library


and Create a binary file with following structure:
o Admission number
o Student name
o Age
 Display the contents of the binary file
 Search a student by admission number given by user

PROGRAM CODE:
RESULT: The program was successfully executed and verified.

OUTPUT:
12. BOOK DATABASE USING STACK

PROGRAM DEFINTION: To write a menu driven program in Python to implement a


stack using a list data structure.
Each node should have
• Book no
• Book name
• Book price

PROGRAM CODE:

RESULT: The program was successfully executed and verified.


OUTPUT:
13. STACK OPERATION USING FUNCTION
PROGRAM DEFINITION: A menu driven Python Program for stack
implementation using function.
PROGRAM CODE:
RESULT: The program was successfully executed and verified.
OUTPUT:
14. STACK OPERATION USING DICTIONARY

PROGRAM DEFINITION: Write a program, with separate user defined functions to


perform the following operations:
o Push the keys (name of the student) of the dictionary into a stack, where the
corresponding value (marks) is greater than 75.
o Pop and display the content of the stack

PROGRAM CODE:

RESULT: The program was successfully executed and verified.

OUTPUT:
15. RANDOM NUMBER GENERATION

PROGRAM DEFINITION: A program that generates random numbers between 1


and 6 (simulates a dice).

PROGRAM CODE:

RESULT: The program was successfully executed and verified.

OUTPUT:
16. CREATING DATABASE AND TABLE

AIM: To create a database, create a table and check the structure of the table.

QUERY:

CHECKING THE EXISTING DATABASE


mysql> show databases;

CREATING A DATABASE
mysql> create database school;

SELECTING THE DATABASE


mysql> use school;

CREATING A TABLE
mysql> create table student(ADMNO int(10),STUD_NAME varchar(40),CLASS int(10),
DOB date,MARKS int(10),ADDRESS varchar(50));

STRUCTURE OF THE TABLE


mysql> desc student;

REMOVING TABLE / RELATION


mysql> drop table student;

REMOVING DATABASE
mysql> drop database school;

RESULT: The above SQL queries were executed and verified successfully.
CHECK FOR EXISTING DATABASES & CREATE NEW DATABASE

CREATING A TABLE & DISPLAY THE STRUCTURE OF THE TABLE

REMOVING TABLE & DATABASE


17. DATA MANIPULATION COMMANDS

AIM: To perform data manipulation in table using insert, select, update, delete commands.

QUERY:

INSERTING THE ROWS IN THE RELATION


mysql> insert into student values(1349,"RAJEEV",12,'1998-12-14',78,'CHENNAI');
mysql> insert into student values(7865,"SANJANA",11,'2000-07-24',86,'DELHI');
mysql> insert into student values(7656,"SWAPNA",10,'2003-02-17',98,'DELHI');
mysql> insert into student values(8793,"RAJA",11,'2002-11-08',75,'BANGLORE');
mysql> insert into student values(4590,"SUSAN",12,'1999-10-30',87,'MUMBAI');

SELECTING THE RECORDS FROM RELATION


mysql> select * from student;

UPDATING RECORD
mysql> update student set stud_name='DEEPAK' where admno=4590;

DELETING A RECORD
mysql> delete from student where admno=7656;

RESULT: The above SQL queries were executed and verified successfully.
INSERTING RECORDS IN A RELATION

SELECTING RECORDS FROM THE TABLE STUDENT

UPDATING & DELETING A RECORD


18. ALTER COMMAND

AIM: To alter the table using add new attributes, modify data type and drop attribute.

QUERY:

ADDING A COLUMN IN A RELATION


Syntax: ALTER TABLE <table_name> ADD <column_name><datatype>(size);
mysql> alter table student ADD section char(5);

ADDING A COLUMN WITH DEFAULT VALUE


Syntax: ALTER TABLE <table_name> ADD <column_name><datatype> default data);
mysql> alter table student ADD state varchar(30) DEFAULT 'CHENNAI';

MODIFYING THE COLUMN DEFINITION


Syntax: ALTER TABLE <table_name> MODIFY <column_name><datatype>;
mysql> alter table student MODIFY section varchar(10);

RENAMING A COLUMN
Syntax: ALTER TABLE <table_name> CHANGE <old_column_name>
<new_column_name> column definition;
mysql> alter table student CHANGE section sec varchar(10);

REMOVING A COLUMN
Syntax: ALTER TABLE <table_name> DROP <column_name;
mysql> alter table student DROP section;

RESULT: The above SQL queries were executed and verified successfully.
ADDING COLUMN/ATTRIBUTE IN A RELATION

MODIFYING THE EXISTING COLUMN DEFINITION

CHANGING THE COLUMN NAME

REMOVING A COLUMN
19. SQL

Consider the table SHOPPE and ACCESSORIES, write the query for (i)
to (iv) and output for (v)

1. To display Name and Price of all the Accessories in descending order of their Price.
mysql> select name,price from accessories order by price desc;
2. To display ID and Sname of all the Shoppe location in “Nehru Place”
mysql> select id,sname from shoppe where area='nehru place';
3. To display Name, Minimum and Maximum Price of each Name from ACCESSORIES
table.
mysql> select name,min(price),max(price) from accessories group by name;
4. To display Name, Price of all Accessories and their respective SName from table
SHOPPE and ACCESSORIES where Price is 5000 or more.
mysql> select name,price,sname from shoppe s, accessories a where [Link]=[Link] and
price>=5000;
5. Select distinct name from accessories where price>5000;
NAME
Mother Board
LCD

RESULT: The above SQL queries were executed and verified successfully.
20. SQL

23. Consider the table ITEMS, write the query for (i) to (iv) and output for (v).

(i) To display all INAME from ITEMS table in descending order of their PRICE.
mysql> select INAME from items order by price desc;

(ii) To display all ITEMS details whose price is in range of 15000 and 25000.
mysql> select * from items where price between 15000 and 25000;

(iii) To display Sum of price and TCode where Qty is more than 50 TCode wise (for
each TCode)
mysql> select sum(price),tcode from items where qty>50 group by tcode;

(iv) To delete all ITEMS from table where CODE is 1003.


mysql> Delete from items where code=1003;

(v) Select Max(Price), Avg(Price) from ITEMS;

OUTPUT:
Max(price) Avg(price)
38000 15940

RESULT: The above SQL queries were executed and verified successfully.
21. SQL

Consider the table EMPLOYEE, write the query for (i) to (iv) and output for (v):

(i) To display all the information of employee whose name begins with M in ascending
order of their DOB.
mysql> select * from employee where name like ‘m%’ order by DOB;

(ii) To display name and DOJ of female employees working in DCODE D01 or D02
mysql> Select name,DOJ from employee where gender =’female’ and dcode in
(‘D01’,’D02’);

(iii) To Display number of employee gender wise.


mysql> Select count(*) from employee group by gender;

(iv) To Display unique DCODE from table Employee.


mysql> Select distinct dcode from employee;

(v) Select Max(DOB), Min(DOB) from EMPLOYEE;

OUTPUT:
Max(DOB) Min(DOB)
1991-09-01 1984-10-19

RESULT: The above SQL queries were executed and verified successfully.
22. SQL

Consider the table TRANSACT, write the query for (i) to (iv) and output for (v)

TABLE: TRANSACT

TRNO ANO AMOUNT TYPE DOT


T001 101 2500 Withdraw 2017-12-21
T002 103 3000 Deposit 2015-06-01
T003 102 2000 Withdraw 2017-05-12
T004 103 1000 Deposit 2017-10-22
T005 102 12000 Deposit 2016-11-06

(i) To display details of all transactions of TYPE Withdraw from TRANSACT table.
mysql> select * from TRANSACT where TYPE=’Withdraw’;

(ii) To display ANO and AMOUNT of all Deposit and Withdrawals done in month of
“May” 2017 from table TRANSACT.
mysql> select ANO, AMOUNT from TRANSACT where DOT like ‘%-05-%’;

(iii) To display first date of transaction (DOT) from table TRANSACT for Account
having ANO as 102.
mysql> select MIN(DOT) from TRANSACT where ANO=102;

(iv) To display ANO, AMOUNT, TYPE of those transactions done in the year 2017
mysql> select ano, amount, type from transact where DOT like ‘2017%’;

(v) SELECT DISTINCT ANO, TYPE FROM TRANSACT;


OUTPUT:
ANO TYPE
101 Withdraw
103 Deposit
102 Withdraw

RESULT: The above SQL queries were executed and verified successfully.
23. SQL

Consider the table PRODUCT, write the query for (i) to (iv) and output for (v)

TABLE: PRODUCT

PCODE PNAME PRICE COLOR MANUFACTURE

S001 Mobile Phones 30000 Silver Samsung


S002 Refrigerator 20000 Cherry LG
S003 TV 45000 Black Samsung
S004 Washing Machine 12000 White LG
S005 Air Conditioner 50000 White voltas

(i) To display PCode, PName of all the product, who manufacture “Samsung”
mysql> select Pcode, Pname from product where manufacture=” Samsung”;

(ii) To display PName, PCode and price of all the products whose price >=23000.
mysql> select Pname, Pcode, Price from Product where price>=23000;

(iii) To display number of product manufacture wise.


mysql> select count (*) from Product group by Manufacture;

(iv) To display information of all the Products whose product color is ‘White’, ‘Silver’.
mysql> select * from Product where color in (‘White’, ‘Silver’);

(v) Select min(price), count ( Distinct Color) from Product;


OUTPUT:
MIN (PRICE) COUNT ( DISTINCT COLOR)
12000 4

RESULT: The above SQL queries were executed and verified successfully.
24. SQL JOINS

Consider the table PRODUCT and BRAND , Write the query for (i) to (iv)

(i) TO display product name and brand name from the tables PRODUCT
and BRAND.
select pname, bname from product p, brand b where [Link]=[Link];

(ii) Display the product name,Uprice,brand name from the corresponding


tables where rating is greater than 5.

Select pname,uprice,bname from product,brand where


[Link]=[Link] and rating>5;

(iii) Display the name, price, and rating of products in descending


order of rating

Select pname, uprice, rating from product order by rating desc;

iv) Display product name,sum of Uprice for each product where the sum
of Uprice must be greater than 300.

Select pname,sum(Uprice) from product group by pname having


sum(Uprice)>300;

RESULT:The above queries were executed and verified successfully.


i)

ii)

iii)

iv)
24. INTERFACE PYTHON WITH MYSQL

PROGRAM DEFINITON: A menu driven program in Python to establish connection


between Python and MySQL. To fetch the records from EMPL table based on the condition
used in the connectivity.

PROGRAM CODE:

RESULT: The above python interface program was implemented and verified successfully.

TABLE: EMPL

OUTPUT:
25. INTERFACE PYTHON WITH MYSQL
PROGRAM DEFINITON: A menu driven program in Python to establish connection
between Python and MySQL. Perform following operations using the connection using the
‘empl’ table created above.
1. Display total number of employees based on different dept
2. Display the maximum salary of employee on all jobs
3. To close the operation.

PROGRAM CODE:

RESULT: The above python interface program was implemented and verified successfully.
TABLE: EMPL

OUTPUT:
26. INTERFACE PYTHON WITH MYSQL - NAVIGATION

PROGRAM DEFINITION: A menu driven program in Python to establish connection


between Python and MySQL. Perform following operations using the connection using the
‘empl’ table created above
a. Display first record
b. Display next record
c. Display previous record
d. Display last record

PROGRAM CODE:

RESULT: The above python interface program was implemented and verified successfully.
TABLE: EMPL

OUTPUT:
27. INTERFACE – PERFORMING INSERTION, UPDATION &
DELETION

PROGRAM DEFINITION: A menu driven program in Python to establish connection


between Python and MySQL. Perform following operations using the connection using the
‘empl’ table created above
a. Insert record
b. Update record
c. Delete Record

PROGRAM CODE:

RESULT: The above python interface program was implemented and verified successfully.
OUTPUT:
TABLE: EMPL

AFTER INSERTING RECORDS

You might also like