Python Practical Question Bank - 2
Python Practical Question Bank - 2
2. Write a program to read and display first and last n lines from a file.
11.Write a program which reverses a string and displays both original and reversed string.
OR
13.Write a program which accepts 10 integers and prints "DUPLICATES" if any of the
values entered are duplicates otherwise prints "ALL UNIQUE".
14.Write a program to read a string from the user and append it into a file.
OR
str1=input("Enter one string:")
f=open('abc.txt','a')
f.write(str1)
f.close()
16.Write a program which reads a text file and counts the number of times a certain letter
appears in the text file.
17.Write a program which accepts an integer value 'n' and prints all prime numbers
till 'n'.
OR
str1=input("please enter your own string:")
total=0
for i in str1:
if i.isalpha():
total=total+1
print("Total number of character in this string=",total)
20.Write a Python program to check if a given key already exists in a dictionary. If key
exists replace with another key/value pair.
21.Write a program to find the length of a set.
22.Write python program to accept and print string in reverse order using recursion.
24.Write a python program to accept NxN matrix and display the same.
26.Read a text file and print all the numbers present in the text file. Also print the size of
the file.
fname = input("Enter file name: ")
with open(fname, 'r') as f:
for line in f:
words = line.split()
for i in words:
for letter in i:
if(letter.isdigit()):
print(letter)
28.Write a Python program to accept n numbers in list and find maximum and minimum
from list.
29.Write a program to create a list of tuples with the first element as the number and
second element as the square of the number.
OR
l_range=int(input("Enter the lower range:"))
u_range=int(input("Enter the upper range:"))
a=[(x,x**2) for x in range(l_range,u_range+1)]
print(a)
30.Write a Python script to generate and print a dictionary that contains a number
(between 1 and n) in the form (x, x*x).
Sample Dictionary (n = 5)
Expected Output : {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
32.Write a Python program to accept n numbers in list and remove duplicates from a list.
34.Write a Python program to accept n numbers in list and find sum and average of
numbers in list.
OR
list=[]
n=int(input("Enter number of element:"))
for i in range (0,n):
ele=int(input("Enter elements:"))
list.append(ele)
count=sum(list)
avg=count/len(list)
print("Sum is:",count)
print("avg is:",avg)
36.Write a Python program to accept n elements in a set and find the length of a set,
maximum, minimum value and the sum of values in a set.
38.Write a Python program to create a tuple of n numbers and print maximum, minimum,
and sum of elements in a tuple.
n = int(input("Enter the number of integers you'd like to enter: "))
print()
l = list()
for i in range(n):
l.append(x)
print()
print()
n1 = tuple(l)
print()
print()
print()
print()
39.Write a program to accept 10 numbers, display the sum of odd numbers and sum of
even numbers.
40.Write a program to create tuple of n numbers, print the first half values of tuple in one
line and the last half values of tuple on next line.
41.Write a program to accept a number 'n', and display the following pattern (Floyd's
triangle)n=3
1
23
456
42.Write a Python program to perform operation on sets which includes union of two sets,
an intersection of sets, set difference and a symmetric difference.
44.Write a Python program to accept two lists and merge the two lists into list of tuple.
45.Write a program to accept a number and display its first ten multiples.
46.Write a Python program to accept a string and from a given string where all
occurrences of its first character have been changed to '$', except the first char itself.
48.Write a Python program to count frequency of each character in a given string using
user defined function.
49.Write a program to create list of tuples with the first element as the number and second
element as the cube of the number.
50.Write a Python program to count the occurrences of given word in sentence. Accept
sentence and word from user.
52.Write a Python program to accept string and remove the characters which have odd
index values of a given string using user defined function.
OR
s=input("Enter a string: ")
print("The original string: ",s)
new_str=""
for i in range (len(s)):
if s[i].isupper():
new_str+=s[i].lower()
elif s[i].islower():
new_str+=s[i].upper()
else:
new_str+=s[i]
print('convert String: ',new_str)
54.Write a program to read the contents of a file in reverse order and display the size of
file.
55.Write a program to perform the add and remove operation on a set.
57. Write a program to get a single string from two given strings and swap the first two characters of each
string.
Sample String: 'abc', 'xyz'
Expected Output: xycabz
58.Write a program to read the contents of a file in reverse order and display the size of
file.
# Open the file in write mode
import os.path
f1 = open("output.txt", "w")
with open("Reverse.txt", "r") as myfile:
data = myfile.read()
data_1 = data[::-1]
f1.write(data_1)
f1.close()
size1=os.path.getsize("Reverse.txt")
print('The file size:',size1)
59.Write a python program which accept word and count the frequency (occurrences) of
given word in a file.
60. Write a program to create a union of sets.