0% found this document useful (0 votes)
4 views11 pages

Python Practice Question

The document contains multiple Python scripts for various tasks including reading a text file to count words, lines, characters, and various character types, as well as connecting to a MySQL database to perform operations on a BOOKS table such as displaying records, inserting, updating, and deleting records. Additionally, it includes a simple stack implementation using a list with operations for push, pop, and display. Each section of the document demonstrates specific functionalities with clear code examples.
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)
4 views11 pages

Python Practice Question

The document contains multiple Python scripts for various tasks including reading a text file to count words, lines, characters, and various character types, as well as connecting to a MySQL database to perform operations on a BOOKS table such as displaying records, inserting, updating, and deleting records. Additionally, it includes a simple stack implementation using a list with operations for push, pop, and display. Each section of the document demonstrates specific functionalities with clear code examples.
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

1. Read a text file and count the total number of words, lines, and characters.

f = open("[Link]", "r")

lines = [Link]()

line_count = len(lines)

word_count = 0

char_count = 0

for line in lines:

char_count += len(line)

word_count += len([Link]())

[Link]()

print("Lines:", line_count)

print("Words:", word_count)

print("Characters:", char_count)

2. Read a text file and count digits, alphabets, spaces, and special characters.

f = open("[Link]", "r")

text = [Link]()

digits = alphabets = spaces = special = 0

for ch in text:

if [Link]():

digits += 1

elif [Link]():
alphabets += 1

elif [Link]():

spaces += 1

else:

special += 1

[Link]()

print("Digits:", digits)

print("Alphabets:", alphabets)

print("Spaces:", spaces)

print("Special Characters:", special)

3. Read a text file and count uppercase and lowercase letters.

f = open("[Link]", "r")

text = [Link]()

upper = lower = 0

for ch in text:

if [Link]():

upper += 1

elif [Link]():

lower += 1

[Link]()

print("Uppercase letters:", upper)


print("Lowercase letters:", lower)

4. Read a text file and count the number of vowels and consonants.

f = open("[Link]", "r")

text = [Link]().lower()

vowels = consonants = 0

for ch in text:

if [Link]():

if ch in 'aeiou':

vowels += 1

else:

consonants += 1

[Link]()

print("Vowels:", vowels)

print("Consonants:", consonants)

5. Read a text file and count words starting with a vowel.

f = open("[Link]", "r")

words = [Link]().split()

count = 0

for word in words:

if word[0].lower() in 'aeiou':

count += 1
[Link]()

print("Words starting with vowel:", count)

6. Read a text file and display the frequency of each digit (0–9).

f = open("[Link]", "r")

text = [Link]()

for d in '0123456789':

print("Digit", d, ":", [Link](d))

[Link]()

7. Read a text file and count blank spaces and newline characters.

f = open("[Link]", "r")

text = [Link]()

spaces = [Link](" ")

newlines = [Link]("\n")

[Link]()

print("Blank spaces:", spaces)

print("Newline characters:", newlines)

8. Read a text file and count special symbols (excluding letters, digits, and spaces).

f = open("[Link]", "r")
text = [Link]()

special = 0

for ch in text:

if not ([Link]() or [Link]()):

special += 1

[Link]()

print("Special symbols:", special)

9. Read a text file and display the longest word.

f = open("[Link]", "r")

words = [Link]().split()

longest = ""

for word in words:

if len(word) > len(longest):

longest = word

[Link]()

print("Longest word:", longest)

10. Read a text file and count occurrences of a given character.

f = open("[Link]", "r")

text = [Link]()
ch = input("Enter character to search: ")

count = [Link](ch)

[Link]()

print("Occurrence of", ch, ":", count)

1. Write a Python program to connect to a MySQL database and display all the
records of the table BOOKS.
import [Link]

# Establishing connection
con = [Link](
host="localhost",
user="root",
password="root",
database="school"
)

cursor = [Link]()

# Executing SQL query


[Link]("SELECT * FROM BOOKS")

# Fetching all records


records = [Link]()

# Displaying records
for row in records:
print(row)

# Closing connection
[Link]()
1. Display all records of table BOOKS

import [Link]
con = [Link](host="localhost", user="root", password="root",
database="school")

cur = [Link]()

[Link]("SELECT * FROM BOOKS")

data = [Link]()

for row in data:

print(row)

[Link]()

2. Display book name and price from BOOKS table

import [Link]

con = [Link](host="localhost", user="root", password="root",


database="school")
cur = [Link]()

[Link]("SELECT book_name, price FROM BOOKS")

for row in [Link]():


print(row)

[Link]()

3. Display records of BOOKS where price is greater than 500


import [Link]

con = [Link](host="localhost", user="root", password="root",


database="school")
cur = [Link]()

[Link]("SELECT * FROM BOOKS WHERE price > 500")


for row in [Link]():
print(row)

[Link]()

4. Insert a new record into BOOKS table

import [Link]

con = [Link](host="localhost", user="root", password="root",


database="school")
cur = [Link]()

sql = "INSERT INTO BOOKS VALUES (101, 'Python Basics', 'Guido', 650)"
[Link](sql)
[Link]()

print("Record inserted successfully")

[Link]()

5. Update the price of a book in BOOKS table

import [Link]

con = [Link](host="localhost", user="root", password="root",


database="school")
cur = [Link]()

[Link]("UPDATE BOOKS SET price = 700 WHERE book_id = 101")


[Link]()

print("Record updated")

[Link]()

6. Delete a record from BOOKS table

import [Link]
con = [Link](host="localhost", user="root", password="root",
database="school")
cur = [Link]()

[Link]("DELETE FROM BOOKS WHERE book_id = 101")


[Link]()

print("Record deleted")

[Link]()

7. Display total number of records in BOOKS table

import [Link]

con = [Link](host="localhost", user="root", password="root",


database="school")
cur = [Link]()

[Link]("SELECT COUNT(*) FROM BOOKS")


count = [Link]()

print("Total records:", count[0])

[Link]()

8. Display the most expensive book from BOOKS table

import [Link]

con = [Link](host="localhost", user="root", password="root",


database="school")
cur = [Link]()

[Link]("SELECT * FROM BOOKS ORDER BY price DESC LIMIT 1")

print([Link]())

[Link]()

9. Display books written by a particular author


import [Link]

author = input("Enter author name: ")

con = [Link](host="localhost", user="root", password="root",


database="school")
cur = [Link]()

[Link]("SELECT * FROM BOOKS WHERE author = %s", (author,))

for row in [Link]():


print(row)

[Link]()

10. Display records of BOOKS table in ascending order of price

import [Link]

con = [Link](host="localhost", user="root", password="root",


database="school")
cur = [Link]()

[Link]("SELECT * FROM BOOKS ORDER BY price ASC")

for row in [Link]():


print(row)

[Link]()

Write a Python program to implement a stack using a list data structure.


Perform PUSH, POP and DISPLAY operations.

stack = []

def push():
item = input("Enter element to push: ")
[Link](item)
print("Element pushed")
def pop():
if stack == []:
print("Stack is empty")
else:
print("Popped element:", [Link]())

def display():
if stack == []:
print("Stack is empty")
else:
print("Stack elements:")
for i in range(len(stack)-1, -1, -1):
print(stack[i])

while True:
print("\n1. Push")
print("2. Pop")
print("3. Display")
print("4. Exit")

choice = int(input("Enter your choice: "))

if choice == 1:
push()
elif choice == 2:
pop()
elif choice == 3:
display()
elif choice == 4:
break
else:
print("Invalid choice")

You might also like