Q1 – Attempt any Eight (8×1=8)
a) What is indentation?
Indentation in Python refers to spaces or tabs at the beginning of a line. It defines blocks of code
(loops, functions, if-else). Python uses indentation instead of braces {}.
b) Code to print the elements of list l1 = [10, 20, 30, 40, 50]
l1 = [10, 20, 30, 40, 50]
for i in l1:
print(i)
c) What is a slice operator?
The slice operator : is used to extract a part (subset) of a sequence like list, tuple, or string.
Example: x[1:4]
d) What is variable-length argument?
A variable-length argument allows a function to accept any number of arguments using *args or
**kwargs.
e) How to add multiple elements at the end of the list?
Use extend() method.
l = [1, 2, 3]
[Link]([4, 5, 6])
f) Explain the remove() method.
remove(x) deletes the first occurrence of value x from a list.
If the value does not exist, it raises an error.
g) Lambda function to add 10 to a given integer
add10 = lambda x: x + 10
h) How to raise an exception with arguments?
raise Exception("Error occurred", 404)
i) List the methods of re package.
Common re methods:
• [Link]()
• [Link]()
• [Link]()
• [Link]()
• [Link]()
• [Link]()
• [Link]()
j) What is wb mode in file?
wb= write in binary mode.
Used for images, audio files, or binary data.
Q2 – Attempt any Four (4×2=8)
a) What is a package? Explain with example how to create a package.
A package is a collection of modules stored in a directory containing an __init__.py file.
Steps to create a package:
1. Create folder mypack/
2. Add file __init__.py
3. Add modules like [Link]
4. Use:
from mypack import calc
b) Usage of tuples: zip(), tuple(), count(), index()
• zip() – Combines multiple iterables into tuple pairs.
• tuple() – Converts an iterable into a tuple.
• count() – Returns number of occurrences of a value in a tuple.
• index() – Finds first index of a value.
c) What is an anonymous function? How to create it?
A function without a name is called an anonymous function.
Created using lambda.
Example:
square = lambda x: x*x
d) Explain loops: While and For
While loop:
i = 1
while i <= 5:
print(i)
i += 1
For loop:
for i in range(1, 6):
print(i)
e) How to perform input-output operations?
Input:
name = input("Enter name: ")
Output:
print("Hello", name)
Q3 – Attempt any Two (2×4=8)
a) Program to accept a string and display reverse after removing ‘s’
s = input("Enter string: ")
s = [Link]('s', '').replace('S', '')
print(s[::-1])
b) Program to raise exception if age < 18
age = int(input("Enter age: "))
if age < 18:
raise Exception("Age is less than 18")
else:
print("Valid age")
c) Program to check string contains only a certain set of characters
Allowed characters: a-z, A-Z, 0-9
import re
s = input("Enter string: ")
pattern = r'^[A-Za-z0-9]+$'
if [Link](pattern, s):
print("Valid string")
else:
print("Invalid string")
Q4 – Attempt any Two (2×4=8)
a) Program to add 'ing' to string (conditions included)
s = input("Enter string: ")
if len(s) < 3:
print(s)
elif [Link]("ing"):
print(s + "ly")
else:
print(s + "ing")
b) Program to combine values in a list of dictionaries
from collections import Counter
data = [
{'item': 'item1', 'amount': 400},
{'item': 'item2', 'amount': 300},
{'item': 'item1', 'amount': 750}
]
result = Counter()
for d in data:
result[d['item']] += d['amount']
print(result)
Output: Counter({'item1': 1150, 'item2': 300})
c) Program to extract year, month, date, time using lambda
import datetime
now = [Link]()
year = lambda dt: [Link]
month = lambda dt: [Link]
date = lambda dt: [Link]
time = lambda dt: [Link]()
print(year(now), month(now), date(now), time(now))
Q5 – Attempt any One (1×3=3)
a) Output of the given code
List mutation tracking:
• check1 = ['Learn','Quiz','Practice','Contribute']
• check2 = check1 → same reference
• check3 = check1[:] → copy
check2[0] = 'Code' → changes check1 and check2
check3[1] = 'Mcq' → only check3 changes
Loops over (check1, check2, check3).
Values:
check1 = ['Code', 'Quiz', 'Practice', 'Contribute']
check2 = ['Code', 'Quiz', 'Practice', 'Contribute']
check3 = ['Learn', 'Mcq', 'Practice', 'Contribute']
Count increments:
• If first element 'Code' → +1
• If second element 'Mcq' → +10
Iteration results:
1. check1 → first element 'Code' → count = 1
2. check2 → 'Code' → count = 2
3. check3 → second element 'Mcq' → count = 12
Final printed output:
12
b) Output of the dictionary counter code
Countries added:
'China'
'Japan'
'china' (lowercase, treated as different key)
So dictionary = {'China':1, 'Japan':1, 'china':1}
len(counter) →3
Output:
3