0% found this document useful (0 votes)
151 views15 pages

PPS Official Paper Solution

The document is an instructional guide for first-year engineering students on programming and problem solving, outlining exam instructions and tips for effective answer writing. It includes detailed explanations of functions, lambda functions, good programming practices, and the differences between local and global variables in Python. The document also provides examples and code snippets to illustrate key concepts.

Uploaded by

lokeshrokade453
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
151 views15 pages

PPS Official Paper Solution

The document is an instructional guide for first-year engineering students on programming and problem solving, outlining exam instructions and tips for effective answer writing. It includes detailed explanations of functions, lambda functions, good programming practices, and the differences between local and global variables in Python. The document also provides examples and code snippets to illustrate key concepts.

Uploaded by

lokeshrokade453
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

[5868]-107

First Year Engineering (SEM I/II)


PROGRAMMING AND PROBLEM SOLVING (2019 Pattern)
Solution With Appropriate Answers
Instruction to the candidate
1) Answer Q.1 or Q.2, Q.3 or Q.4, Q.5 or Q.6, Q.7 or Q.8.
2) Neat diagrams must be drawn wherever necessary.
3) Figures to the right indicate full marks.

Tips to Remember You Start Writing Answer Sheet


1) Read the question carefully.
2) Plan your answer before writing.
3) Start with a strong introduction.
4) Organize your thoughts in a logical manner.
5) Support your arguments with evidence and examples.
6) Use clear and concise language.
7) Be specific and precise in your responses.
8) Address all parts of the question.
9) Manage your time effectively.
10) Review and revise your answers.

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_()

 vii) d) All of these

 viii) b) def

 ix) c) file object

 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

- In the above example, ‘add_numbers’ is the function name. It accepts two


parameters, ‘num1’ and ‘num2’, enclosed within the parentheses.
- Inside the function body, it performs the addition operation and assigns the result
to the ‘sum’ variable.
- Finally, the ‘return’ statement is used to return the computed 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.

example of calling the ‘add_numbers’ function and printing the result:


result = add_numbers(5, 3)
print(result)

- 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.

example to illustrate the use of a lambda function:


multiply = lambda x, y: x * y
result = multiply(5, 3)
print(result)

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.

1. Consistent Code Formatting:


Adhere to the PEP 8 style guide for consistent code formatting, naming conventions,
and code layout.
- Consistency in formatting improves code readability and maintainability.

2. Use Descriptive Naming:


Choose meaningful names for variables, functions, and classes to enhance code
understanding.
- Descriptive names make code self-explanatory and aid in comprehension.3.
Modularize Code:

3. Break down tasks :


break down complex tasks into reusable functions or classes, promoting code
reusability and maintainability.
- Modular code is easier to understand, test, and update.

4. Handle Exceptions:
Use try-except blocks to gracefully handle expected errors and exceptions.
- Exception handling prevents program crashes and enables proper error recovery.

5. Write Unit Tests:


Create automated tests to verify code correctness, using testing frameworks like
‘unittest’ or ‘pytest’.
- Testing ensures code reliability, catches bugs early, and provides confidence in
code changes.
By following these practices, developers can produce well-structured, readable,
and maintainable Python code.
Q.No. TOTAL
E 6
Savitribai Phule Pune University M SPPU HUB

प्र.क्र./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.

example demonstrating required arguments:


# Step 1: Define the function with keyword arguments
def greet(name, age):
print("Hello", name, "!")
print("You are", age, "years old.")

# Step 2: Call the function using keyword arguments


greet(name="Bob", age=30)

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.

ii) Keyword Arguments:


- Keyword arguments are function arguments that are identified by their parameter
name when calling the function.
- Unlike required arguments, keyword arguments do not need to follow a specific
order.
Q.No. TOTAL
E 7
Savitribai Phule Pune University M SPPU HUB

प्र.क्र./Q. No.

Q. 3
- Instead, they are specified with the corresponding parameter name followed by a
colon and the value.

example demonstrating keyword arguments:


# Step 1: Define the function with keyword arguments
def greet(name, age):
print("Hello", name, "!")
print("You are", age, "years old.")

# Step 2: Call the function using keyword arguments


greet(name="Bob", age=30)

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.)

FOR ANY QUERY / SUPPORT


. (CLICK ON THE RIGHT SIDED ‘ Message Us’ BUTTON)

You might also like