0% found this document useful (0 votes)
26 views

Practice Python Programs 2024

The document contains 6 Python programs: 1) A program to count uppercase, lowercase, and digit characters in a text file. 2) A program to create and read a CSV file containing product data. 3) A program to store and retrieve student data from a binary file using pickle. 4) A program to count the frequencies of words in a text file. 5) A program to demonstrate push and pop operations on a stack implemented as a list. 6) A program to count the frequencies of vowels and consonants in a text file.

Uploaded by

zaidn8724
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)
26 views

Practice Python Programs 2024

The document contains 6 Python programs: 1) A program to count uppercase, lowercase, and digit characters in a text file. 2) A program to create and read a CSV file containing product data. 3) A program to store and retrieve student data from a binary file using pickle. 4) A program to count the frequencies of words in a text file. 5) A program to demonstrate push and pop operations on a stack implemented as a list. 6) A program to count the frequencies of vowels and consonants in a text file.

Uploaded by

zaidn8724
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
You are on page 1/ 7

#1 ) WAP to create a text file.

Display total number of uppercase , lowercase characters and digits in


it.

f = open('Test.txt' , 'w') #Opening Test File for Writing

while True:

line = input('Enter a line:')

f.write(line+'\n')

print()

a = input('Press y to continue:')

if a != 'y':

break

f.close()

f = open('Test.txt' , 'r') #Opening Test file for reading

uc = lc = d = 0

r = f.read()

for i in r:

if i.isupper(): #Function to check uppercase character

uc+=1

elif i.islower():

lc+=1

if i.isdigit():

d+=1

print('Frequency of Uppercase Characters:' , uc)

print('Frequency of Lowercase Characters:' , lc)

print('Frequency of Digits:' , d)

f.close()
"""2) WAP to create a csv file with the following data given below. Read that file and display data
stored in that file.

1st record should be PID, PNAME , COST , QUANTITY. sample of product.csv file is given below.

P1, Brush , 50 , 200

P2 , Toothpaste , 120 , 150

P3 , Comb , 40 , 300

P4 , Sheets , 100 , 500

P5 , Pen , 10 , 250"""

import csv

f = open('Product.csv' , 'w' , newline = '') #Opening Product file To Write

w = csv.writer(f)

w.writerows([['PID' , 'PNAME' , 'COST' , 'QUANTITY'] , #Writing Data

['P1' , 'BRUSH' , 50 , 200] ,

['P2' , 'Toothpaste' , 120 , 150] ,

['P3' , 'Comb' , 40 , 300] ,

['P4' , 'Sheets' , 100 , 500] ,

['P5' , 'Pen' , 10 , 250]

])

f.close()

f = open('Product.csv' , 'r') #Opening File to Read

r = csv.reader(f)

for i in r:

print(i)

f.close()
#3) WAP to create a binary file. Store Name , Roll No & Percentage of a student using pickle module.
Display contents of the file

import pickle

f = open('Stu.dat' , 'wb') #Opening Stu File to write

i=1

while True:

n = input('Enter Name of Student:')

rno = int(input('Enter roll No:'))

p = float(input('Enter Percentage:'))

d = {'Name' : n , 'Roll No': rno , 'Percentage':p}

pickle.dump(d,f)

a = input('Press y to continue:')

if a != 'y':

break

f.close()

f = open('Stu.dat' , 'rb') #Opening File to read

try:

while True:

r = pickle.load(f)

print(r)

except:

f.close()
#4) WAP to create a text file. Display frequency of words 'the' & 'The' in it.

f = open('abc.txt' , 'w') #Opening file to Write

while True:

l = input('Enter a line:')

f.write(l+'\n')

print()

a = input('Press y to continue:')

if a != 'y':

break

f.close()

f = open('abc.txt' , 'r') #Opening FIle To Read

r = f.read() #Reading File

s = r.split()

t1 = t2 = 0

for i in s:

if i == 'the':

t1+=1

if i=='The':

t2+=1

print('Frequency of the : ' , t1)

print('Frequency of The: ' , t2)

f.close()
#5) WAP to perform push and pop operations in a stack implemented as list

def push(item,stk): #Function to push in stack

stk.append(item)

def pop(stk): # Function to pop in stack

if len(stk) == 0:

print('Error!\nStack UnderFlow')

else:

i = stk.pop()

print('Item Popped : ' , i)

def Display(stk): #Function to Display Stack

if len(stk) == 0:

print('Stack Empty')

else:

top = len(stk) - 1

for i in range(top, -1 , -1):

if i == top:

print(stk[i] , '<---- top')

else:

print(stk[i])

#__main__

stack = []

top = len(stack) -1

while True:

print('Stack Operations\n1)Push\n2)Pop\n3)Display\n4)Exit')

a = int(input('Choose(1-4):'))

if a == 1:

item= int(input('Enter item to be pushed on to the stack:'))


push(item, stack)

elif a == 2:

pop(stack)

elif a==3:

Display(stack)

elif a == 4:

break

else:

print("Did not choose a correct option\nTry Again")


#6) WAP to create a text file. Display frequency of vowels and consonants

f = open('Sun____.txt' , 'w') #Opening File To Write

while True:

l = input('Enter a line:')

f.write(l+'\n')

print()

a = input('Press y to continue:')

if a != 'y':

break

f.close()

f = open('Sun____.txt' , 'r') #Opening File to Read

v=c=0

r = f.read()

for i in r:

if i.isalpha(): #Checking for alphabet

if i in 'aeiouAEIOU':

v+=1

else:

c+=1

print('Frequency of Vowels:' ,v)

print('Frequency of Consonants:' ,c)

f.close()

You might also like