Syntax:
def function_name(parameters):
statements
Example:
def add(a, b):
return a + b
print(add(5, 10))
---
5. Explain return statement with example.
return sends a value back from a function.
Example:
def area(r):
return 3.14 * r * r
print(area(5))
---
6. Explain two types of scope with example.
Local Scope
Variable inside a function.
def f():
x = 10
print(x)
Global Scope
Variable declared outside functions.
x = 20
def f():
print(x)
---
7. Usage of global keyword. Explain with example.
global is used to modify a global variable inside a function.
Example:
x = 10
def fun():
global x
x = 20
fun()
print(x) # 20
---
8. Explain member aliasing with example.
Assigning a short name to functions of a module.
import math as m
print(m.sqrt(16))
---
9. How to create a file? Explain with example.
f = open("sample.txt","w")
f.write("Hello World")
f.close()
---
10. Explain try..except block with example.
Used to handle exceptions.
try:
a = 10 / 0
except ZeroDivisionError:
print("Cannot divide!")
---
11. Explain finally with example.
finally runs always, whether exception occurs or not.
try:
f = open("x.txt")
except:
print("Error")
finally:
print("This always runs")
---
12. Example for multiple exception handling.
try:
a = int("abc")
except ValueError:
print("Value error")
except TypeError:
print("Type error")
---
13. NumPy array operations: Joining, splitting, searching, sorting
import numpy as np
a = np.array([1,2,3])
b = np.array([4,5])
# Joining
print(np.concatenate((a,b)))
# Splitting
print(np.split(a, 3))
# Searching
print(np.where(a == 2))
# Sorting
print(np.sort([3,1,2]))
---
------------------------------------------
📌 PART – C (8 MARKS EACH)
------------------------------------------
---
1. Explain recursion in detail with example. (8 marks)
Recursion is a method where a function calls itself. Every recursive function has:
1. Base condition (stops recursion)
2. Recursive call (continues recursion)
Example: Factorial using recursion
def fact(n):
if n == 1: # base case
return 1
return n * fact(n-1) # recursive call
print(fact(5))
Working
fact(5) = 5 * fact(4)
fact(4) = 4 * fact(3)
...
fact(1) = 1
Uses of recursion
Tower of Hanoi
Fibonacci series
Searching tree nodes
Divide and conquer algorithms
---
2. Calling function without arguments & with arguments.
Without arguments
def greet():
print("Hello")
greet()
With arguments
def add(a,b):
print(a+b)
add(10,20)
---
3. Types of arguments: default, keyword & arbitrary (8 marks)
1️⃣ Default arguments
def show(name="Guest"):
print(name)
show()
show("Ram")
2️⃣ Keyword arguments
def student(name, age):
print(name, age)
student(age=20,)
*3️⃣ Arbitrary arguments (args)
Used to pass any number of arguments.
def total(*n):
print(sum(n))
total(1,2,3)
---
4. User-defined module with calculator operations + how to use it.
Step 1: Create calculator.py
def add(a,b):
return a+b
def sub(a,b):
return a-b
def mul(a,b):
return a*b
def div(a,b):
return a/b
Step 2: Use this module
import calculator as c
print(c.add(10,20))
print(c.mul(5,6))
---
5. Functions used in reading and writing files.
open()
read()
readline()
write()
writelines()
close()
Example
f = open("a.txt","w")
f.write("Hello")
f.close()
f = open("a.txt")
print(f.read())
---
6. How to raise an exception with message.
age = -5
if age < 0:
raise Exception("Age cannot be negative")
---
7. Explain assertion with example.
assert checks a condition and stops program if condition is false.
Example:
x = 5
assert x > 0, "Number must be positive"
print("Valid")
If false → AssertionError is raised.
✅ MODULE III
Start from the beginning
