0% found this document useful (0 votes)
16 views24 pages

Python Programming Projects for Class XII

Uploaded by

blazeshorts999
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)
16 views24 pages

Python Programming Projects for Class XII

Uploaded by

blazeshorts999
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

ARMY PUBLIC SCHOOL

DHAULA KUAN

ACADEMIC YEAR : 2024-25


REPORT FILE’

NAME : ANUJ KUMAR PATHAK


CLASS/SECTION : XII - E
SUBJECT : COMPUTER SCIENCE
SUBJECT CODE : 083

PROJECT GUIDE : Ms. Pallavi Sharma


PGT (Computer Science)
Army Public School’
INDEX

TABLE OF CONTENTS
[Link] INDEX PAGE NO

01 o6
Write code for a function void oddEven (S,N) in
python, to add 5 in all the odd values and 10 in all the
even values of the list S.

02 07
Write a code in python for a function Convert (T,N) ,
which repositions all the elements of an array by
shifting each of them to next position and shifting the
first element to last position?

03 08
Write a function SWAP2BEST ( ARR, Size) in python
to modify the content of the list in such a way that the
elements, which are multiples of 10, swap with the
value present in the very next position in the list?
04 09
WAP to input ‘n’ classes and names of their class teacher to
store them in the dictionary and display the same?

05 10
Accept a particular class from the user and display the
name of the class teacher of that class? (Let the
dictionary be same as the above question)

06 10
Write a function definition for SUCCESS (), to read the
content of a text file [Link], and count the
presence of word STORY and display the number of
occurrences of this word?

07 11
A text file “[Link]” has the following data written
in it: Living a life you can be proud of Doing your best
Spending your time with people and activities that are
important to you standing up for things that are right
even when it’s hard Becoming the best version of
[Link] a user defined function to count and display
the total number of words starting with ‘P’ present in a
file?

08 12
Write a Program that reads characters from the
keyboard one by one. All lower case characters get
stored inside the file LOWER, all upper case characters
get stored inside the file UPPER and all other
characters get stored inside OTHERS?

09 13
Write a Program to find no of lines starting with F in
[Link].

10 14
Write a Program to find how many ‘firewall’ or ‘to’ are
present in a file [Link]?

11 14
Write a python function to search and display the
record of that product from the file [Link]
which has maximum cost.

12 16
Write a definition for a function Itemadd() to insert a
record into the binary file
[Link],([Link]-id,gift,cost). info should be
stored in the form of a list.

13 17
write a python function writecsv () to write the
information into [Link]. using a dictionary..

14 18
Write a definition for function COSTLY() to read each
record of a binary file [Link], find and display
those items, which are priced more than 50.

15 19
Write a function SHOW(carNo) in Python which
accepts the car number as parameter and display details
of all those cars whose mileage is from 100 to 150
stored in the binary file [Link].

16 19
Write a function in python PUSH (A), where A is a list
of numbers. From this list, push all numbers divisible
by 3 into a stack implemented by using a list.
17 20
Write a function in python POP (Arr), where Arr is a
stack implemented by a list of numbers. The function
returns the value deleted from the stack.

18 21
Write a MySQL-Python connectivity code display
ename, empno, designation, sal of those employees
whose salary is more than 3000 from the table emp.
Name of the database is “Emgt”.

19 22
Write a MySQL-Python connectivity code to increase
the salary (sal) by 100 of those employees whose
designation (job) is clerk from the table [Link] of
the database is “Em”.

20 Write a Python function that that prints out 23


the first n rows of Pascal's triangle.
CODE
Q1. Write code for a function void oddEven (S,N) in
python, to add 5 in all the odd values and 10 in all the even
values of the list S?
Ans: (coding)
def oddEven (S,N):

for i in range(N):

if(S[i]%2!=0):

S[i]=S[i]+5

else:

S[i]=S[i]+10

print(S)

S=[2,3,45,68,32,23]

N=len(S)

oddEven(S,N)

(Output)

[12, 8, 50, 78, 42, 28]

Q2: Write a code in python for a function Convert (T,N) ,


which repositions all the elements of array by shifting each
of them to next position and shifting firstelement to last
position?
Ans: (Coding)
def Convert ( T, N):

t=T[0]

for i in range(N-1):

T[i]=T[i+1]

T[N-1]=t

print("after conversion",T)

d=[10,14,11,21]

print("Original List",d)

r=len(d)

Convert(d,r)

(OUTPUT)

Original List [10, 14, 11, 21]

after conversion [14, 11, 21, 10]

Q3: Write a function SWAP2BEST ( ARR, Size) in python


to modify the content of the list in such a way that the
elements, which are multiples of 10 swap with the value
present in the very next position in the list?
Ans: (Coding)
def SWAP2BEST(A,size):

i=0

while(i

if(A[i]%10==0):

A[i],A[i+1]=A[i+1],A[i]

i=i+2

else:

i=i+1

return(A)

d=[90,56,45,20,34,54]

print("actual list",d)

r=len(d)

print("after swapping",SWAP2BEST(d,r))

(Output)

Actual list [90, 56, 45, 20, 34, 54]

After swapping [56, 90, 45, 34, 20, 54]

Q4: WAP to input ‘n’ classes and names of their class


teacher to store them in dictionary and display the same?
Ans: (Coding)
d={}

n=int(input("enter number of classes"))

for i in range(n):

k=input("Enter class ")

d[k]=input("Enter name of class teacher")

print(d)

(Output)

enter number of classes2

Enter class 12-E

Enter name of class teacherMrs Meenakshi sher

Enter class 12-F

Enter name of class teacherMrs Pallavi Sharma

{'12-E': 'Mrs Meenakshi sher', '12-F': 'Mrs Pallavi Sharma'}

Q5: Accept a particular class from the user and display


the name of the class teacher of that class? (Let the
dictionary be same as the above question)
Ans: (Coding)
while True:

h=input("Enter class")

if(h in

[Link]()):

print("Class teacher name is",d[h])

else:

print("Class doesn't exist ")

(Output)

Enter class12-E

Class teacher name is Mrs Meenakshi sher

Enter class12-A

Class doesn't exist

Q6:Write function definition for SUCCESS (), to read the


content of a text [Link], and count the presence
of word STORY and display the number of occurrences of
this word?
Ans: (Output)
def SUCCESS():

f=open("[Link]")
r=[Link]()

c=0

for i in [Link]():

if(i=="STORY"):

i=[Link]()

c=c+1

print(c)

[Link]()

Q7:A text file “[Link]” has the following data written


in it: Living a life you can be proud of Doing your best
Spending your time with people and activities that are
important to you standing up for things that are right even
when it’s hard Becoming the best version of [Link] a
user defined function to count and display the total
number of words starting with ‘P’ present in a file?
Ans: (Output)
def count():

f=open(“[Link]”, “r”)

r=[Link]()

n=0

l=[Link]()
a=[]

for i in l:

if(i[0]==‘p’):

n=n+1

[Link](i)

else:

continue

print(“total no. of word starting with P are”, n)

print(a)

Q8: Write a Program that reads character from the


keyboard one by one. All lower case characters get store
inside the file LOWER, all upper case characters get
stored inside the file UPPER and all other characters get
stored inside OTHERS?
Ans: (coding)
f=open(r"C:\Users\user\Desktop\[Link]")

f1=open("[Link]","a")

f2=open("[Link]","a")

f3=open("[Link]","a")

r=[Link]()
for i in r:

if(i>='a' and i<='z'):

[Link](i)

elif(i>='A' and i<='Z'):

[Link](i)

else:

[Link](i)

[Link]()

[Link]()

[Link]()

[Link]()

Q9:Write a Program to find no of lines starting with F in


[Link]
Ans: (Coding)
f=open(r"C:\Users\hp\Desktop\cs\networking\[Link]")

c=0

for i in [Link]():

if(i[0]=='F'):

c=c+1

print(c)
(Output)

Q10:Write a Program to find how many ‘firewall’ or ‘to’


are present in a file [Link]?
Ans: (Coding)
f=open(r"C:\Users\user\Desktop\[Link]")

t=[Link]()

c=0

for i in [Link]():

if(i=='firewall')or (i=='is'):

c=c+1

print(c)

(OUTPUT

10

Q11:Write a python function to search and display the


record of that product from the file [Link]
which has maximum cost?
Sample of [Link] is given below:
pid,pname,cost,quantity; p1,brush,50,200;
p2,toothbrush,120,150; p3,comb,40,300; p4,sheets,100,500;
p5,pen,10,250
Ans: (Coding)
import csv

def searchcsv():

f=open("[Link]","r")

r=[Link](f)

next(r)

m=-1

for i in r:

if (int(i[2])>m):

m=int(i[2])

d=i

print(d)

writecsv()

searchcsv()

(OUTPUT)

['p2', 'toothbrush', '120', '150']

Q12:Write a definition for a function Itemadd() to insert


record into the binary file
[Link],([Link]-id,gift,cost). info should be stored
in the form of list.
Ans: (Coding)
def Itemadd():

f=open("[Link]","wb")

n=int(input(“enter how many records”))

for i in range(n):

r= int(input('enter id'))

a=input(“enter giftname”)

p=float(input(“enter cost”))

v=[r,a,p]

[Link](v,f)

print(“record added”)

[Link]()

Itemadd()#function calling

(Output)

enter how many records 2

enter id 1

enter giftname pencil

enter cost 45

record added
enter id 2

enter giftname pen

enter cost 120

record added

Q13. write a python function writecsv () to write the


information [Link]. using dictionary. columns of
product .csv is as follows:
pid,pname,cost,quantity
Ans: (Coding)
def writecsv():

f=open("[Link]","w",newline="")

h=['pid','pname','cost','qty']

r=[Link](f1,fieldnames=h)

[Link]()

while True:

i=int(input("enter id"))

n=input("enter product name")

c=int(input("enter cost"))

q=int(input("enter qty"))

v={'pid':i,'pname':n,'cost':c,'qty':q}
[Link](v)

ch=input("more records")

if(ch=='n'):

break

[Link]()

Q14: Write a definition for function COSTLY() to read


each record of a binary file [Link], find and display
those items, which are priced more than 50?
([Link]- id,gift,cost).Assume that info is stored in the
form of list
Ans: (Coding)

def COSTLY():

f=open("[Link]","rb")

while True:

try:

r=[Link](f)

if(r['cost']>50):

print(r)

except:

break

[Link]()
Q15: Write a function SHOW(carNo) in Python which
accepts the car number as parameter and display details
of all those cars whose mileage is from 100 to 150 stored in
the binary file [Link]?
Ans: (Coding)
def Show(CarNo):

f=open(“[Link]”, “rb”)

while True:

Try:

d=[Link](f)

if(d[0]==CarNo):

print(d)

except:

continue

[Link]()

Q16:Write a function in python PUSH (A), where A is a


list of numbers. From this list, push all numbers divisible
by 3 into a stack implemented by using a list. Display the
stack if it has at least one element, otherwise display
appropriate error message?
Ans: (Coding)
st=[]

def PUSH(A):

for i in range(0,len(A)):

if(A[i]%3==0):

[Link](A[i])

if(len(st)==0):

print("stack empty")

else:

print(st)

Q17:Write a function in python POP (Arr), where Arr is a


stack implemented by a list of numbers. The function
returns the value deleted from the stack?
Ans: (Coding)
def POP(Arr):

if(len(st)>0):

r=[Link]()

return r
else:

print("stack empty")

Q18 :Write a MySQL-Python connectivity code display


ename, empno, designation, sal of those employees whose
salary is more than 3000 from the table emp. Name of the
database is “Emgt”?
ANS,
import [Link]

def get_employees_with_high_salary():
connection = [Link](
host="localhost",
user="your_username",
password="your_password",
database="Emgt"
)

cursor = [Link]()
[Link]("SELECT ename, empno, designation, sal
FROM emp WHERE sal > 3000")
rows = [Link]()
for row in rows:

print(f"Name: {row[0]}, Emp No: {row[1]}, Designation:


{row[2]}, Salary: {row[3]}")

[Link]()

[Link]()

get_employees_with_high_salary()

Q19:Write a MySQL-Python connectivity code to increase


the salary (sal) by 100 of those employees whose
designation (job) is clerk from the table [Link] of the
database is “Em”.
Ans: (Coding)
import [Link] as m

db=[Link](host="localhost",user="root",passwd="1234",
database="Em")

c=[Link]()

[Link]("update emp set sal=sal+100 where job=”clerk”)

[Link]()
Q20: Write a Python function that that prints out
the first n rows of Pascal's triangle.
Ans;
def pascal_triangle(n):
trow = [1]
y = [0]
for x in range(max(n,0)):
print(trow)
trow=[l+r for l,r in zip(trow+y, y+trow)]
return n>=1
pascal_triangle(6)
OUTPUT:
[1]
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]
[1, 5, 10, 10, 5, 1]
Thank you

Common questions

Powered by AI

The `SUCCESS()` function reads the contents of 'STORY.TXT' and splits the text into words, iterating to count occurrences of the word 'STORY'. The function uses a case-sensitive comparison; however, to ensure accuracy regardless of capitalization, it could convert all words to lowercase before counting, ensuring consistent counting of 'story' in all cases. The function focuses on simplicity, processing the file linearly, but this enhancement would make the counting more comprehensive across different cases of the word .

The PUSH operation appends numbers divisible by 3 from a list to a designated stack, implemented as another list. If the stack has at least one element, it's displayed; otherwise, an 'empty stack' message appears. The POP operation removes and returns the last added element (top of stack) if available. This implementation assumes that input can be modified and storage is sufficient for all operations, typical of list-based stack implementations .

You can store class-teacher information in a dictionary by using class names as keys and teacher names as values. A `for` loop can be used to input `n` class-teacher pairs, storing them in the dictionary. Retrieval is performed by querying the dictionary with a specific class name, returning the corresponding teacher's name. This method allows efficient storage and retrieval of class-specific information .

The `Itemadd()` function opens a binary file in write mode and enters a loop to collect a specified number of records, each containing an ID, a gift name, and a cost. These pieces of information are stored as a list, which is then serialized and written to the binary file using the `pickle` module. The use of serialization ensures complex data structures are correctly stored as binary data, making it crucial for preserving data integrity across program executions .

The function `oddEven(S, N)` iterates through a list `S` and adds 5 to each odd value and 10 to each even value. It performs this operation using a loop iterating from 0 to N, where N is the size of the list. The modified list is then printed .

The `COSTLY()` function reads each record from a binary file, utilizing the `pickle` module to deserialize records stored as lists. It checks each item's cost field and prints those with a cost greater than 50. Challenges include ensuring data corruption does not occur in deserialization and handling exceptions from end-of-file (EOF) conditions correctly without prematurely terminating the file reading operation .

The `SWAP2BEST(ARR, Size)` function swaps elements that are multiples of 10 with the element immediately following them in the list `ARR`. It iterates through the list, uses a modulus check to identify multiples of 10, and performs a swap with the next element using tuple unpacking. The index `i` is incremented by 2 after a swap to prevent re-checking the swapped pair. This manipulation results in reordered elements in the list based on the condition specified .

The algorithm for finding lines starting with 'F' iterates over each line in the file, checking the first character of each line. It keeps a count of lines that meet the condition by incrementing a counter when the first character matches 'F'. This approach efficiently scans through the file line by line, allowing quick determination of how many lines begin with the specified letter .

The `Convert(T, N)` function shifts each element of the array `T` to the next position and moves the first element to the last position. This is achieved by storing the first element in a temporary variable, shifting the rest of the elements in the loop, and finally placing the stored element at the end of the array. This effectively rotates the array one position to the left .

The function reads characters from the keyboard or a specified input file and segregates them into three files based on type: lowercase letters are written into 'lower.txt', uppercase letters into 'upper.txt', and all other characters into 'others.txt'. It checks each character using conditional statements to determine type and uses file operations to append appropriate characters to their respective files, ensuring organized storage and categorization of inputs .

You might also like