SlideShare uma empresa Scribd logo
1 de 61
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 !

Mais conteúdo relacionado

Mais procurados (20)

Python programming : Strings
Python programming : StringsPython programming : Strings
Python programming : Strings
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
 
Python functions
Python functionsPython functions
Python functions
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
Regular expressions in Python
Regular expressions in PythonRegular expressions in Python
Regular expressions in Python
 
Python Data-Types
Python Data-TypesPython Data-Types
Python Data-Types
 
Python : Data Types
Python : Data TypesPython : Data Types
Python : Data Types
 
Chapter 07 inheritance
Chapter 07 inheritanceChapter 07 inheritance
Chapter 07 inheritance
 
Operators in python
Operators in pythonOperators in python
Operators in python
 
Pointer in c
Pointer in cPointer in c
Pointer in c
 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
 
Python
PythonPython
Python
 
Datastructures in python
Datastructures in pythonDatastructures in python
Datastructures in python
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
Python: Modules and Packages
Python: Modules and PackagesPython: Modules and Packages
Python: Modules and Packages
 
Python variables and data types.pptx
Python variables and data types.pptxPython variables and data types.pptx
Python variables and data types.pptx
 
Python ppt
Python pptPython ppt
Python ppt
 
Python Flow Control
Python Flow ControlPython Flow Control
Python Flow Control
 
File Handling Python
File Handling PythonFile Handling Python
File Handling Python
 
Functions in python
Functions in pythonFunctions in python
Functions in python
 

Semelhante a Functions in Python

Chapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.pptChapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.pptAmanuelZewdie4
 
pythontraining-201jn026043638.pptx
pythontraining-201jn026043638.pptxpythontraining-201jn026043638.pptx
pythontraining-201jn026043638.pptxRohitKumar639388
 
Using Python Libraries.pdf
Using Python Libraries.pdfUsing Python Libraries.pdf
Using Python Libraries.pdfSoumyadityaDey
 
Modules and Packages in Python Programming Language.pptx
Modules and Packages in Python Programming Language.pptxModules and Packages in Python Programming Language.pptx
Modules and Packages in Python Programming Language.pptxarunavamukherjee9999
 
Python for katana
Python for katanaPython for katana
Python for katanakedar nath
 
20120314 changa-python-workshop
20120314 changa-python-workshop20120314 changa-python-workshop
20120314 changa-python-workshopamptiny
 
Pa1 session 4_slides
Pa1 session 4_slidesPa1 session 4_slides
Pa1 session 4_slidesaiclub_slides
 
What is Python?
What is Python?What is Python?
What is Python?PranavSB
 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptxmiki304759
 
Programming with Python: Week 1
Programming with Python: Week 1Programming with Python: Week 1
Programming with Python: Week 1Ahmet Bulut
 
Python presentation of Government Engineering College Aurangabad, Bihar
Python presentation of Government Engineering College Aurangabad, BiharPython presentation of Government Engineering College Aurangabad, Bihar
Python presentation of Government Engineering College Aurangabad, BiharUttamKumar617567
 
Python's dynamic nature (rough slides, November 2004)
Python's dynamic nature (rough slides, November 2004)Python's dynamic nature (rough slides, November 2004)
Python's dynamic nature (rough slides, November 2004)Kiran Jonnalagadda
 
python interview prep question , 52 questions
python interview prep question , 52 questionspython interview prep question , 52 questions
python interview prep question , 52 questionsgokul174578
 

Semelhante a Functions in Python (20)

Chapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.pptChapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.ppt
 
Python Tutorial Part 2
Python Tutorial Part 2Python Tutorial Part 2
Python Tutorial Part 2
 
pythontraining-201jn026043638.pptx
pythontraining-201jn026043638.pptxpythontraining-201jn026043638.pptx
pythontraining-201jn026043638.pptx
 
Python Functions
Python FunctionsPython Functions
Python Functions
 
Python 01.pptx
Python 01.pptxPython 01.pptx
Python 01.pptx
 
Python training
Python trainingPython training
Python training
 
Using Python Libraries.pdf
Using Python Libraries.pdfUsing Python Libraries.pdf
Using Python Libraries.pdf
 
Modules and Packages in Python Programming Language.pptx
Modules and Packages in Python Programming Language.pptxModules and Packages in Python Programming Language.pptx
Modules and Packages in Python Programming Language.pptx
 
Python for katana
Python for katanaPython for katana
Python for katana
 
Functions
FunctionsFunctions
Functions
 
20120314 changa-python-workshop
20120314 changa-python-workshop20120314 changa-python-workshop
20120314 changa-python-workshop
 
Python libraries
Python librariesPython libraries
Python libraries
 
Python Course In Chandigarh
Python Course In ChandigarhPython Course In Chandigarh
Python Course In Chandigarh
 
Pa1 session 4_slides
Pa1 session 4_slidesPa1 session 4_slides
Pa1 session 4_slides
 
What is Python?
What is Python?What is Python?
What is Python?
 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptx
 
Programming with Python: Week 1
Programming with Python: Week 1Programming with Python: Week 1
Programming with Python: Week 1
 
Python presentation of Government Engineering College Aurangabad, Bihar
Python presentation of Government Engineering College Aurangabad, BiharPython presentation of Government Engineering College Aurangabad, Bihar
Python presentation of Government Engineering College Aurangabad, Bihar
 
Python's dynamic nature (rough slides, November 2004)
Python's dynamic nature (rough slides, November 2004)Python's dynamic nature (rough slides, November 2004)
Python's dynamic nature (rough slides, November 2004)
 
python interview prep question , 52 questions
python interview prep question , 52 questionspython interview prep question , 52 questions
python interview prep question , 52 questions
 

Mais de Kamal Acharya

Programming the basic computer
Programming the basic computerProgramming the basic computer
Programming the basic computerKamal Acharya
 
Introduction to Computer Security
Introduction to Computer SecurityIntroduction to Computer Security
Introduction to Computer SecurityKamal Acharya
 
Making decision and repeating in PHP
Making decision and repeating  in PHPMaking decision and repeating  in PHP
Making decision and repeating in PHPKamal Acharya
 
Working with arrays in php
Working with arrays in phpWorking with arrays in php
Working with arrays in phpKamal Acharya
 
Text and Numbers (Data Types)in PHP
Text and Numbers (Data Types)in PHPText and Numbers (Data Types)in PHP
Text and Numbers (Data Types)in PHPKamal Acharya
 
Capacity Planning of Data Warehousing
Capacity Planning of Data WarehousingCapacity Planning of Data Warehousing
Capacity Planning of Data WarehousingKamal Acharya
 
Information Privacy and Data Mining
Information Privacy and Data MiningInformation Privacy and Data Mining
Information Privacy and Data MiningKamal Acharya
 
Association Analysis in Data Mining
Association Analysis in Data MiningAssociation Analysis in Data Mining
Association Analysis in Data MiningKamal Acharya
 
Classification techniques in data mining
Classification techniques in data miningClassification techniques in data mining
Classification techniques in data miningKamal Acharya
 
Introduction to Data Mining and Data Warehousing
Introduction to Data Mining and Data WarehousingIntroduction to Data Mining and Data Warehousing
Introduction to Data Mining and Data WarehousingKamal Acharya
 

Mais de Kamal Acharya (20)

Programming the basic computer
Programming the basic computerProgramming the basic computer
Programming the basic computer
 
Computer Arithmetic
Computer ArithmeticComputer Arithmetic
Computer Arithmetic
 
Introduction to Computer Security
Introduction to Computer SecurityIntroduction to Computer Security
Introduction to Computer Security
 
Session and Cookies
Session and CookiesSession and Cookies
Session and Cookies
 
Functions in php
Functions in phpFunctions in php
Functions in php
 
Web forms in php
Web forms in phpWeb forms in php
Web forms in php
 
Making decision and repeating in PHP
Making decision and repeating  in PHPMaking decision and repeating  in PHP
Making decision and repeating in PHP
 
Working with arrays in php
Working with arrays in phpWorking with arrays in php
Working with arrays in php
 
Text and Numbers (Data Types)in PHP
Text and Numbers (Data Types)in PHPText and Numbers (Data Types)in PHP
Text and Numbers (Data Types)in PHP
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
Capacity Planning of Data Warehousing
Capacity Planning of Data WarehousingCapacity Planning of Data Warehousing
Capacity Planning of Data Warehousing
 
Data Warehousing
Data WarehousingData Warehousing
Data Warehousing
 
Search Engines
Search EnginesSearch Engines
Search Engines
 
Web Mining
Web MiningWeb Mining
Web Mining
 
Information Privacy and Data Mining
Information Privacy and Data MiningInformation Privacy and Data Mining
Information Privacy and Data Mining
 
Cluster Analysis
Cluster AnalysisCluster Analysis
Cluster Analysis
 
Association Analysis in Data Mining
Association Analysis in Data MiningAssociation Analysis in Data Mining
Association Analysis in Data Mining
 
Classification techniques in data mining
Classification techniques in data miningClassification techniques in data mining
Classification techniques in data mining
 
Data Preprocessing
Data PreprocessingData Preprocessing
Data Preprocessing
 
Introduction to Data Mining and Data Warehousing
Introduction to Data Mining and Data WarehousingIntroduction to Data Mining and Data Warehousing
Introduction to Data Mining and Data Warehousing
 

Último

Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...JojoEDelaCruz
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYKayeClaireEstoconing
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 

Último (20)

Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 

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
  • 18.
  • 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
  • 26.
  • 27. Python Global, Local and Nonlocal variables • Global Variables in Python • Local Variables in Python • Global and Local Variables Together • Nonlocal Variables in Python
  • 29.
  • 31. Global and Local Variables Together
  • 32.
  • 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
  • 36.
  • 37.
  • 38.
  • 39. Global Variables Across Python Modules
  • 40.
  • 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.
  • 50.
  • 52. The dir() built-in function • We can use the dir() function to find out names that are defined inside a module.
  • 53.
  • 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
  • 57.
  • 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)