SlideShare a Scribd company logo
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
Functions in Python
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
Functions 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
Functions in Python
Local Variables in Python
Global and Local Variables Together
Functions in Python
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
Functions in Python
Functions in Python
Functions in Python
Global Variables Across Python
Modules
Functions in Python
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.
Functions in Python
Reloading a module
The dir() built-in function
• We can use the dir() function to find out
names that are defined inside a module.
Functions in Python
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
Functions in Python
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 !

More Related Content

What's hot (20)

PPSX
Modules and packages in python
TMARAGATHAM
 
PPSX
python Function
Ronak Rathi
 
PPTX
Chapter 05 classes and objects
Praveen M Jigajinni
 
PPTX
Class, object and inheritance in python
Santosh Verma
 
PPTX
Python Exception Handling
Megha V
 
PPTX
Conditional and control statement
narmadhakin
 
ODP
Python Modules
Nitin Reddy Katkam
 
PPTX
Looping Statements and Control Statements in Python
PriyankaC44
 
PDF
Python programming : Classes objects
Emertxe Information Technologies Pvt Ltd
 
PPT
File handling in c
David Livingston J
 
PDF
Functions and modules in python
Karin Lagesen
 
PPTX
Control Flow Statements
Tarun Sharma
 
PPTX
Python: Modules and Packages
Damian T. Gordon
 
PPTX
Member Function in C++
NikitaKaur10
 
PDF
Strings in python
Prabhakaran V M
 
PPTX
Inline function
Tech_MX
 
PPTX
Regular expressions in Python
Sujith Kumar
 
PPTX
File in C language
Manash Kumar Mondal
 
Modules and packages in python
TMARAGATHAM
 
python Function
Ronak Rathi
 
Chapter 05 classes and objects
Praveen M Jigajinni
 
Class, object and inheritance in python
Santosh Verma
 
Python Exception Handling
Megha V
 
Conditional and control statement
narmadhakin
 
Python Modules
Nitin Reddy Katkam
 
Looping Statements and Control Statements in Python
PriyankaC44
 
Python programming : Classes objects
Emertxe Information Technologies Pvt Ltd
 
File handling in c
David Livingston J
 
Functions and modules in python
Karin Lagesen
 
Control Flow Statements
Tarun Sharma
 
Python: Modules and Packages
Damian T. Gordon
 
Member Function in C++
NikitaKaur10
 
Strings in python
Prabhakaran V M
 
Inline function
Tech_MX
 
Regular expressions in Python
Sujith Kumar
 
File in C language
Manash Kumar Mondal
 

Similar to Functions in Python (20)

PPT
Chapter Introduction to Modular Programming.ppt
AmanuelZewdie4
 
PPT
chapterintroductiontomodularprogramming-230112092330-e3eb5a74 (1).ppt
harinipradeep15
 
PPTX
Python Tutorial Part 2
Haitham El-Ghareeb
 
PPTX
pythontraining-201jn026043638.pptx
RohitKumar639388
 
PPTX
Python Functions
Sampad Kar
 
PPTX
Python 01.pptx
AliMohammadAmiri
 
PPTX
Python training
Kunalchauhan76
 
PPTX
_Python_ Functions _and_ Libraries_.pptx
yaramahsoob
 
PDF
Using Python Libraries.pdf
SoumyadityaDey
 
PPTX
Object oriented programming design and implementation
afsheenfaiq2
 
PPTX
Object oriented programming design and implementation
afsheenfaiq2
 
PPTX
Modules and Packages in Python Programming Language.pptx
arunavamukherjee9999
 
PDF
Python for katana
kedar nath
 
PPTX
Functions
Gaurav Subham
 
ODP
20120314 changa-python-workshop
amptiny
 
PDF
Python libraries
Prof. Dr. K. Adisesha
 
PPTX
Introduction to Python Basics for PSSE Integration
FarhanKhan978284
 
PDF
Python Course In Chandigarh
Excellence Academy
 
PPTX
NMOUP unit 1hrefe jrei g. rwih. werhi wehriw ue
ShashankKumar30032
 
PPTX
python-fefedfasdgsgfahfdshdhunctions-190506123237.pptx
avishekpradhan24
 
Chapter Introduction to Modular Programming.ppt
AmanuelZewdie4
 
chapterintroductiontomodularprogramming-230112092330-e3eb5a74 (1).ppt
harinipradeep15
 
Python Tutorial Part 2
Haitham El-Ghareeb
 
pythontraining-201jn026043638.pptx
RohitKumar639388
 
Python Functions
Sampad Kar
 
Python 01.pptx
AliMohammadAmiri
 
Python training
Kunalchauhan76
 
_Python_ Functions _and_ Libraries_.pptx
yaramahsoob
 
Using Python Libraries.pdf
SoumyadityaDey
 
Object oriented programming design and implementation
afsheenfaiq2
 
Object oriented programming design and implementation
afsheenfaiq2
 
Modules and Packages in Python Programming Language.pptx
arunavamukherjee9999
 
Python for katana
kedar nath
 
Functions
Gaurav Subham
 
20120314 changa-python-workshop
amptiny
 
Python libraries
Prof. Dr. K. Adisesha
 
Introduction to Python Basics for PSSE Integration
FarhanKhan978284
 
Python Course In Chandigarh
Excellence Academy
 
NMOUP unit 1hrefe jrei g. rwih. werhi wehriw ue
ShashankKumar30032
 
python-fefedfasdgsgfahfdshdhunctions-190506123237.pptx
avishekpradhan24
 
Ad

More from Kamal Acharya (20)

PPTX
Programming the basic computer
Kamal Acharya
 
PPTX
Computer Arithmetic
Kamal Acharya
 
PPTX
Introduction to Computer Security
Kamal Acharya
 
PPTX
Session and Cookies
Kamal Acharya
 
PPTX
Functions in php
Kamal Acharya
 
PPTX
Web forms in php
Kamal Acharya
 
PPTX
Making decision and repeating in PHP
Kamal Acharya
 
PPTX
Working with arrays in php
Kamal Acharya
 
PPTX
Text and Numbers (Data Types)in PHP
Kamal Acharya
 
PPTX
Introduction to PHP
Kamal Acharya
 
PPTX
Capacity Planning of Data Warehousing
Kamal Acharya
 
PPTX
Data Warehousing
Kamal Acharya
 
PPTX
Search Engines
Kamal Acharya
 
PPTX
Web Mining
Kamal Acharya
 
PPTX
Information Privacy and Data Mining
Kamal Acharya
 
PPTX
Cluster Analysis
Kamal Acharya
 
PPTX
Association Analysis in Data Mining
Kamal Acharya
 
PPTX
Classification techniques in data mining
Kamal Acharya
 
PPTX
Data Preprocessing
Kamal Acharya
 
PPTX
Introduction to Data Mining and Data Warehousing
Kamal Acharya
 
Programming the basic computer
Kamal Acharya
 
Computer Arithmetic
Kamal Acharya
 
Introduction to Computer Security
Kamal Acharya
 
Session and Cookies
Kamal Acharya
 
Functions in php
Kamal Acharya
 
Web forms in php
Kamal Acharya
 
Making decision and repeating in PHP
Kamal Acharya
 
Working with arrays in php
Kamal Acharya
 
Text and Numbers (Data Types)in PHP
Kamal Acharya
 
Introduction to PHP
Kamal Acharya
 
Capacity Planning of Data Warehousing
Kamal Acharya
 
Data Warehousing
Kamal Acharya
 
Search Engines
Kamal Acharya
 
Web Mining
Kamal Acharya
 
Information Privacy and Data Mining
Kamal Acharya
 
Cluster Analysis
Kamal Acharya
 
Association Analysis in Data Mining
Kamal Acharya
 
Classification techniques in data mining
Kamal Acharya
 
Data Preprocessing
Kamal Acharya
 
Introduction to Data Mining and Data Warehousing
Kamal Acharya
 
Ad

Recently uploaded (20)

PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PPTX
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
PDF
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
PPTX
infertility, types,causes, impact, and management
Ritu480198
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPTX
Introduction to Biochemistry & Cellular Foundations.pptx
marvinnbustamante1
 
PPTX
Difference between write and update in odoo 18
Celine George
 
PDF
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PDF
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
PDF
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
PDF
Horarios de distribución de agua en julio
pegazohn1978
 
PPTX
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
PPTX
DAY 1_QUARTER1 ENGLISH 5 WEEK- PRESENTATION.pptx
BanyMacalintal
 
PPTX
Introduction to Indian Writing in English
Trushali Dodiya
 
PPTX
DIGITAL CITIZENSHIP TOPIC TLE 8 MATATAG CURRICULUM
ROBERTAUGUSTINEFRANC
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
infertility, types,causes, impact, and management
Ritu480198
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
Introduction to Biochemistry & Cellular Foundations.pptx
marvinnbustamante1
 
Difference between write and update in odoo 18
Celine George
 
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
Horarios de distribución de agua en julio
pegazohn1978
 
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
DAY 1_QUARTER1 ENGLISH 5 WEEK- PRESENTATION.pptx
BanyMacalintal
 
Introduction to Indian Writing in English
Trushali Dodiya
 
DIGITAL CITIZENSHIP TOPIC TLE 8 MATATAG CURRICULUM
ROBERTAUGUSTINEFRANC
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 

Functions in Python

  • 2. 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
  • 4. Example of a function
  • 7. How Function works in Python?
  • 8. 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.
  • 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
  • 15. Python Recursion • What is recursion in Python? • Python Recursive Function • Advantages of Recursion • Disadvantages of Recursion
  • 16. 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
  • 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 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.
  • 23. How to use lambda Functions in Python? • Syntax of Lambda Function in python
  • 24. Example of Lambda Function in python
  • 25. Use of Lambda Function in python
  • 27. Python Global, Local and Nonlocal variables • Global Variables in Python • Local Variables in Python • Global and Local Variables Together • Nonlocal Variables in Python
  • 31. Global and Local Variables Together
  • 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. Global Variables Across Python Modules
  • 41. Global in Nested Functions
  • 42. 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
  • 43. 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
  • 44. How to import modules in Python? • Python import statement • Import with renaming • Python from...import statement • Import all names
  • 49. 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.
  • 52. The dir() built-in function • We can use the dir() function to find out names that are defined inside a module.
  • 54. Python Package • What are 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 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
  • 58. 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)
  • 59. • 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)
  • 60. • 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)