0% found this document useful (0 votes)
17 views6 pages

Module 3-1

Uploaded by

aljockzz99
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)
17 views6 pages

Module 3-1

Uploaded by

aljockzz99
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/ 6

MODULE 3

1. What is module in python?

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.

2. What are the advantages of modules in python?

Python modules have the following advantages:

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

Example: User-defined Function

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.

6. Explain Function Composition in Python with example

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

7. Explain different Math functions in python


 sin(x) - returns the sine of value passed as argument. The value passed in this function
should be in radians

Example
sin(0.88) : 0.77
sin(0) : 0.0

 floor(x) - Decreases value to the previous integer value

Example
floor(4.3) : 4.

 pow(x,y) - Used to raise x value toy


Example :
pow(5,2) : 25

 sqrt(x) - Used to get square root of the value passed as argument.

Example
sqrt(25) : 5

 factorail(x) - Used to get factorail of the value passed as argument.


Example
factorial(5): 120

 gcd(x,y) : Used to get common divisor of x and y

Example
gcd(60, 48) : 12

 fmod(x,y) : Returns the remainder division of x and y.

Example
fmod(25,5) : 0.0
fmod(20,3):2.0

 ceil(x) : Returns the next postive integer

Example :
ceil(100.12) : 101.0

8. What is a parameter in a function?

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.

9. What is return statements in python?

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.

10. Explain global and local variable in python


In Python, a variable declared outside of the function or in global scope is known as a global variable.
This means that a global variable can be accessed inside or outside of the function.
A variable declared inside the function's body or in the local scope is known as a local variable.

11. What are the different function arguments in python?

Following are the different type of functional type of arguments


 Required arguments
 Keyword arguments
 Default arguments
 Variable-length arguments
Required arguments
Required arguments are the arguments passed to a function in correct positional order. Here, the
number of arguments in the function call should match exactly with the function definition.
def add(a,b,c):
return (a+b+c)
print(5,3,6)

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

12. Explain Return statement in python

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)

Explain recursive function in python?


Recursion is a method of programming or coding a problem, in which a function calls itself one or
more times in its body. Each recursive implementation has a base case, which is when the desired
state has been reached, and a recursive case where the desired state has not been reached and the
function enters another recursive step.
Example
def factorial(x):
if x == 1:
return 1
else:
return (x * factorial(x-1))

You might also like