Python Journal Grade 12
Python Journal Grade 12
[Document title]
[Document subtitle]
srinivasan rajamanickam
1
iii. Exit
Modules used: nil
Data structures used: integer , string
Code:
Output:
Program 2
Aim: Write a program to input two integer numbers in the main program.
Write a function power() which finds the power without using pow()
function nor **. Print the answer in the main program.
Module used: nil
2
Data structure used: integer , string
Code:
Output:
Program 3
Aim: Write a program to input a number in the main program and write a
function fact() which finds the factorial of the number and prints the
answer in the main program.
Module used: nil
3
Data structure used: integer , string
Code:
Output:
Program 4
Aim: Write a function series() which accepts two values (x and n as
arguments) and finds the sum of the series 1 + x + x 2+ x3 +……+ xn and
prints the answer in the main program
Module used: nil
4
Data structure used: integer , string
Code:
Output:
Program 5
Aim: Write a program to input two numbers in the main program and write
a function find big() which returns the bigger number.
Module used: nil
Data structure used: integer , string
Code:
5
Output:
Program 6
Aim: Write a function sum() which accepts a variable length argument and
finds the sum of the numbers
Module used: nil
Data structure used: integer , string
Code:
6
Output:
Program 7
Aim: Write a program to find the sum of the series in a function series()
7
Output:
Program 8
Aim: Write a function count() which accepts a variable length argument
and counts the number of odd and even numbers and returns both the
values.
Module used: nil
Data structure used: integer , strings
Code:
8
Output:
Program 9
Aim: Write a function search2() to accept a list and a number to be
searched as arguments and search for the number using binary search.
Return location if item is found or else return n.
Module used: nil
Data structure used: integer , string
Code:
9
Output:
Program 10
Aim: Write a function swap2() to convert list a=[1,2,3,4,5,6] to
a=[2,1,4,3,6,5].
Module used: nil
Data structure used: integer , string , list
Code:
10
Output:
Program 11
Aim: Write a function move() to accept a list of numbers as arguments and
put all numbers divisible by 5 to the right.
Module used: nil
Data Structure : integer , string , list
Code:
11
Output:
TEXT FILE
Program 1
Aim: write a program to create a text file containing ten phishing emails
(or any text file) and find most commonly occurring word(s).
Module used: nil
Data Structure: list, string, integer
12
Code:
Output:
Program 2
Aim: Write a menu driven program to perform / manipulate a text file
called as ‘POETIC.TXT’ containing ‘n’ no. of lines , Using the following user
defined functions .(The text file is created using the create() user defined
function .
(1) CREATE()
(2) DISPLAY() : to display the complete content of the file
(3) COUNTCHAR() :read the text file and display the number of vowels/
consonants/ uppercase/ lowercase characters in the file.
13
(4) HASHSHOW():read the text file “POETIC.TXT “line by line and display
each word separated by a #.
(5) COPY() : to copy all those lines which contains ‘#’ to another file called
“special.txt” , that is ,special.txt should contain only lines that contain ‘#’
as a character
(6) REPLACE() : write a function called replace () to replace a word with
another user given word into another file called ‘duplicate.txt”.display
both files.
(7) DELETE( ): write a function called deleteword() , which deletes a given
word in text file “POETIC.TXT” ( hint : you need two files & add a space
after each word)
(8)COUNTEND() :write a function called countend (), which counts the no.
Of lines ending with the character ‘y’ or ‘i’. (do not take into consideration
the case of the character )
(9) VOWEL() :write a function called vowel() , which copies all words that
starts with a vowel to another file called vowel.txt, display both the file
content.
(10)CHANGE() :write a function called change() , that replaces every
space with “**” and display both files .
Module used: nil
Data Structure: integer, string, list
Code:
14
15
16
Output:
17
18
Program 3
Aim: Read a text file line by line and display each word separated by a #.
Module used: nil
Data structure: list, string
Code:
Output:
Program 4
19
Aim: Read a text file and display the number of
vowels/consonants/uppercase/lowercase characters in the file.
Module used: nil
Data structure: integer, string
Code:
Output:
Program 5
20
Aim: Remove all the lines that contain the character 'a' in a file and write
it to another file.
Module used: nil
Data structure: list, string
Code:
Output:
Binary File
21
Program 1
Aim: Write the following functions:
a) create() to create a binary file named as employee.dat that contains
the details employee number, employee name, designation, salary in the
dictionary form.
b) search() for an employee detail by taking employee number as a key
field in the file employee.dat. If employee is not found, give appropriate
messages, otherwise print the details and give bonus of 2000 to those
whose salary is less than 20000,make the changes in file.
Module used: pickle
Data structure: integer, string, dictionary
Code:
Output:
22
Program 2
Aim: Write a function to insert more data at the end of the file
employee.dat
Module used: pickle
Data structure: integer, string, dictionary
Code:
Output:
23
Program 3
Aim: Write a program to split the file employee.dat into two where one file
named as manager.dat has employee details where designation is
Manager(user defined function) and other file named as accountant.dat
which contains only the details of accountant
Module used: pickle
Data structure: integer, string, dictionary
Code:
Output:
24
Program 4
Aim: Write a menu driven program in python, using functions to perform
the following:
1)CREATE() : to create a file name ‘student.dat’ containing n records
having the following details:
a) roll number
b) name
3) marks
4) house
Write all these details for every record as a list into the file
2)DISPLAY(): to display the contents of the file
3)SEARCHNAME(): to search and display the contents of a given name,
name is to be given by the user, if name not present provide a
appropriate message.
4)SEARCHID(): to search and display the contents of a given roll number ,
roll number is to be given by the user, if not present provide a appropriate
message.
5) APPEND() : to add additional n records into the file ‘student.dat’.
6)COUNT() : to count the total number of records in the file ‘student.dat’
and also print the average of all the students.
7) HIGHEST(): to copy the records of those students whose mark is more
than 90 into the file named ‘high.dat’
8)MODIFY() : to modify the marks of those students whose marks are less
than 23 by adding 10 marks to them.
9)DELETE() : to delete all the student whose house is ‘emerald’.
10)DELETEROLL(): to delete a student record with a given roll number,
give appropriate message if student record not present
Module used: pickle, os
Data structure: integer, string, list.
Code:
25
26
Output:
27
28
29
CSV File
Program 1
Aim: Code in python , using functions to perform the following:
1)CREATE(): o create a to.csv file containing records having the following
details: [name , price, category, stock]
2)DISPLAY(): display the details of the file
3)SEARCH(): to search and display details of a toy whose name is given by
the user, display appropriate message if record not found
4)APPEND(): to add more records of toys
5)HIGHEST(): to copy the records of all the toys whose price is greater
than 100 into another file named highest.csv
6)MODIFY(): to modify the stock of those toys whose stock is less than 10,
by adding 10 to their existing stock
7)DELETE(): to delete all the toys where category is fun
Module used: OS, CSV
Data structure: integer, string, list,
Code:
30
Output:
31
32
Program 2
Aim: Code in python by a menu driven program, using functions to
perform the following functions
1) CREATE(): create a 'student.csv' file containing N records having the
following details: [name, english mark, math mark, physics mark,
chemistry mark,cs mark]
2)DISPLAY(): to display the contents of the file
3)SEARCH(): to search and display for a given name as inpute given by the
user, give appropriate message if name not found
4)APPEND(): to add additional n records into the file
5)FAILURE(): to copy the names of those students who failed even in one
subject into another file called fail.txt
6)APPEND(): to modify marks of cs of those students who have scored less
than 50 by adding 10 marks to their marks
7)DELETE(): to delete the records of all the students whose average is less
than 40 %
Module used: CSV, OS
Data structure: integer, string, list
Code:
33
34
Output:
35
STACKS
36
Program 1
Aim: write a menu driven program to implement a stack named as ‘train’,
where each element of the stack contains (train_no, train_name). Using
functions, implement all the operations permissible on a stack —push(),
pop(), display() and peek().
Module used: nil
Data structure: list, string, integer
Code:
Output:
37
Program 2
Aim: Sanjana has created a dictionary containing ProdName and Price as
the key-value pair of 8 products. Write a program with separate user-
defined functions to perform the following operations:-
• Push the keys (ProdName) of the dictionary into a stack, where
corresponding Price range is 5000-25000 (inclusive of both values).
• Pop and display the contents of the stack.
Module used: nil
Data structure: dictionary, list, integer, string
Code:
Output:
38
Program 3
Aim: Samantha has created a dictionary containing ID and their Names as
the key-value pairs of 8 students. Write a program with separate user-
defined functions to perform the following operations:-
• Push the keys (ID of the students) of the dictionary into a stack, where
corresponding name begins from letter ‘A’.
• Pop and display the contents of the stack.
Module used: nil
Data structure: dictionary, list, string, integer
Code:
Output:
39
Program 4
Aim: Alam has a list containing 10 integers. You need to help him to create
a program with separate user defined functions to perform the following
operations based on this list.
i. Traverse the content of the list and push the even numbers into a stack.
ii. Pop and display the content of the stack.
Module used: nil
Data structure: list, string, integer
Code:
Output:
40
MYSQL
41
Program 1
Aim: Write MySQL queries for the questions 1 to 6 and give outputs for
SQL queries 7 to 8, based on Table PASSENGERS shown below:
1. To display the name and seatno where class is II and age is more than
18
2. To display trainno and class where the fare is between 900 and 1300
3. To display the list of passengers in ascending order of age
42
4. To display a column NEWFARE showing new FARE by increasing FARE by
10%
5. Insert a new row in the above table with the details
(9,Ajay,2011,II,19,20,500)
6. Display all records in the table as strings to display NAME is AGE years
old.
7. SELECT TNO, NAME FROM PASSENGERE WHERE FARE<1200 ORDER BY
FARE, NAME DESC;
8. UPDATE PASSENGERS SET FARE=FARE-50 WHERE NAME LIKE ‘_R%A’;
Code:
1. SELECT NAME,SEATNO FROM PASSENGERS WHERE CLASS = ‘II’ AND
AGE > 18;
2. SELECT TRAINNO,CLASS FROM PASSENGERS WHERE FARE IS BETWEEN
900 AND 1300;
3. SELECT NAME FROM PASSENGERS ORDER BY AGE;
4. SELECT 1.1*FARE NEWFARE FROM PASSENGERS;
5. INSERT INTO PASSENGERS VALUES(9,Ajay,2011,II,19,20,500);
6. SELECT NAME,’is’,AGE,’years old’ FROM PASSENGERS;
7.
8.
43
Program 2
Aim: Write MySQL queries for questions 1 to 6, and give the output for
questions 7 and 8 based on the Table SHOP, shown below.
TABLE SHOP
44
1. Show all the names of all shops which are in the area ‘South’ and
c_perc<75
2.To display names of cities that and end with the letter ‘i’ and have the
letter ‘l’ as the 3rd character.
3. To display a list of all the shops with sale > 300000 in descending order
of shopname.
4. To display shopname, area and rating for only those shops whose sale is
between 350000 and 400000 (including both values)
5. To display the shops whose rating is A
6. To display name of the shop whose c_perc is unknown
7. SELECT shopname, city FROM SHOP WHERE CITY IN (‘Mumbai’,’Delhi’);
8. SELECT shopname, ‘is in’, city FROM SHOP WHERE shopname NOT LIKE
‘%t_r%’;
Code:
1. SELECT NAME FROM SHOP WHERE AREA = ‘SOUTH’ AND C_PERC<75;
2. SELECT NAME FROM SHOP WHERE NAME LIKE ‘__I%L’ ;
3. SELECT NAME FROM SHOP WHERE SALE > 300000 ORDER BY NAME
DESC;
4. SELECT NAME,AREA,RATING FROM SHOP WHERE SALE BETWEEN
350000 AND 400000;
5. SELECT NAME FROM SHOP WHERE RATING = ‘A’ ;
6. SELECT NAME FROM SHOP WHERE C_PERC = NULL;
7.
45
8.
Program 3
Aim: Write SQL commands for the following questions:
Table: graduate
46
Table: guide
1.list the names of those students who have obtained div 1 sorted by
name
2. display the report listing name, stipend, subject and amount of stipend
received in a year assuming stipend is paid every month
3. to count the number of students who are either physics or chemistry
graduate
4. to insert a new row
5. to increase stipend by 100 if div is 1
6. select name, advisor from graduate, guide where gradute.subject=
guide.mainstream;
7. select avg(stipend) from graduate where average>=65;
Code:
47
1.
2.
3.
4.
5.
48
6.
7.
Program 4
Aim: Write SQL commands:
Table: stationary
49
Table: consumer
50
1.
2.
3.
51
4.
5.
6.
52
7.
8.
53