Professional Documents
Culture Documents
AIM
To write a to make
program a
simple Arithmetic Calculator
ALGORITHM:
Step 1 GETthe option from the user for +, - .*,/,/1,%
Step 2 Perform arithmetic operation based on user choice
Step 3 Display the output
CODING:
# define functions
def add(x, y):
return x +y
def subtract(x, y):
returnx-y
def multiply(x, y):
return x *y
def divide(x, y):
return x /y
def floordiv(x, y):
return x y
def moddiv(x, y):
return x %y
#take input from the user
print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
print("5.Floor Division")
print("6.Modulo Division")
choice input("Enter choice(1/2/3/4/5/6):")
while choice<7':
numl int(input("Enter first number: ")
num2 int(input("Enter second number: "))
ifchoice='1':
print(num1,"+"num2,"=", add(numl,num2))
elif choice=2:
print(num1,""num2,"=", subtract(numl,num2))
elif choice='3:
print(num1,"*"num2,"=", multiply(num1,num2))
elif choice== 4':
print(num1,"/",num2,"=", divide(num1,num2))
elif choice="5:
print(num1,"//",num2,"=",floordiv(numl,num2))
elif choice=6':
print(num1,"%",num2,"=",moddiv(numl,num2))
choice = input("Enter choice(1/2/3/4/5/6):")
print("Exited")
OUTPUT:
Select operation.
1.Add
2.Subtract
3.Multiply
4.Divide
5.Floor Division
6.Modulo Division
Enter choice(1/2/3/4/5/6):1
Enter first number: 10
Enter second number: 20
10+ 20 30
Enter choice(1/2/3/4/5/6):2
Enter first number: 30
Enter second number: 15
30 -15 15
Enter choice(1/2/3/4/5/6):3
Enter first number: 30
Enter second number: 10
30 * 1 0 = 300
Enter choice(1/2/3/4/5/6):4
Enter first number: 30
Enter second number: 4
30/4 7.5
Enter choice(1/2/3/4/5/6):5
Enter first number: 30
Enter second number: 4
30//4 7
Enter choice(1/2/3/4/5/6):6
Enter first number: 30
Enter second number: 4
30 % 4 2
Enter choice(1/2/3/4/5/6):7
Exited
>>>
RESULT:
Thus the program has been executed successfully for designing a simple arithmetic
calculator and output was verified
Admn Date
Age Integer
Fee
Integer
Gender Char(2)
b) Insert data in the table STUDENT.
c) Alter the table STUDENT with the new attribute, father's name with the
d) Modify the attribute, gender with type of Char(7) in the table STUDENT.
type of Char(10).
e) Drop the attribute father's name from the table
f) Update the table STUDENT, and modify the data.
STUDENT
g) Use Order by to display data in ascending/ descending order
h) Use delete command to remove the last field from the student table.
i) Group the table STUDENT by dept.
j) Give the Output of following:
i. Select COUNT(distinct dept) from student.
ii. Select MAX(age) from the student where Gender="F°
ii. Select MIN(age) from the student where Gender-"M.
iv. Select AVG(fee) from the student where Admn=01/01/2000".
v. Select SUM(age) from the student where Admn>'05/09/2000.
SQL Commands:
mysq> create database lab;
Query Ok
mysql>use lab;
database changed
a) mysql> create table student(name char(20),dept char(10),
Admn date, age
admn;
g)mysql>select * from student order by
no nane dept adnn age fee gender
english} 1996-12-12 20 12000
asha 1998-01-10 22 I 2500 1m
jeni history 25 3000
mala history I 1998-02-02
Pakesh computer 2006-02-203 4000
3075
tanil7 2000-07-03 1
maria
2ulu conputer i 2560-09-05 4500
desc:
select *
from student order by name
mysq>
Iadmn age fee gender
no nane dept
computer 2000-09-05 19 4500
2ulu 23 4000
rakesh computer 2000-U2-20
2000-07-03 22 30?5
marla tani 1998-02-02 25 3000
mala ! history 22 2500 n
Jen 1998-01-10 2000 f
asha english 1996-12-12 20
rakesh 2000-02-20
asha 1996-12-12
jeni 1998-81-10
)
1. mysql>select count(distinct dept) from student;
Result
Thus the sQL commands are verified and executed successfully.
17. SQL COMMANDS FOR THE RELATIONS STATIONARY AND CONSUMER
AIM:
To write SQL commands for the following on the basis of given relations.
Table: Stationary
SID StationaryNane Company Price
DPO1 Dot Pen ABC 10
PLO2 Pencil XYZ
ERO5 Eraser XYZ
PLO1 Pencil CAM
Table: Consumer
SQL Commands:
mysql>use lab;
database changed
mysql>create table stationary(s_id varchar(20),stationaryname char(20),company char(10),price
integer);
Query Ok
mysql>create table consumer(c_id integer,consumername char(20),address char(10),s_id
varchar(10);
Query Ok
mysql>insert into stationary values('DPO1',' DotPen',' ABC',10);
Query Ok
mysql>insert into stationary values('PL02',' Pencil',' XYZ',6);
Query Ok
mysql>insert into stationary values("ERO5','Eraser',' XYZ',7);
Query Ok
mysql>insert into stationary values(°PLO1','Pencil',' CAM',5);
Query Ok
mysql>insert into stationary values( GP02',' GelPen',' ABC',15):
Query Ok
mysq>insert into consumer values(01,' goodlearner',' delhi'", pl00");
Query Ok
mysql>insert into consumer values(06,'writewell',mumbai",'gp02");
Query Ok
mysql>insert into consumer values(12,'topper,' delhi','dp0l');
Query Ok
mysqinsert into consumer values(15,'write&draw',' delhi','pl02');
Query Ok
mysq>insert into consumer values(16,'motivation','bangalore','pl01):
Query Ok
. select *from consumer where address= delhi':
mysq>
consunername address sid
C_id
de lhi p100
goodlearner de lhi dpbl
topper
Write draw de lhi p102
and 15:
11.
mysqi>select
*
from stationary where price between 8
s-id stationarynane conpany price
dp01 dotpen abc
9p02 gelpen abc
where
111. mysql select consumername.address.company.price from consumer.stationary
consumer.s_id=stationary.s id;
Consunernane address conpany price
.---
xyz 9
Result:
Thus the SQL commands are verified and executed successfully.
Table: Salary
1D BASIC ALLOWANCE CONSULTATION
101 12000 1000 300
- -+---==-t-=-
id nane experience
114 lara
Lucy
13000.0800
TABLE: FARE
vi)
DELHI" AND FLIGHTS.FL_NO = FARES.FL_NO
SQL COMMANDS:
t--=---
fl no startingflight | ending no_flights | no stops
t -t-
AM501 DELHI TRIVENDRUM
AMB12 KANPUR BANGLORE
C799I BANGALORE DELHI 2 1
IC301 MUMBAI DE LHI 8
IC302 DELHI LMUMBAI 8
IC701 DE LHI TAHMEDABAD 4
IC899 MUMBAT T KOCHI
MC101 INDORE MUMBAI 3
MUA99 MUMBAI MADRAS 3 3
------
111 mysql> Select flights.fl_no, FARE+FARE+(TAX/100) from FLIGHTS, FARES where
Startinglight"DELHI" AND Ending-="MUMBAI;
fno FARE FARE (TAX/100)
+-
IC302 13000.10ee
IC302 18800.05e0
IC302 26900.08e0
IC302 1 16600.D4eo
IC302 8600.1000
IC302 21000.100e
IC302 7000.0400
mysql> Select min(FARE) fronm FARES Where AlRLINES="Indian Airlines"
nin(FARF)I
4303
V1.
FLNO | NO_FLIGHTS I ATRLINES
AIM:
To create two tables for item and traders and execute the given commands using SQL.
TABLE: 1TEM
Qty Price Company TCode
Code IName
i) To display the details of all the items in ascending order of item names (i.e IName)
ii) To display item name and price of all those items, whose price is in the range of
10000 and 22000 (both values inclusive)
ii) To display the number ofitems, which are traded by each trader. The expected
output of this query should be
TO1
TO2
TO3
d i s p l a y the Price. item name (i.e IName) and quantity (i.e Qty) of those item
which have quantity more than 150.
v) To display the names of those traders, who are either from DELHI or from
MUMBAl.
CREATE TABLE ITEM(Code int . IName char(25).Qty int. Price int, Company
char(25).
TCode char(5):
INSERTINTO ITEM VALUES(1001,"DIGITAL PAD 121".120. 11000."XENTIA.
TO1 "):
INSERT INTO ITEM VALUES(1006,"LED SCREEN 40".70. 38000,"S.ANTORA".
T02"):
CREATE TABLETRADERS(TCode char(5). TName char(25), City char(20):
INSERT INTO TRADERS VALUES("TO1"."ELECTRONICS
SALES."MUMBAl"):
INSERTINTO TRADERSs VALUES("T03":"BUSY STORE CORp":"DELHT):
i) select * from ITEM order by IName;
elect IName , Price from ITEM where Pricebetween 10000 and 22000:
IName Price
DIGITAL PAD 121 11000
RESULT:
Thus the given program executed successfully.
AIM:
To connect with database and create database and create table and insert value in the table.
ALGORITHM:
Step 1: Create the connectorobject sqltor for the class mysql connector.
Step 2: Create Connection Object myeon using the connector Object sqltor.
Step 3: Create Cursor object 'cursor' through myeon.
Step 4: Execute the query using cursor.execute()
Step 5: Close the connection object as mycon.close()
Coding:
import mysql.comnector as sqltor
con=sqltor.connect(host=localhost, user=root.password-admin')
if con.is_connected()==False:
print("error connecting to MySQL database")
cursor=con.cursor()
cursor.execute("use lab3")
cursor.execute("create table emp(name varchar(20))")
for i in range(5):
name=input("enter the name")
cursor.execute(st)
con.commit()
con.close()
output
enter the name alma
MYSQL MODULE TO
PYTHON BY IMPORTING THE
22.INTEGRATE SQL WITH
TABLE
SELECT VALUE FROM THE
AIM:
record of the student.
To connect with database and select first three
ALGORITHM:
for the class mysql connector.
Step 1: Create the connector object sqltor
using the connector Object sqltor.
Step 2: Create Connection Object mycon
'cursor' through mycon.
Step 3: Create Cursor object
cursor.execute()
Step 4: Execute the query using
connection object as mycon.close(0
Step 5: Close the
CODING:
import mysql.comnector as sqltor
c o n = s q l t o r . c o n n e c t ( h o s t = l o c a l h o s t , u s e r = r o o t ' , p a s s w o r d = - a d m i n ' )
if con.is_connected()=False:
print(error connecting to MySQL database")
cursor=con.cursor())
cursor.execute("create database lab5")
cursor.execute("use lab5")
varchar(20))")
cursor.execute("create table emp(name
for i in range(3):
name-input("enter the name")
st-"insert into emp(name) values('}')".format(name)
cursor.execute(st)
con.commit()
cursor.execute("select * from emp")
data-cursor.fetchmany(3)
count=cursor.rowcount
for row in data:
print(row)
con.close()
OUTPUT:
enter the name AlLMA
enter the name ANNIE
enter the name RIMO
ALMA')
(ANNIE,)
RIMO'.)
RESULT:
Thus the program is
executed
successfully for connecting with database.
23. INTEGRATE sQL WITH PYTHON BY
DELETE VALUE FROM THE TABLE IMPORTING THE MYSsQL MODULE TO
AIM:
To connect with database and
delete the record from the table.
ALGORITHM:
Step 1: Create the connector object sqltor for the class
Step 2: Create Connection Object mycon mysqi connector.
Step 3: Create Cursor object 'cursor' using the connector
Object sqltor.
Step 4: Execute the query using through mycon.
cursor.execute()
Step 5: Close the connection object
CODINGS:
as
mycon.close()
import mysql.connector as sqltor
con=sqltor.connect(host="localhost'
if con.is_connected()=False:
,user=root.password='admin')
print("error connecting to MysQL database")
cursor=con.cursor()
cursor.execute("ereate database lab5")
cursor.execute("use lab5")
cursor.execute("create table emp(name varchar(20))")
for i in range(3):
name=input("enter the name")
st="insert into emp(name) values(}')".format(name)
cursor.execute(st))
con.commit()
cursor.execute("delete from emp where name='alma ")
con.commit()
con.close0
QUTPUT:
cnter the name alma
enter the name annie
enter the name rimo
RESULT:
Thus the program is executed successfully for connecting with database.
con-sqltor.connect(host="localhost',user="root,password='admin',database='student')
cursor-con.cursor()
cursor.execute("update student set mark1=90 where name=raja' ")
con.commit()
print(cursor.rowcount,"record(s) affected")
con.close0
OUTPUT:
I record(s) affected
RESULT:
Thus the program is executed successfully for connecting with database.