output
output
output
print('Arithmetic Operators')
ch='yes'
while(ch=='yes'):
result=0
val1=float(input('Enter first value:'))
val2=float(input('Enter second value:'))
op=input('Enter any one operator(+,-,*,/,%,//):')
if op=='+':
result=val1+val2
elif op=='-':
result=val1-val2
elif op=='*':
result=val1*val2
elif op=='/':
result=val1/val2
elif op=='%':
result=val1%val2
elif op=='//':
result=val1//val2
else:
print('Enter the correct given operator:')
print('The result:',result)
ch=input('Do you want to continue?(yes/no):')
if ch=='no':
break
OUTPUT:
Arithmetic Operators
Enter first value:50
Enter second value:25
Enter any one operator(+,-,*,/,%,//):+
The result: 75.0
Do you want to continue?(yes/no):yes
Enter first value:52
Enter second value:22
Enter any one operator(+,-,*,/,%,//):-
The result: 30.0
Do you want to continue?(yes/no):yes
Enter first value:50
Enter second value:27
Enter any one operator(+,-,*,/,%,//):*
The result: 1350.0
Do you want to continue?(yes/no):yes
Enter first value:58
Enter second value:14
Enter any one operator(+,-,*,/,%,//):/
The result: 4.142857142857143
Do you want to continue?(yes/no):yes
Enter first value:200
Enter second value:4
Enter any one operator(+,-,*,/,%,//):%
The result: 0.0
Do you want to continue?(yes/no):yes
Enter first value:44
Enter second value:4
Enter any one operator(+,-,*,/,%,//)://
The result: 11.0
Do you want to continue?(yes/no):no
2.creating a menu driven program to find factorial and sum of list of numbers
using function
def Factorial(n):
F=1
if n<0:
print("Sorry, we cannot take Factorial for Negative number")
elif n==0:
print("The factorial of 0 is 1")
else:
for i in range(1, n+1):
F=F*1
print("The Factorial of",n, "is:",F)
def Sum_List(L):
Sum=0
for i in range(n):
Sum=Sum+L[i]
print("The Sum of List is:", Sum)
#Main Program
if op==1:
n=int(input("Enter a number to find Factorial:"))
Factorial(n)
elif op==2:
L=[]
n=int (input("Enter how many elements you want to store in List?:"))
for i in range (n):
el=int (input())
L.append(el)
Sum_List(L)
OUTPUT:
1. To Find Factorial
2. To Find sum of List elements
Enter your choice:1
Enter a number to find Factorial:2
The Factorial of 2 is: 1
1. To Find Factorial
2. To Find sum of List elements
Enter your choice:2
Enter how many elements you want to store in List?:7
10
25
40
55
70
95
100
The Sum of List is: 395
OUTPUT:
Enter sentence :'Python is a programming Language'
enter the spacing :
The string entered is a word False
The string entered in lower case: 'python is a programming language'
The string entered is in lower case: False
The string entered in lower case : 'PYTHON IS A PROGRAMMING LANGUAGE'
The string entered is in lower case: False
The string entered after removing the space from left side: 'Python is a
programming Language'
The string entered after removing the space from right side: 'Python is a
programming Language'
The string entered contains whitespace: False
The string entered is titlecased : False
The string entered after joining with : ' P y t h o n i s a p r o g r a m m i n
g Language'
The string entered after swaping case: 'pYTHON IS A PROGRAMMING
lANGUAGE'
5. Text file - to copy one file to another
print('---TEXT FILE---')
file=open('recordsample.txt','r')
lines=file.readlines()
file.close()
file=open('csc.txt','w')
file1=open('output.txt','w')
for line in lines:
if 'a' in line or 'A' in line:
file1.write(line)
else:
file.write(line)
print('All lines that contains ''a'' character has been removed in csc.txt file')
print('All lines that contains ''a'' character has been saved in output.txt file')
file.close()
file1.close()
OUTPUT:
--TEXT FILE---
All lines that contains a character has been removed in csc.txt file
All lines that contains a character has been saved in output.txt file
6. Text file - to read file and display each word separated by #
print("----TEXT FILE----")
file=open("recordsample.txt","r")
lines=file.readlines()
for line in lines:
words=line.split()
for word in words:
print(word+"#",end="")
print("")
file.close()
OUTPUT:
----TEXT FILE----
I#shall#be#telling#with#a#sigh#
Somewhere#ages#and#ages#hence:#
Two#roads#diverge#in#a#wood,#and#I#
I#took#the#one#less#travelled#by#
-ROBERT#FROST#
7. Text file - to read file and display number of vowels , consonants , uppercase , lowercase characters in the
file
print("----TEXT FILE----")
file=open("recordsample.txt","r")
content=file.read()
vowels=0
consonants=0
lower_case_letters=0
upper_case_letters=0
for ch in content:
if(ch.islower()):
lower_case_letters+=1
if(ch.isupper()):
upper_case_letters+=1
if(ch in ["a","e","i","o","u"]):
vowels+=1
elif(ch in ['b','c','d','f','g','h',,'j','k','l','m','n','p','q','r','s','t','v','w','x','y','z']):
consonants+=1
file.close()
print("Vowels:",vowels)
print("Consonants",consonants)
print("lower_case_letters:",lower_case_letters)
print("upper_case_letters:",upper_case_letters)
OUTPUT:
----TEXT FILE----
Vowels: 38
Consonants 58
lower_case_letters: 96
upper_case_letters: 16
8. Binary file - to create a student data and search for a data
print("---BINARY FILE---")
import pickle
student_data={}
list_of_students=[]
no_of_students=int(input("Enter number of students:"))
for i in range(no_of_students):
student_data["roll_no"]=int(input("Enter student rollno:"))
student_data["name"]=input("Enter student name:")
list_of_students.append(student_data)
student_data={}
file=open("student_data.dat","wb+")
pickle.dump(list_of_students,file)
print("Data inserted sucessfully")
file.close()
while True:
file=open("student_data.dat","rb+")
rollno=int(input("Enter rollno to be searched:"))
list_of_students=pickle.load(file)
found=False
for student_data in list_of_students:
if student_data["roll_no"]==rollno:
print(student_data["name"],"Found in file")
found=True
if(found==False):
print("student data not found please try again")
found=True
ch=input("Do you want to continue?(yes/no):")
if ch=="no":
break
file.close()
OUTPUT:
---BINARY FILE---
Enter number of students:4
Enter student rollno:1
Enter student name:ANK
Enter student rollno:2
Enter student name:BALA
Enter student rollno:3
Enter student name:RAM
Enter student rollno:4
Enter student name:SAM
Data inserted sucessfully
Enter rollno to be searched:4
SAM Found in file
Do you want to continue?(yes/no):yes
Enter rollno to be searched:2
BALA Found in file
Do you want to continue?(yes/no):no
import fun
from fun import area
from fun import perimeter
from fun import ke
from fun import pe
from fun import vo
print('****simple scientific calculator****')
print('----enter all the quantity in SI units----')
l=int(input('enter the length:'))
b=int(input('enter the width:'))
h=int(input('enter the height:'))
m=int(input('enter the mass:'))
v=int(input('enter the velocity:'))
he=int(input('enter the height at which object is situated:'))
print('area is',area(l,b))
print('perimeter is',perimeter(l,b))
print('kinetic energy is',ke(m,v))
print('potential energy is',pe(m,he))
print('volume is',vo(l,b,h))
OUTPUT:
****simple scientific calculator****
----enter all the quantity in SI units----
enter the length:20
enter the width:30
enter the height:17
enter the mass:250
enter the velocity:27
enter the height at which object is situated:17
area is 600
perimeter is 100
kinetic energy is 91125.0
potential energy is 41692.5
volume is 10200
AIM: To write Queries for the following Questions based on the given table
(f) Write a Query to insert all the rows of above table into STU table.
mysql> INSERT INTO STU VALUES (1,'Arun','M', 24,'COMPUTER','1997-01-10',
120);
Query OK, 1 row affected
mysql> INSERT INTO STU VALUES (2,'Ankit','M', 22,'PHYSICS','1998-03-11',
250);
Query OK, 1 row affected
mysql> INSERT INTO STU VALUES (3,'Akash','M',25,'HISTORY','1999-07-
07',300);
Query OK, 1 row affected
mysql> INSERT INTO STU VALUES(4,'Anusha','F',27,'ENGLISH','1998-09-
19',210);
Query OK, 1 row affected
mysql> INSERT INTO STU VALUES(5,'Umesh','M',26,'HISTORY','1995-05-
07',240);
Query OK, 1 row affected
mysql> INSERT INTO STU VALUES(6,'Usha','F',29,'COMPUTER','1992-02-
22',270);
Query OK, 1 row affected
mysql> INSERT INTO STU VALUES(7,'Yukesh','M',23,'COMPUTER','1996-02-
07',145);
Query OK, 1 row affected
mysql> INSERT INTO STU VALUES(8,'Usha','F',24,'ENGLISH','1994-05-05',175);
Query OK, 1 row affected
(g) Write a Query to display all the details of the Employees from the above
table 'STU'.
mysql> select * from STU;
(h) Write a query to Rollno, Name and Department of the students from STU
table
mysql>SELECT ROLLNO,NAME,DEPT FROM STU;
20. SQL Commands Exercise - 2
AIM: To write Queries for the following Questions based on the given table:
(e) Write a Query to display the name of the students whose name is starting
with 'A'
mysql> SELECT NAME FROM STU WHERE NAME LIKE 'A%';
(f) Write a query to list the names of those students whose name have
second alphabet 'n' in their names.
mysql> SELECT NAME FROM STU WHERE NAME LIKE '_N%';
21. SQL Commands Exercise - 3
AIM: To write Queries for the following Questions based on the given table:
(b) Write a Query to change the fess of Student to 170 whose Roll number is
1, if the existing fess is less than 130.
mysql> UPDATE STU SET FEES=170 WHERE ROLLNO=1 AND FEES<130;
Query OK, 1 row affected
(c) Write a Query to add a new column Area of type varchar in table STU
mysql> ALTER TABLE STU ADD AREA VARCHAR(20);
Query OK, 7 rows affected
(d) Write a Query to Display Name of all students whose Area Contains NULL .
mysql> SELECT NAME FROM STU WHERE AREA IS NULL;
(e) Write a Query to delete Area Column from the table STU.
mysql> ALTER TABLE STU DROP AREA
while True:
own_id=input("enter the owner id of the owner:")
oname=input("enter the name of owner:")
profit=input("enter the profit of the owner:")
query="insert into owner values('{}','{}','{}')".format(own_id,oname,profit)
cur.execute(query)
con.commit()
print("row inserted successfully")
ch=input("do you want to enter more records?(yes/no)")
if ch=="no":
break
cur.execute("select*from owner")
data=cur.fetchall()
for i in data:
print(i)
print("total number of rows retrived=",cur.rowcount)
OUTPUT:
table created successfully
enter the owner id of the owner:1
enter the name of owner:Nikhil
enter the profit of the owner:20000
row inserted successfully
do you want to enter more records?(yes/no)yes
enter the owner id of the owner:2
enter the name of owner:Vishwa
enter the profit of the owner:30000
row inserted successfully
do you want to enter more records?(yes/no)yes
enter the owner id of the owner:3
enter the name of owner:Yashwanth
enter the profit of the owner:25000
row inserted successfully
do you want to enter more records?(yes/no)no
('1', 'Nikhil', '20000')
('2', 'Vishwa', '30000')
('3', 'Yashwanth', '25000')
total number of rows retrived= 3
23. Write a program to integrate SQl with python by importing mysql module to update and
display the employee details.
import mysql.connector as ms
con=ms.connect(host="localhost",user="root",passwd="1234567",
database="employee")
cur=con.cursor()
cur.execute("create table employeee(EMP_ID varchar(10),ENAME
char(10),SALARY int)")
print("table created successfully")
while True:
emp_id=input("enter the employeee id of the worker:")
ename=input("enter the name of worker:")
salary=int(input("enter the salary of the worker:"))
query="insert into employeee
values('{}','{}','{}')".format(emp_id,ename,salary)
cur.execute(query)
con.commit()
print("row inserted successfully")
ch=input("do you want to enter more records? (yes/no)")
if ch=="no":
break
cur.execute("select*from employeee")
data=cur.fetchall()
for i in data:
print(i)
print("total number of rows retrived=",cur.rowcount)
OUTPUT:
table created successfully
enter the employeee id of the worker:100
enter the name of worker:Sam
enter the salary of the worker:20000
row inserted successfully
do you want to enter more records? (yes/no)yes
enter the employeee id of the worker:101
enter the name of worker:Ram
enter the salary of the worker:30000
row inserted successfully
do you want to enter more records? (yes/no)yes
enter the employeee id of the worker:103
enter the name of worker:Ron
enter the salary of the worker:40000
row inserted successfully
do you want to enter more records? (yes/no)no
('100', 'Sam', 20000)
('101', 'Ram', 30000)
('103', 'Ron', 40000)
total number of rows retrived= 3
24. Write a program to integrate SQl with python by importing mysql module to create,alter and
display the student details.
import mysql.connector as ms
con=ms.connect(host="localhost",user="root",passwd="1234567",database=
"sch")
cur=con.cursor()
cur.execute("create table student(ROLL_NO int,STUDENT_NAME
char(25),CLASS int)")
print("table created successfully")
cur.execute("alter table student add section char(4)")
print("column created succesfully")
while True:
roll_no =int(input("Enter the roll number="))
stu_name=input("enter the name of the student=")
Class =int(input("enter the class of student="))
section=input("enter the section of the student=")
query="insert into student values({},'{}',
{},'{}')".format(roll_no,stu_name,Class,section)
cur.execute(query)
con.commit()
print("row inserted succesfully")
ch=input("do you want to enter more records?(yes/no)")
if ch =="no":
break
cur.execute("select*from student")
data=cur.fetchall()
for i in data:
print(i)
print("total number of rows retrieved=",cur.rowcount)
OUTPUT:
table created successfully
column created succesfully
Enter the roll number=1001
enter the name of the student=Ankit
enter the class of student=12
enter the section of the student=A
row inserted succesfully
do you want to enter more records?(yes/no)yes
Enter the roll number=1002
enter the name of the student=Arnav
enter the class of student=12
enter the section of the student=A
row inserted succesfully
do you want to enter more records?(yes/no)yes
Enter the roll number=1003
enter the name of the student=Rakesh
enter the class of student=12
enter the section of the student=B
row inserted succesfully
do you want to enter more records?(yes/no)no
(1001, 'Ankit', 12, 'A')
(1002, 'Arnav', 12, 'A')
(1003, 'Rakesh', 12, 'B')
total number of rows retrieved= 3
25. Write a program to integrate SQl with python by importing mysql module to delete and
display the student details.