Python Functions
What is a function in Python?
• Is a group of related statements that perform
a specific task
• Help break our program into smaller and
modular chunks
• As program grows larger and larger, functions
make it more organized and manageable
• Furthermore, it avoids repetition and makes
code reusable
Syntax of Function
Example of a function
Docstring
The return statement
How Function works in Python?
Scope and Lifetime of variables
• Scope of a variable is the portion of a program where
the variable is recognized
• Parameters and variables defined inside a function is
not visible from outside. Hence, they have a local
scope.
• Lifetime of a variable is the period throughout which
the variable exits in the memory.
• The lifetime of variables inside a function is as long as
the function executes.
• They are destroyed once we return from the function.
Hence, a function does not remember the value of a
variable from its previous calls.
Example
Types of Functions
• Can be divided into the following two types:
– Built-in functions: Functions that are built into
Python
– User-defined functions: Functions defined by the
users themselves
Arguments
Python Default Arguments
Python Keyword Arguments
Python Arbitrary Arguments
Python Recursion
• What is recursion in Python?
• Python Recursive Function
• Advantages of Recursion
• Disadvantages of Recursion
What is recursion in Python?
• Recursion is the process of defining something
in terms of itself
• A physical world example would be to place
two parallel mirrors facing each other
• Any object in between them would be
reflected recursively
Python Recursive Function
Advantages of Recursion
• Make the code look clean and elegant
• A complex task can be broken down into
simpler sub-problems using recursion
Disadvantages of Recursion
• Sometimes the logic behind recursion is hard
to follow through
• Recursive calls are expensive (inefficient) as
they take up a lot of memory and time
• Recursive functions are hard to debug
Python Anonymous/Lambda Function
• What are lambda functions in Python?
• How to use lambda Functions in Python?
– Syntax of Lambda Function in python
– Example of Lambda Function in python
– Use of Lambda Function in python
What are lambda functions in Python?
• In Python, anonymous function is a function
that is defined without a name.
• While normal functions are defined using the
def keyword, in Python anonymous functions
are defined using the lambda keyword.
• Hence, anonymous functions are also called
lambda functions.
How to use lambda Functions in
Python?
• Syntax of Lambda Function in python
Example of Lambda Function in python
Use of Lambda Function in python
Python Global, Local and Nonlocal
variables
• Global Variables in Python
• Local Variables in Python
• Global and Local Variables Together
• Nonlocal Variables in Python
Global Variables in Python
Local Variables in Python
Global and Local Variables Together
Nonlocal Variables in Python
Python Global Keyword
• Python global Keyword
– Rules of global Keyword
– Use of global Keyword (With Example)
• Global Variables Across Python Modules
• Global in Nested Functions in Python
Python global Keyword
• The basic rules for global keyword in Python are:
– When we create a variable inside a function, it’s local
by default
– When we define a variable outside of a function, it’s
global by default. We don’t have to use global
keyword.
– We use global keyword to read and write a global
variable inside a function.
– Use of global keyword outside a function has no effect
Global Variables Across Python
Modules
Global in Nested Functions
Python Modules
• What are modules in Python?
• How to import modules in Python?
– Python import statement
– Import with renaming
– Python from...import statement
– Import all names
• Python Module Search Path
• Reloading a module
• The dir() built-in function
What are modules in Python?
• File containing Python statements and
definitions
– Example: example.py, is called a module
• Use to break down large programs into small
manageable and organized files.
• Provide reusability of code
• Can define our most used functions in a
module and import it, instead of copying their
definitions into different programs
How to import modules in Python?
• Python import statement
• Import with renaming
• Python from...import statement
• Import all names
Python import statement
Import with renaming
Python from...import statement
Import all names
Python Module Search Path
• While importing a module, Python looks at
several places.
• Interpreter first looks for a built-in module then
(if not found) into a list of directories defined in
sys.path.
• The search is in this order.
– The current directory.
– PYTHONPATH (an environment variable with a list of
directory).
– The installation-dependent default directory.
Reloading a module
The dir() built-in function
• We can use the dir() function to find out
names that are defined inside a module.
Python Package
• What are packages?
• Importing module from a package
What are packages?
• We don't usually store all of our files in our
computer in the same location.
• Similar files are kept in the same directory, for
example, we may keep all the songs in the
"music" directory.
• Analogous to this, Python has packages for
directories and modules for files.
• As our application program grows larger in size
with a lot of modules, we place similar modules
in one package and different modules in different
packages.
• Similar, as a directory can contain sub-directories
and files, a Python package can have sub-
packages and modules.
• A directory must contain a file named __init__.py
in order for Python to consider it as a package.
• This file can be left empty but we generally place
the initialization code for that package in this file
Importing module from a package
• We can import modules from packages using the
dot (.) operator.
• For example, if we want to import the start
module in the above example, it is done as
follows.
– import Game.Level.start
• Now if this module contains a function named
select_difficulty(), we must use the full name to
reference it.
– Game.Level.start.select_difficulty(2)
• We can also import the module without the
package prefix as follows
– from Game.Level import start
• We can now call the function simply as
follows.
– start.select_difficulty(2)
• Yet another way of importing just the required
function (or class or variable) form a module
within a package would be as follows.
– from Game.Level.start import
select_difficulty
• Now we can directly call this function.
– select_difficulty(2)
Thank You !

Functions in Python

  • 1.
  • 2.
    What is afunction in Python? • Is a group of related statements that perform a specific task • Help break our program into smaller and modular chunks • As program grows larger and larger, functions make it more organized and manageable • Furthermore, it avoids repetition and makes code reusable
  • 3.
  • 4.
    Example of afunction
  • 5.
  • 6.
  • 7.
  • 8.
    Scope and Lifetimeof variables • Scope of a variable is the portion of a program where the variable is recognized • Parameters and variables defined inside a function is not visible from outside. Hence, they have a local scope. • Lifetime of a variable is the period throughout which the variable exits in the memory. • The lifetime of variables inside a function is as long as the function executes. • They are destroyed once we return from the function. Hence, a function does not remember the value of a variable from its previous calls.
  • 9.
  • 10.
    Types of Functions •Can be divided into the following two types: – Built-in functions: Functions that are built into Python – User-defined functions: Functions defined by the users themselves
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
    Python Recursion • Whatis recursion in Python? • Python Recursive Function • Advantages of Recursion • Disadvantages of Recursion
  • 16.
    What is recursionin Python? • Recursion is the process of defining something in terms of itself • A physical world example would be to place two parallel mirrors facing each other • Any object in between them would be reflected recursively
  • 17.
  • 19.
    Advantages of Recursion •Make the code look clean and elegant • A complex task can be broken down into simpler sub-problems using recursion
  • 20.
    Disadvantages of Recursion •Sometimes the logic behind recursion is hard to follow through • Recursive calls are expensive (inefficient) as they take up a lot of memory and time • Recursive functions are hard to debug
  • 21.
    Python Anonymous/Lambda Function •What are lambda functions in Python? • How to use lambda Functions in Python? – Syntax of Lambda Function in python – Example of Lambda Function in python – Use of Lambda Function in python
  • 22.
    What are lambdafunctions in Python? • In Python, anonymous function is a function that is defined without a name. • While normal functions are defined using the def keyword, in Python anonymous functions are defined using the lambda keyword. • Hence, anonymous functions are also called lambda functions.
  • 23.
    How to uselambda Functions in Python? • Syntax of Lambda Function in python
  • 24.
    Example of LambdaFunction in python
  • 25.
    Use of LambdaFunction in python
  • 27.
    Python Global, Localand Nonlocal variables • Global Variables in Python • Local Variables in Python • Global and Local Variables Together • Nonlocal Variables in Python
  • 28.
  • 30.
  • 31.
    Global and LocalVariables Together
  • 33.
  • 34.
    Python Global Keyword •Python global Keyword – Rules of global Keyword – Use of global Keyword (With Example) • Global Variables Across Python Modules • Global in Nested Functions in Python
  • 35.
    Python global Keyword •The basic rules for global keyword in Python are: – When we create a variable inside a function, it’s local by default – When we define a variable outside of a function, it’s global by default. We don’t have to use global keyword. – We use global keyword to read and write a global variable inside a function. – Use of global keyword outside a function has no effect
  • 39.
  • 41.
  • 42.
    Python Modules • Whatare modules in Python? • How to import modules in Python? – Python import statement – Import with renaming – Python from...import statement – Import all names • Python Module Search Path • Reloading a module • The dir() built-in function
  • 43.
    What are modulesin Python? • File containing Python statements and definitions – Example: example.py, is called a module • Use to break down large programs into small manageable and organized files. • Provide reusability of code • Can define our most used functions in a module and import it, instead of copying their definitions into different programs
  • 44.
    How to importmodules in Python? • Python import statement • Import with renaming • Python from...import statement • Import all names
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
    Python Module SearchPath • While importing a module, Python looks at several places. • Interpreter first looks for a built-in module then (if not found) into a list of directories defined in sys.path. • The search is in this order. – The current directory. – PYTHONPATH (an environment variable with a list of directory). – The installation-dependent default directory.
  • 51.
  • 52.
    The dir() built-infunction • We can use the dir() function to find out names that are defined inside a module.
  • 54.
    Python Package • Whatare packages? • Importing module from a package
  • 55.
    What are packages? •We don't usually store all of our files in our computer in the same location. • Similar files are kept in the same directory, for example, we may keep all the songs in the "music" directory. • Analogous to this, Python has packages for directories and modules for files.
  • 56.
    • As ourapplication program grows larger in size with a lot of modules, we place similar modules in one package and different modules in different packages. • Similar, as a directory can contain sub-directories and files, a Python package can have sub- packages and modules. • A directory must contain a file named __init__.py in order for Python to consider it as a package. • This file can be left empty but we generally place the initialization code for that package in this file
  • 58.
    Importing module froma package • We can import modules from packages using the dot (.) operator. • For example, if we want to import the start module in the above example, it is done as follows. – import Game.Level.start • Now if this module contains a function named select_difficulty(), we must use the full name to reference it. – Game.Level.start.select_difficulty(2)
  • 59.
    • We canalso import the module without the package prefix as follows – from Game.Level import start • We can now call the function simply as follows. – start.select_difficulty(2)
  • 60.
    • Yet anotherway of importing just the required function (or class or variable) form a module within a package would be as follows. – from Game.Level.start import select_difficulty • Now we can directly call this function. – select_difficulty(2)
  • 61.