Professional Documents
Culture Documents
Xii - Practicals 24-25
Xii - Practicals 24-25
Xii - Practicals 24-25
SCHOOL
PROGRAM NO: 1
PROGRAM CODE:
def factorial(n):
f=1
for i in range(1,n+1):
f=f*i
return f
n=int(input("Input a number to compute the factorial : "))
print("Factorial of ",n,"is:",factorial(n))
OUTPUT:
PROGRAM NO: 2
To write a Python program to find the number of upper case and lower case
letters in a string which is passed as an argument to the function.
PROGRAM CODE:
def string_test(s):
up=0
low=0
for i in s:
if i.isupper():
up=up+1
if i.islower():
low=low+1
print("original string is:",s)
print("no.of upper case character:",up)
print("no.of lower case character:",low)
string=input("enter a string:")
string_test(string)
OUTPUT:
PROGRAM NO: 3
Write a program that multiplies two integer numbers without using the * operator,
using repeated addition.
AIM:
To write a Python program that multiplies two integer numbers without using
PROGRAM
OUTPUT:
PROGRAM NO : 4
AIM:
To write a menu driven Python Program to find Area of Circle, Rectangle a Triangle
using function.
PROGRAM CODE :
def Circle(r):
Area = 3.141*(r**2)
print(“The Area of circle is : “,Area)
def Rectangle(L,B):
R=L*B
print(“The Area of rectangle is : “,R)
def Triangle(B, H):
R=0.5*B*H
print (“The Area of triangle is : “,R)
print (“1.Area of Circle”)
print (“2.Area of Rectangle”)
print (“3.Area of Triangle”)
ch=int(input(“Enter your choice:”))
if ch==1:
r = float(input(“Enter the radius value : “))
Circle(r)
elif ch==2:
L=int(input(“Enter the Length of the Rectangle : “))
B=int(input(“Enter the Breadth of the Rectangle : “))
Rectangle(L,B)
elif ch==3:
B=int(input(“Enter the Base of the triangle:”))
H=int(input(“Enter the height of the triangle:”))
Triangle(B, H)
else:
print (“invalid option”)
PROGRAM NO:5
Program to add one to all the even numbers and subtract one for all
odd numbers in a given list using the function EOReplace() and print
the modified list
AIM:
To write a program to add number one to all the even numbers and
subtract number one for all odd numbers in a given list using the
function EOReplace() and print the modified list
PROGRAM CODE:
def EOReplace(L):
newList = [ ]
for i in L:
if i % 2 ==0:
newList.append(i +1)
else:
newList.append(i -1)
return newList
L=[10, 20, 30, 40, 35, 55]
modified = EOReplace(L)
print(modified)
OUTPUT:
PROGRAM NO:6
PROGRAM CODE:
import random
import time
print("Press CTRL+C to stop the dice!!!")
play='y'
while play=='y':
try:
while True:
for i in range(1):
print()
n = random.randint(1,6)
print(n)
time.sleep(0.5)
except KeyboardInterrupt:
print("\nYour Number is :",n)
ans=input("Play More? (Y) :")
if ans.lower()!='y':
play='n'
break
OUTPUT:
PROGRAM NO:7
AIM:
PROGRAM CODE:
def countWord(str1,word):
s = str1.split()
count=0
for w in s:
if w==word:
count+=1
return count
str1 = input(“Enter any sentence :”)
word = input(“Enter word to search in sentence :”)
count = countWord(str1,word)
if count==0:
print(“## Sorry! “,word,” not present “)
else:
print(“## “,word,” occurs “,count,” times ## “)
Output:-
PROGRAM NO:8
program to read a text file line by line and display each word separated by a ‘#’
AIM:
To Write a Python program to read a text file line by line and display each word
separated by a ‘#’
PROGRAM CODE
file=open("AI.TXT","r")
lines=file.readlines()
for line in lines:
words=line.split()
for word in words:
print(word+"#",end="")
print("")
file.close()
OUTPUT:
PROGRAM NO:9
AIM:
PROGRAM CODE:
myfile=open("E:\\TextFile\\Poem.txt","r")
vowels="aeiouAEIOU"
count1=0
count2=0
data=myfile.read()
for i in data:
if i in vowels:
count1=count1+1
else:
count2=count2+1
print("Number of Vowels:",count1)
print("Number of Consonants:",count2)
OUTPUT:
Number of Vowels: 4
Number of Consonants: 21
PROGRAM NO:10
COPYING LINES IN A FILE Copy all the lines that contain the character `a' in a file
and write it to another file.
AIM:
To write a python program to Copy all the lines that contain the character `a' in a
file and write it to another file.
PROGRAM:
Fin=open(“xiics2.txt”,”r”)
Fout=open(“xiics4.txt”,”w”)
for line in fin:
word=line.split( )
for i in word:
for letter in i:
if letter==’a’ or letter==’A’:
Fout.write(line)
Fin.close( )
Fout.close( )
OUTPUT:
xiics2.txt
We are aware if ATM cards that are used in ATM machines.
xiics4.txt
We are aware if ATM cards that are used in ATM machines.
RESULT:
OUTPUT:
Total lines started with M or m : 1
PROGRAM NO:12
Write a program to Create a binary file with name and roll number using
dictionary. And search for a record with roll number as 34 or 36, if found display
the record.
AIM:
To write a program to Create a binary file with name and roll number
using dictionary. And search for a record with roll number as 34 or 36, if
found display the record.
PROGRAM
output:-
#create
Enter no of students: 2
Enter name: Jai
Enter roll no: 36
Enter name: vignesh
Enter roll no: 35
Data added successfully
#Search
Jai found in file
PROGRAM NO: 13
import csv
myfile=open('Product.csv','w',newline='')
prod_writer=csv.writer(myfile)
prod_writer.writerow(['CODE','NAME','PRICE'])
for i in range(2):
print("Product", i+1)
code=int(input("Enter Product code:"))
name=input("Enter Product Name:")
price=float(input("Enter Price:"))
prod=[code,name,price]
prod_writer.writerow(prod)
print("File created successfully!")
print('###############')
myfile.close()
myfile = open(‘Product.csv’,’r’)
prod_reader=csv.reader(myfile)
Data = list(prod_reader)
Count= len(Data)
print(“No of records:”, Count)
myfile.close()
Output :
Product 1
Enter Product code:104
Enter Product Name: Pen
Enter Price:200
Product 2
Enter Product code:405
Enter Product Name: Marker
Enter Price:50
File created successfully!
###############
No of records: 2
PROGRAM NO: 14
AIM:
PROGRAM CODE:
AIM:
PROGRAM CODE:
PROGRAM:
def isEmpty(Emp):
if Emp==[]:
return True;
else:
return False;
def Push(Emp,Name):
Emp.append(Name)
top=len(Emp)-1
def Pop(Emp):
if isEmpty(Emp):
return "Underflow"
else:
Name=Emp.pop()
if len(Emp)==0:
top=None
else:
top=len(Emp)-1
return Name
def Peek(Emp):
if isEmpty(Emp):
return "Underflow"
else:
top=len(Emp)-1
return Emp[top]
def Display(Emp):
if isEmpty(Emp):
print ("Stack Underflow")
else:
top=len(Emp)-1
print(Emp[top],"<-Top")
for a in range(top-1,-1,-1):
print(Emp[a])
# main segment
Stack=[]
top=None
while True:
print("----------------------")
print("Stack Operations")
print("----------------------")
print("1.Push")
print("2.Pop")
print("3.Peek")
print("4.Display")
print("5.Exit")
ch=int(input("Enter your choice(1-5):"))
if ch==1:
name=input("Enter Name of the Employee:")
Push(Stack,name)
elif ch==2:
name=Pop(Stack)
if name=="Underflow":
print("Stack Underflow!")
else:
print("Deleted Name is:",name)
elif ch==3:
name=Peek(Stack)
if name=="Underflow":
print("Stack Underflow")
else:
print("Topmost Name in Stack is:",name)
elif ch==4:
Display(Stack)
elif ch==5:
break
else:
print("Invalid Choice!")
OUTPUT:
Stack Operations
-----------------------
1.Push
2.Pop
3.Peek
4.Display
5.Exit
Enter your choice(1-5):1
Enter Name of the Employee: Kumar
-----------------------
Stack Operations
-----------------------
1.Push
2.Pop
3.Peek
4.Display
5.Exit
4.Display
5.Exit
Enter your choice(1-5):3
Topmost Name in Stack is: Arun
-----------------------
Stack Operations
-----------------------
1.Push
2.Pop
3.Peek
4.Display
5.Exit
Enter your choice(1-5):2
Deleted Name is: Arun
----------------------
Stack Operations
----------------------
1.Push
2.Pop
3.Peek
4.Display
5.Exit
Enter your choice(1-5):4
Kumar <-Top
----------------------
Stack Operations
----------------------
1.Push
2.Pop
3.Peek
4.Display
5.Exit
Enter your choice(1-5):2
Deleted Name is: Kumar
----------------------
Stack Operations
----------------------
1.Push
2.Pop
3.Peek
4.Display
5.Exit
Enter your choice(1-5):2
Stack Underflow!
-----------------------
Stack Operations
-----------------------
1.Push
2.Pop
3.Peek
4.Display
5.Exit
Enter your choice(1-5):5
###SQL QUERIES ###
DDL Commands:
• Create
• Alter
• Drop
18. DML Commands
• Insert
• Update
• Delete
DML Commands
19.SQL QUERIES - ORDER BY CLAUSE.
20. SQL QUERIES - GROUP BY CLAUSE.
21. COMPANY AND CUSTOMER
AIM:
To create two tables for company and customer and execute the given commands using SQL.
TABLE:COMPANY
TABLE:CUSTOMER
1. To display those company name which are having price less than 30000.
3. To increase the price by 1000 for those customer whose name starts with ‘S’
4. To add one more column total price with decimal (10,2) to the table customer
varchar(15));
int(3));
RESULT:
OUTPUT:
NAME
NEHA SONI
2. select name from company order by name desc;
NAME
SONY
ONIDA
NOKIA
DELL
BLACKBERRY
3. Update customer set price = price + 1000 where name like ‘s%’; select * from
customer;
To write a Python program to fetch all the records from the Student
Table.
PROGRAM ALGORITHM:
1. Start.
2. Import mysql.connector module.
3. Establish connection using connect().
4. Create Cursor instance using cursor().
5. Fetch all the records from the table using fetchall().
6. Traverse the result set using for loop and print the records.
7. Close the connection using close().
8. Stop.
PROGRAM CODE :
# Program to fetch all the records from Student table
import mysql.connector as sqltor
con=sqltor.connect(host="localhost",user="root",passwd="ss123",database="s
ps")
if con.is_connected==False:
print('Error')
cursor=con.cursor()
cursor.execute("select * from Student")
data=cursor.fetchall()
count=cursor.rowcount
print("No.Rows:",count)
for row in data:
print(row)
con.close()
SAMPLE OUTPUT :
No.Rows: 5
(101, 'Arun', 12, 'A', 450, 90)
(102, 'David', 12, 'B', 400, 80)
(103, 'Anand', 12, 'B', 350, 70)
(104, 'Reena', 12, 'A', 380, 76)
(105, 'Neha', 12, 'A', 420, 84)
23. PROGRAMS T0 FETCH RECORDS FROM STUDENT TABLE ON A
GIVEN CONDITION
AIM :
To write a Python program to fetch the records from the Student Table
PROGRAM ALGORITHM:
1. Start.
2. Import mysql.connector module.
3. Establish connection using connect().
4. Create Cursor instance using cursor().
5. Execute SQL query with string formatting.
6. Fetch all the records from the table using fetchall().
7. Traverse the result set using for loop and print the records.
8. Close the connection using close().
9. Stop.
PROGRAM CODE :
con=sqltor.connect(host="localhost",user="root",passwd="ss123",database="s
ps")
if con.is_connected==False:
print('Error')
cur=con.cursor()
data=cur.fetchall()
count=cur.rowcount
print("No.Rows:",count)
print(row)
con.close()
SAMPLE OUTPUT :
No.Rows: 3
AIM :
PROGRAM ALGORITHM :
1. Start.
2. Import mysql.connector module.
3. Establish connection using connect().
4. Create Cursor instance using cursor().
5. Execute insert command with string formatting and apply commit() to save
the changes permanently.
6. Execute select query and fetch all the records using fetchall().
7. Traverse the result set using for loop and print the records.
8. Close the connection using close().
9. Stop.
PROGRAM CODE :
Record Inserted
AIM :
PROGRAM ALGORITHM :
1. Start.
2. Import mysql.connector module.
3. Establish connection using connect().
4. Create Cursor instance using cursor().
5. Execute insert command with string formatting and apply commit()
to save the changes permanently.
6. Execute select query and fetch all the records using fetchall().
7. Traverse the result set using for loop and print the records.
8. Close the connection using close().
9. Stop.
PROGRAM CODE :
Record Updated