0% found this document useful (0 votes)
6 views3 pages

Python SQL Practical Programming Guide

The document contains practical Python and SQL programs covering various tasks such as reading and manipulating text files, counting characters, handling binary files, simulating dice rolls, implementing a stack, searching CSV files, and integrating Python with MySQL. Each program is presented with code snippets and demonstrates specific functionalities like file operations, data storage, and user interaction. Overall, it serves as a comprehensive guide for practical applications of Python and SQL.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Python SQL Practical Programming Guide

The document contains practical Python and SQL programs covering various tasks such as reading and manipulating text files, counting characters, handling binary files, simulating dice rolls, implementing a stack, searching CSV files, and integrating Python with MySQL. Each program is presented with code snippets and demonstrates specific functionalities like file operations, data storage, and user interaction. Overall, it serves as a comprehensive guide for practical applications of Python and SQL.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PYTHON & SQL PRACTICAL NOTES – FULL

PROGRAMS

1. Read a text file and display words separated by #


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

2. Count vowels, consonants, uppercase and lowercase


characters
f = open("[Link]", "r")
v = c = u = l = 0
for ch in [Link]():
if [Link]():
if [Link]() in "aeiou":
v += 1
else:
c += 1
if [Link]():
u += 1
else:
l += 1
print("Vowels:", v)
print("Consonants:", c)
print("Uppercase:", u)
print("Lowercase:", l)
[Link]()

3. Remove lines containing 'a' and write to another file


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

4. Binary file – search roll number


import pickle
f = open("[Link]", "wb")
while True:
roll = int(input("Roll: "))
name = input("Name: ")
[Link]([roll, name], f)
if input("More? (y/n): ") == 'n':
break
[Link]()

f = open("[Link]", "rb")
r = int(input("Enter roll to search: "))
found = False
try:
while True:
data = [Link](f)
if data[0] == r:
print("Name:", data[1])
found = True
except:
pass
if not found:
print("Roll not found")
[Link]()

5. Binary file – update marks


import pickle
f = open("[Link]", "rb")
data = []
try:
while True:
[Link]([Link](f))
except:
pass
[Link]()

r = int(input("Enter roll: "))


for i in data:
if i[0] == r:
i[2] = int(input("Enter new marks: "))

f = open("[Link]", "wb")
for i in data:
[Link](i, f)
[Link]()

6. Dice simulator
import random
print("Dice value:", [Link](1, 6))

7. Stack using list


stack = []
def push():
[Link](input("Enter element: "))
def pop():
if stack:
print("Popped:", [Link]())
else:
print("Stack empty")
def display():
print(stack)

8. CSV file – user id and password search


import csv
with open("[Link]", "w", newline="") as f:
w = [Link](f)
[Link](["user","password"])
[Link](["admin","1234"])

uid = input("Enter user id: ")


with open("[Link]", "r") as f:
r = [Link](f)
for row in r:
if row[0] == uid:
print("Password:", row[1])
break
else:
print("User not found")

9. Python – MySQL integration


import [Link]
db = [Link](
host="localhost",
user="root",
password="root",
database="school"
)
cur = [Link]()
[Link]("SELECT * FROM student")
for row in cur:
print(row)
[Link]()

You might also like