Python PRactice Questions
Python PRactice Questions
your answer:
Given that:
list1 = [1, 2, 3]
list2 = ['a', 1, 'z', 26,'d', 4]
What will be the output of the following? Justify your answer.
Identify the errors in the following code and rewrite the correct code:
Question 6 Define a Python class BankAccount that keeps track of a bank’s
customers having the following data members:
name: Name of the customer
accountNum: Account Number (auto generated value, beginning at 1)
balance: Amount deposited in the Bank account
Keep an additional variable count to record the number of objects
created for this class. Every time a new account is opened in the bank, the
count is updated.
Which of the following are valid identifiers? If they are invalid, then mention the
cause of violation.
#ofElements
No of elements
3elements
noOfElements
for
In
What will be the value associated with the variable name a on execution of the
following statements? Justify your answers.
b = 10
a *= b + 2
a = 2 + 3 ** 2 // 4 % 3
a = "32 * 76"
a = 'ho' * 5
a = 'string' > 'Strings'
a = ~-12
Q3
You are given a dictionary priceList = {"Pen" : 10, "Pencil" : 5,
"Eraser" : 5, "Ruler" : 20}, representing products and their rates.
write a function rate that accepts this dictionary along with the name of a
product and returns the price of that product. If the product does not exist in
the dictionary, then it should return -1. For example, if the name of the product
is Ruler then the function should return 20.
write another function update to modify the dictionary. The function should
accept the dictionary, name of a product and new rate for it. The function
should return the updated dictionary as per the following cases:
Case 1: if the rate of the product is negative or zero and the product exists in
the dictionary then the product should be removed from the dictionary
Case 2: if the rate of the product is positive and the product exists in the
dictionary then the rate of the product in the dictionary should be changed to
the new rate that is passed to the function
Case 3: if the rate of the product is positive and the product does not exist in
the dictionary then the product-rate pair should be added to the dictionary
s = set(str) # Line 9
s[3] = 'i' # Line 10
print(s[2 : 4]) # Line 11
t = tuple(s) # Line 12
t[3] = 'i' # Line 13
print(s[2 : 4]) # Line 14
Q4
Consider the following two tuples showing games played by Ramesh and Suresh:
gamesRamesh = ('lawn tennis', 'cricket')
gamesSuresh = ('cricket', 'hockey', 'badminton')
Write the statements to compute the following:
games played by Suresh but not Ramesh
games that are common to both of them
all the games that are played by either of them
games that are not common to both of them
What will be the output produced on execution of the following code segments? Justify
your answers.
print(fun(5, 6))
print(fun(6))
print(fun(b = 5))
print(fun())
import copy
lst1 = [13, 23, [38, 42], 52]
lst2 = copy.deepcopy(lst1)
lst3 = lst1
lst2[2][0] = 78
lst3[3] = 61
print(lst1, lst2, lst3)
def m(s):
ans = ''
for i in range(len(s)):
if i % 2 == 0:
ans += s[i]
return ans
print(m('HelloWorld'))
lst = [x * y for x in range(3, 9, 2) for y in range(6,
1, -1) if x * y % 2 == 0 ]
print(lst)
lst = [1, 2, 3]
try:
fn(lst, 1)
fn(lst, 3)
fn(lst, 2)
except ValueError as msg:
print(msg)
Q5
Write a program to read three sides of a triangle and display whether the triangle
is a scalene or equilateral or isosceles triangle, provided that the sides entered by
the user are positive and they form a valid triangle. The program should have
assert statements to ensure that these two conditions are satisfied.
Write a function that accepts the name of a file as an argument and returns the
number of lines in the file. The function should return -1 if the specified file
does not exist.
Write a function for linear search that accepts two arguments: a list of numbers
and a number to be searched. Assume that the list can have duplicate elements.
The function should return a list of all the indices corresponding to the element
being searched. If the element is not there, then the program should return the
list [-1]. For example, if the list is [68, 24, 68, 68, 24, 14] and 68 is to be
searched
then the function should return [0, 2, 3].
Q6 Define a class Student that keeps a record of students. The class should contain the
following:
data members for every student: rollNo, Name, Class, Section,
marks1, marks2, and marks3. Assume that the maximum marks for each
subject are 100.
an additional data member count to keep a record of the number of objects
created for this class. Add appropriate statements in the code to increment the
value of count by 1, when an object is created.
Also define function members:
a constructor function to initialize the members
a function grade, that returns the overall grade of the student according
to the following criteria:
A : if percentage ≥ 90
B : if percentage ≥ 80 and < 90
C : if percentage ≥ 70 and < 80
D : if percentage < 70
str function to display the details of a student including the overall
grade of the student