Unit 3 Python - Ipynb - Colaboratory
Unit 3 Python - Ipynb - Colaboratory
phone book with names and phone numbers. Now alphabetically sort the phonebook.
def getEntries(n):
f = open('phonebook.txt','a+')
f.truncate(0)
print('Enter the contacts along with the phonenumbers')
for i in range(n):
print('Enter the contact number',(i+1))
contact = input();
f.write(contact)
f.write("\n")
f.seek(0)
f.close()
print("Enter the number of entries to be entered")
n = int(input())
print('Entering contacts into the file')
getEntries(n);
f = open('phonebook.txt','r+')
l = f.readlines()
f.seek(0)
l.sort()
f.truncate(0)
f.writelines(l)
f.close()
2) Design and implement a python program to create and read a file, store it in a list of lists, with
each inner list containing the product name, net weight and the unit price of a product, by name
shopper_list.txt that contains the name, weight and the unit price of the products in supermarket:
Wheat flour 1kg 25.00 Rice flour 1kg 40.00
def getproducts(n):
allproducts = []
product = []
f = open('shopper_list.txt','a+')
f.truncate(0)
for i in range(n):
product = []
print('Enter the product details')
s = input()
f.write(s)
f.write("\n")
product.append(s)
allproducts.append(product)
print(allproducts)
f seek(0)
f.seek(0)
f.close()
print('Enter the number of entries')
n = int(input())
print('Entering products into the file')
getproducts(n)
3) Design and implement a program to open a file and count the occurrences of each letter in the
file.
f = open('shopper_list.txt','r')
string = ""
for line in f:
string += line
f.seek(0)
f.close()
d = {}
for i in string:
if i in d:
d[i] += 1
else:
d[i] = 1
print(d)
{'R': 1, 'i': 1, 'c': 1, 'e': 3, ' ': 6, '1': 3, 'k': 3, 'g': 3, '2': 1, '0': 3,
4) Design and implement a program that reads a file and outputs a new file with the lines in a sorted
order.
f = open('shopper_list.txt','r')
l = f.readlines()
f.seek(0)
f.close()
l.sort()
f = open('4th_program_output.txt','a+')
f.writelines(l)
f.seek(0)
f.close()
5) Write a program segment that reads a text file named original_text, and displays how many times
the letter ‘c’ occurs.
f = open('original_text.txt','r')
f.seek(0)
text = ""
text =
for line in f:
text += line
count = 0
for i in text:
if(i=='c' || i == 'C'):
count += 1
print(count)
f = open('shopper_list.txt','r')
f.seek(0)
l = f.readlines()
print('Number of lines : ',len(l))
f.close()
Number of lines : 3
7) Write a Python program to get current time in milliseconds in Python using the time module.
import time
print(int(round(time.time()*1000)))
1620797364630
8) Write a Python program to generate random even integers in a specific numerical range using the
random module
import random
print('Enter the starting point and ending point')
l = list(map(int,input().split()))
starting_point = l[0]
ending_point = l[1]
print('Enter the number of random numbers to be generated')
n = int(input())
for i in range(n):
print(random.randrange(starting_point,ending_point,2))
9) Write a function that takes a list of integers, and returns the number of primes in the list.
import math
def isprime(n):
if(n == 1):
return False
for i in range(2,int(math.sqrt(n)+1)):
if(n != i and n%i == 0):
return False
return True
print('Enter a list of numbers')
l = list(map(int,input().split()))
count = 0
for i in l:
if(isprime(i)):
count += 1
print(count)
10) Write a program that reads a file and writes out a new file with the lines in reversed order (i.e.
the first line in the old file becomes the last one in the new file).
f = open('shopper_list.txt','r')
l = f.readlines()
f.seek(0)
f.close()
l.reverse()
f = open('reverse_lines.txt','a+')
f.truncate(0)
f.writelines(l)
f.seek(0)
f.close()
11) Write a Python program to generate a series of unique random numbers using the random
module.
import random
print("Enter the number of unique random numners to be generated")
n = int(input())
print(random.sample(range(1,100),n))
12) Write a Python program to read a file line by line and store it into a list. Write a python program
to find the longest words.
f = open('shopper_list.txt','r')
l = f.readlines()
f.close()
max1 = len(l[0])
s = l[0]
for i in range(1,len(l)):
if(len(l[i])>max1):
max1 = len(l[i])
s = l[i]
print(s,max1)
Barley 1kg 40
14
13) Write a python program to produce a list of unique element from the input list which contains
duplicates of the elements in the list Expected output: Input list: [1,2,3,3,3,3,5,6,9,9] Output list:
[1,2,3,5,6,9]
print('Enter the list of numbers')
l = list(map(int,input().split()))
print(list(set(l)))
14) Write a program that reads a file and prints only those lines that contain the substring snake
f = open('snake_list.txt','r')
for line in f:
if(line.find('snake') != -1):
print(line)
f.seek(0)
f.close()
i am not a snake
he is fond of snakes
15) Write a Python program to get a single random element from a specified string
import random
print('Enter a string')
s = input()
print(random.choice(s))
Enter a string
jaswanth
a
16) Write a Python program to print a string five times, delay three seconds using the time module.
import time
print('Enter a string')
s = input()
print()
for i in range(5):
print(s)
time.sleep(3)
Enter a string
python
python
python
python
python
python
f = open('shopper_list.txt','r')
text = ""
for line in f:
text += line
l = text.split()
d = {}
for i in l:
if i in d:
d[i] += 1
else:
d[i] = 1
d[i] 1
print(d)
18) Design and implement a program that reads a file and prints only those lines that contain the
substring “msrit”
f = open('msrit_list.txt','r')
for line in f:
if(line.find('msrit') != -1):
print(line)
f.seek(0)
f.close()