Record File
Record File
: 1
#PROGRAM NAME: ARITHMETIC OPERATIONS BETWEEN TWO NUMBERS
result = 0
val1= float(input("Enter the first value: "))
val2= float(input("Enter the second value: "))
op=input("Enter any one of the operator(+,-,*,/,//,%): ")
if op=="+":
result=val1+val2
elif op=="-":
result=val1-val2
elif op=="*":
result=val1*val2
elif op=="/":
if val2==0:
print("Please enter a value other than 0")
else:
result=val1/val2
elif op=="//":
result=val1//val2
else:
result=val1%val2
print("The result is: ", result)
#OUTPUT
def pernum(num):
divsum=0
for i in range(1,num):
if num%i==0:
divsum+=i
if divsum==num:
print("Perfect number")
else:
print("Not a perfect number")
#OUTPUT
#OUTPUT
#OUTPUT
#OUTPUT
0 1 1 2 3 5 8 13 21 34 55
#PROGRAM NO.: 6
#PROGRAM NAME: PALINDROME STRING
#OUTPUT
my_list=['p','r','o','b','e']
print(my_list[0])
print(my_list[2])
print(my_list[4])
n_list=["Happy",[2,0,1,5]]
print(n_list[0][1],n_list[0][2],n_list[0][3])
print(n_list[1][3])
#OUTPUT
p
o
e
a p p
5
#PROGRAM NO.: 8
#PROGRAM NAME: LIST OPERATIONS
memo=[]
for i in range(5):
x=int(input("enter no.: "))
memo.insert(i,x)
i+=1
print(memo)
memo.append(25)
print("Second List")
print(memo)
msg=input("Enter any string: ")
newlist=[]
newlist[:0]=msg
l=len(newlist)
print(l)
#OUTPUT
enter no.: 1
enter no.: 2
enter no.: 3
enter no.: 4
enter no.: 5
[1, 2, 3, 4, 5]
Second List
[1, 2, 3, 4, 5, 25]
Enter any string: PYTHON LEARNING
15
#PROGRAM NO.: 9
#PROGRAM NAME: FLOYD’S TRIANGLE
#OUTPUT
65
654
6543
65432
654321
#PROGRAM NO.: 10
#PROGRAM NAME: FACTORIAL USING DEFINED MODULE
def fact(no):
f=1;
while no>0:
f*=no
no=no-1
return f
x=int(input("Enter value for factorial: "))
print(fact(x))
#OUTPUT
arr=[]
def array_sorting():
appendarray()
print()
print('''Sorting techniques
1.Bubble Sort
2.Inserstion Sort''')
ch=int(input("Enter sorting technique: "))
if ch==1:
bubble_sort()
elif ch==2:
insertion_sort()
def appendarray():
n=int(input("Enter the size of array: ")
for i in range(0,n):
x=int(input("enter number: "))
arr.insert(i,x)
def bubble_sort():
print("Original array is: ",arr)
n=len(arr)
list1=[]
for i in arr:
list1.append(i)
for i in range(n):
for j in range(0,n-i-1):
if list1[j]>list1[j+1]:
list1[j],list1[j+1]=list1[j+1],list1[j]
print("Array after bubble sorting is : ",list1)
def insertion_sort():
print("Original array is: ",arr)
list1=[]
for i in arr:
list1.append(i)
for i in range(1,len(arr)):
key=list1[i]
j=i-1
while j>=0 and key<list1[j]:
list1[j+1]=list1[j]
j=j-1
else:
list1[j+1]=key
print("Array after insertion sorting is: ",list1)
array_sorting()
#OUTPUT
Sorting techniques
1.Bubble Sort
2.Inserstion Sort
Enter sorting technique: 1
Original array is: [75, 19, 46, 83, 28, 64]
Array after bubble sorting is: [19, 28, 46, 64, 75, 83]
#PROGRAM NO.: 12
#PROGRAM NAME: FILE READING FUNCTIONS
f=open("test.txt","r")
print(f.name)
f_contents=f.read()
print(f_contents)
f_contents=f.readlines()
print(f_contents)
for line in f:
print(line,end="")
f_contents=f.read(50)
print(f_contents)
size_to_read=10
f_contents=f.read(size_to_read)
while len(f_contents)>0:
print(f_contents)
print(f.tell())
f_contents=f.read(size_to_read)
#OUTPUT
test.txt
Hello User
you are working with
Python
Files
#PROGRAM NO.: 13
#PROGRAM NAME: FILE APPENDING FUNCTIONS
af=open("test.txt","a")
lines_of_text=("one line of text here,"+"\t"
"and another line here,"+"\t"
"and yet another here,"+" and so on and so forth")
af.writelines("\n"+lines_of_text)
af.close()
#OUTPUT
Hello User
you are working with
Python
Files
one line of text here, and another line here, and yet another
here, and so on and so forth
#PROGRAM NO.: 14
#PROGRAM NAME: COUNT WORD OCURRENCES IN A FILE
f=open("test.txt","r")
read=f.readlines()
f.close()
times=0
times2=0
chk=input("enter string to search: ")
count=0
for sentence in read:
line=sentence.split()
times+=1
for each in line:
line2=each
times2+=1
if chk==line2:
count+=1
print("the search string",chk,"is present",count,"time(s)")
print(times)
print(times2)
#OUTPUT
f=open("test.txt",'r')
read=f.readlines()
f.close()
id=[]
for ln in read:
if ln.startswith("T"):
id.append(ln)
print(id)
#OUTPUT
['Test program\n']
#PROGRAM NO.: 16
#PROGRAM NAME: CSV OPERATIONS
import csv
def CreateCSV1():
Csvfile=open('student.csv','w',newline='')
Csvobj=csv.writer(Csvfile)
while True:
Rno=int(input("Rno:"))
Name=input("Name:")
Marks=float(input("Marks:"))
Line=[Rno,Name,Marks]
Csvobj.writerow(Line)
ch=input("More Y/N:")
if ch=='N' or ch=='n':
break;
Csvfile.close()
def CreateCSV2():
Csvfile=open('student.csv','w',newline='')
Csvobj=csv.writer(Csvfile)
Line=[]
while True:
Rno=int(input("Rno:"))
Name=input("Name:")
Marks=float(input("Marks:"))
Line.append([Rno,Name,Marks])
Csvobj.writerow(Line)
ch=input("More Y/N:")
if ch=='N' or ch=='n':
break;
Csvobj.writerows(Line)
Csvfile.close()
def ShowAll():
Csvfile=open('student.csv','r',newline='')
Csvobj=csv.reader(Csvfile)
for Line in Csvobj:
print(Line)
Csvfile.close()
#OUTPUT
#OUTPUT
1.PUSH
2.POP
3.Display
Enter your choice: 1
Enter any number: 3
Do you want to continue or not? (y/n): y
1.PUSH
2.POP
3.Display
Enter your choice: 1
Enter any number: 5
Do you want to continue or not? (y/n): y
1.PUSH
2.POP
3.Display
Enter your choice: 3
5
3
Do you want to continue or not? (y/n): y
1.PUSH
2.POP
3.Display
Enter your choice: 2
Deleted element is: 5
Do you want to continue or not? (y/n): n
#PROGRAM NO.: 18
#PROGRAM NAME: DISPLAY UNIQUE VOWELS IN WORD USING STACK
vowels=['a','e','i','o','u']
word=input("Enter the word to search of vowels:")
Stack=[]
for letter in word:
if letter in vowels:
if letter not in Stack:
Stack.append(letter)
print(Stack)
print("No. of different vowels present in",word,"is",len(Stack))
#OUTPUT
3) Change the price of the Product with Qty less then 100 to Rs.
10.
Ans:- UPDATE Product
SET Price=10 WHEREQty<100;
11) Display product name and rating whose price is between 10 and
20.
Ans:- SELECT Pro_Name, Rating FROM Product
WHERE Price BETWEEN 10 AND 20;
16) Create view to select product name whose rating is not B1 and
price is greater than 10.
Ans:- CREATE VIEW New AS
SELECT Pro_NameFROM Product
WHERE Rating<>’B1’ AND Price>10;
EMPLOYEE
Employeei Name Sales Jobid
d
E1 SUMIT SINHA 1100000 102
E2 VIJAY SINGH TOMAR 1300000 101
E3 AJAY RAJPAL 1400000 103
E4 MOHIT RAMNANI 1250000 102
E5 SJAILJA SINGH 1450000 103
JOB
JOBID JOBTITLE SALARY
(iv) Write SQL command to change the JOBID to 104 if the EMPLOYEE
with ID as E4 in the table ‘EMPLOYEE’.
Ans: UPDATE EMPLOYEE, JOB
SET EMPLOYEE.Jobid= 104
WHERE Employeeid=E4 AND
EMPLOYEE. Jobid = JOB. Jobid;
(v) Show the average salary for all departments with more than 3
people for a job.
Ans: SELECT AVG(Salary),count(Jobid)
FROM JOB
GROUP BY Jobid
HAVING count(Jobid)>3;
(vi) Display only the jobs with maximum salary greater than or
equal to 3000 job wise.
Ans: SELECT Jobtitle ,MAX(Salary)
FROM JOB
GROUP BY Jobtitle
HAVING MAX(Salary)>= 3000;
FLIGHT
FNO START END F_DATE FARE
#OUTPUT
Successfully connected
import mysql.connector
con=mysql.connector.connect(host='localhost',user='root',password
='',db='school')
stmt=con.cursor()
query='select * from student;'
stmt.execute(query)
data=stmt.fetchone()
print(data)
#OUTPUT
#OUTPUT
#Fetching all the records from EMP table having salary more than
70000
import mysql.connector as MYsql
db1=MYsql.connect(host="localhost",user="root",passwd='',db="empl
oyee")
cursor=db1.cursor()
sql="SELECT*FROM EMP WHERE salary>80000;"
try:
cursor.execute(sql)
resultset=cursor.fetchall()
for row in resultset:
print(row)
except:
print("Error: unable to fetch data")
db1.close()
#OUTPUT
#OUTPUT