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

PRACTICAL PROGRAMS

The document outlines a series of practical programming tasks in Python related to computer science. These tasks include writing functions for factorial series, manipulating text files, implementing stack operations, and establishing database connectivity with MySQL. Each task provides specific requirements and example code snippets to guide the implementation.

Uploaded by

ynpjp7h8hy
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)
2 views

PRACTICAL PROGRAMS

The document outlines a series of practical programming tasks in Python related to computer science. These tasks include writing functions for factorial series, manipulating text files, implementing stack operations, and establishing database connectivity with MySQL. Each task provides specific requirements and example code snippets to guide the implementation.

Uploaded by

ynpjp7h8hy
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/ 6

COMPUTER SCIENCE PRACTICAL PROGRAMS

1) Write a python program using a function to print factorial number series from n to m numbers

2) Write a program to replace all spaces from text with - (dash) from the file intro.txt.

3) Create a text file "intro.txt" in python and ask the user to write a single line of text by user input.
4) Write a python program to accept the username "Admin" as the default argument and password 123 entered by the
user to allow login into the system.

5) Create a python program to create a stack of student’s marks .


 Write function PUSH to add marks in to the stack and
 Write function POP to remove the marks from stack and display the same.

def push(ele):
stu.append(ele)
def pop():
print(stu.pop(),"is popped")
stu=[]
push(70)
push(30)
push(60)
pop()
pop()
pop()

Output:-
60 is popped
30 is popped
70 is popped
6) Write a program to implement following stack methods for the employee details (empno, name)
1. Push
2. Display
def push(eid,ename):
emp.append([eid,ename])
def display(emp):
for i in emp:
print(i)
emp=[]
push(1,'rajesh')
push(2,'ramesh')
push(3,'suresh')
display(emp)

output:-
[1, 'rajesh']
[2, 'ramesh']
[3, 'suresh']
7) Create a python program to create a stack of Book names. (4)
 Write function PUSH to add book names in to the stack and
 Write function display all the names.
def push(bookname):
book.append(bookname)
def display(book):
for i in book:
print(i)
book=[]
push('python')
push('java')
push('c++')
display(book)

output:-
python
java
c++

8) Write a program to implement following stack methods for the (4)


employee details (empno, name)
1. PUSH
2. POP
def push(eid,ename):
emp.append([eid,ename])
def pop():
item=emp.pop()
return item
emp=[]
push(1,'alice')
push(2,'bob')
push(3,'jack')
print(pop())
print(pop())
print(pop())

output:-
[3, 'jack']
[2, 'bob']
[1, 'alice']

9) Complete the following database connectivity program by writing missing statements and perform the given
query.
import ______________________________ as sqltor #1
mycon = sqltor.________( host = “localhost”, user = “root”, passwd = “123”, database =
“medicine” ) #2
cursor = mycon.cursor( )
cursor.execute(______________________ ) #3
data = cursor._________________ #4
for rec in data:
print ( rec )
mycon.close( )
1. Complete the statement #1,by writing the name of library / package need to import for database
connectivity.
2. Complete the statement #2, write the name of method require to create connection between python and
mysql.
3. Complete the statement #3, write the query to display those drug records which price is between the 50
to 100 from table DRUG.
4. Complete the statement #4, to retrieve all records from the result set.
Answers:-
1)mysql.connector
2)connect
3)select * from DRUG where price between 50 and 100
4)fetchall()

10) STUB PROGR AM) A table Student is created in the database School with password ‘admin’.
The details of table are given below.
StuID Name Class Total Grade
ST/12 Tanmay 12 C 499 A+

import mysql.connector as sqltor


mycon = sqltor.connect( _______, user = “root”, _________, database = ___________ ) #1
cursor = mycon.cursor( )
cursor.execute(______________________ ) #2
data = _________________ #3
for rec in data:
print ( rec )
_____________________ #4
mycon.close( )

1. Complete the statement #1 to write appropriate missing parameter and values.


2.Write the statement #2, to fetch Name, Class, Grade from table Student who have scored less than 400.
3. Complete the statement #3, to fetch all records from the resultset.
4. What statement you will write in place of statement #2 to insert one more record in table Student, as well as in
statement #4, to make your changes permanent in the table Student.
StuID Name Class Total Grade
ST/15 Amrit 12 D 496 A+
Answers:-
1) mycon = sqltor.connect( host=”localhost”, user = “root”, password=”admin”, database = “School” )
2)select Name,Class,Grade from Student where Total<400;
3)cursor.fetchall()
4) insert into Student values(‘ST/16,”ALICE”,”12 C”,500,”A”)
Mycon.commit()

11)Complete the following database connectivity program by writing missing statements and perform the given
query. (4)
import connector
mycon = connector.________( host = “localhost”, user = “root”, passwd = “123 ) #1
cursor = mycon.________________( ) #2
cursor.execute(______________________ ) #3
print(cursor._________________ ) #4
mycon.close( )
1. Complete the statement #1, write the name of method require to create connection between python and
mysql.
2. Complete the statement #2, write the name of method require to create cursor object.
3. Complete the statement #3, write the query to display all the database names.
4. Complete the statement #4, to retrieve & display all records from the result set

Answers:-
1)connect
2)cursor()
3) show databases;
4) fetchall()
12) (STUB PROGRAM) The given program is used to connect with MYSQL and show all the records from table
“Emp” from the database “Test”.You are required to complete the statements so that code can be
executed properly.
Code:
import ___________________________ #1
db=mysql.__________________________(host=”localhost”,user=”root”
,password=”admin”,database=””Test”) #2
cur=db.______________() #3
cur.execute(“_______________________________________”) #4
print(cursor.fetchall())
db.close()
1. Complete the statement #1, write the name of module require to import
2. Complete the statement #2, write the name of method require to connect to mysql.
3. Complete the statement #3, write the method required to create cursor object.
4. Complete the statement #4, write the query to retrieve all the details from employee table.

Answers:-
1) mysql.connector
2) mysql.connector.connect
3)cursor()
4)select * from emp

You might also like