Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
53 views19 pages

VISHAL12-F Program File

This document contains a computer science practical file in Python programming language. It includes programs on array manipulation, list sorting, stack operations, queue operations, file handling, database connectivity etc. along with their expected outputs. The file also acknowledges the school authorities and the teacher for their support in learning.

Uploaded by

kunal.1214155669
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)
53 views19 pages

VISHAL12-F Program File

This document contains a computer science practical file in Python programming language. It includes programs on array manipulation, list sorting, stack operations, queue operations, file handling, database connectivity etc. along with their expected outputs. The file also acknowledges the school authorities and the teacher for their support in learning.

Uploaded by

kunal.1214155669
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/ 19

COMPUTER SCIENCE

(83)
PRACTICAL FILE

PROGRAMMING LANGUAGE:
PYTHON

NAME: VISHAL YADAV


CLASS: XII F

ACKNOWLEDGEMENT
I take the privilege to extend my sincere thanks
to the principal of Army Public School Dhaula
Kuan and the school authorities for providing us
with a very well-equipped lab with all the
necessities. I take this opportunity to express my
profound gratitude and deep regard to my
teacher Ms. Pallavi Sharma for her exemplary
guidance, monitoring and constant
encouragement throughout the course of this
learning.
VISHAL YADAV XII-F

INDEX
S. PROGRAM T. SIGN
No
Q1) Write a python code for a
function(t,n), which repositions
all the elements of array by
shifting each of them to next
position and shifting element to
last position
Q2) 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.

Q3) WAP a program to find the


number of lines starting with ‘F’
in firewall.txt.
Q4) write a definition for a function
economic() to read each record
of a binary file ITEMS.DAT, find
and display those items which
cost less than 2500.

Q5) Write the definition of a


member function PUSH() in
python to add a new book in a
dynamic stack of BOOKS
considering the following code
is already included in the
program: ISBN, TITLE.

Q6) 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.

Q7) 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”.
Q8) WAP to show entered string is
palindrome or not.
Q9) WAP to remove all odd numbers
from the given list.

Q10) Write a program to display


frequencies of all the element of
a list.
Q11) Write a python function to
perform push operation in a
dynamically allocated stack.
Q12) Write a program to display and
delete element from a queue.
Q13) Write a program to check
whether the MySQL database is
connected to python not.
Q14) To display longest line in text
file.
Q15) create a csv file with roll no,
name, marks of students.
Q16) WAP to find the highest
common factor (HCF) of two
numbers.
Q17) Program using FETCHONE

Q18) Program using rowcount


Q19) WAP to bubble sort a particular
list in ascending order.
Q20) WAP traversing a 2D List.

Q21) Find the output of the following


code.

Q22) Write a python program to


create the tuple
(‘a’,’bb’,’ccc’,’dddd’…….) that
ends with 26 copies of the letter
Z.

#Program1: 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
element to last position.

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=[34,38,43,51]
print("Original List",d)
r=len(d)
Convert(d,r)

#OUTPUT:
Original List [34, 38, 43, 51]
after conversion [38, 43, 51, 34]

#Program2: 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.

def SWAP2BEST(A,size):
i=0
while(i,size):
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]

#Program3: WAP a program to find the number of lines


starting with ‘F’ in firewall.txt.
F=open(r”C:\Users\hp\Desktop\cs\networking\firewall.txt”)
C=0
For i in f.readline():
If(i==’F’):
C=c+1
Print(c)

#OUTPUT:
1

#Program4: write a definition for a function economic()


to read each record of a binary file ITEMS.DAT, find and
display those items which cost less than 2500.

def Economic():
f1=open("items.dat","ab")
while true:
try:
g=pickle.load(f1)
if(g['cost']<250):
print(g)
except:
break
Economic()

#OUTPUT:
Enter gift name flowers
Enter id 2
Enter cost 500
{‘giftname’ :’ flowers’,’id’:2,’cost’:500.0}

#Program5: Write the definition of a member function


PUSH () in python to add a new book in a dynamic stack
of BOOKS considering the following code is already
included in the program: ISBN, TITLE.

s=[]
def push():
a=int(input("enter ISBN no"))
t=input("enter title")
I=[a,t]
s.append(I)
def disp(s):
if(s==[]):
print("list is empty")
else:
top=len(s)-1
print(s[top],"---top")
for i in range(top-1,-1,-1):
print(s[i])
push()
disp(s)

#OUTPUT:
Enter ISBN no 2
Enter title Encyclopedia
[2,’Encyclopedia’] ---top

#Program 6: 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.

def POP(Arr):
if (len(st)>0):
r=st.pop()
return r
else:
print("stack empty")

#OUTPUT:
Blank

#Program 7: 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”.
import mysql.connector as m
db=m.connect(host="localhost",user="root",passwd="1234",database="Emgt")
c=db.cursor()
c.execute("select *from emp where sal>3000")
r=c.fetchall()
for i in r:
print(i)

#Program 8: WAP to show entered string is palindrome


or not.

def ispalindrome(x):
k=str(x)
I=k[::-1]
if k==I:
print("it is a palindrome")
else:
print("not a palindrome")
x=input("enter next string")
ispalindrome(x)

#OUTPUT:
it is a palindrome
enter next stringswsss
not a palindrome
enter next stringqwer
not a palindrome
#Program 9: WAP to remove all odd numbers from the
given list.
def removeodd(x):
y=[]
for i in x:
if i%2!=0:
k=x.pop(x.index(i))
y.append(k)
else:

pass
print(x)

#OUTPUT:
Remove odd([10,45,88,98,88,100])
[45,98,100]

#Program 10: Write a program to display frequencies of


all the element of a list.
def countitem(x,y):
#x=list entered by user
#y=element whose occurences are to be counted
k=x.count(y)
print(k)

#Program 11: Write a python function to perform push


operation in a dynamically allocated stack.
stk=[]

def pushin():
a=int(input("enter x"))
b=int(input("enter y"))
I=[a,b]
st.append(I)

#OUTPUT:
Pushin()
Enter x1
Enter y2
Stk
[[1,2]]

#Program 12: Write a program to display and delete


element from a queue.
que=[]
def pop():
que.pop(0)
def display():
if len(que)==0:
print("empty queue")
else:
print(que)
#Program 13: Write a program to check whether the
MySQL database is connected to python not.

'''import mysql.connector as con

mycon=con.connect(host="127.0.0.1",user="root",passwd="5971",database="
my school")

#connection check
if mycon.is_connected():
print("connectiion successful")
else:
print("not successful")

#OUTPUT:
Connection successful

#Program 14: To display longest line in text file.

file=open('helloworld.txt','r')
line1=file.readline()
a=len(line1)
for i in file:
line=file.readline()
x=len(line)
if x>a:
print(line)
file.close()

#OUTPUT:
This line is the longest line

#Program 15: create a csv file with roll no, name, marks
of students.

import csv
f=open("Student.csv","w",newline='')
s_writer=csv.writer(f,defaulter=';')
s_writer.writerow(['roll no','name','marks'])
rec=[]
while True:
r=int(input("enter roll no:"))
n=int(input("enter marks:"))
lst=[r,n,m]
rec.append(lst)
ch=input("do you want to enter more records?(y/n)")
if ch=='n':
break
for i in rec:
s_writer.writerow(i)
f.close()

#OUTPUT:

Roll no;name;marks
1;arnav;99
2;amit;97

#Program 16: WAP to find the highest common factor


(HCF) of two numbers.
def HCF(x,y):
if x>y:
smaller=y
else:
smaller=x
for i in range(1,smaller+1):
if((x%i==0)and(y%i==0)):
hcf=i
return hcf
num1=int(input("enter fist number"))
num2=int(input("enter second number"))
print("The HCF of of",num1,"and",num2,"is",HCF(num1,num2))

#OUTPUT:
Enter first number: 7
Enter second number: 21
The HCF of 7 and 21 is 7

#Program17: using FETCHONE

import mysql.connector
mydb=mysql.connector.connect(host='localhost',user='root',passwd='passwor
d',database='arnavai')
cursor.execute("select *from table1")
records=cursor.fetchone()
for i in records:
print(i)

#OUTPUT:
Arnav Sharan
3
A
Mysql> use arnavai;
Database changed
Mysql>select* from table1;

Name Roll no Section


Arnav Sharan 3 A
Arnav Singh 4 A
ABC 1 B
PQR 10 C
XYZ 11 Null
DEF null A

#Program18: ROWCOUNT
import mysql.connector
mydb=mysql.connector.connect(host='localhost',user='root',passwd='passwor
d',database='arnavai')
cursor=mydb.cursor()
cursor.execute(“UPDATE table1 set Roll=05 where Section=’A’ ”)
mydb.commit(0
print(cursor.rowcount,”Records updated”)

#OUTPUT:
3 Records updated

#Program 19: WAP to bubble sort a particular list in


ascending order.

d=[14,67,32,12]
n=len(d)
for i in range(n):
for j in range(n-i-1):
if(d[j]>d[j+1]):
d[j],d[j+1]=d[j+1],d[j]
print(d)

#OUTPUT:
[12,14,32,67]

#Program 20: WAP traversing a 2D List.


a=[[1,2],[3,4],[5,6]]
for i in range(3):
for j in range(2):
print(a[i][j])
#OUTPUT:

1
2
3
4
5
6

#Program 21: Find the output of the following code.


a=""
c=0
b="hello world"
for i in b:
if c%2!=0:
a+=str(c)
elif c%2==0:
if i.islower():
a+=i.upper()
else:
a+=i
c+=1
a+=b[:1]
print("the new string is:",a)

#OUTPUT:
H1L305W7R9Dh
#Program 22: Write a python program to create the tuple
(‘a’,’bb’,’ccc’,’dddd’…….) that ends with 26 copies of the
letter Z.
n=int(input("Enter no"))
x=65
for i in range(0,n):
for j in range(0,i+1):
a=chr(x)
print(a,end=' ')
x+=1
print("\r")

#OUTPUT:
Z is printed 26 times.

You might also like