Functions in Python allow programmers to organize their code into reusable chunks. Functions define a group of related statements that perform a specific task. This makes programs more modular and manageable as they grow in size. Functions avoid repetition and make code reusable. Functions have a local scope, meaning variables defined inside a function are only visible within that function. Variables are destroyed once the function returns. Functions can take arguments and return values. Python supports built-in, user-defined, recursive, anonymous, and nested functions. Modules allow further organization of code into files while packages organize modules into directories.
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
33 views
MMT Function Py
Functions in Python allow programmers to organize their code into reusable chunks. Functions define a group of related statements that perform a specific task. This makes programs more modular and manageable as they grow in size. Functions avoid repetition and make code reusable. Functions have a local scope, meaning variables defined inside a function are only visible within that function. Variables are destroyed once the function returns. Functions can take arguments and return values. Python supports built-in, user-defined, recursive, anonymous, and nested functions. Modules allow further organization of code into files while packages organize modules into directories.
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 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. – G a m e . L e v e l . s t a r t . s e l e c t _ d i ff i c u l t y ( 2 ) • We can also import the module without the package prefix as follows – from Game.Level import s t a r t • 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 . s t a r t import select_difficulty • Now we can directly call this function. – select_difficulty(2) Thank You !