PRACTICAL PROGRAMS
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.
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++
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+
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