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 square')
else:
pernum(9)
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
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
Program9: 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 10: 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 11: 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:
#Program to read data from data file in read mode and #count the particular word occurrences in given
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
count=0
line=sentence.split() times+=1
times2+=1
if chk==line2: count+=1
print("The search String ", chk, "is present : ", count, "times") print(times)
print(times2)
Program 12: 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
read=f.readlines() f.close()
id=[]
for ln in read:
if ln.startswith("T"):
id.append(ln)
print(id)
Program 13: 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 14: 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:
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)
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
>>>