Practical File
Practical File
CLASS – XII NM
Write a Program to enter the number of terms and to print the Fibonacci
5 Series.
Write a Program to enter the string and to check if it’s palindrome or not
6 using loop.
9 Write a Program to enter the number and print the Floyd’s Triangle in
decreasing order
Write a Program to find factorial of entered number using user-defined
10 module fact().
Write a Program to enter the numbers and find Linear Search, Binary
11 Search, Lowest Number and Selection Sort using list/array code.
Write a Program to read data from data file and show Data File Handling
12 related functions utility in python.
Write a Program to read data from data file in append mode and use
13 writeLines function utility in python.
Write a Program to read data from data file in read mode and count the
14 particular word occurrences in given string, number of times in python.
15 Write a Program to read data from data file in read mode and append the
words starting with letter ‘T’ in a given file in python.
Write a Program to show MySQL database connectivity in python.
16
Write a Python program to implement all basic operations of a stack, such
17 as adding element (PUSH operation), removing element (POP operation)
and displaying the stack elements (Traversal operation) using lists.
18 Write a program to display unique vowels present in the given word
using Stack.
Solution:
def pernum(num):
divsum=0
for i in range(1,num):
if num%i == 0:
divsum+=i
if divsum==num:
print('Perfect Number')
else:
pernum(6)
pernum(15)
Program 3: Write a Program to check if the entered number is Armstrong or not.
Solution:
#An Armstrong number has sum of the cubes of its digits is equal to the number itself no=int(input("Enter any
no1 = no sum
=0
while(no>0):
ans = no % 10;
Number")
else:
Solution:
#Program to read data from data file in read mode and
read=f.readlines()
f.close()
id=[]
for ln in read:
if ln.startswith("T"):
id.append(ln)
print(id)
Program 16: Write a Program to show MySQL database connectivity in python.
Solution:
import mysql.connector
con=mysql.connector.connect(host='localhost',user='root',password='',db='school')
stmt=con.cursor()
stmt.execute(query)
data=stmt.fetchone()
print(data)
Program 17: Write a Python program to implement all basic operations of a stack, such as adding
element (PUSH operation), removing element (POP operation) and displaying the stack elements
(Traversal operation) using lists.
Solution:
s=[]
c="y"
while (c=="y"):
(choice==1):
s.append(a)
elif (choice==2):
if (s==[]):
elif (choice==3):
l=len(s)
for i in range(l-1,-1,-1): #To display elements from last element to first print
(s[i])
else:
print("Wrong Input")
Solution:
vowels =['a','e','i','o','u']
if letter in vowels:
if letter not
in Stack:
Stack.appen
d(letter)
print(Stack)
Solution:
import MySQLdb
db1 = MySQLdb.connect("localhost","root","","TESTDB" ) #
cursor = db1.cursor()
sql = "CREATE TABLE EMP(empno integer primary key,ename varchar(25) not null,salary float);"
cursor.execute(sql)
db1.close()
Inserting a record in ‘emp’
import MySQLdb
db1.cursor()
# Prepareing SQL statement to insert one record with the given values sql = "INSERT
try:
cursor.execute(sql ql)
db1.commit()
except:
db1.rollback()
db1.close()
Fetching all the records from EMP table having salary more than 70000.
import MySQLdb
db1.cursor()
try:
cursor.execute(sql)
#using fetchall() function to fetch all records from the table EMP and store in resultset
resultset:
db1.close()
import MySQLdb
db1.cursor()
#Preparing SQL statement to increase salary of all employees whose salary is less than 80000
sql = "UPDATE EMP SET salary = salary +1000 WHERE salary<80000;" try:
cursor.execute(sql)
db1.commit() except:
db1.rollback()
db1.close()
Deleting record(s) from table using DELETE
import MySQLdb
db1.cursor()
statement to delete records as per given condition sql = "DELETE FROM EMP
try:
cursor.execute(sql)
db1.commit()
except:
db1.rollback()
db1.close() Output:
deleted
This PDF document was edited with Icecream PDF Editor.