0% found this document useful (0 votes)
8 views8 pages

Python & MySQL Programming Exercises

The document provides a collection of Python programs covering various topics such as finding the greatest number, generating Fibonacci series, printing Pascal's Triangle, and performing file operations. It also includes examples of MySQL operations like inserting, searching, updating, and deleting records. Each program is accompanied by its output, demonstrating practical applications of Python and MySQL in computer science.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views8 pages

Python & MySQL Programming Exercises

The document provides a collection of Python programs covering various topics such as finding the greatest number, generating Fibonacci series, printing Pascal's Triangle, and performing file operations. It also includes examples of MySQL operations like inserting, searching, updating, and deleting records. Each program is accompanied by its output, demonstrating practical applications of Python and MySQL in computer science.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Computer Science (083)

Practical File on Python Programs and MySQL

Write a program to input 3 numbers and print the greatest number using
nested if.
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))

if a > b:
if a > c:
print("Greatest number:", a)
else:
print("Greatest number:", c)
else:
if b > c:
print("Greatest number:", b)
else:
print("Greatest number:", c)

Output

Write a program to print Fibonacci series up to n terms and find sum.


n = int(input("Enter number of terms: "))
a, b = 0, 1
total = 0
for i in range(n):
print(a, end=" ")
total += a
a, b = b, a+b
print("\nSum =", total)

Output

Write a program to print Pascal Triangle.


n = int(input("Enter number of rows: "))
for i in range(n):
num = 1
for j in range(i+1):
print(num, end=" ")
num = num * (i-j) // (j+1)
print()
Output

Write a program to print factorial of a list.


lst = [2, 4, 6]
for n in lst:
fact = 1
for i in range(1, n+1):
fact *= i
print("Factorial of", n, "=", fact)

Output

Write a program for word frequency counter.


text = input("Enter paragraph: ")
words = [Link]()
freq = {}
for w in words:
freq[w] = [Link](w, 0) + 1
print(freq)

Output

Write a program to check prime number.


n = int(input("Enter number: "))
if n > 1:
for i in range(2, n):
if n % i == 0:
print("Not Prime")
break
else:
print("Prime")
else:
print("Not Prime")

Output

Generate random number between 1 and 6 (Dice).


import random
print([Link](1,6))

Output

Bubble sort a list.


lst = [5, 3, 8, 1]
for i in range(len(lst)):
for j in range(0, len(lst)-i-1):
if lst[j] > lst[j+1]:
lst[j], lst[j+1] = lst[j+1], lst[j]
print(lst)

Output

Function Div3 and Div5.


def Div3Div5(t):
s=0
for i in t:
if i % 3 == 0 and i % 5 == 0:
s += i
return s

t = (10, 15, 30, 22, 45, 60, 9, 3, 5, 75)


print(Div3Div5(t))

Output

Odd or Even.
n = int(input("Enter number: "))
print("Even" if n % 2 == 0 else "Odd")

Output

Read file and separate words with #.


f = open("[Link]","r")
for line in f:
print("#".join([Link]()))
[Link]()

Output

Count vowels, consonants, upper and lower case.


f = open("[Link]","r")
data = [Link]()
vowels = consonants = upper = lower = 0
for ch in data:
if [Link]():
if [Link]() in 'aeiou':
vowels += 1
else:
consonants += 1
if [Link]():
upper += 1
else:
lower += 1
print(vowels, consonants, upper, lower)
[Link]()

Output

Copy file excluding lines with letter 'a'.


f1 = open("[Link]","r")
f2 = open("[Link]","w")
for line in f1:
if 'a' not in line:
[Link](line)
[Link]()
[Link]()

Output

Binary file: roll no and name search.


import pickle
f = open("[Link]","wb")
[Link]({1:"Aman",2:"Riya"}, f)
[Link]()

f = open("[Link]","rb")
data = [Link](f)
r = int(input("Enter roll no: "))
print([Link](r,"Roll no not found"))
[Link]()

Output

Binary file update marks.


import pickle
f = open("[Link]","wb")
[Link]({1:["Aman",85],2:["Riya",90]}, f)
[Link]()

f = open("[Link]","rb")
data = [Link](f)
r = int(input("Enter roll: "))
if r in data:
data[r][1] = int(input("Enter new marks: "))
[Link]()
f = open("[Link]","wb")
[Link](data,f)
[Link]()

Output

CSV file employee search.


import csv
with open("[Link]","w",newline="") as f:
w = [Link](f)
[Link]([1,"Aman",50000])
[Link]([2,"Riya",60000])

with open("[Link]","r") as f:
r = [Link](f)
eid = int(input("Enter emp no: "))
found = False
for row in r:
if int(row[0]) == eid:
print(row)
found = True
if not found:
print("Employee not found")

Output

Stack operations.
stack = []
while True:
print("[Link] [Link] [Link] [Link]")
ch = int(input())
if ch == 1:
[Link](input("Enter element: "))
elif ch == 2:
print([Link]() if stack else "Empty")
elif ch == 3:
print(stack)
else:
break
Output

Linear search.
lst = [4,7,2,9]
x = int(input("Enter number: "))
if x in lst:
print("Found")
else:
print("Not Found")

Output

Binary search.
lst = [1,3,5,7,9]
x = int(input("Enter number: "))
low, high = 0, len(lst)-1
found = False
while low <= high:
mid = (low+high)//2
if lst[mid] == x:
found = True
break
elif lst[mid] < x:
low = mid+1
else:
high = mid-1
print("Found" if found else "Not Found")

Output

Queue operations.
queue = []
while True:
print("[Link] [Link] [Link] [Link]")
ch = int(input())
if ch == 1:
[Link](input("Enter element: "))
elif ch == 2:
print([Link](0) if queue else "Empty")
elif ch == 3:
print(queue)
else:
break
Output

MySQL insert and display.


import [Link]
con =
[Link](host="localhost",user="root",password="",database="school")
cur = [Link]()
[Link]("select * from employee")
for row in cur:
print(row)
[Link]()

Output

MySQL search.
import [Link]
con =
[Link](host="localhost",user="root",password="",database="school")
cur = [Link]()
eno = int(input("Enter emp no: "))
[Link]("select * from employee where empno=%s",(eno,))
row = [Link]()
print(row if row else "Not Found")
[Link]()

Output

MySQL update.
import [Link]
con =
[Link](host="localhost",user="root",password="",database="school")
cur = [Link]()
eno = int(input("Enter emp no: "))
sal = int(input("Enter new salary: "))
[Link]("update employee set salary=%s where empno=%s",(sal,eno))
[Link]()
[Link]()

Output

MySQL delete.
import [Link]
con =
[Link](host="localhost",user="root",password="",database="school")
cur = [Link]()
eno = int(input("Enter emp no: "))
[Link]("delete from employee where empno=%s",(eno,))
[Link]()
[Link]()

Output

You might also like