Python Tutorials - Herong's Tutorial Examples
∟Function Statement and Function Call
This chapter provides introductions and tutorial examples on user defined functions. Topics include defining and calling a function; assigning calling arguments to function parameters; returning object to the calling code; local, enclosing and global variables; using built-in functions; using lambda expression to create anonymous function.
These sections are omitted from this Web preview version. To view the full content,
see information on how to obtain the full version this book.
What Is Function
Built-In Functions
"def" Statement - Define Functions
"f(...)" - Function Call Operation
What Is Function Parameter
What Is Function Call Argument
Assign Function Call Arguments to Parameters
Special Function Parameters - "*" and "/"
"return" Statement Used in Functions
Variable Scope: LEGB (Local, Enclosing, Global, Built-in)
"global" Statement - Declare Global Variables Locally
"nonlocal" Statement - Declare Enclosing Variables Locally
"lambda" Expression - Simple Anonymous Function
Takeaways:
- A function represents a block of sub-statements that can be called
later to perform a specific function.
- A built-in function is a function provided by the Python system.
- "def f(parameter_list)" statements allow you to create user-defined function.
- "f(argument_list)" represents a function call operation to execute
a given function.
- A parameter is a local variable declared in a function to receive
an object reference from the calling code.
- A starred parameter can receive multiple positional parameters as
"tuple" object.
- A double-starred parameter can receive multiple keyword parameters as
"dict" object.
- An argument is an object reference to be assigned to a function parameter
in a function call operation.
- "return object" statements allow you terminate the function
and return the given object.
- A local variable is a variable defined locally inside a function.
It is considered to be in the local scope and accessible only in the defining function.
- A global variable is a variable defined globally in the main code block.
It is considered to be in the global scope and accessible everywhere.
- An enclosing variable is a variable defined in enclosing functions.
It is considered to be in the enclosing scope, and accessible in
the defining function tree.
- "global x" statements allow you to declare a global variable inside a function.
- "nonlocal x" statements allow you to declare an enclosing variable
in the nearest enclosing function.
- "lambda" expression creates a "function" object
which can be called to evaluate a parameterized sub-expression.
Table of Contents
About This Book
Running Python Code Online
Python on macOS Computers
Python on Linux Computers
Built-in Data Types
Variables, Operations and Expressions
Statements - Execution Units
►Function Statement and Function Call
Iterators, Generators and List Comprehensions
Classes and Instances
Modules and Module Files
Packages and Package Directories
"sys" and "os" Modules
"pathlib" - Object-Oriented Filesystem Paths
"pip" - Package Installer for Python
SciPy.org - Python Libraries for Science
pandas - Data Analysis and Manipulation
Anaconda - Python Environment Manager
Jupyter Notebook and JupyterLab
References
Full Version in PDF/EPUB