REVISION TOUR OF PYTHON
PROGRAM-1
INPUT:
OUTPUT:
REVISION TOUR OF PYTHON
PROGRAM-2
INPUT:
OUTPUT:
REVISION TOUR OF PYTHON
PROGRAM-3
INPUT:
OUTPUT:
REVISION TOUR OF PYTHON
PROGRAM-4
INPUT:
OUTPUT:
REVISIOH TOUR OF PYTHON
PROGRAM-5
INPUT:
#program takes names and costs of five fruits from the user,
stores them in a dictionary, and prints the names #of fruits
whose cost is greater than 200.
OUTPUT:
FUNCTIONS
PROGRAM-1
INPUT:
#A user defined function to input a string and display
it's reverse
OUTPUT:
FUNCTIONS
PROGRAM-2
INPUT:
OUTPUT:
FUNCTIONS
PROGRAM-3
INPUT:
#program to shuffle the items of a list using shuffle () function of
random module
OUTPUT:
FUNCTIONS
PROGRAM-4
INPUT:
#program to import the math module and print square
root of a number
OUTPUT:
FUNCTIONS
PROGRAM-5
INPUT:
#A program to randomly select two items in a list using choice ()
function of random module
OUTPUT:
TEXT FILES
PROGRAM-1
INPUT:
#create a new text file, ‘text.txt’ with user’s input and
display it
with open ( ‘text.txt’ , ‘a’ ) as f:
while True :
line = input (‘Enter a line: ’ )
f.write (line)
if len (line) == 0:
break
with open (‘text.txt’ , 'r') as z:
print ( z.read() )
OUTPUT:
Enter a line: hello
Enter a line: abc
Enter a line: xyz
Enter a line:
hello abcxyz
TEXT FILES
PROGRAM-2
INPUT:
#Program to read content of file line by line and display
#each word separated by '#'
f = open(‘text.txt’ , ‘r’ )
for line in f:
words=line.split()
for w in words:
print(w+'#',end="")
print()
f.close()
OUTPUT:
hello#abc#xyz#
TEXT FILES
PROGRAM-3
INPUT:
#Program to read the content of text file and display the
#total number of vowels.
with open(‘text.txt’ , ‘r’) as f :
v=0
data = f.read()
vowels=['a','e','i','o','u’]
for ch in data:
if ch.isalpha():
if ch.lower() in vowels:
v+=1
print("Total Vowels in file :",v)
OUTPUT:
Total Vowels in file : 3
TEXT FILES
PROGRAM-4
INPUT:
#program to read the text file and display the words
starting with ‘a’
with open('text.txt' , 'r') as f :
a = f.readlines()
c=0
for i in a :
for k in i.split():
if k[0] in 'aA':
c=c+1
print(k)
print('the number of words starting with ''a'' or ''A'' is’, c)
OUTPUT:
abc
air
the number of words starting with a or A is 2
TEXT FILES
PROGRAM-5
INPUT:
#program to display reverse of each word of text file
with open('text.txt' , 'r') as f :
a= f.readline ()
for i in a.split ():
print( i , i [::-1] )
OUTPUT:
hello olleh
abc cba
xyz zyx
air ria
BINARY FILES
PROGRAM-1
INPUT:
## Code to append Mno. and Nam to Mem.DAT
import pickle
with open("Member.DAT", "ab") as F:
while True:
Mno = int(input("Mno: "))
Nam = input("Name: ")
pickle.dump([Mno, Nam], F)
C = input("More (Y/N)?")
if C in ('N', 'n'):
break
OUTPUT:
Mno: 101
Name: John
More (Y/N)? y
Mno: 102
Name: Alice
More (Y/N)? n
BINARY FILES
PROGRAM-2
INPUT:
# Display Mno, Nam from Mem.DAT
import pickle
def ReadData():
with open("Member.DAT", "rb") as f:
while True:
try:
R = pickle.load(f)
print(R)
except:
break
ReadData()
OUTPUT:
[201, 'Rahul']
[202, 'Neha']
BINARY FILES
PROGRAM-3
INPUT:
# Adding a list to a file using pickle
import pickle
with open("FIRST", "wb") as f:
pickle.dump([1, "AMAR"], f)
import pickle
with open("FIRST", "rb") as f:
print(pickle.load(f))
OUTPUT:
[1, 'AMAR']
BINARY FILES
PROGRAM-4
INPUT:
# Adding a dictionary to a file using pickle
import pickle
with open("TWO.DAT", "wb") as f:
pickle.dump({"Rno": 12, "Name": "AAM"}, f)
import pickle
with open("TWO.DAT", "rb") as f:
print(pickle.load(f))
OUTPUT:
{'Rno': 12, 'Name': 'AAM'}
BINARY FILES
PROGRAM-5
INPUT:
# Reading a list from a binary file using pickle
import pickle
with open("FIRST", "rb") as f:
print(pickle.load(f))
OUTPUT:
[1, 'AMAR']