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

Python Programs Short Versions

Uploaded by

sanjay kanth
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)
3 views3 pages

Python Programs Short Versions

Uploaded by

sanjay kanth
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

1. Create a python program that reads a text file “Sample.

txt” line by line and


display each word separated by a #.

with open("[Link]", "r") as file:


for line in file:
words = [Link]().split()
output = "#".join(words)
print(output)

2. Write a python program to remove all the lines that contain the character 'a' in a
file “[Link]” and write it to another file “[Link]”.

with open("[Link]", "r") as src, open("[Link]", "w") as dest:


for line in src:
if 'a' not in line:
[Link](line)
3. Create a python program named [Link] to read a text file – [Link] and
display the number of vowels/consonants/lower case/ upper case characters.

v=c=l=u=0
with open("[Link]") as f:
for ch in [Link]():
if [Link]():
v += [Link]() in "aeiou"
c += [Link]() not in "aeiou"
l += [Link]()
u += [Link]()

print("Vowels:", v)
print("Consonants:", c)
print("Lowercase:", l)
print("Uppercase:", u)
4. Create a CSV file by entering user-id and password, read and search the password
for given user-id.

import csv

with open("[Link]", "w", newline="") as f:


w = [Link](f)
[Link](["user_id", "password"])
n = int(input("Enter number of users: "))
for i in range(n):
uid = input("Enter user id: ")
pwd = input("Enter password: ")
[Link]([uid, pwd])

search = input("Enter user id to search: ")

with open("[Link]", "r") as f:


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

5. Write a python program using list to implement stack data structure and
perform the following
operations: a. Push(), b. Pop(), c. Display() d. Peek()
s=[]
n=int(input("Max size: "))
while 1:
c=int(input("1-Push 2-Pop 3-Display 4-Peek 5-Exit: "))
if c==1:
if len(s)==n:
print("Stack Full")
else:
[Link]([input("Name: "),int(input("Salary: "))])
elif c==2:
print("Pop:",[Link]() if s else "Empty")
elif c==3:
print(s if s else "Empty")
elif c==4:
print(s[-1] if s else "Empty")
elif c==5:
break

You might also like