Python- practice paper – 1

Python- practice paper – 1

In this post, we will see different types of questions based on python language which checks your python basic skills.

Ques 1: Give the output that will be produced on the execution of the following code

  • x=3
    if x>2:
        if y>2:
            z = x+y
            print("Z is: " , z)
            print("x is: ", x)
  • for i in range(1,5):
        j = 0
        while j<i: 
            print(j, end=" ")
            j += 1
  • d={'Name':'Alice', 'Age':7}
    print(d.get(1, 'Invalid'))
  • float(4+int(2.39)%2)
  • "asdf"[::-1]
  • print(list('Hello'))

Ques 2: Write a function series_sum(n) in Python to calculate the sum of the first
n terms of the following series:

1/2 – 1/4 +1/ 8 – 1/16 + 1/32 – 1/64+ .....

Ques 3: Write Python statements to read name and age as input from a user and print
the year in which the user turns 100 years old.

Ques 4: Give the output that will be produced on the execution of the following code

  • l1= [10, 22, 'x', 'y',33,44]
  • max(l1)
  • type(l1[2])
  • l2=[4,5]
  • l1.reverse()
  • try:
        f = open("MyFile.txt", "r")
        f.write("This is my file")
    except IOError:
        print("Cant open file")
        print("Content writen")
  • list=['a',0,2]
    for x in list:
            print("The value is ", x)
        except Exception as e:
            print(e," occured")
            print("Next value")
    print("Reciprocal of ", x, " is ", r)
  • Word1= " Hello first year students"
    Word2=" Hello second year students"
    for i in Word1:
        if i in Word2:
            print(i , end=" ")

Ques 5: Write statements to create a file Countries.txt with the following rows:


Consider the file Countries.txt. Give the output that will be produced
on the execution of the following code segment:

f1 = open("Countries.txt", "r")
while name:
    if name.startswith("I"):

Ques 6: Give the output that will be produced on execution of the following code:

def f():
    except ZeroDivisionError:
        print("Divided by zero")

def main(): 
     print("After the function call")
    except IndexError:
     print("Index out of bound")
     print("Exception in main")

Ques 7: Apply Insertion sort scheme of sorting on the following list to sort it in ascending order:
Show the list after each iteration.
How many iterations are required to sort the above list?

Apply Binary Search to search for item 9 in the sorted list. At each step, show the
index at which the value is compared with 9.
Under what circumstances, you would prefer to use linear search over binary search?
Justify your answer.

Ques 8: Define a class Rectangle having length and breadth of the rectangle as
the data members and the methods to do the following:

  • Constructor to initialize the data members’ length and breadth.
  • area() to calculate area of the rectangle.
  • perimeter() to calculate perimeter of the rectangle.
  • __str__ to return string representation for displaying the data members suitably.

Also, write Python statements to:

  • Create an object of class Rectangle of length 4 and breadth 3.
  • Invoke the method area().
  • Invoke the method perimeter().
  • Print all the data members of the class.

Ques 9: Give the output that will be produced on execution of the following code

class Abc:
    def __init__(self,name,id): = name = id
Abc.const = 99
A = Abc("John",123)
B = Abc("Diana",444)
B.const = 9
C = Abc("William",222)
print("A :", A.const, "B :", B.const, "C :",C.const)

Ques 10: Consider the following two sets:

setx = set(["green", "blue", "yellow", "red"])
sety = set (["blue", "yellow", "pink", "orange"])

Write the Python statements for each of the following operations:

  • Adding an element to the set setx.
  • Compute xUy as union of setx and sety
  • Compute xminusy as set difference between setx and sety.
  • Remove “blue” from setx.

Ques 11: Consider the following string:

greeting = "Good Morning. Have a Good Day!! "

Give the output for the following function calls:

  • greeting.find(“a”)
  • greeting.swapcase()
  • greeting.istittle()
  • greeting.replace(“Good”, “Sweet”)
  • greeting.strip()
  • greeting.endswith(“!!”)

Ques 12: Consider the tuple t1 defined below :

t1 = (12, 5, 2, 4, 17, 44,7, 6, 9, 10)

Write a Python statement to:

  • Print first half of the values of t1 in one line and the other half in another line.
  • Produce a list comprising all the even numbers in the tuple t1.

Hope you guys find this practice paper useful.

Leave a Reply

Your email address will not be published. Required fields are marked *

📢 Need further clarification or have any questions? Let's connect!

Connect 1:1 With Me: Schedule Call

If you have any doubts or would like to discuss anything related to this blog, feel free to reach out to me. I'm here to help! You can schedule a call by clicking on the above given link.
I'm looking forward to hearing from you and assisting you with any inquiries you may have. Your understanding and engagement are important to me!

This will close in 20 seconds