You are on page 1of 31

Sl.

No Topic Page
number
1 Arithmetic Operations 1-2

2 Perfect Number 3

3 Armstrong Number 4

4 Palindrome Number 5

5 List Operations 6-7

6 Floyd’s Triangle 8

7 Modifying Elements of a List 9


Counting the Number of Odd and Even Number in a
8 10
Tuple
9 Counting the Number of Vowels in a String 11

10 Random number between 100 and 1000 12

11 Counting the Occurrence of “my” 13


Counting Number of Digits, Upper-Case and Lower-
12 14
Case
13 Creating and Reading a Binary File 15
Searching for a record in Binary File
14 16

15 Creating CSV to enter Product Details 17

16 Stack Operations 18-19

17 Vowels in a String Using Stacks 20

18 Queries in SQL 21-23

19 Joining of Tables 24-25

20 SQL Connectivity 26-29

EXP NO:1 ARITHMETIC OPERATIONS


2
AIM:
To write a program to implement all the arithmetic operations.

CODE:
x=int(input("enter number 1:"))
y=int(input("enter number 2:"))
print("""
A-Addition
B-Subtraction
C-Multiplication
D-Division
E-Floor division
F-Remainder
G-Exponentiation""")

ans="y"
while ans=="y":
n=input("Enter the number of your choise: A or B or C or D or E or F or G.")
if n=="A":
print(x+y)
elif n=="B":
print(x-y)
elif n=="C":
print(x*y)
elif n=="D":
print(x/y)
elif n=="E":
print(x//y)
elif n=="F":
print(x%y)
elif n=="G":
print(x**y)
else:
print("INVALID ALPHABET ENTERED")
ans=input("Do you want to continue?.ANS with y(yes) or n(no)")

OUTPUT:

2
RESULT:
The above program was executed and output obtained.

2
EXP NO:2 PERFECT NUMBER
AIM:
To write a program to check whether the given number is a perfect number or not.

CODE:
n=int(input("Enter any number:"))
cs=0
for i in range(1,n):
if n%i==0:
cs=cs+i
if cs==n:
print("Yes ,the given number is a perfect number")
else:
print("No,the given number is not a perfect number")

OUTPUT:

RESULT:
The above program was executed and output obtained.

EXP NO:3 ARMSTRONG NUMBER


2
AIM:
To write a program to check whether the given number is an armstrong number or not.

CODE:
n=int(input("Enter any number:"))
a=str(n)
l=len(a)
cs=0
for i in a:
cs=cs+int(i)**l
if cs==n:
print("Yes ,the given number is an armstrong number")
else:
print("The given number is not an armstrong number")

OUTPUT:

RESULT:
The above program was executed and output obtained.

EXP NO:4 PALINDROME OF A NUMBER


AIM:
2
To write a program to find the palindrome of a number.

CODE:

def Palindrome(s):
l1=list(s)
l2=list(s)
l2.reverse()
if l1==l2:
return True
else:
return False
s=input("Enter a String:")
f=Palindrome(s)
if f==True:
print("The String",s,"is Palindrome")
else:
print("The String",s,"is Not a Palindrome")
OUTPUT:

RESULT:
The above program was executed and the output obtained.

EXP NO:5 ALL METHODS OF A LIST


AIM:
To write a program to implement all the methods of a list.
2
CODE:
n=eval(input("Enter any list you want:"))
print("The length of the above list is:,",len(n))
n.append(30)
print("Appending 30 to the last we get,",n)
n.extend([3,4])
print("Adding multiple elements to the list we get,:",n)
n.pop()
print("Deleting the last element of the list we get,:",n)
n.pop(3)
print("Deleting the element at index 1,we get,:",n)
n.reverse()
print("Reversing the above list we get,:",n)
n.sort()
print("Sorting the above list will result in:",n)
m=sorted(n)
print("Using the sorted function we get:",m)
s=n.copy()
print("Creating a perfect copy of the list :",s)
print("The number of times 1 is present:,",n.count(1))
print("The sum of the list is:,",sum(n))
print("The minimum value of the list is",min(n))
print("The maximum value of the list is ",max(n))
print("Finding the index value of element 2 we get,:",n.index(2))
print("Clearing the whole list we get,:",n.clear())
del n
print("Deleting the elements of the list:,", n)

OUTPUT:

2
RESULT:
The above program was executed and the output obtained.

EXP NO:6 FLOYD’S TRIANGLE


AIM:

2
To write a program to print the Floyd’s triangle pattern.

CODE:
n=int(input('enter no of rows'))
a=1
for i in range(1, n+1):
for j in range(1,i +1):
print(a, end = ' ')
a=a+1
print()

OUTPUT:

RESULT:
The above program was executed and the output obtained.

EXP NO:7 MODIFYING ELEMENTS OF A LIST


AIM:

2
To write a program to modify the elements of a list.

CODE:
n=eval(input('Enter a list:'))
o=1
while o>0:
x=eval(input('Enter the element to be searched for and modified:'))
y=eval(input('Enter the element to be replaced:'))
n[n.index(x)]=y
print("The modified list is",n)
s=input('do you want to continue(y/n)')
if(s=='y'):
pass
else:
print("The program has now come to an end.")
break
print(n)

OUTPUT:

RESULT:
The above program was executed and the output obtained.

EXP NO:8 COUNTING THE NUMBER OF EVEN AND ODD


NUMBERS IN A TUPLE

AIM:
2
To write a program to count the number of even and odd numbers in a tuple.

CODE:
n=eval(input('Enter a tuple which consists of numbers only:'))
odd=0
eve=0
for i in n:
if i%2==0:
eve+=1
else:
odd+=1
print('Number of odd numbers:',odd)
print('Number of even numbers:',eve)

OUTPUT:

RESULT:
The above program was executed and the output obtained.

EXP NO:9 COUNTING THE NUMBER OF VOWELS IN A STRING

AIM:
To write a program to count the number of vowels in a string.

2
CODE:
n=input('Enter any string of your choice:')
d={}
x=['a','i','o','e','u','A','E','I','O','U']
for i in n:
if i in x:
d[i]=n.count(i)
print(d)

OUTPUT:

RESULT:
The above program was executed and the output obtained.

EXP NO:10 RANDOM NUMBERS BETWEEN 10 AND 100

AIM:
To write a program to print random numbers between 10 and 100.

2
CODE:
import random
print(random.randint(10,100))

OUTPUT:

RESULT:
The above program was executed and the output obtained.

EXP NO:11 COUNTING THE NUMBER OF TIMES THE WORD ‘MY’ HAS
OCCURRED IN A GIVEN TEXT FILE

AIM:
To write a program to count the number of times the work ‘MY’ occurred in a given text file.

CODE:
2
def displayMy():

num=0
f=open("C:\\Users\\Student\\Documents\\STORY.txt","r")
N=f.read()
M=N.split()
for x in M:
if x=="MY":
print(x)
num=num+1
f.close()
print("Count of MY in file:",num)
displayMy()

OUTPUT:

RESULT:
The above program was executed and the output obtained.

EXP NO:12 COUNTING THE NUMBER OF DIGITS AND


UPPERCASE LETTERS IN A FILE
AIM:
To write a program to count the number of digits and uppercase letters in a file.

CODE:
myfile=open("C:\\Users\\Student\\Desktop\\inba\\New folder inban\\record\\m.txt","r")
dc=0
uc=0
2
x = myfile.read()
for i in x:

if i.isupper()==True:
uc=uc+1
elif i.isdigit()==True:
dc=dc+1
continue
print("The number of digits present-:",dc)
print("The number of uppercase characters present-:",uc)
myfile.close()

OUTPUT:

RESULT:
The above program was executed and the output obtained.

EXP NO:13 WRITE A PROGRAM TO ENTER STUDENT


NUMBER,NAME,MARKS OF THREE SUBJECTS UNTIL
THE USER ENDS,USING A BINARY FILE
AIM:
To write a program to create and read a binary file and enter the details of students until the user wants to .

CODE:
import pickle
stu={}
stufile=open('Stu.dat','ab')

2
ans='y'
while ans =='y':
rno=int(input("Enter roll number:"))
name=input("Enter name:")
pmarks=float(input("Enter marks in physics:"))
cmarks=float(input("Enter marks in chemistry:"))
mmarks=float(input("Enter marks in mathematics:"))
stu['Rollno']=rno
stu['Name']=name
stu['Marks in physics']=pmarks
stu['Marks in chemistry']=cmarks
stu['Marks in mathematics']=mmarks
pickle.dump(stu,stufile)
ans=input("Want to append more records?(y/n)...")
stufile.close()

OUTPUT:

RESULT:
The above program was executed and the output obtained.

EXP NO:14 SEARCH FOR A RECORD IN BINARY FILE

AIM:
To write a program to open a binary file and search for records with roll no as 12,24.

CODE:
import pickle
stu={}
found=False
fin=open('Stu.dat','rb')
searchkeys=[12,24]
try:
2
print("Searching........")
while True:
stu=pickle.load(fin)
if stu['Rollno'] in searchkeys:
print(stu)
found=True
except EOFError:
if found==False:
print("No such records found in the file")
else:
print("Search successful.")
fin.close()

OUTPUT:

RESULT:
The above program was executed and the output obtained.

EXP NO:15 CREATE A CSV FILE TO ENTER PRODUCT DETAILS

AIM:
To write a program to create a CSV file to enter the product details.

CODE:
import csv
n=int(input("Enter how many product record you want to create?"))
fh=open("Student.csv","w")
stuwriter=csv.writer(fh)
stuwriter.writerow(['ProductID','Product Name','Product Price'])
for i in range(n):
print("Student record:",(i+1))
2
productid=int(input("Enter product ID:"))
productname=input("Enter product name:")
productprice=float(input("Enter product price:"))
sturec=[productid,productname,productprice]
stuwriter.writerow(sturec)
fh.close()

OUTPUT:

RESULT:
The above program was executed and the output obtained.

EXP NO:16 STACK OPERATIONS

AIM:
To write a program to perform all stack operations.

CODE:
def Push(it):
global top
if S==[ ]:
top=0
S.append(it)
else:
top=top+1

2
S.append(it)
print("Element added successfully into Stack")
def Pop( ):
global top
if S==[ ]:
print("Underflow occurs. No Element to display")
else:
e=S.pop()
print("Deleted Element is:", e)
if S==[ ]:
top=None
else:
top=top-1
def Display( ):
if S==[ ]:
print("Stack is Empty. No Element to display")
else:
print("Element in the Stack are:")
t=top
while t>=0:
print(S[t], end='->')
t=t-1
print("!!!")
def Peek( ):
if S==[ ]:
print("Stack is Empty. No Element to display")
else:
print("Top Most Element in the Stack is:", S[top])
S=[ ]
top=None
while True:
ch=int(input("Enter 1 to insert, 2 to delete, 3 to display, 4 to peek & 5 to exit:"))
if ch==1:
it=int(input("Enter an element to insert:"))
Push(it)
elif ch==2:
Pop( )
elif ch==3:
Display( )
elif ch==4:
Peek( )
elif ch==5:
break
else:
print("Enter valid choice between 1 and 5")

2
OUTPUT:

RESULT:
The above program was executed and the output obtained.

EXP NO:17 PRINTING OF VOWELS USING STACKS CONCEPT

AIM:
To write a program to print the vowels in a string using stacks concept.

CODE:
def push(n):
global top
for i in n:
if i in'AEIOUaeiou':
s.append(i)
top=len(s)-1
print('Vowels in the stack are :')
t=top
while t>=0:

2
print(s[t], end=',')
t=t-1
print("!!!")

s=[]
top=None
n=input("Enter a string to be added:")
push(n)

OUTPUT:

RESULT:
The above program was executed and the output obtained.

EXP NO:18 QUERIES-SQL


AIM:
To create a table “Student” in SQL and perform the following queries
i) Display records of students who have scored more than 90 in Math
ii) Display the records of students whose name starts with A
iii) Display records of students whose fourth letter is E
iv) Display records by arranging names in descending order
v) Increase the mark by 2 in Physics for students who secured 80
vi) Display the set of students who reside only in Coimbatore
vii) Change the size of column of ‘Name’ field from 20 to 40
viii) Display the Total Mark and Average in Math
ix) Display the record with maximum marks in Physics
x) Write a command to delete a record from the table
xi) Write a command to drop the table
2
xii) Show the list of databases
xiii) Write a command to show the list of tables in a database
xiv) Write a command to describe the structure of the table

QUERIES:
1. create table Student(Sno int, AdmNo int, Name varchar(20), City varchar(30), Math
int, Physics int, Chemistry int);
2. select * from Student where Math>90;
3. select * from Student where Name like "A%";
4. select * from Student where Name like " E%";
5. select * from Student order by Name desc;
6. update Student set Physics = Physics + 2 where Physics = 80;
7. select * from Student where City = "Coimbatore";
8. alter table Student modify Name varchar(40);
9. select sum(Math) as "Total", avg(Math) as "Average" from Student;
10. select max(Physics) as "Maximum" from Student;
11. delete from Student;
12. drop table Student;
13. show databases;
14. show tables;
15. desc Student;

OUTPUT:
2)

3)

2
4)

5)

7)

9)

10)

2
15)

RESULT:
The above program was executed and the output obtained.

EXP NO:19 JOINING OF TABLES


AIM:
Create a table library with the following fields: Bid, Bname, IssueDate, ReturnStatus (yes/no), Sno (from
the previous Student table) and execute the following commands.
i. Display the student details along with the books issued for them.
ii. Display the names of the students who haven’t returned the books still.
iii. Display the details of the book along with the respective student name from old to new of IssueDate.
iv. Update the ReturnStatus to Yes for books issued before 2010.
v. Display the count of pending books.

CODE:
i. select * from Student,library where Student.SNo=library.SNo;
ii. select Name from Student,library where Student.SNo=library.Sno and ReturnStatus=”No”;
iii. select Name,Bid,Bname,IssueDate from Student,library where Student.SNo=library.Sno order by
IssueDate;

2
iv. update library set ReturnStatus=”Yes” where IssueDate

OUTPUT:

i)
ii)

iii)

iv)

v)

RESULT:
The above program was executed and the output obtained.

2
EXP NO:20 SQL CONNECTIVITY
AIM:
Write a Python program to connect the Student table to perform the following operations.
i. Insert a record
ii. Display the entire content of the table
iii. Delete a record
iv. Update a record

CODE:
import mysql.connector as mc
mycon = mc.connect(host="localhost", user="root", passwd="123456", database="Student") mycursor =
mycon.cursor()
def reg():

2
sno = int(input("Enter serial number:"))

Admno = input("Enter admission number:")

name = input("Enter name of the student:")

city = input("Enter city:")


math = int(input("Enter mathematics
mark"))
phy = int(input("Enter physics mark"))
chem = int(input("Enter the chemistry mark"))
s = "insert into student values(%s,'%s','%s','%s',%s,%s,%s)" % (sno, Admno, name, city, math, phy,
chem)
mycursor.execute(s)
mycon.commit()
def view():
s = "select * from Student"
mycursor.execute(s)
data = mycursor.fetchall()
print(data)
def delete():
nom = input("Enter the Admission number to delete record")

s = "delete from emp where Emp_nom='{}'"


mycursor.execute(s)
mycon.commit()

2
def upcity():
nom = input("Enter the Student number to update the city")

n = input("Enter the city to be updated")


s = "update Student set City = '%s' where AdmnNo = '%s'" % (n, no) mycursor.execute(s)
mycon.commit()
ch = "y"
while ch == "y":
print("1.To Register")
print("2.To Display the content of the table")
print("3.To Delete a record")
print("4.To Update the city")
n = int(input("Enter Any choice: """))

if n == 1:
register()
elif n == 2:
view()
elif n == 3:
dele()
elif n == 4:
upcity()
else:
print("Choose within the given range(1-4)")
ch = input("Do you want to continue?(y/n)")

OUTPUT:
i)Insert a record

28
ii)Display the contents of the table

iii)Delete a record

29
iv)Update a record

RESULT:
The above program was executed and the output obtained.

30
1

You might also like