[go: up one dir, main page]

0% found this document useful (0 votes)
13 views27 pages

Share 3-Functions - and - Modules

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

Share 3-Functions - and - Modules

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

Python Modules

A file containing a set of functions you want to


include in your application.
Consider a module to be the same as a code library.
We use modules to break down large programs into
small manageable and organized files. Furthermore,
modules provide reusability of code.
We can define our most used functions in a module
and import it, instead of copying their definitions into
different programs.
Example Program
Functions
A function is a block of code which only runs when it
is called.
You can pass data, known as parameters, into a
function.
A function can return data as a result.
Functions
print( ) function max( ) function
type( ) function round( ) function
input( ) function divmod( ) function
abs( ) function id( ) function
pow( ) function ord( ) function
dir( ) function len( ) function
sorted( ) function sum( ) function
help( ) function

Link:- https://www.w3schools.com/python/python_ref_functions.asp
Math Constant Methods
Math Methods
Functions
A function is a block of code which only runs when it
is called.
You can pass data, known as parameters, into a
function.
A function can return data as a result.
Functions
2) User-defined Functions:
Functions that we define ourselves to do certain specific
task are referred as user-defined functions.

Advantages of user-defined functions


User-defined functions help to decompose a large
program into small segments which makes program easy
to understand, maintain and debug.
If repeated code occurs in a program. Function can be
used to include those codes and execute when needed by
calling that function.
Example Program
Functions
3. Lambda Functions:

They are called as anonymous function that are


defined without a name.
While normal functions are defined using
the def keyword in Python, anonymous functions are
defined using the lambda keyword.
This function can have any number of arguments but
only one expression, which is evaluated and returned.
One is free to use lambda functions wherever
function objects are required.
Lambda()
Functions
4. Recursion Functions
A recursive function is a function defined in terms of
itself via self-referential expressions. This means that
the function will continue to call itself and repeat its
behavior until some condition is met to return a result
Examples
Examples
Local vs Global Variables
Global:-Variables that are created outside of a fun.
Local:- Variables that are created inside of a fun and
you can only be used inside that function.
Global keyword:- To create/manage a global variable
inside a function, you can use the global keyword.
Types of Python Arguments
1) Required arguments
2) Keyword arguments
3) Default arguments
4) Variable-length (Arbitrary) arguments
1) Required arguments
Arguments passed to a function in correct positional
order.
No.of arguments in function call should match with
function definition
2) Keyword arguments
During a function call, values passed through
arguments need not be in the order.
No need to give in correct position order.
But all the keyword arguments should match the
parameters in the function definition.
3) Default arguments
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
4) Variable length arguments
also known as arbitrary arguments.
If we don’t know the number of arguments needed for
the function in advance, we can use arbitrary arguments
Modules: Consider a module to be the same as a code
library.
A file containing a set of functions you want to include in
your application.
Library: A library is a collection of pre-combined codes that
can be used iteratively to reduce the time required to code.
Python library is a collection of modules that contain
functions and classes that can be used by other programs to
perform various tasks.
Example libraries:-
 Math, Numpy, Pandas, Matplotlib, TensorFlow,

 Scikit-Learn, Keras, PyTorch, SciPy, Theano,

You might also like