Pps question bank
Unit 03 • Unit 04 • Unit 05 • Unit 06
Unit No:-03
1. What do you mean by function. Mention the advantages of using function in program
In programming, a function is a named block of code that performs a specific task or operation. It is a fundamental
concept in most programming languages and is used to organize code into reusable and modular units. Functions
take input parameters (if required), perform a series of actions or calculations, and may return a value as a result.
Advantages of using functions in programming include:
1. Reusability: Functions allow you to write a block of code once and reuse it multiple times throughout your
program. This avoids duplicating code and promotes code reuse, which leads to cleaner and more maintainable
code.
2. Modularity: Functions break down complex programs into smaller, more manageable units. By dividing code
into functions, you can focus on solving specific problems or implementing specific functionalities. This modular
approach enhances code organization and readability.
3. Abstraction: Functions provide a way to encapsulate complex logic behind a simple interface. When calling a
function, you don't need to know how it is implemented internally; you only need to understand its purpose and
how to use it. This abstraction simplifies program development and allows for easier collaboration among
developers.
4. Code maintenance: Functions make code easier to maintain and debug. If a bug is found or a change is required,
you only need to modify the code within the function, rather than searching through the entire program. This
saves time and effort, especially in large projects.
5. Code readability: Functions can improve the readability of your code by giving meaningful names to different
sections of your program. Well-named functions can act as self-documenting units of code, making it easier for
others (including yourself) to understand the purpose and behavior of the code.
6. Testing and debugging: Functions enable easier testing and debugging. Since functions perform specific tasks,
you can test them individually to ensure they work correctly. Additionally, if an error occurs within a function, you
can isolate and debug that specific function without affecting the rest of the program.
Overall, using functions in programming promotes code reuse, modularity, abstraction, maintainability, readability,
and facilitates testing and debugging, making it an essential and beneficial concept in software development.
2. Explain various ways of calling function with suitable example
In Python, there are several ways to call a function, depending on the requirements and the way the function is
defined. Here are some common ways to call functions in Python:
pg. 1
https://msha.ke/btechnotes
,1. Standard function call: This is the most basic way to call a function in Python. You simply write the function
name followed by parentheses, optionally passing any required arguments.
def greet(name):
print("Hello, " + name + "! ")
greet("Alice") # Output: Hello, Alice!
2. Keyword arguments: When calling a function, you can explicitly specify the values of the arguments using their
corresponding parameter names. This allows you to pass arguments in any order, as long as you specify the
parameter name.
def greet(name, age):
print("Hello, " + name + "! You are " + str(age) + " years old. ")
greet(age = 25, name = "Bob") # Output: Hello, Bob! You are 25 years old.
3. Default arguments: You can define default values for function parameters. If an argument is not passed when
calling the function, the default value will be used.
def greet(name, age = 30):
print("Hello, " + name + "! You are " + str(age) + " years old. ")
greet("Charlie") # Output: Hello, Charlie! You are 30 years old.
greet("Dave", 35) # Output: Hello, Dave! You are 35 years old.
4. Variable-length arguments: Python allows you to define functions that can accept a variable number of
arguments. There are two ways to achieve this: using `*args` for positional arguments or `**kwargs` for keyword
arguments.
def sum_numbers(∗ args):
total = 0
for num in args:
total += num
return total
print(sum_numbers(1, 2, 3, 4, 5)) # Output: 15
def print_person_info(∗∗ kwargs):
for key, value in kwargs. items():
pg. 2
https://msha.ke/btechnotes
, print(key + ": " + str(value))
print_person_info(name = "Eve", age = 27, city = "New York")
# Output:
# name: Eve
# age: 27
# city: New York
5. Call by reference: In Python, when you pass mutable objects (like lists or dictionaries) to a function, any
modifications made within the function will affect the original object.
def add_one(numbers):
for i in range(len(numbers)):
numbers[i] += 1
my_list = [1, 2, 3]
add_one(my_list)
print(my_list) # Output: [2, 3, 4]
These are some of the ways you can call functions in Python, offering flexibility and versatility depending on your
specific needs and scenarios.
3. Write short note on Modules in python
In Python, a module is a file that contains Python code, typically with a `.py` extension, which can define functions,
classes, and variables. Modules allow you to organize your code into separate files and provide a way to reuse
code across multiple programs or projects. Here are some key points about modules in Python:
1. Code organization: Modules help in organizing code by providing a way to separate code into logical units. You
can group related functions, classes, and variables within a module, making it easier to manage and maintain your
codebase.
2. Code reusability: Modules promote code reuse. Once you have defined a module, you can import it into other
Python programs or modules, and use its functions, classes, and variables. This avoids duplicating code and allows
you to leverage existing functionality in different contexts.
3. Namespace isolation: Modules provide a namespace for variables, functions, and classes. By importing a
module, you can access its contents using the module's name as a prefix, which helps avoid naming conflicts. For
example, if you have a function called `calculate()` in two different modules, you can differentiate them as
`module1.calculate()` and `module2.calculate()`.
pg. 3
https://msha.ke/btechnotes
, 4. Standard library and third-party modules: Python comes with a rich standard library that provides a wide range
of modules for various tasks, such as file I/O, networking, data processing, and more. Additionally, the Python
ecosystem has a vast collection of third-party modules available through package managers like pip. These
modules extend the capabilities of Python and allow you to leverage ready-made solutions for specific
requirements.
5. Creating and using modules: To create a module, you can simply create a Python file and define your functions,
classes, and variables within it. To use a module, you need to import it in your program using the `import`
statement. There are different ways to import a module, such as importing the entire module or specific elements
from the module. For example:
# Importing the entire module
import math
result = math. sqrt(16)
# Importing specific elements from a module
from datetime import datetime
current_time = datetime. now()
# Importing a module with an alias
import numpy as np
array = np. array([1, 2, 3, 4, 5])
6. Creating your own modules: You can create your own modules by defining functions, classes, and variables in a
Python file. To use your custom module in another program or module, make sure the module file is in the same
directory or in a directory listed in the Python path.
Overall, modules in Python offer a way to organize, reuse, and extend code. They enhance code modularity,
encourage good code organization practices, and allow developers to leverage existing functionality from both the
standard library and third-party packages.
4. What is lambda function? Explain it with proper syntax
A lambda function in Python is a small anonymous function that can be defined without a name. It is also known as
an "anonymous function" because it does not require a function definition using the `def` keyword. Lambda
functions are commonly used for simple operations or as a way to define a function inline.
The syntax for a lambda function is as follows:
pg. 4
https://msha.ke/btechnotes
Unit 03 • Unit 04 • Unit 05 • Unit 06
Unit No:-03
1. What do you mean by function. Mention the advantages of using function in program
In programming, a function is a named block of code that performs a specific task or operation. It is a fundamental
concept in most programming languages and is used to organize code into reusable and modular units. Functions
take input parameters (if required), perform a series of actions or calculations, and may return a value as a result.
Advantages of using functions in programming include:
1. Reusability: Functions allow you to write a block of code once and reuse it multiple times throughout your
program. This avoids duplicating code and promotes code reuse, which leads to cleaner and more maintainable
code.
2. Modularity: Functions break down complex programs into smaller, more manageable units. By dividing code
into functions, you can focus on solving specific problems or implementing specific functionalities. This modular
approach enhances code organization and readability.
3. Abstraction: Functions provide a way to encapsulate complex logic behind a simple interface. When calling a
function, you don't need to know how it is implemented internally; you only need to understand its purpose and
how to use it. This abstraction simplifies program development and allows for easier collaboration among
developers.
4. Code maintenance: Functions make code easier to maintain and debug. If a bug is found or a change is required,
you only need to modify the code within the function, rather than searching through the entire program. This
saves time and effort, especially in large projects.
5. Code readability: Functions can improve the readability of your code by giving meaningful names to different
sections of your program. Well-named functions can act as self-documenting units of code, making it easier for
others (including yourself) to understand the purpose and behavior of the code.
6. Testing and debugging: Functions enable easier testing and debugging. Since functions perform specific tasks,
you can test them individually to ensure they work correctly. Additionally, if an error occurs within a function, you
can isolate and debug that specific function without affecting the rest of the program.
Overall, using functions in programming promotes code reuse, modularity, abstraction, maintainability, readability,
and facilitates testing and debugging, making it an essential and beneficial concept in software development.
2. Explain various ways of calling function with suitable example
In Python, there are several ways to call a function, depending on the requirements and the way the function is
defined. Here are some common ways to call functions in Python:
pg. 1
https://msha.ke/btechnotes
,1. Standard function call: This is the most basic way to call a function in Python. You simply write the function
name followed by parentheses, optionally passing any required arguments.
def greet(name):
print("Hello, " + name + "! ")
greet("Alice") # Output: Hello, Alice!
2. Keyword arguments: When calling a function, you can explicitly specify the values of the arguments using their
corresponding parameter names. This allows you to pass arguments in any order, as long as you specify the
parameter name.
def greet(name, age):
print("Hello, " + name + "! You are " + str(age) + " years old. ")
greet(age = 25, name = "Bob") # Output: Hello, Bob! You are 25 years old.
3. Default arguments: You can define default values for function parameters. If an argument is not passed when
calling the function, the default value will be used.
def greet(name, age = 30):
print("Hello, " + name + "! You are " + str(age) + " years old. ")
greet("Charlie") # Output: Hello, Charlie! You are 30 years old.
greet("Dave", 35) # Output: Hello, Dave! You are 35 years old.
4. Variable-length arguments: Python allows you to define functions that can accept a variable number of
arguments. There are two ways to achieve this: using `*args` for positional arguments or `**kwargs` for keyword
arguments.
def sum_numbers(∗ args):
total = 0
for num in args:
total += num
return total
print(sum_numbers(1, 2, 3, 4, 5)) # Output: 15
def print_person_info(∗∗ kwargs):
for key, value in kwargs. items():
pg. 2
https://msha.ke/btechnotes
, print(key + ": " + str(value))
print_person_info(name = "Eve", age = 27, city = "New York")
# Output:
# name: Eve
# age: 27
# city: New York
5. Call by reference: In Python, when you pass mutable objects (like lists or dictionaries) to a function, any
modifications made within the function will affect the original object.
def add_one(numbers):
for i in range(len(numbers)):
numbers[i] += 1
my_list = [1, 2, 3]
add_one(my_list)
print(my_list) # Output: [2, 3, 4]
These are some of the ways you can call functions in Python, offering flexibility and versatility depending on your
specific needs and scenarios.
3. Write short note on Modules in python
In Python, a module is a file that contains Python code, typically with a `.py` extension, which can define functions,
classes, and variables. Modules allow you to organize your code into separate files and provide a way to reuse
code across multiple programs or projects. Here are some key points about modules in Python:
1. Code organization: Modules help in organizing code by providing a way to separate code into logical units. You
can group related functions, classes, and variables within a module, making it easier to manage and maintain your
codebase.
2. Code reusability: Modules promote code reuse. Once you have defined a module, you can import it into other
Python programs or modules, and use its functions, classes, and variables. This avoids duplicating code and allows
you to leverage existing functionality in different contexts.
3. Namespace isolation: Modules provide a namespace for variables, functions, and classes. By importing a
module, you can access its contents using the module's name as a prefix, which helps avoid naming conflicts. For
example, if you have a function called `calculate()` in two different modules, you can differentiate them as
`module1.calculate()` and `module2.calculate()`.
pg. 3
https://msha.ke/btechnotes
, 4. Standard library and third-party modules: Python comes with a rich standard library that provides a wide range
of modules for various tasks, such as file I/O, networking, data processing, and more. Additionally, the Python
ecosystem has a vast collection of third-party modules available through package managers like pip. These
modules extend the capabilities of Python and allow you to leverage ready-made solutions for specific
requirements.
5. Creating and using modules: To create a module, you can simply create a Python file and define your functions,
classes, and variables within it. To use a module, you need to import it in your program using the `import`
statement. There are different ways to import a module, such as importing the entire module or specific elements
from the module. For example:
# Importing the entire module
import math
result = math. sqrt(16)
# Importing specific elements from a module
from datetime import datetime
current_time = datetime. now()
# Importing a module with an alias
import numpy as np
array = np. array([1, 2, 3, 4, 5])
6. Creating your own modules: You can create your own modules by defining functions, classes, and variables in a
Python file. To use your custom module in another program or module, make sure the module file is in the same
directory or in a directory listed in the Python path.
Overall, modules in Python offer a way to organize, reuse, and extend code. They enhance code modularity,
encourage good code organization practices, and allow developers to leverage existing functionality from both the
standard library and third-party packages.
4. What is lambda function? Explain it with proper syntax
A lambda function in Python is a small anonymous function that can be defined without a name. It is also known as
an "anonymous function" because it does not require a function definition using the `def` keyword. Lambda
functions are commonly used for simple operations or as a way to define a function inline.
The syntax for a lambda function is as follows:
pg. 4
https://msha.ke/btechnotes