Python Question Bank
Python Question Bank
Python Question Bank
What is short circuit evaluation? What is printed by the following Python program?
a=0
b=2
5 10
c=3
x = c or a
print(x)
i += 1
else:
print(0)
4 How can you randomize the items of a list in place in Python? 5
Write a Python program to construct the following pattern, using a nested for loop.
*
**
***
5 5
****
***
**
*
7 Write a for loop that prints numbers from 0 to 57, using range function. 2
Write a program that accepts a sentence and calculate the number of digits, uppercase
10 4
and lowercase letters.
11 Write a program to check an input year is leap year or not. 4
S.NO. Unit 3 MARKS
Write a recursive Python function “rprint” to print all elements in a list in reverse.
1 rprint([]) prints nothing 2
rprint([1,2,3]) prints 3 2 1
Write a Python function average to compute the average of a list of numbers. The
function must use try-except to handle the case where the input list is empty.
3 10
Further, in that case the average for the empty list should be set to 0.0 using the
except block.
Write a Python program, triangle(N), that prints a right triangle having base and
height consisting of N * symbols as shown in these examples:
triangle(3) prints:
*
**
***
4 10
triangle(5) prints:
*
**
***
****
*****
What will be the output of following code?
def cube(x):
5 return x * x * x 2
x = cube(3)
print (x)
6 Explain Tuples and Unpacking Sequences in Python Data Structure. 5
Write a Python program to change a given string to a new string where the first and
7 5
last chars have been exchanged.
Write a Python function, alternating(lst), that takes as argument a sequence lst. The
function returns True if the elements in lst are alternately odd and even, starting with
an even number. Otherwise it returns False.
4 For example: 10
alternating([10, 9, 9, 6]) returns False
alternating([10, 15, 8]) returns True
alternating([10]) returns True
alternating([]) returns True
alternating([15, 10, 9]) returns False
Write a Python function, searchMany(s, x, k), that takes as argument a sequence s
and integers x, k (k>0). The function returns True if there are at most k occurrences
of x in s. Otherwise it returns False.
5 For example: 10
searchMany([10, 17, 15, 12], 15, 1) returns True
searchMany([10, 12, 12, 12], 12, 2) returns False
searchMany([10, 12, 15, 11], 17, 1) returns False
6 How do we define an Interface for an ADT? 2
There is a file named Input.Txt. Enter some positive numbers into the file named
11 Input.Txt. Read the contents of the file and if it is an odd number write it to 4
ODD.TXT and if the number is even, write it to EVEN.TXT
Discuss Exceptions and Assertions in Python. Explain with a suitable example.
12 4
Explain any two built-in exceptions.
What is the significance of module in Python? What are the methods of importing a
13 4
module? Explain with suitable example
What do you mean by recursion? Write a recursive function to compute the factorial
14 4
of an input number N.
How do you read an input from a user in Python to be used as an integer in the rest of
3 2
the program? Explain with an example.
What is the output of the following program?
4 2
(lambda x,y : y - 2*x) (1, 11)
Write a Python function removekth(s, k) that takes as input a string s and an integer
k>=0 and removes the character at index k. If k is beyond the length of s, the whole
of s is returned. For example,
6 10
removekth(“PYTHON”, 1) returns “PTHON”
removekth(“PYTHON”, 3) returns “PYTON”
removekth(“PYTHON”, 0) returns “YTHON”
removekth(“PYTHON”, 20) returns “PYTHON”
7 Describe the differences between a linear search and a binary search? 10
How can you create Python file that can be imported as a library as well as run as a
8 10
standalone script?
Describe the difference between
import library
9 and 10
from library import *
When used in a python program. Here library is some python library.
11 Solve the Tower of Hanoi problem for n= 3 disk and show all the steps. 5
Explain the terms Merge List and Merge Sort in Python Programming. 5
14