xii cs practicals
xii cs practicals
xii cs practicals
Solution:
result = 0
if op == "+":
elif op == "-":
elif op == "*":
elif op == "/":
if val2 == 0:
else:
elif op == "//":
else:
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
no1 = no
sum = 0
while(no>0):
ans = no % 10;
if sum == no1:
print("Armstrong Number")
else:
Solution:
fact = 1
i=1
while i<=num:
fact = fact*i
i=i+1
Program 5: Write a Program to enter the number of terms and to print the Fibonacci
Series.
Solution:
#fibonacci
x=0
y=1
z=1
while(z<= i):
x=y
y=z
z=x+y
Program 6: Write a Program to enter the string and to check if it’s palindrome or not using loop.
Solution:
# Program to enter the string and check if it’s palindrome or not using ‘for’ loop.
newlist=[]
newlist[:0]=msg
l=len(newlist)
ed=l-1
for i in range(0,l):
if newlist[i]!=newlist[ed]:
break
if i>=ed:
break
l=l-1
ed = ed - 1
Solution:
my_list = ['p','r','o','b','e']
# Output: p
print(my_list[0])
# Output: o
print(my_list[2])
# Output: e
print(my_list[4])
# Nested List
# Nested indexing
# Output: a
print(n_list[0][1],n_list[0][2],n_list[0][3])
# Output: 5
print(n_list[1][3])
Program 8: Write a Program to enter the numbers in a list using split () and to use all the functions
related to list.
Solution:
#Program to enter the numbers in a list using split () and to use all the functions related to
list.
# print (len(numbers))
memo=[]
memo.insert(i,x)
i+=1
print(memo)
memo.append(25)
print("Second List")
print(memo)
newlist=[]
newlist[:0]=msg
l=len(newlist)
print(l)
Program 9: Write a Program to enter the number and print the Floyd’s Triangle in
decreasing order.
Solution:
#Floyd's triangle
for i in range(5,0,-1):
for j in range(5,i-1,-1):
print('\n')
Program 10: Write a Program to find factorial of entered number using user-defined module
fact().
Solution:
#Using function
import factfunc
ans=factfunc.fact(x)
print (ans)
Program 11: Write a Program to enter the numbers and find Linear Search, Binary Search,
Lowest Number and Selection Sort using list/array code.
Solution:
arr=[]
def array_operation():
ch=1
while ch!=10:
print('10 Exit\n')
if ch==1 :
appendarray()
elif ch==2 :
print_array()
elif ch==3 :
reverse_array()
elif ch==4 :
linear_search()
elif ch==5 :
binary_search()
elif ch==6 :
min_number()
elif ch==7 :
selection_sort()
def appendarray():
for i in range(0,10):
arr.insert(i,x)
#-------------------------------------------------------------------------------------------------------------------------
----------------
def print_array():
for i in range(0,10):
print(arr[i]),
#-------------------------------------------------------------------------------------------------------------------------
----------------
def reverse_array():
for i in range(1,11):
print(arr[-i]),
#-------------------------------------------------------------------------------------------------------------------------
----------------
def lsearch():
try:
n=arr.index(x)
except:
#-------------------------------------------------------------------------------------------------------------------------
----------------
def linear_search():
fl=0
for i in range(0,10):
if arr[i]==x :
fl=1
print ('Number Found at %d location'% (i+1))
break
if fl==0 :
#-------------------------------------------------------------------------------------------------------------------------
----------------
def binary_search():
fl=0
low=0
heigh=len(arr)
while low<=heigh :
mid=int((low+heigh)/2)
if arr[mid]==x :
fl=1
break
elif arr[mid]>x :
low=mid+1
else :
heigh=mid-1
if fl==0 :
#-------------------------------------------------------------------------------------------------------------------------
----------------
def min_number():
n=arr[0]
k=0
for i in range(0,10):
if arr[i]<n :
n=arr[i]
k=i
#-------------------------------------------------------------------------------------------------------------------------
----------------
def selection_sort():
for i in range(0,10):
n=arr[i]
k=i
for j in range(i+1,10):
if arr[j]<n :
n=arr[j]
k=j
arr[k]=arr[i]
arr[i]=n
array_operation()
Program 12: Write a Program to read data from data file and show Data File Handling
related functions utility in python.
Solution:
f=open("test.txt",'r')
print(f.name)
f_contents=f.read()
print(f_contents)
f_contents=f.readlines()
print(f_contents)
f_contents=f.readline()
print(f_contents)
for line in f:
print(line, end='')
f_contents=f.read(50)
print(f_contents)
size_to_read=10
f_contents=f.read(size_to_read)
while len(f_contents)>0:
print(f_contents)
print(f.tell())
f_contents=f.read(size_to_read)
Program 13: Write a Program to read data from data file in append mode and use
writeLines function utility in python.
Solution:
#Program to read data from data file in append mode
af=open("test.txt",'a')
af.writelines('\n' + lines_of_text)
af.close()
Program 14: Write a Program to read data from data file in read mode and count the
particular word occurrences in given string, number of times in python.
Solution:
f=open("test.txt",'r')
read=f.readlines()
f.close()
times=0 #the variable has been created to show the number of times the loop runs
times2=0 #the variable has been created to show the number of times the loop runs
line=sentence.split()
times+=1
line2=each
times2+=1
if chk==line2:
count+=1
print("The search String ", chk, "is present : ", count, "times")
print(times)
print(times2)
Program 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.
Solution:
#Program to read data from data file in read mode and
f=open("test.txt",'r')
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"):
if (choice==1):
s.append(a)
elif (choice==2):
if (s==[]):
else:
elif (choice==3):
l=len(s)
print (s[i])
else:
print("Wrong Input")
Solution:
#using Stack
vowels =['a','e','i','o','u']
Stack = []
if letter in vowels:
Stack.append(letter)
print(Stack)
Solution:
a=[]
c='y'
while (c=='y'):
if (choice==1):
a.append(b)
elif (choice==2):
if (a==[]):
print("Queue Empty")
else:
a.pop(0)
elif (choice==3):
l=len(a)
for i in range(0,l):
print (a[i])
else:
print("wrong input")
c=input("Do you want to continue or not: ")
Program 20: Perform all the operations with reference to table ‘Employee’ through
MySQL-Python connectivity.
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 = MySQLdb.connect("localhost","root","","TESTDB" )
cursor = db1.cursor()
# Prepareing SQL statement to insert one record with the given values
try:
cursor.execute(sql)
db1.commit()
except:
db1.rollback()
db1.close()
Fetching all the records from EMP table having salary more than 70000.
import MySQLdb
db1 = MySQLdb.connect("localhost","root","","TESTDB" )
cursor = db1.cursor()
try:
cursor.execute(sql)
#using fetchall() function to fetch all records from the table EMP and store in
resultset
resultset = cursor.fetchall()
print (row)
except:
db1.close()
Updating record(s) of the table using UPDATE
import MySQLdb
db1 = MySQLdb.connect("localhost","root","","TESTDB" )
cursor = db1.cursor()
#Preparing SQL statement to increase salary of all employees whose salary is less than
80000
try:
cursor.execute(sql)
db1.commit()
except:
db1.rollback()
db1.close()
Deleting record(s) from table using DELETE
import MySQLdb
db1 = MySQLdb.connect("localhost","root","","TESTDB" )
cursor = db1.cursor()
try:
cursor.execute(sql)
db1.commit()
except:
db1.rollback()
db1.close()
Output:
1 record(s) deleted
>>>