Scientific Computing with Python Exam Solutions
July 25, 2025
Q1(a) Write the OUTPUT of the following code
def xyz(k):
my_new_string = ""
my_list = [ element for element in k. lower () if ord( element ) in
range (ord('a'), ord('z') + 1)]
key = my_list [0]
j = -1
while j >= -0 and key < my_list [j]:
my_list [j+1] = my_list [j]
j -= 1
my_list [j+1] = key
for element in my_list :
my_new_string += element
return my_new_string
x = xyz(" Welcome ␣To␣The␣ International ␣ Institute ␣of␣ Information ␣
Technology ")
print (x)
Output: welcometotheinternationlinstituteofinformationtechnology
Q1(b) What is namespace? Explain
A namespace in Python is a system to ensure that names (e.g., variables, functions) are
unique and avoid conflicts. It maps names to objects and includes built-in, global, and
local namespaces. For example, print is in the built-in namespace, while a function’s
variables are in its local namespace.
Q1(c) Differentiate between mutable and immutable
objects in Python
• Mutable objects can be changed after creation (e.g., lists, dictionaries). Exam-
ples: [1, 2, 3], {"a": 1}.
• Immutable objects cannot be changed after creation (e.g., strings, tuples). Ex-
amples: "hello", (1, 2, 3).
1
Student ID: b2203 Scientific Computing with Python - July 2025
Q1(d) What is the use of __init__ () and self-parameter
in Python?
• __init__ is a constructor in Python classes, initializing object attributes when an
instance is created.
• self refers to the instance, allowing access to its attributes and methods. Example:
class Person :
def __init__ (self , name):
[Link] = name
p = Person (" Alice ")
Q1(e) What is the difference between modify and copy
operations performed in dictionary?
• Modify changes the original dictionary (e.g., dict["key"] = value updates an
existing key).
• Copy creates a new dictionary with the same content (e.g., newd ict = [Link]()), leavingtheoriginalun
Q3(a) Write a program in Python to create a class
Student
class Student :
def __init__ (self):
self. students = []
def Accept (self):
name = input (" Enter ␣name:␣")
roll = int( input (" Enter ␣roll␣ number :␣"))
sub1 = float ( input (" Enter ␣marks ␣for␣ subject ␣1:␣"))
sub2 = float ( input (" Enter ␣marks ␣for␣ subject ␣2:␣"))
self. students . append ({"name": name , "roll": roll , "sub1":
sub1 , "sub2": sub2 })
def Display (self):
for student in self. students :
print ( student )
def Search (self):
roll = int( input (" Enter ␣roll␣ number ␣to␣ search :␣"))
for student in self. students :
if student ["roll"] == roll:
print ( student )
return
print (" Student ␣not␣ found ")
2
Student ID: b2203 Scientific Computing with Python - July 2025
def Delete (self):
roll = int( input (" Enter ␣roll␣ number ␣to␣ delete :␣"))
for i, student in enumerate (self. students ):
if student ["roll"] == roll:
del self. students [i]
print (" Student ␣ deleted ")
return
print (" Student ␣not␣ found ")
def Update (self):
old_roll = int( input (" Enter ␣old␣roll␣ number :␣"))
new_roll = int( input (" Enter ␣new␣roll␣ number :␣"))
for student in self. students :
if student ["roll"] == old_roll :
student ["roll"] = new_roll
print ("Roll␣ number ␣ updated ")
return
print (" Student ␣not␣ found ")
# Example usage
s = Student ()
s. Accept ()
s. Display ()
s. Search ()
s. Delete ()
s. Update ()
Q5(a) Write a python program for employee salary
calculation
employees = {}
for i in range (10):
name = input (f"Enter ␣name␣for␣ employee ␣{i+1}:␣")
emp_id = input (f" Enter ␣ employee ␣ID␣for␣ employee ␣{i+1}:␣")
basic_salary = float ( input (f" Enter ␣ basic ␣ salary ␣for␣ employee ␣{i
+1}:␣"))
da = basic_salary * 0.70
hra = basic_salary * 0.30
ta = basic_salary * 0.10
gross_salary = basic_salary + da + hra + ta
employees [ emp_id ] = {"name": name , " basic_salary ": basic_salary ,
" gross_salary ": gross_salary }
for emp_id , details in employees . items ():
print (f" Employee ␣ID:␣{ emp_id },␣Name:␣{ details ['name ']},␣Gross ␣
Salary :␣{ details [' gross_salary ']}")