PPS Official Paper Solution
PPS Official Paper Solution
These points should help you structure and write your answers
effectively during the exam. Good luck!
Q.No. TOTAL
E 1
Savitribai Phule Pune University M SPPU HUB
प्र.क्र./Q. No.
Q. 1
i) a) f_name()
ii) c) .py
iii) b) chr
iv) b) r
v) b) upper
vi) a) _init_()
viii) b) def
x) d) file.close()
Q.No. TOTAL
E 2
Savitribai Phule Pune University M SPPU HUB
प्र.क्र./Q. No.
Q. 2
a) Function and function Definition:
- a function is a block of reusable code that performs a specific task.
- It helps in organizing code, promoting reusability, and improving readability.
- To define a function, you use the ‘def’ keyword followed by the function name,
parentheses, and a colon.
- The function body is indented below the function definition.
- syntax of function definition - def function_name() :
example of a simple function that adds two numbers and returns the result:
def add_numbers(num1, num2):
sum = num1 + num2
return sum
Function Call:
- After defining a function, you can call or invoke it to execute the code within the
function body.
- To call a function, you simply write the function name followed by parentheses
and provide any required arguments or parameters within the parentheses.
- In the above example, ‘add_numbers(5, 3)’ is the function call. It passes the
arguments
Q.No. TOTAL
E 3
Savitribai Phule Pune University M SPPU HUB
प्र.क्र./Q. No.
Q. 2
the arguments ‘5’ and ‘3’ to the ‘add_numbers’ function.
- The function executes the code inside its body, performs the addition operation,
and returns the sum.
- The returned value, ‘8’, is then assigned to the ‘result’ variable.
Finally, we print the ‘result’, which print appropriate output.
Complete program :
def add_numbers(num1, num2):
sum = num1 + num2
return sum
result = add_numbers(5, 3)
print(result)
Output :
8
b) lambda Function:
- A lambda function is an anonymous function because it does not have a name.
It is defined using the ‘lambda’ keyword followed by the function's arguments and
a colon.
- Lambda functions are typically concise and written in a single line.
They are suitable for situations where a small function is required for a specific
purpose.
Syntax:
- The syntax of a lambda function is -
‘lambda arguments: expression’.
Where The arguments represent input parameters to the function, the
expression represents the single-line computation/ operation to be performed.
Q.No. TOTAL
E 4
Savitribai Phule Pune University M SPPU HUB
प्र.क्र./Q. No.
Q. 2
- Lambda functions can take any number of arguments, including zero or multiple
arguments.
- The arguments are specified after the ‘lambda’ keyword and before the colon,
similar to regular function definitions.
- Lambda functions are limited to a single expression, which means they can execute
only one line of code.
- This expression is evaluated and returned as the result of the lambda function.
- Lambda functions provide convenient way to define simple functions, especially
when a named function is not required or for use with higher-order functions that
expect function arguments.
code explanation :
- In the above example, we define a lambda function named ‘multiply’ that takes
two arguments, ‘x’ and ‘y’.
- The lambda function's body consists of a single expression, ‘x * y’, which represents
the multiplication of ‘x’ and ‘y’.We then call the lambda function by passing
arguments ‘5’ and ‘3’ as ‘multiply(5, 3)’.
- The lambda function is invoked, performs the multiplication operation, and returns
the result, which is ‘15’.
- Finally, we print the result, which displays ‘15’ as the output.
Output :
15
Q.No. TOTAL
E 5
Savitribai Phule Pune University M SPPU HUB
प्र.क्र./Q. No.
Q. 2
c) five key points for good Python programming practices.
4. Handle Exceptions:
Use try-except blocks to gracefully handle expected errors and exceptions.
- Exception handling prevents program crashes and enables proper error recovery.
प्र.क्र./Q. No.
Q. 3
a) i) Required Arguments:
- Required arguments are the basic type of function arguments that are mandatory
to provide when calling a function.
- These arguments are defined in the function definition with specific names, and
their values must be provided in the same order when calling the function.
code explanation :
- In the above example, the ‘greet’ function has two required arguments:
- ‘name’ and ‘age’. When calling the function ‘greet("Alice", 25)’, we provide values
for both arguments in the correct order.
- The function then prints a greeting and the age of the person.
Output :
Hello Bob !
You are 30 years old.
प्र.क्र./Q. No.
Q. 3
- Instead, they are specified with the corresponding parameter name followed by a
colon and the value.
code explanation :
- In this example, the function greet expects two keyword arguments: name and
age.
- When calling the function, we use the parameter names (name and age) followed
by a colon and the corresponding values ("Bob" and 30).
- The output displays the greeting message and the provided age.
Output:
Hello Bob !
You are 30 years old.
Additional information :
- required arguments are the basic arguments that must be provided in a specific
order, while keyword arguments are identified by their parameter names and can be
provided in any order.
- Both types of arguments serve different purposes and contribute to the flexibility
and readability of function calls in Python.
Q.No. TOTAL
E 8
Savitribai Phule Pune University M SPPU HUB
प्र.क्र./Q. No.
Q. 3
b) - local and global variables are used to store and access data within different scopes.
- The scope refers to the region of the code where a particular variable is visible
and accessible.
1. Local Variables:
- Local variables are defined within a specific block or function and are only
accessible within that block or function.
- They have a limited scope, and their existence is restricted to the block in which
they are defined.
Example:
def my_function():
x = 10 # Local variable
print("Inside the function:", x)
my_function()
# print("Outside the function:", x)
# This would raise an error
code explanation :
- In the above example, the variable ‘x’ is defined inside the ‘my_function()’
function.
- It is a local variable because it is accessible only within that function.
- When the function is called, the value of ‘x’ is printed correctly.
- However, if we try to access ‘x’ outside the function, it would raise an error
because the variable is not defined in the global scope.
Output:
Inside the function: 102.
Q.No. TOTAL
E 9
Savitribai Phule Pune University M SPPU HUB
प्र.क्र./Q. No.
Q. 3
Global Variables:
- Global variables are defined outside any function or block and can be accessed
from any part of the code.
- They have a global scope, making them accessible throughout the program.
Example:
y = 20
# Global variable
def my_function():
print("Inside the function:", y)
my_function()
print("Outside the function:", y)
Code explanation :
- In the above example, the variable ‘y’ is defined outside the function, making it
a global variable.
- It can be accessed from both the function and the code outside the function.
- When the function is called, the value of ‘y’ is printed correctly, and the same -
value is accessible outside the function as well.
Output:
Inside the function: 20
Outside the function: 20
Additional information :
- if you modify a global variable within a function, you need to explicitly specify
that you want to use the global variable by using the ‘global’ keyword.
- Otherwise, Python will create a new local variable within the function's scope.
TO READ FULLY PDF,
(Click On the Above ‘Read More’ Button to access full PDF.)