0% found this document useful (0 votes)
70 views25 pages

Lesson 4 - Functions

A function is a block of code that performs a specific task and can be called multiple times. Functions make code more modular and reusable. There are several benefits to dividing a program into functions including modularity, reusability of code, and manageability. Functions can take parameters as input and return values. Functions in Python are defined using the def keyword and called by their name. Functions can take positional, arbitrary, and keyword arguments. Functions can return values using the return statement. Functions can also call themselves recursively.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
70 views25 pages

Lesson 4 - Functions

A function is a block of code that performs a specific task and can be called multiple times. Functions make code more modular and reusable. There are several benefits to dividing a program into functions including modularity, reusability of code, and manageability. Functions can take parameters as input and return values. Functions in Python are defined using the def keyword and called by their name. Functions can take positional, arbitrary, and keyword arguments. Functions can return values using the return statement. Functions can also call themselves recursively.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 25

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 are units which perform a particular task, take some input, and which
may give some output. One of the most obvious advantages is the division of a program
into smaller parts.

The advantages of dividing the program into functions

 Modular Programming - If a program is divided into small parts in such a way


that different parts each perform some specific task, then each part can be called
as per the requirement.
 Reusability of Code - A function can be called many times. This spares the
programmer from the horror of rewriting the same code again, which in turn can
reduce the length of the program.
 Manageability - Dividing a bigger task into smaller functions makes the program
manageable. It becomes easy to locate bugs and therefore make the program
reliable. It also becomes easy to carry out local optimization in a function.

DEFINITION AND INVOCATION

The definition of a function contains the following:

 Name of a function: The name of a function is any valid identifier. It should be


noted though that the name of a function should be meaningful and, if
possible, convey the task to be performed by the function.
 Parameter: The list of parameters (separated by commas) is given in the
parentheses following the name of the function. The parameters are basically
the input to the function. A function may have any parameters.
 Body of the function: The body of the function contains the code that
implements the task to be performed by the function.

SYNTAX:
Creating a Function

In Python a function is defined using the def keyword:

Calling a Function

To call a function, use the function name followed by parenthesis:

Parameter and Argument

Arguments

Information can be passed into functions as arguments.

Arguments are specified after the function name, inside the parentheses.
You can add as many arguments as you want, just separate them with a comma.

The following example has a function with one argument (fname). When
the function is called, we pass along a first name, which is used inside the
function to print the full name:
Arguments are often shortened to args in Python documentations.

Parameters or Arguments?

The terms parameter and argument can be used for the same thing:
information that are passed into a function.

From a function's perspective: A parameter is the variable listed inside


the parentheses in the function definition. An argument is the value that is
sent to the function when it is called.

Number of Arguments

By default, a function must be called with the correct number of arguments.


Meaning that if your function expects 2 arguments, you have to call the function with 2
arguments, not more, and not less.

Example

This function expects 2 arguments, and gets 2 arguments:

If you try to call the function with 1 or 3 arguments, you will get an error:

This function expects 2 arguments, but gets only 1:


*args

Arbitrary Arguments, *args

If you do not know how many arguments that will be passed into your function,
add a * before the parameter name in the function definition.

This way the function will receive a tuple of arguments, and can access the items
accordingly:

Example

If the number of arguments is unknown, add a * before the parameter name:

Arbitrary Arguments are often shortened to *args in Python documentations.

Keyword arguments

You can also send arguments with the key = value syntax. This way the order of
the arguments does not matter.
The phrase Keyword Arguments are often shortened to kwargs in Python
documentations.

**kwargs

Arbitrary Keyword Arguments, **kwargs

If you do not know how many keyword arguments that will be passed into your
function, add two asterisk: ** before the parameter name in the function definition.

This way the function will receive a dictionary of arguments, and can access the
items accordingly:

Example:

If the number of keyword arguments is unknown, add a double ** before the


parameter name:

Arbitrary Keyword Arguments are often shortened to **kwargs in Python


documentations.

Variable parameter list

Optional parameter / default parameter value

Default Parameter Value


The following example shows how to use a default parameter value. If we call the
function without argument, it uses the default value:

Passing a List as an argument

You can send any data types of argument to a function (string, number, list,
dictionary etc.), and it will be treated as the same data type inside the function.

E.g. if you send a List as an argument, it will still be a List when it reaches the
function:

Function return value and Function Return type

To let a function return a value, use the return statement:


Pass statement in functions

function definitions cannot be empty, but if you for some reason have a function
definition with no content, put in the pass statement to avoid getting an error.

Recursive functions

Python also accepts function recursion, which means a defined function can
call itself.

Recursion is a common mathematical and programming concept. It means that a


function calls itself. This has the benefit of meaning that you can loop through data to
reach a result.

The developer should be very careful with recursion as it can be quite easy to
slip into writing a function which never terminates, or one that uses excess amounts of
memory or processor power. However, when written correctly recursion can be a very
efficient and mathematically-elegant approach to programming.

In this example, tri_recursion() is a function that we have defined to call itself


("recurse"). We use the k variable as the data, which decrements (-1) every time we
recurse. The recursion ends when the condition is not greater than 0 (i.e. when it is 0).

To a new developer it can take some time to work out how exactly this works,
best way to find out is by testing and modifying it.

Recursion Example
String Functions

Python has a set of built-in methods that you can use on strings.

String capitalize() Method

Upper case the first letter in this sentence:


For other functions: https://www.w3schools.com/python/python_strings_methods.asp

Other built-in functions

Python has a set of built-in functions.

For other built-in function in python:


https://www.w3schools.com/python/python_ref_functions.asp

Higher order functions

A function is called Higher Order Function if it contains other functions as a


parameter or returns a function as an output i.e, the functions that operate with
another function are known as Higher order Functions. It is worth knowing that this
higher order function is applicable for functions and methods as well that takes
functions as a parameter or returns a function as a result. Python too supports the
concepts of higher order functions.

Properties of higher-order functions:

 A function is an instance of the Object type.

 You can store the function in a variable.

 You can pass the function as a parameter to another function.

 You can return the function from a function.

 You can store them in data structures such as hash tables, lists, …

Functions as objects

In Python, a function can be assigned to a variable. This assignment does not call the
function, instead a reference to that function is created. Consider the below example,
for better understanding.

Example:

In the above example, a function object referenced by shout and creates a


second name pointing to it, yell.
Passing Function as an argument to other function

Functions are like objects in Python, therefore, they can be passed as


argument to other functions. Consider the below example, where we have created a
function greet which takes a function as an argument.

Example:

Returning function

As functions are objects, we can also return a function from another function. In
the below example, the create_adder function returns adder function.

Example:

Decorators

Decorators are the most common use of higher-order functions in Python. It


allows programmers to modify the behavior of function or class. Decorators allow us to
wrap another function in order to extend the behavior of wrapped function, without
permanently modifying it. In Decorators, functions are taken as the argument into
another function and then called inside the wrapper function.

Syntax:
In the above code, gfg_decorator is a callable function, will add some code on
the top of some another callable function, hello_decorator function and return the
wrapper function.

Example:

Lambda

A lambda function is a small anonymous function.


A lambda function can take any number of arguments, but can only have one
expression.

Syntax

The expression is executed and the result is returned:

Example

Add 10 to argument a, and return the result:

Lambda functions can take any number of arguments:

Example

Multiply argument a with argument b and return the result:

Example

Summarize argument a, b, and c and return the result:


Why Use Lambda Functions?

The power of lambda is better shown when you use them as an anonymous
function inside another function.

Say you have a function definition that takes one argument, and that argument
will be multiplied with an unknown number:

Use that function definition to make a function that always doubles the number
you send in:

Or, use the same function definition to make a function that always triples the
number you send in:
Or, use the same function definition to make both functions, in the same
program:

Use lambda functions when an anonymous function is required for a short period
of time.

MODULES

As your program gets longer, you may want to split it into several files for easier
maintenance. You may also want to use a handy function that you’ve written in several
programs without copying its definition into each program.

To support this, Python has a way to put definitions in a file and use them in a
script or in an interactive instance of the interpreter. Such a file is called a module;
definitions from a module can be imported into other modules or into the main module

Module is a file containing a set of functions you want to include in your


application. Python files contains different related functions.
Create a Module

To create a module just save the code you want in a file with the file extension
.py:

Use a Module

Now we can use the module we just created, by using the import statement:

Note: When using a function from a module, use the syntax:


module_name.function_name.

Variables in Module

The module can contain functions, as already described, but also variables of all
types (arrays, dictionaries, objects etc):
Naming a Module

You can name the module file whatever you like, but it must have the file
extension .py

Re-naming a Module

You can create an alias when you import a module, by using the as keyword:

Built-in Modules

There are several built-in modules in Python, which you can import whenever
you like.

Example

Import and use the platform module:

Using the dir() Function

There is a built-in function to list all the function names (or variable names)
in a module. The dir() function:
Example

List all the defined names belonging to the platform module:

Note: The dir() function can be used on all modules, also the ones you
create yourself.

Python Dates Module

A date in Python is not a data type of its own, but we can import a module
named datetime to work with dates as date objects.

Example

Import the datetime module and display the current date:

Import From Module

You can choose to import only parts from a module, by using the from keyword.

Example
The module named mymodule has one function and one dictionary:

Example

Import only the person1 dictionary from the module:

Note: When importing using the from keyword, do not use the module name
when referring to elements in the module. Example: person1["age"], not
mymodule.person1["age"]

Packages

- Collection of modules (directory with different modules or python files)

When developing a large application, you may end up with many different modules
that are difficult to manage. In such a case, you’ll benefit from grouping and organizing
your modules. That’s when packages come into play.

Python packages are basically a directory of a collection of modules. Packages


allow the hierarchical structure of the module namespace. Just like we organize our files
on a hard drive into folders and sub-folders, we can organize our modules into
packages and subpackages.

To be considered a package (or subpackage), a directory must contain a file named


__init__.py. This file usually includes the initialization code for the corresponding
package.
For example, we can have the following package my_model with modules related to
our data science project:

We can import specific modules from this package using the dot notation. For
example, to import the dataset module from the above package, we can use one of the
following code snippets:

There are a lot of built-in and open-source Python packages that you are probably
already using.

For example:

 NumPy is the fundamental Python package for scientific computing.

 pandas is a Python package for fast and efficient processing of tabular data,
time series, matrix data, etc.

 pytest provides a variety of modules to test new code, including small unit tests
or complex functional tests.

Package Manager

What makes Python a true power tool is the ecosystem of free and open source
libraries like Tensorflow, Netmiko, and Flask. These can be installed with a single
command using a package manager.
PyPI: The Package Index

Similar to NuGet.org & Npmjs.org, Python also has its own official third-party


software repository. The Python Package Index (PyPI) is a repository of software that
hosts an extensive collection of Python packages, development frameworks, tools, and
libraries.

PyPI packages allow developers to share and reuse code rather than having to
reinvent the wheel. As PyPI grew, the need for a package manager became so
apparent that Python eventually created its own standard package manager: pip.

Pip: The Standard Package Manager

Pip is built-in to Python, and can install packages from many different sources.
But PyPI.org is the primary and default package source used.

By default, pip installs packages onto a project’s global Python


environment resulting in packages being accessible by all projects. This can be an issue
due to packages being dependent on specific versions of other packages. Since all
packages are in a global environment, its easy to run into a dependency conflict that
may prevent your application from building. 

Thankfully, pip automates package management by first resolving all


dependencies then proceeding to install the request packages. However, the standard
method for preventing dependency conflicts is to create separate Python environments
for each project.

Pip Alternatives (Pipenv & Poetry)

Pip is the “original” python package manager that others have attempted to
improve upon. Pipenv & Poetry are two package managers that have done this with
great success.

Pipenv is a package management tool that “aims to bring the best of all
packaging worlds” to Python. Pipenv is similar in spirit to Node.js’s npm and
Ruby’s bundler. It’s popular among the Python community because it merges virtual
environments and package management into a single tool. While pip is sufficient for
personal use, Pipenv is recommended for collaborative projects as it’s a higher-level
tool that simplifies dependency management for common use cases and can create
virtual environments.

Poetry prides itself on making Python packaging and dependency management


“easy”. Besides package management, it can help build distributions for applications
and deploy them to PyPI. It also allows the declaration of the libraries a project depends
on and installs/updates them avoiding any conflicting package requirements.
Furthermore, Poetry isolates development versus production dependencies into
separate virtual environments.
Conda: Alternative Package Management

Conda is a multi-purpose package management tool. It manages package


dependencies, can create virtual environments for applications, installs compatible
Python distributions, and packages applications for deployment to production. It
originated from Anaconda, which started as a data science package for Python. Conda
installs packages from Anaconda rather than PyPI and can be used with multiple
programming languages.

Compared to Pip, the package selection is much smaller, but what Conda lacks
in quantity it makes up for in quality. Anyone can publish to PyPI, but only packages
curated by Anaconda are published in its repository. While Anaconda requires a paid
subscription, it grants access to thousands of curated packages and provides support
as well. Conda is an ideal package manager for those that are willing to pay to not worry
about license, quality, and vulnerability issues when dealing with third party/open-
source packages.

Library

A library is an umbrella term referring to a reusable chunk of code. Usually,


a Python library contains a collection of related modules and packages. Actually, this
term is often used interchangeably with “Python package” because packages can also
contain modules and other packages (subpackages). However, it is often assumed that
while a package is a collection of modules, a library is a collection of packages.

Oftentimes, developers create Python libraries to share reusable code with the
community. To eliminate the need for writing code from scratch, they create a set of
useful functions related to the same area.

There are thousands of useful libraries available today. I’ll give just a few examples:

 Matplotlib library is a standard library for generating data visualizations in


Python. It supports building basic two-dimensional graphs as well as more
complex animated and interactive visualizations.

 PyTorch is an open-source deep-learning library built by Facebook’s AI


Research lab to implement advanced neural networks and cutting-edge research
ideas in industry and academia.

 pygame provides developers with tons of convenient features and tools to make


game development a more intuitive task.

 Beautiful Soup is a very popular Python library for getting data from the web.
The modules and packages inside this library help extract useful information from
HTML and XML files.
 Requests is a part of a large collection of libraries designed to make Python
HTTP requests simpler. The library offers an intuitive JSON method that helps
you avoid manually adding query strings to your URLs.

 missingno is very handy for handling missing data points. It provides informative
visualizations about the missing values in a dataframe, helping data scientists to
spot areas with missing data. It is just one of the many great Python libraries for
data cleaning.

By the way, the NumPy and pandas packages that were mentioned before are also


often referred to as libraries. That is because these are complex packages that have
wide applications (i.e. scientific computing and data manipulation, respectively). They
also include multiple subpackages and so basically satisfy the definition of a Python
library.

Python Frameworks

Similar to libraries, Python frameworks are a collection of modules and packages


that help programmers to fast track the development process. However, frameworks are
usually more complex than libraries. Also, while libraries contain packages that perform
specific operations, frameworks contain the basic flow and architecture of the
application.

If you compare application development to house construction , Python


frameworks provide all the essential building blocks like the foundation, walls, windows,
and roof. Then, the developers build their application around this foundation by adding
functionalities comparable to a house’s alarm system, furniture, appliances, etc.

For a better understanding, let’s review several popular frameworks:

 Django is a Python framework for building web applications with less coding.
With all the necessary features included by default, developers can focus on their
applications rather than dealing with routine processes.

 Flask is a web development framework that is known for its lightweight and
modular design. It has many out-of-the-box features and is easily adaptable to
specific requirements.

 Bottle is another lightweight framework for web development that was originally
meant for building APIs. Its unique features are that it has no dependencies other
than the Python Standard Library and it implements everything in a single source
file.

Python frameworks allow programmers to streamline the web development process


by providing a necessary foundation while still being flexible. No wonder that top
applications – including Netflix, Airbnb, Reddit, and Udemy – leverage the benefits of
Python frameworks.
References:

https://learnpython.com/blog/python-modules-packages-libraries-frameworks/

https://blog.inedo.com/python-managing-python-packages

https://www.w3schools.com/python/python_functions.asp

You might also like