Unit III Question bank
Q1)Define function.and list types of function
Ans: Python Functions is a block of statements that return the specific task. The idea is to put
some commonly or repeatedly done tasks together and make a function so that instead of
writing the same code again and again for different inputs, we can do the function calls to
reuse code contained in it over and over again.
Types of Functions in Python
Built-in library function: These are Standard functions in Python that are available to use.
User-defined function: We can create our own functions based on our requirements.
Q2)explain calling function in python.
Ans: # A simple Python function
def fun():
print("Welcome to GFG")
# Driver code to call a function
fun()
Output: Welcome to GFG
Q3)describe following terms with an example.
i)return statement
ii)scope of variable
ii)lambda function
Ans:i)return statement: A return statement is used to end the execution of the function call
and it “returns” the value of the expression following the return keyword to the caller. The
statements after the return statements are not executed. If the return statement is without any
expression, then the special value None is returned. A return statement is overall used to
invoke a function so that the passed statements can be executed.
Example:
def add(a, b):
# returning sum of a and b
return a + b
def is_true(a):
# returning boolean of a
return bool(a)
# calling function
res = add(2, 3)
print(res)
res = is_true(2<5)
print(res)
output:
5
True
ii)scope of variable: The location where we can find a variable and also access it if required is
called the scope of a variable.
Python Local variable
Local variables are those that are initialized within a function and are unique to that function. It
cannot be accessed outside of the function.
Ex: def f():
# local variable
s = "Hello"
print(s)
# Driver code
f()
output:Hello
Python Global variables
Global variables are the ones that are defined and declared outside any function and are not
specified to any function. They can be used by any part of the program.
Example:
# This function uses global variable s
def f():
print(s)
# Global scope
s = "hello"
f()
output:hello
iii)lambda function: A lambda function is a small anonymous function.
A lambda function can take any number of arguments, but can only have one expression.
Syntax
lambda arguments : expression
Ex: x = lambda a : a + 10
print(x(5))
Output:15
Q4)Explain module.
Ans: Python Module is a file that contains built-in functions, classes,its and variables. There
are many Python modules, each with its specific work.
Create a Python Module:
To create a Python module, write the desired code and save that in a file with .py extension.
Let’s understand it better with an example:
Example:
Let’s create a simple calc.py in which we define two functions, one add and another subtract.
# A simple module, calc.py
def add(x, y):
return (x+y)
def subtract(x, y):
return (x-y)
Import module in Python
We can import the functions, and classes defined in a module to another module using the import
statement in some other Python source file.
When the interpreter encounters an import statement, it imports the module if the module is
present in the search path.
Syntax to Import Module in Python
import module
Importing modules in Python Example
Now, we are importing the calc that we created earlier to perform add operation.
# importing module calc.py
import calc
print(calc.add(10, 2))
Output:
12
Q5) explain packages
Ans:Python packages are a way to organize and structure code by grouping related modules
into directories. A package is essentially a folder that contains an __init__.py file and one or
more Python files (modules).
Key Components of a Python Package
Module: A single Python file containing reusable code (e.g., math.py).
Package: A directory containing modules and a special __init__.py file.
Sub-Packages: Packages nested within other packages for deeper organization.
How to create and access packages in python
1. Create a Directory: Make a directory for your package. This will serve as the root folder.
2. Add Modules: Add Python files (modules) to the directory, each representing specific
functionality.
3. Include __init__.py: Add an __init__.py file (can be empty) to the directory to mark it as a
package.
4. Add Sub packages (Optional): Create subdirectories with their own __init__.py files for
sub packages.
5. Import Modules: Use dot notation to import, e.g., from mypackage.module1 import greet.