0% found this document useful (0 votes)
19 views48 pages

Python Practical File for Class 12 Science

Uploaded by

nitinkumar321555
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)
19 views48 pages

Python Practical File for Class 12 Science

Uploaded by

nitinkumar321555
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

Computer – Science

PYTHON PRACTICAL FILE


2024–25
NAME: AYUSH TIWARI
CLASS: 12TH ‘SCIENCE’
ROLL NO: 05
SUBMITTED TO: [Link]
I would like to express my special thanks of
gratitude to my computer teacher Mr. Vinod as
well as our principal Mr. Vipul Tyagi who gave me
the golden opportunity to do this wonderful
program file, which also helped me in doing a lot
of Research and I came to know about so many
new things I am really thankful to them.

Secondly, I would also like to thank my parents


and friends who helped me a lot in finalizing this
program file within the limited time frame.
1. WAP to count the number of digit in a number?
2. WAP to check if a number is a palindrome?
3. WAP to count the number of digit in a number?
4. WAP to find and display the sum of all the values which are ending
with 3 from a list?
5. Write a program to show all prime numbers in the entered range.
6. Write a program to read data from a text file [Link], and display
each word with a number of vowels and consonants.
7. Using “[Link]” file, create a python function to count the
number of words having first character capital
8. Write a program to find no of lines starting with ‘F’ in [Link].
9. Write a Python program to check if a string is a palindrome.
10. Write a Python program to implement binary search.
11. Write a program to creating a tuple and slice it.
12. Write a program to show and count the number of words in a text file
‘[Link]’ which is starting/ended with an word ‘The’, ‘the’, ‘my’,
‘he’, ‘they’.
13. Write a program to show push and pop operation using stack.
14. Write a program to insert list data in CSV File and print it
15. Write a program to show sorting of elements of a list step-by-step.
16. Alternate Python code to retrieve one record at a time from the
relation RESULT
17. Python code to retrieve all the record and display few columns from
the relation RESULT
18. Python code to delete a record / row from the relation RESULT
19. Python code to update a record / row from the relation RESULT
20. Write SQL Commands/output for the following on the basis of the
given table GRADUATE
1. WAP to count the number of digit in a
number?

PROGRAM
output :
2. WAP to check if a number is a
palindrome?

PROGRAM
output :
3. WAP to count the number of digit in a
number?
PROGRAM
output :
4. WAP to find and display the sum of all
the values which are ending with 3 from
a list?
PROGRAM
output :
5. Write
a program to show all
prime numbers in the entered
range.
PROGRAM
output :
6. Write a program to read data from a text
file [Link], and display each word
with a number of vowels and consonants.
PROGRAM
output :
7. Using “[Link]” file, create a
python function to count the number of
words having first character capital
PROGRAM
output :
8. Write a program to find no of lines
starting with „F‟ in [Link].
PROGRAM
output :
9. Write a Python program to check if a
string is a palindrome.
PROGRAM
output :
10. Write a Python program to implement
binary search.
PROGRAM
output :
11. Write a program to creating
a tuple and slice it.
PROGRAM
output :
12. Write a program to show and count
the number of words in a text file
‘[Link]’ which is starting/ended
with an word ‘The’, ‘the’, ‘my’, ‘he’, ‘they’.
PROGRAM
output :
13. Write a program to show push and
pop operation using stack.
PROGRAM
output :
14. Write a program to insert list data in
CSV File and print it
PROGRAM
output :
15. Write a program to show sorting of
elements of a list step-by-step.
PROGRAM
output :
16. Alternate Python code to retrieve
one record at a time
#from the relation RESULT
PROGRAM
Import the package [Link]

import [Link] as SqlCon

MYCon = [Link](host=”localhost”, user=”root”, passwd=”gms”,

database = “AECS”)

if MyCon.is_connected() :

print(“MySQL database is successfully connected”)

else:

print(“Error in connecting to MySQL database”)

#Create cursor instance and execute SQL query

CurObj = [Link]()

[Link](“select * from result”)

DataRow = [Link]() # fetch each record

while DataRow is not None:

print (DataRow)

DataRow = [Link]()

NumRows = [Link]

print(“Number of records retrieved from the table : “, NumRows);

[Link]()
output :
MySQL database is successfully connected

First, all the rows will be displayed

Then,

Number of records retrieved from the table : 10 will be displayed


17. Python code to retrieve all the record
and display few columns
#from the relation RESULT
PROGRAM
Import the package [Link]

import [Link]

conn = [Link](host=”localhost”, user=”root”,


passwd=”gms”,

database = “AECS”)

if conn.is_connected() :

print(“MySQL database is successfully connected”)

else:

print(“Error in connecting to MySQL database”)

#Create cursor instance and execute SQL query

cob = [Link]()

[Link](“select * from result”)

dr = [Link]() #Fetch all the rows from the database

print(“Number of records retrieved from the table : “, [Link]);

#To display all the records

for row in dr:

rno = row[0] #Assign RollNo

nam = row[1] #Assign Name

sub = row[3] #Assign Subject


mark= row[5] #Assign Marks

print (‘%-6d %-15s% %-15s %-6d’%(rno, nam, sub, mark)) #Display

[Link]() #Terminate the connection


output :
MySQL database is successfully connected

First,

Number of records retrieved from the table : 10 will be displayed

Then,

RollNo, Name, Subject and Marks of each student will be displayed


18. Python code to delete a record / row
from the relation RESULT
import [Link] as sqcon
#Function to delete a row from the
relation
PROGRAM
def Delete_Rows(rno):

conn = [Link](host=”localhost”, user=”root”,


passwd=”gms”, database = “AECS”)

if conn.is_connected() :

print(“MySQL database is successfully connected”)

else:

print(“Error in connecting to MySQL database”)

#Create cursor instance and execute SQL query

cob = [Link]()

sq = “delete from result where rno = ‘%d’”

arg = (rno)

try:

[Link](sq%arg)

[Link]() #save the changes in the database

print(“1 row deleted…”)

except:
[Link]() #un-save the changes in the database

finally:

[Link]() #close the connection

[Link]()

#__main__

rn = int(input(‘Enter roll number =’))

Delete_Rows(rn) #call the function


output :
MySQL database is successfully connected

Enter roll number = 1012

1 row deleted…
19. Python code to update a record /
row from the relation RESULT
import [Link] as sqcon
PROGRAM
def Update_Rows(sub):

conn = [Link](host=”localhost”, user=”root”,


passwd=”gms”, database = “AECS”)

if conn.is_connected() :

print(“MySQL database is successfully connected”)

else:

print(“Error in connecting to MySQL database”)

#Create cursor instance and execute SQL query cob = [Link]()

sq = “update result set marks = marks +2 where subject = ‘%s’” arg = (sub)

try:

[Link](sq%arg)

[Link]() #save the changes in the database print(“Marks in


Physics updated…”)

except:

[Link]() #un-save the changes in the database finally:

[Link]() #close the connection [Link]()

# main

st = input(‘Enter Subject : ’) Update_Rows(st) #call the function


output :
MySQL database is successfully connected Enter Subject : Physics

Marks in Physics updated…


20. Write SQL Commands/output for the
following on the basis of the given table
GRADUATE
Write SQL commands.
Table: GRADUATE

S. NAME STIPEND SUBJECT AVERAGE RANK


No.
1 KARAN 400 PHYSICS 68 1
2 RAJ 450 CHEMISTRY 68 1
3 DEEP 300 MATHS 62 2
4 DIVYA 350 CHEMISTRY 63 1
5 GAURAV 500 PHYSICS 70 1
6 MANAV 400 CHEMISTRY 55 2
7 VARUN 250 MATHS 64 1
8 LIZA 450 COMPUTER 68 1
9 PUJA 500 PHYSICS 62 1
10 NISHA 300 COMPUTER 57 2

(i) List the names of those students who have obtained rank 1
sorted by NAME.
(ii) Display a list of all those names whose AVERAGE is
greater than 65.
(iii) Display the names of those students who have opted
COMPUTER as a SUBJECT with an AVERAGE of more
than 60.
(iv) List the names of all the students in alphabetical order.
(v) SELECT * FROM GRADUATE WHERE NAME LIKE
"%I%";
(vi) SELECT DISTINCT RANK FROM GRADUATE;
ANSWER

(i)

SELECT NAME
FROM GRADUATE
WHERE `RANK` = 1
ORDER BY NAME;
+--------+
| NAME |
+--------+
| DIVYA |
| GAURAV |
| KARAN |
| LIZA |
| PUJA |
| RAJ |
| VARUN |
+--------+

(ii)

SELECT NAME
FROM GRADUATE
WHERE AVERAGE > 65;
+--------+
| NAME |
+--------+
| KARAN |
| RAJ |
| GAURAV |
| LIZA |
+--------+

(iii)

SELECT NAME
FROM GRADUATE
WHERE SUBJECT = 'COMPUTER' AND AVERAGE > 60;
+------+
| NAME |
+------+
| LIZA |
+------+

(iv)

SELECT NAME
FROM GRADUATE
ORDER BY NAME;
+--------+
| NAME |
+--------+
| DEEP |
| DIVYA |
| GAURAV |
| KARAN |
| LIZA |
| MANAV |
| NISHA |
| PUJA |
| RAJ |
| VARUN |
+--------+

(v) SELECT * FROM GRADUATE WHERE NAME LIKE "%I%";

+-------+-------+---------+-----------+---------+------+
| [Link]. | name | stipend | subject | average | rank |
+-------+-------+---------+-----------+---------+------+
| 4 | DIVYA | 350 | CHEMISTRY | 63 | 1 |
| 8 | LIZA | 450 | COMPUTER | 68 | 1 |
| 10 | NISHA | 300 | COMPUTER | 57 | 2 |
+-------+-------+---------+-----------+---------+------+

(vi) Since 'RANK' is a reserved keyword in SQL, we encounter an error while running
this query. To avoid such errors, we can enclose the column name 'RANK' in
backticks to treat it as a literal identifier.

The corrected query is :

SELECT DISTINCT `RANK` FROM GRADUATE;

+------+
| RANK |
+------+
| 1 |
| 2 |
+------+

You might also like