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

Practical File of Python Class 12

The document contains programs written in Python to perform various tasks like displaying elements of a list, checking if a number is odd or even, finding sum of numbers ending with 4 in a list, finding sum of even numbers in a list, creating patterns using nested loops, reading and processing text files, implementing stack using list, generating random numbers, working with CSV files, math functions, checking palindromes, finding factorials, connecting to databases, inserting, updating, deleting and searching records in databases, creating and manipulating tables.

Uploaded by

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

Practical File of Python Class 12

The document contains programs written in Python to perform various tasks like displaying elements of a list, checking if a number is odd or even, finding sum of numbers ending with 4 in a list, finding sum of even numbers in a list, creating patterns using nested loops, reading and processing text files, implementing stack using list, generating random numbers, working with CSV files, math functions, checking palindromes, finding factorials, connecting to databases, inserting, updating, deleting and searching records in databases, creating and manipulating tables.

Uploaded by

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

Triveni Memorial Sr.

Secondary School

2022-2023
Computer science with python
Submitted to: Mrs. Sakshi
Submitted by:
Name: Kamal Sethi
Class: 12th Commerce
Roll no.:
Acknowledgment
I would like to express my special thanks of
gratitude to my teacher Mrs. Sakshi, who gave
me the golden opportunity to do this wonderful
practical of computer science with python for
the session 2022-23. I came to know about so
many new things in completing my practicals.
I am really thankful to them.
Secondly , I would also like to thank my
parents and friends who helped me lot in
finalizing this practical within the limited time
frame.

Kamal Sethi
12th commerce
Certificate
This is to certify that the content of the
practical for the session 2022-2023 by KAMAL
SETHI is the bonafide work of his.
Submitted to Mrs. Sakshi for consideration
in the fulfillment of the requirement of CBSE
during the year 2022-2023.

Teacher’s signature:
Program 1:
To display the elements of list twice, if it is a number and
display the element terminated with ‘*’ if it is not a number.
lst = eval(input("Enter a list :-"))

for i in lst :

if i.isdigit() :

print(i*2)

elif i.isalpha() :

print(i+"#")

OUTPUT:
Enter a list :-['kamal']
kamal#
Enter a list :-['23']
2323
Program 2:
To create a function chkOdd() that takes one argument(a
positive integer)and reports if the argument is odd or not.
def chkOdd(x):
if x%2==0:
return "even"
else :
return "Odd"
a = int (input ("Enter the positive number :- "))
print(chkOdd(a))

OUTPUT:
Enter the positive number :- 568
even
Program 3:
Write the definition of the function Fending(SCORES) to find the
sum of numbers ending with 4. The numbers are stored in the
list SCORES
def Fending(SCORES):
Fending=0
for i in l:
if i%10==4:
Fending+=i
return Fending
l=[44,64,69]
s=Fending(l)
print(s)

OUTPUT:
108
Program 4:
Write the definition of a function SumE(L),which should find the sum of
even numbers of the list.For example, if L=[21,22,12,5,8,6,90], output
should be 138

def SumE(L):

S=0

for n in L:

if n%2 == 0:

S+=n

return S

number=[20,22,3,42,29,38]

s=SumE(number)

print(s)

OUTPUT:
122
Program 5:
To create A to E pattern with one character increased in every
next line using nested for loop.
n=5
for i in range(n):
t=65
for j in range(i+1):
print(chr(t),end='')
t+=1
print()

OUTPUT:
A
AB
ABC
ABCD
ABCDE
Program 6:
Read a text file line by line and display each word separatedby a
“#”
f =open("E:\\file.txt",'r')

item=[]

for line in f:

words=line.split()

for i in words:

item.append(i)

print('#'.join(item))

OUTPUT:
hello#the#python#is#working#great

now#its#the#time#to#do#practical#work
Program 7:
Read a text file and display the number of
vowels/consonants/uppercase/lowercase
characters in the file.

f=open("e:\\file.txt","r")

vow=con=up=low=0

a=f.read()

for i in a:

if i.isalpha():

if i in["a","e","i","o","u","A","E","I","O","U"]:

vow+=1

else:

con+=1

if i.isupper():

up+=1
elif i.islower():

low+=1

print("no of vowels",vow)

print("no of consonants",con)
print("no of

uppercase",up)

print("no of

lowercase",low)

f.close()

OUTPUT :

no of vowels 20

no of

consona

nts 38no

of

uppercas

e 0 no of

lowercas

e 58
Program 8:
Remove all the lines that contain the character 'a' ina file and write it
to another file.
f=open("c:\users\documents\character.txt",'r')
file=open("c:\users\documents\character.txt",'w')
l=f.readlines()

for i in l:

if a‟ not in i:
file.write()i
file.close()
f.close()

Output:
List of following input
Number of lists

Here are the

List of following input


Program 9:
Create a binary file with name and roll number.
Search for a given roll number and display the
name,if not found display appropriate message.
import pickle

f=open("E:\\stock.txt","wb+")

a={}

found=False

for i in range(5):

roll=int(input("enter the roll no.:"))

name=input("enter the name")

a["roll"]=roll

a["name"]=name

pickle.dump(a,f)

f.seek(0)

c=int(input("enter the roll no to be searched"))

try:

while True:

b=pickle.load(f)

if b ["roll"]==c:
print(b)

found=True

except EOFError:

if found:

f.close()

else:

print("student details not found")

f.close()

OUTPUT:
enter the roll no.:1

enter the namekamal

enter the roll no.:2

enter the namekrish

enter the roll no.:3

enter the nameaman

enter the roll no.:4

enter the namesethi

enter the roll no.:5

enter the nameajay

enter the roll no to be searched2

{'roll': 2, 'name': 'krish'}


Program 11 :

Write a random number generator that generates random


numbers between 1 and 6 (simulates a dice).

import random

a="y"

while a=="y":

print("random number",random.randint(1,6))

a=input("WOULD YOU LIKE TO CONTINUE y/n")

OUTPUT:
random number 5

WOULD YOU LIKE TO CONTINUE y/n


Program 10:
Create a binary file with roll
number, name and marks. Input a
roll number and update the marks.
OUTPUT:
Program 12:
Write a menu-driven Python program to implement a
stack using list.
stack=["one","two","three"]

stack.append("four")

stack.append("Five")

print("elements in stack ",stack)

print("pop 1",stack.pop())

print("after performing first pop",stack)

print("pop 2" , stack.pop())

print("After performing second pop",stack)

OUTPUT:
elements in stack ['one', 'two', 'three', 'four', 'Five']

pop 1 Five

after performing first pop ['one', 'two', 'three', 'four']

pop 2 four

After performing second pop ['one', 'two', 'three']


Program 13:
Create a CSV file by entering user-id and password,
read andsearch the password for given user-id.

OUTPUT:
Program 14:
Write a program to implement various functions of math
module.
import math

# Example 1: Finding the square root


# Suppose we want to find the square root of 64
sqrtResult = math.sqrt(64)
print("The square root of 64 is : ", sqrtResult)

# Example 2: Finding the floor value


# Suppose we want to find the floor value of 2.7
floorResult = math.floor(2.7)
print("The floor value of 2.7 is : ", floorResult)

# Example 3: Finding the ceil value


# Suppose we want to find the ceil value of 4.5
ceilResult = math.ceil(4.5)
print("The ceil value of 4.5 is : ", ceilResult)

# Example 4: Finding the factorial


# Suppose we want to find the factorial of 6
factorialResult = math.factorial(6)
print("The factorial of 6 is : ", factorialResult)

OUTPUT:
The square root of 64 is : 8.0
The floor value of 2.7 is : 2
The ceil value of 4.5 is : 5
The factorial of 6 is : 720
Program 15:
Write a program to check whether a string is palindrome or
not.

def ispalindrome(s):
return s== s[: :-1]

s=str(input("enter the string:"))


ans=ispalindrome(s)

if ans:
print("YES")
else:

print("NO")

OUTPUT:

enter the string:hello


NO
Program 16:
Write a program to find factorial of a number.
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
num = int(input("Enter a number: "))
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
print("The factorial of", num, "is", factorial(num))

OUTPUT:
Enter a number: 7
The factorial of 7 is 5040
Program 17:
Write a program to connect with database, insert
records into table and display all records.
#connection with database and inserting a value –
#display records
Program 20:
Write a program to delete a record from the table by
importing suitable module.

:
Program 18:
Write a program to search a particular record by
importing suitable module.If not found,display
appropriate message.

OUTPUT:
Program 19:
Write a program to update a particular record. If not
found,display appropriate message.

OUTPUT:
Program 21:
Create a table and insert data into it , describe the table and show distinct
cities from table.
Program 22:
Create a table and do the following commands:
1.Display the record of those persons whose name begins with k.
2. Insert a new column marks.
3. Update the marks from 194 to 200 .
4. Display the names of those student who have scored below 75.
Program 23:
Create two tables and do the following commands:
1. To display the frequency of employees department wise.
2. To display names of employees, sales and qualification who have achieved sales more
than 20000.
3. To change the basic salary of employees whose depid is 103.
4. To delete the bonus column.
5. To find the max sales.
Program 24:
Create two tables and do the following commands:
1. Add a new column co partnership.
2. Show the records of those employees who have “h” in their name.
3. Arrange the data in descending order.
4. Find average extra time salary.
5. Display distinct qualification from employees.
Program 25:
Create two tables and do the following commands:
1. To list the books name of market type.
2. To increase the price of books with fiction type.
3. To insert a new row in the table issued having the following data :”19022”,22.
4. To display the bookid,bookname for all books which have been issued(query will
require contents from both the tables).
5. Remove the table issued.
Index
s.no Date Topic Teachers
sign
1. 28/04/2022 To display the elements of list twice, if it is a number
and display the element terminated with ‘*’ if it is not
a number.

2. 30/04/2022 To create a function chkOdd() that takes one


argument(a positive integer)and reports if the
argument is odd or not.

3. 2/05/2022 Write the definition of the function Fending(SCORES)


to find the sum of numbers ending with 4. The
numbers are stored in the list SCORES.

4. 6/05/2022 Write the definition of a function SumE(L),which


should find the sum of even numbers of the list.For
example, if L=[21,22,12,5,8,6,90], output should be
138.

5. 9/05/2022 To create A to E pattern with one character increased


in every next line using nested loop.

6. 13/05/2022 Read a text file line by line and display each word
separated by a #.

7. 18/05/2022 Read a text file and display the number of


vowels/consonants/uppercase/lowercase characters in
the file.

8. 21/05/2022 Remove all the lines that contain the character 'a' in a
file and write it to another file.

27/05/2022 Create a binary file with name and roll number. Search
9. for a given roll number and display the name, if not
found display appropriate message.

10. 4/07/2022 Create a binary file with roll number, name and marks.
Input a roll number and update the marks.
11. 7/07/2022 Write a random number generator that generates
random numbers between 1 and 6 (simulates a dice).

12. 11/07/2022 Write a menu-driven Python program to


implement a stack using list.

13. 17/07/2022 Create a CSV file by entering user-id and password,


read and search the password for given user-id.

14. 29/07/2022 Write a program to implement various functions of


math module.

15. 1/08/2022 Write a program to check whether a string is


palindrome or not.

16. 16/08/2022 Write a program to find factorial of a number.

17. 7/10/2022 Write a program to connect with databse,insert


records into table and display all records.

18. 9/10/2022 Write a program to search a particular record by


importing suitable module.If not found,display
appropriate message.

19. 17/10/2022 Write a program to update a particular record. If not


found,display appropriate message.

20. 22/10/2022 Write a program to delete a record from the table by


importing suitable module.

21. 26/10/2022 Create a table and insert data into it , describe the
table and show distinct cities from table.

22. 6/11/2022 Create a table and do the following commands:


1.Display the record of those persons whose name
begins with k.
2. Insert a new column marks.
3. Update the marks from 194 to 200 .
4. Display the names of those student who have
scored below 75.
23. 8/11/2022 Create two tables and do the following commands:

To display the frequency of employees department


wise.
To display names of employees, sales and qualification
who have achieved sales more than 20000.
To change the basic salary of employees whose depid
is 103.
To delete the bonus column.
To find the max sales
24. 11/11/2022 Create two tables and do the following
commands:
Add a new column co partnership.
Show the records of those employees who have “h” in
their name.
Arrange the data in descending order.
Find average extra time salary.
Display distinct qualification from employees
25. 20/11/2022 Create two tables and do the following commands:
To list the books name of market type.
To increase the price of books with fiction type.
To insert a new row in the table issued having the
following data :”19022”,22.
To display the bookid,bookname for all books which
have been issued(query will require contents from
both the tables).
Remove the table issued.

You might also like