0% found this document useful (0 votes)
7 views38 pages

pytoh

The document outlines practical programming tasks for Class XII students, including reading and manipulating text files, creating binary and CSV files, and performing various database operations using SQL. It also covers Python functions for counting occurrences of words, checking for palindromes, and managing student records in a dictionary. Additionally, it provides steps for connecting Python applications to a MySQL database and executing SQL commands for data manipulation.

Uploaded by

lakshaybagri23
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)
7 views38 pages

pytoh

The document outlines practical programming tasks for Class XII students, including reading and manipulating text files, creating binary and CSV files, and performing various database operations using SQL. It also covers Python functions for counting occurrences of words, checking for palindromes, and managing student records in a dictionary. Additionally, it provides steps for connecting Python applications to a MySQL database and executing SQL commands for data manipulation.

Uploaded by

lakshaybagri23
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
You are on page 1/ 38

Class- XII

Name- Akshit Goel


Roll Number- 17621470
INDEX
PAGE
SNO PRACTICAL QUESTIONS NO
1 READ A TEXT FILE BY LINE AND DISPLAY EACH 3
WORD SEPARATED BY A #.

2 READ A TEXT FILE AND DISPLAY THE NUMBER OF VOWELS /4


CONSONANTS / UPPERCASE / LOWERCASE CHARACTERS
IN A FILE.
3 READ ALL THE LINES THAT CONTAIN THE CHARACTER ‘A’ 5
IN A FILE AND WRITE IT TO ANOTHER FILE
4 CREATE A BINARY FILE WITH NAME AND ROLL NUMBER. 6
SEARCH FOR A GIVEN ROLL NUMBER AND DISPLAY THE
NAME, IF NOT FIND DISPLAY AN APPROPRIATE MESSAGE
5 CREATE A BINARY FILE WITH ROLL NUMBER, NAME AND 8
MARKS. INPUT A ROLL NUMBER AND UPDATE THE
MARKS
6 WRITE A RANDOM NUMBER GENERATOR THAT 10
GENERATES RANDOM NUMBERS
BETWEEN 1 AND 6 (SIMULATES A DICE)
7 CREATE A CSV FILE BY ENTERING USER-ID AND 11
PASSWORD, READ AND SEARCH THE PASSWORD FOR
GIVEN ID
81. WRITE A FUNCTION COUNTMY() IN PYTHON TO READ THE TEXT
12
FILE “DATA.TXT” AND COUNT THE NUMBER OF TIMES
2. “MY” OCCURS IN THE FILE.
9 WRITE A FUNCTION DISPLAYWORDS() IN PYTHON TO 13
READ LINES FROM A TEXT FILE POEM.TXT AND DISPLAY
THOSE WORDS WHICH ARE LESS THAN 4 CHARACTERS
101. WRITE A MENU DRIVEN PROGRAM TO INPUT A 14
2. NUMBER AND DISPLAY
 IF IT IS A PALINDROME OR NOT
 IF IT IS AN ARMSTRING NUMBER
 IF IT IS A PERFECT NUMBER
 IF IT IS PRIME NUMBER
111. WRITE A PROGRAM TO INPUT A STRING AND PRINT IF IT 16
2
2. IS PALINDROME.

12 WRITE A PROGRAM TO STORE STUDENT NAMES AND 17


THEIR PERCENTAGE IN A DICTIONARY AND DELETE A
PARTICULAR STUDENT NAME FROM THE DICTIONARY
AND ALSO DISPLAY THE DICTIONARY AFTER DELETION
13 WRITE A PROGRAM TO DISPLAY ALL RECORDS OF TABLE 25
STUDENT
14 WRITE A PROGRAM TO DISPLAY THE TOTAL NUMBER OF 25
RECORDS IN THE TABLE STUDENT SAVED IN MySQL
15 WRITE A PROGRAM TO DISPLAY THE RECORDS OF THE 25
TABLE STUDENT ONE BY ONE
16 WRITE A PROGRAM TO UPDATE THE AGE OF A 26
PARTICULAR RECORD IN THE TABLE STUDENT
17 SQL QUERIES 27-38

3
1. READ A TEXT FILE BY LINE AND DISPLAY EACH WORD SEPARATED BY A #.

4
2. READ A TEXT FILE AND DISPLAY THE NUMBER OF VOWELS / CONSONANTS / UPPERCASE / LOWERCASE
CHARACTERS IN A FILE.

5
3. READ ALL THE LINES THAT CONTAIN THE CHARACTER ‘A’ IN A FILE AND WRITE IT TO ANOTHER FILE.

6
4. CREATE A BINARY FILE WITH NAME AND ROLL NUMBER. SEARCH FOR A GIVEN ROLL NUMBER AND DISPLAY THE
NAME, IF NOT FIND DISPLAY AN APPROPRIATE MESSAGE.

7
8
5. CREATE A BINARY FILE WITH ROLL NUMBER, NAME AND MARKS. INPUT A ROLL NUMBER AND UPDATE THE MARKS.

9
10
6. WRITE A RANDOM NUMBER GENERATOR THAT GENERATES RANDOM NUMBERS BETWEEN 1 AND 6 (SIMULATES A DICE).

11
7. CREATE A CSV FILE BY ENTERING USER-ID AND PASSWORD, READ AND SEARCH THE PASSWORD FOR GIVEN ID.

12
8. WRITE A FUNCTION COUNTMY() IN PYTHON TO READ THE TEXT FILE “DATA.TXT” AND COUNT THE NUMBER OF TIMES
“MY” OCCURS IN THE FILE.

13
9. WRITE A FUNCTION DISPLAYWORDS() IN PYTHON TO READ LINES FROM A TEXT FILE POEM.TXT AND DISPLAY THOSE
WORDS WHICH ARE LESS THAN 4 CHARACTERS.

14
10. WRITE A MENU DRIVEN PROGRAM TO INPUT A NUMBER AND DISPLAY
 IF IT IS A PALINDROME OR NOT
 IF IT IS AN ARMSTRING NUMBER
 IF IT IS A PERFECT NUMBER
 IF IT IS PRIME NUMBER

15
16
11. WRITE A PROGRAM TO INPUT A STRING AND PRINT IF IT IS PALINDROME.

17
12. WRITE A PROGRAM TO STORE STUDENT NAMES AND THEIR PERCENTAGE IN A DICTIONARY AND DELETE A PARTICULAR
STUDENT NAME FROM THE DICTIONARY AND ALSO DISPLAY THE DICTIONARY AFTER DELETION.

18
19
There are following steps to connect a python application to ourdatabase.

1. Import mysql.connector module.

2. Create the connection object.

3. Create the cursor object.

4. Execute the query.

To create a connection between the MYSQL database and the python


application , the connect() method of mysql.connectormodule is used.

Pass the database details like hostname, username and the databasepassword in the
method call. The method returns the connection object.

EXAMPLE:-

OUTPUT:

20
The cursor object can be defined as an abstraction specified in the Python DB-
API2.0. It facilitates us to have multiple separate workingenvironments through the
same connection to the database . We
can create the cursor object by calling the ‘cursor’ function of theconnection object.

CREATING A DATABASE PROGRAM:


CREATING DATABASE

OUTPUT:

21
PROGRAM: SHOWING DATABASE

OUTPUT:

PROGRAM: CREATING A TABLE

OUTPUT:

22
PROGRAM: TO DISPLAY THE TABLES

OUTPUT:

PROGRAM: ADDING A COLUMN INTO TABLE

OUTPUT:

PROGRAM: INSERTING DATA INTO TABLE

23
OUTPUT:

READ Operation on any database means to fetch some useful


information from the database.

 Fetchone( ) : It fetches one row from the result table set in theform of
tuple or a list. A result set is an object that is returned when a cursor object is
used to query a table. This function shall return onerecord from the result set,
i.e. first time it will return the first record, next time it will return the second
record and so on. If no more record is left in the table, it will return None.

 fetchall( ) : It fetches all the rows in a result set and returns a list of
tuples. If some rows have already been extracted from the result set, then it
retrieves the remaining rows from the result set. Ifno more rows are available,
it returns an empty set.

 fetchmany(size) : It fetches the specified number of rows from the


result set in the form of list of tuples. The default size is 1 ;If there are no more
rows in result set, an empty list() is returned.

 rowcount( ): This is a read-only attribute and returns the number of


rows that were affected by an execute() method andretrieved from the
the cursor

PROGRAM: TO DISPLAY ALL RECORDS

24
1. USING FETCHALL

OUTPUT:

2. USING ROWCOUNT

OUTPUT:

3. USING FETCHONE

OUTPUT:

25
4. USING FETCHMANY

OUTPUT:

PROGRAM: UPDATING RECORDS

OUTPUT:

26
STRUCTURED
QUERY
LANGUAGE

27
Sql(Structured Query Language) is a standard language for accessing and manipulating
database. SQL commands are used to create transform and retrieve information from
Relational Database Management System and also to create interface between user
and database. By , using SQL commands, one can search any data in the database and
perform other functions like create tables, add records,modify data, remove rows, drop
table, etc.

SQL has following features:

 It can retrieve data from database.


 It can insert records in a database.
 It can update records in a database.
 It can create new databases.
 It can create new tables in a database.
 It can create views in a database.

SQL has following advantages:


 Ease of use: It is very easy to learn and use and does not
require high-end professional training to work upon it.
 No coding required: It is non-procedural and a unified
language, i.e., we need not to specify the procedures to
accomplish a task but only need to give a command to perform
the activity.
 Portable: It is compatible with other database programs likeDbase
IV, FoxPro, MS-Access, DB2, MS-SQL Server, etc.
 It is not a case sensitive language.

28
SQL provides different type of statements or commands fordifferent
purposes. The statements are classified into four categories :

 Data Definition Language (DDL).


 Data Manipulation Language (DML).
 Data Control Language (DCL).
 Transaction Control Language.

The DDL part of SQL permits database tables to be created ordeleted. It contains
the necessary statements for creating, manipulating, altering and deleting
tables.

Examples of DDL commands in SQL are:

 CREATE DATABSE: - create a new database.


 USE command: - to select and open an already existing
database.
 CREATE TABLE: - creates a new table.
 ALTER TABLE: - modifies a table.
 DROP TABLE: - deletes a table.

DML consists a set of statements to

 Retrieve data from tables of the database.


 Insert data into tables of the database.
 Delete data from the tables of database.
 Update data among rows/records in the tab les of the
database.
29
DML commands carry out query processing operations and manipulate data in
the database objects. Several DML commandsavailable are:

 SELECT statement: - to extract information from the tables.


 INSERT INTO statement: - to insert new data (record) into a
table.
 UPDATE statement: - to modify or change the data in a table.
 DELETE statement: - to delete data from a table.

Create a database School and create a table Student. Insert data with10 records.

STEP 1: Create database ‘School’.

STEP 2: Create table ‘Student’.

STEP 3: The structure of the table ‘Student’.

30
STEP 4: Inserting the records in table ‘Student’.

STEP 5: Display table with records.

31
WRITE THE COMMANDS TO THE FOLLOWING:

1. Add a new column City with datatype varchar(20).

2. Update marks of Rollno 4 to 85.

3. Display data in descending order of marks.

32
4. Display data in alphabetical order of Name.

5. Delete column City.

6. Display the records having marks greater then equal to 90


gender wise.

33
7. Display max, min, sum, count and average marks from the table
Student.

8. Display Rollno, Name and Marks from the Student table who
got the marks between 80 and 100.

9. Display details of the student whose name ends with ‘a’.

34
` 10. Give output for the following code:

a) Select count (distinct stream) from student;

b) Select max(marks) from student where gender =”M”;

PROGRAM 2: - WORKING ON RELATIONSHIP BETWEEN THE


TABLES

Step1: - Creating a database

Step 2: Use database

Step 3: Create table 1

Step 4: Describe table 1:


35
Step 5: Insert into table 1:

Step 6: Display all records from table 1:

Step 7: Creating table 2

Step 8: Describe table

36
Step 9: Insert into table 2

Step 10: Display all the records of table 2

WRITE THE QUESRIES FOR THE FOLLOWING

1. To display the books of “FIRST PUBL” publisher written by


“Purohit”

2. To display the cost for all the books published for FIRST PUBL.

37
3. To depreciate the price of all books of EPB by 5%.

4. To display BOOK_NAME and price of all the books more


than 3copies of which have been issued.

5. To show total cost of all the books of all type

6. To show the details of the costliest books.

38

You might also like