Module 3-1
Module 3-1
A module is a file consisting of Python code that can define functions, classes and variables
related to a particular task. A module allows us to organize our code by grouping related code,
which makes the code easier to understand and use.
Putting code into modules is useful because of the ability to import the module
functionality.
A module can be used in some other Python code. Hence, it provides the facility of code
reusability.
A module allows us to logically organize our Python code.
Grouping related code into a module makes the code easier to understand and use.
Similar types of attributes can be categorized and placed in a single module.
3. What is a function in Python?
A function in python is a group of statements within a program that performs a specific task.
Usually functions input data, process it, and “return” a result. Once a function is written, it can
be used repeatedly.
4. Explain different parts of a function
Function Header - Always starts with the “def” keyword followed by the function name
and its parameters, ends with a colon (:)
Parameters - Variables itemized in brackets of the function header
Functions Body - Block of statements/instructions that define the action performed by
the function
Indentation - White space at the beginning of every statement with the same block
5. How to Define a Python Function?
To define a function, Python provides the def keyword. The following is the syntax of defining a
function.
Syntax:
def function_name(parameters):
statement1
statement2
...
...
return [expr]
The keyword def is followed by a suitable identifier as the name of the function and parentheses.
One or more parameters may be optionally mentioned inside parentheses. The: symbol after
parentheses starts an indented block. The keyword def is followed by a suitable identifier as the
name of the function and parentheses. One or more parameters may be optionally mentioned
inside parentheses. The: symbol after parentheses starts an indented block.
The function body contains one or more statements that perform some actions. Optionally, the
last statement in the function block is the return statement.
def greet():
print('Hello World!')
To call a defined function, just use its name as a statement anywhere in the code. By default, all
the functions return none if the return statement does not exist.
Function composition is the way of combining two or more functions in such a way that the
output of one function becomes the input of the second function and so on.
Example:
def add(x):
return x + 2
def multiply(x):
return x * 2
print multiply(add(5))
OUTPUT:
14
Example
sin(0.88) : 0.77
sin(0) : 0.0
Example
floor(4.3) : 4.
Example
sqrt(25) : 5
Example
gcd(60, 48) : 12
Example
fmod(25,5) : 0.0
fmod(20,3):2.0
Example :
ceil(100.12) : 101.0
A parameter is a special kind of variable used in a function to refer to one of the pieces of data
provided as input to the function. Formal parameters are mentioned in the function
definition. Actual parameters (arguments) are passed during a function call.
The Python return statement is a special statement that you can use inside a function
or method to send the function’s result back to the caller. A return statement consists of
the return keyword followed by an optional return value.
Keyword arguments
Keyword arguments are related to the function calls. When you use keyword arguments in a
function call, the caller identifies the arguments by the parameter name. This allows you to skip
arguments or place them out of order because the Python interpreter is able to use the keywords
provided to match the values with parameters.
Example:
def add(a,b,c):
return (a+b+c)
print(b=5,c=3,a=6)
Default arguments
A default argument is an argument that assumes a default value if a value is not provided in the
function call for that argument. Default arguments are values that are provided while defining
functions. The assignment operator = is used to assign a default value to the argument. Default
arguments become optional during the function calls. If we provide a value to the default
arguments during function calls, it overrides the default value. The function can have any number
of default arguments. Default arguments should follow non-default arguments.
Example:
def add(a,b=5,c=10):
return (a+b+c)
This function can be called in 3 ways
Print (add(3)) # output 18
print (add(3,4)) # output 17
print(add(2,3,4)) # output 9
Variable-length arguments
You may need to process a function for more arguments than you specified while defining the
function. These arguments are called variable-length arguments and are not named in the
function definition
Example
def add(*b):
result=0
for i in b:
result=result+i
return result
print (add(1,2,3,4,5))
The Python return statement is a special statement that you can use inside a function or method to
send the function’s result back to the caller. A return statement consists of
the return keyword followed by an optional return value.
The return value of a Python function can be any Python object. Everything in Python is an object. So,
your functions can return numeric values ( int , float, and complex values), collections and sequences
of objects (list, tuple, dictionary, or set objects)
An explicit return statement immediately terminates a function execution and sends the return value
back to the caller code. To add an explicit return statement to a Python function, you need to
use return followed by an optional return value.
A Python function will always have a return value. If you don’t explicitly use a return value in
a return statement, or if you totally omit the return statement, then Python will implicitly return a
default value for you. That default return value will always be none.
You can use a return statement to return multiple values from a function. To do that, you just need to
supply several return values separated by commas.
Example of multiple return statements
def calc(a,b)
return a+b, a-b, a*b
m,n,p=calc(10,5)