Computer SCIENCE Practical File

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

1.

Read a text file line by line and display each word


separated by a “#”.

PROGRAM CODE:

#Program to read a text file line by line and display each word separated by a “#”.
file=open('text-1.txt','r')
for i in file:
words=i.split()
for a in words:
print(a+"#",end=' ')
file.close()

TEXT FILE: text-1

OUTPUT:
2. Read a text file and display the number of vowels
and consonants in the file.
PROGRAM CODE:
#Program to read a text file and display the number of vowels and consonants in the
file
a=0
b=0
vowels='aeiouAEIOU'
consonants='bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ'
f=open('text-2.txt','r')
for i in f:
for j in i:
if j in vowels:
a+=1
elif j in consonants:
b+=1
print(a,'vowels and',b,'consonants')
f.close()

TEXT FILE: text-2

OUTPUT:
3. Read a text file and display the number of uppercase
and lowercase characters in the file.
PROGRAM CODE:
a=0
b=0
f=open('text-3.txt','r')
fori in f:
for j in i:
ifj.isupper():
a+=1
elifj.islower():
b+=1
print(a,'UPPERCASE letters and',b,'LOWERCASE letters')
f.close()

TEXT FILE: text-3

OUTPUT:
4. Define a function to find whether a number is
perfect or not.

PROGRAM CODE:
#To find whether a number is perfect or not
defperfnum(num):
divsum=0
fori in range(1,num):
ifnum%i==0:
divsum+=i
ifdivsum==num:
print(num,'Perfect Number')
else:
print(num,'Not a Perfect Number')

OUTPUT:
5. Define a function to check whether the entered
number is Armstrong or not.
PROGRAM CODE:
#Program to check whether entered number is Armstrong or not.
#An Armstrong number has sum of cubes of its digits is equal to the number
itself.
def armsnum(num):
num1=num
sum=0
while(num>0):
a=num%10
sum=sum+(a**3)
num=num//10
if sum==num1:
print(num1,'is an armstrong number')
else:
print(num1,'is not an armstrong number')

OUTPUT:
6. Define a function to calculate the factorial of
entered number.

PROGRAM CODE:

#Program to calculate factorial of an inputted number


def fact(num):
fact=1
i=1
fori in range(num,0,-1):
fact=fact*i
print("The factorial of",num,"=",fact)

OUTPUT:
7. Program to add data to an existing file.

PROGRAM CODE:
#program to add data to an existing file
f=open("test-3.txt",'a')
f.write("We are using Python \n")
f.write("Python is easy to use")
print("More data appended to the file")
f.close()

TEXT FILE: text-3

OUTPUT:
8. Program to implement all basic operations of a stack,
such as adding element (PUSH operation) and displaying
the stack elements (Traversal operation) using lists.

PROGRAM CODE:
#Implementation of list as stack
s=[]
c='y'
while(c=='y' or c=='Y'):
print('1.PUSH')
print('2.POP')
print('3.Display')
choice=int(input('Enter your choice: '))
if (choice==1):
a=input("Enter any number: ")
s.append(a)
elif (choice==2):
if (s==[]):
print('Stack Empty')
else:
print('Deleted item is:',s.pop())
elif (choice==3):
l=len(s)
fori in range(l-1,-1,-1):
print(s[i])
else:
print('Wrong Input')
c=input('Do you want to continue or not?')
OUTPUT:
9. Program to display unique vowels present in the given
word using stack.

PROGRAM CODE:
#Program to display unique vowels present in the given word using stack.
vowels=['a','e','i','o','u','A','E','I','O','U']
word=input("Enter the word to search for vowels:")
Stack=[]
for letter in word:
if letter in vowels:
if letter not in Stack:
Stack.append(letter)
print(Stack)
print('The number of different vowels present in','"',word,'"','is',len(Stack),'.')

OUTPUT:
10. Program to add, delete and display elements from a
queue using list.

PROGRAM CODE:
#Implementing List as a Queue - Using function pop()
a=[]
c='y'
while(c=='y' or c=='Y'):
print('1.INSERT')
print('2.DELETE')
print('3.Display')
choice=int(input('Enter your choice: '))
if (choice==1):
b=int(input("Enter new number: "))
a.append(b)
elif (choice==2):
if (a==[]):
print('Queue Empty')
else:
print('Deleted item is:',a[0])
a.pop(0)
elif (choice==3):
l=len(a)
fori in range(0,l):
print(a[i])
else:
print('Wrong Input')
c=input('Do you want to continue or not? ')
OUTPUT:
11.Program to create a new database ‘school’ in MySQL
through Python.

PROGRAM CODE:
#to create a new database ‘school’ in MySQL through Python.
importmysql.connector
mydb =
mysql.connector.connect(host='localhost',user='root',passwd='51411987')
mycursor = mydb.cursor()
mycursor.execute("CREATE DATABASE school")

MySQL PROMPT:
12.Program to create a table ‘student’ inside the database
‘school’ using Python script mode as the interface.

PROGRAM CODE:
#To create a table in MySQL using Python Interface
importmysql.connector
mydb = mysql.connector.connect(host='localhost',\
user='root',\
passwd='51411987',\
database='school')
mycursor = mydb.cursor()
mycursor.execute("CREATE TABLE student(Rollnoint(3) Primary key,\
Name varchar(15), age integer, city char(8))")

MySQL PROMPT:
13.Program to check for the created table ‘student’ using
Python.

PROGRAM CODE:
#to check whether the created table exists in
#MySQL using Python interface
importmysql.connector
mydb = mysql.connector.connect(host='localhost',\
user='root',\
passwd='51411987',\
database='school')
mycursor = mydb.cursor()
mycursor.execute("SHOW TABLES")
for x in mycursor:
print(x)

OUTPUT:
14.Program to ADD a new column ‘marks’ in the student
table.

PROGRAM CODE:
#to modify table student (adding a new column) in
#MySQL using Python interface
importmysql.connector
mydb = mysql.connector.connect(host='localhost',\
user='root',\
passwd='51411987',\
database='school')
mycursor = mydb.cursor()
mycursor.exzecute("Alter table student add(marks int(3))")

MySQL PROMPT:
15.Program to INSERT a record into the table student using
Python interface.

PROGRAM CODE:
#To insert a new record into the table student in
#MySQL using Python interface
importmysql.connector
mydb = mysql.connector.connect(host='localhost',\
user='root',\
passwd='51411987',\
database='school')
mycursor = mydb.cursor()
mycursor.execute("INSERT INTO student
VALUES(1,'Tarun',23,'Mumbai',398)")
mydb.commit()
print(mycursor.rowcount,"Record Inserted")
OUTPUT:

MySQL PROMPT:
16.Program to INSERT multiple records into the table
student through Python shell.
PROGRAM CODE:
#To insert multiple records into the table student in
#MySQL using Python interface
importmysql.connector
mydb = mysql.connector.connect(host='localhost',\
user='root',\
passwd='51411987',\
database='school')
mycursor = mydb.cursor()
mycursor.execute("INSERT INTO student VALUES(2,'Pooja',21,'Chail',390)")
mycursor.execute("INSERT INTO student
VALUES(3,'Radhika',18,'Shimla',388)")
mycursor.execute("INSERT INTO student VALUES(4,'Sonia',24,'Goa',300)")
mycursor.execute("INSERT INTO student VALUES(5,'Vinay',25,'Pune',410)")
mycursor.execute("INSERT INTO student
VALUES(10,'Shaurya',15,'Delhi',345)")
mydb.commit()
print(mycursor.rowcount,"Record Inserted")
OUTPUT:

MySQL PROMPT:
17.Program to DISPLAY all the records of student table using
Python.

PROGRAM CODE:
#Executing SELECT statement using Python
importmysql.connector
mydb = mysql.connector.connect(host='localhost',\
user='root',\
passwd='51411987',\
database='school')
mycursor = mydb.cursor()
mycursor.execute("Select * from student")
myrecords = mycursor.fetchall()
for x in myrecords:
print(x)
OUTPUT:
18.Program for deleting records through Python Interface.

PROGRAM CODE:
#Deleting records through Python Interface
importmysql.connector
mydb = mysql.connector.connect(host='localhost',\
user='root',\
passwd='51411987',\
database='school')
mycursor = mydb.cursor()
mycursor.execute("DELETE FROM student where Rollno = 1")
mydb.commit()
print(mycursor.rowcount,"Record(s) Deleted")
OUTPUT:

MySQL PROMPT:
19.Program for updating records through Python Interface.

PROGRAM CODE:
#Updating records through Python Interface
importmysql.connector
mydb = mysql.connector.connect(host='localhost',\
user='root',\
passwd='51411987',\
database='school')
mycursor = mydb.cursor()
mycursor.execute("UPDATE student set age = 28 where Name = 'Vinay'")
mydb.commit()
print(mycursor.rowcount,"Record(s) Updated")
OUTPUT:

MySQL PROMPT:

RECORD UPDATED
20.Program to delete the record of a student on the basis of
name fetched from the user at run-time.

PROGRAM CODE:
#Deleting records through Python Interface
importmysql.connector
mydb = mysql.connector.connect(host='localhost',\
user='root',\
passwd='51411987',\
database='school')
mycursor = mydb.cursor()
nm=input("Enter name of the student whose record is to be deleted:")
#Preparing SQL statement to delete records as per given condition
#sql ="DELETE FROM student WHERE Name = 'nm'"
try:
mycursor.execute("DELETE FROM student WHERE Name = 'nm'")
print(mycursor.rowcount,"record(s) deleted")
mydb.commit()
except:
mydb.rollback()
mydb.close()
OUTPUT:

MySQL PROMPT:

You might also like