Computer Programming - Python (Assessment Test – II Answers)
PART-A (2 Marks Answers)
1. Define a nested loop with an example.
A nested loop is a loop inside another loop. The inner loop will run completely for
every single iteration of the outer loop. Nested loops are used when we need to perform
repeated operations inside another repeated process, such as printing patterns or working
with multi-dimensional data.
Example:
for i in range(3): # Outer loop
for j in range(2): # Inner loop
print(i, j)
In this example, the inner loop executes 2 times for each iteration of the outer loop.
2. What is the difference between function definition and function call?
A function definition is the process of creating a function with a name, parameters, and a
block of code to perform a task. It tells Python what the function does.
Example:
def greet():
print("Hello")
A function call is the process of executing the code written inside the function definition. It
tells Python to run the function.
Example:
greet()
3. What is the use of the pass statement in Python?
The pass statement in Python is a null statement used as a placeholder. It is used when a
statement is required syntactically but you do not want to execute any code. It helps avoid
syntax errors during program [Link]:
for i in range(5):
pass # Placeholder for future code
4. Define anonymous function and give one Python example.
An anonymous function is a function without a name, created using the lambda
keyword. It is mainly used for small one-line operations.
Example:
square = lambda x: x * x
print(square(5)) # Output: 25
5. Write a simple Python function greet() that prints "Good Morning!".
Program:
def greet():
print("Good Morning!")
greet()
This function definition and call prints the message “Good Morning!” on execution.
Below are detailed answers for Part-B based on your question paper.
---
✅ PART-B – Detailed Answers
Question 6(a)
Write a Python program to test whether a given year is leap year or not.
A year is a leap year if:
It is divisible by 4
If it is divisible by 100, it must also be divisible by 400
This rule ensures that the calendar stays in alignment with Earth’s revolution around the
Sun.
Program
year = int(input("Enter a year: "))
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print(year, "is a Leap Year")
else:
print(year, "is Not a Leap Year")
Explanation
year % 4 == 0: checks divisibility by 4
year % 100 != 0: ensures years like 1900 are not leap years
year % 400 == 0: makes years like 2000 leap years
This ensures accurate leap year calculation.
Question 6(b)
Write a function calc() to perform addition, subtraction & multiplication using if-elif-else.
Functions help modularize code. The below function accepts two numbers and an operator,
then performs the respective operation.
Program
def calc(a, b, op):
if op == "+":
return a + b
elif op == "-":
return a - b
elif op == "*":
return a * b
else:
return "Invalid Operator"
# Function Call
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
operator = input("Enter operator (+, -, *): ")
result = calc(num1, num2, operator)
print("Result =", result)
Explanation
Function calc() receives three arguments (a, b, op)
if-elif-else used to check operator and perform operation
Invalid operator handled by else
Question for 6(b)
Types of function arguments in Python with examples
Argument Type Description Example
Positional arguments Values passed in order add(10, 20)
Keyword arguments Parameter name used while calling add(a=10, b=20)
Default arguments Parameter has default value def greet(name="Guest")
Variable-length arguments Accepts multiple values def sum(*n)
Examples
# Positional Argument
def add(a, b):
print(a + b)
add(10, 20)
# Keyword Argument
add(a=5, b=15)
# Default Argument
def greet(name="Guest"):
print("Hello", name)
greet()
greet("Ram")
# Variable Length Argument
def total(*nums):
s=0
for i in nums:
s += i
print("Total =", s)
total(10, 20, 30, 40)
Question 7(a)
Differentiate formal and actual arguments
Feature Formal Arguments Actual Arguments
Meaning Variables in function definition Values supplied during function call
Purpose Receive data Provide data
Example def add(a, b): → a, b add(10, 20) → 10, 20
Example
def add(x, y): # x & y are formal arguments
print(x + y)
add(5, 3) # 5 & 3 are actual arguments
Question 7(b)
Write a program using for loop to find the sum of even and odd numbers from 1 to 100
Program
sum_even = 0
sum_odd = 0
for i in range(1, 101):
if i % 2 == 0:
sum_even += i
else:
sum_odd += i
print("Sum of Even Numbers =", sum_even)
print("Sum of Odd Numbers =", sum_odd)
Explanation
Loop runs from 1 to 100
% 2 == 0 → even numbers
Separate sums maintained and printed
---
Question 7(b)
What is a lambda function? How is it different from a normal function?
Lambda function = Small anonymous function defined using lambda keyword.
Lambda Function Normal Function
Single expression Multiple statements allowed
Anonymous (no name)Has a name
Used for short tasks Used for complex logic
Examples
# Lambda function
square = lambda x: x * x
print(square(5))
# Normal function
def square_fn(x):
return x * x
print(square_fn(5))
# Lambda with two arguments
add = lambda a, b: a + b
print(add(10, 20))
PART-C – 14 MARKS
---
8(a) Discuss different types of loops in Python (for, while, nested loops) with syntax and
programs for each.
Looping statements are used to execute a block of code repeatedly until a certain condition
is satisfied. Python provides for loop, while loop, and nested loops.
---
⭐ 1. For Loop
A for loop is used to iterate over a sequence like list, string, tuple, or range.
Syntax
for variable in sequence:
statement(s)
Example – Printing numbers 1 to 5
for i in range(1, 6):
print(i)
Explanation
range(1,6) generates values from 1 to 5
Loop runs once for every value of i
---
⭐ 2. While Loop
A while loop executes a block of code as long as the condition is true.
Syntax
while condition:
statement(s)
Example – Print 1 to 5
i=1
while i <= 5:
print(i)
i += 1
Explanation
Loop runs until i becomes 6
i += 1 increments value in each iteration
---
⭐ 3. Nested Loop
A nested loop is a loop inside another loop. It is used to handle multi-level iteration, such as
printing patterns or working with matrices.
Syntax
for i in range(n):
for j in range(m):
statement
Example – Print pattern
for i in range(1, 4):
for j in range(1, 4):
print(i, j)
Output
11
12
13
21
22
23
31
32
33
8(b) OR – Discuss importance of return statement in functions with examples
The return statement in Python is used to send a result back from a function to the calling
program. It ends the function execution and returns a value. It improves reusability and
modularity.
---
⭐ Importance of Return Statement
Importance Explanation
Returns value to caller Helps use result elsewhere
Ends function execution No code after return will execute
Allows multiple values Python can return tuple values
Useful in real programs Used in calculations, APIs, DB functions
---
✅ 1. Function returning a single value
def square(x):
return x * x
result = square(5)
print("Square =", result)
---
✅ 2. Function returning multiple values
Python allows returning multiple values separated by commas. They are returned as a
tuple.
def calc(a, b):
add = a + b
sub = a - b
mul = a * b
return add, sub, mul
x, y, z = calc(10, 5)
print("Addition:", x)
print("Subtraction:", y)
print("Multiplication:", z)
Output
Addition: 15
Subtraction: 5
Multiplication: 50
---
✅ 3. Function without return statement
If return keyword is not used, Python returns None by default.
def greet():
print("Good Morning!")
result = greet()
print(result) # Output: None