www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior FUNCTIONS IN PYTHON By : Mrs Sangeeta M Chauhan , Gwalior 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior By: Sangeeta M Chauhan
www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior FUNCTIONS IN PYTHON Def Function is a piece of code written to carry out a specified task. You can Pass Data(input) known as parameter to a function A Function may or may not return any value(Output) 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
There are three types of functions in Python Built-in functions The Python interpreter has a number of functions built into it that are always available. They are listed here in alphabetical order. User-Defined Functions (UDFs): The Functions defined by User is known as User Defined Functions. These are defined with the keyword def Anonymous functions, which are also called lambda functions because they are not declared with the standard def keyword 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior Functions vs Methods : A method refers to a function which is part of a class. You access it with an instance or object of the class. A function doesn’t have this restriction: it just refers to a standalone function. This means that all methods are functions, but not all functions are methods. 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
Structure of Python Function def functionname ( parameters ): "function_docstring“ function_body return [expression] 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior DOC STRING Python Docstring is the documentation string which is string literal, and it occurs in the class, module, function or method definition, and it is written as a first statement. Docstrings are accessible from the doc attribute for any of the Python object and also with the built-in help() function can come in handy. 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
After execution of the program/function >>> Info.__doc__ 'This Function is to display info about author.' 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
Without execution of Program/function >>> use_doc.Info.__doc__ 'This Function is to display info about author.' >>> 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
Example of Function Definition Example for Creating a Function without parameter In Python a function is defined using the def keyword: >>> def MyMsg1(): print("Learning to create function") 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
Example of Function Definition Example for Creating a Function with parameter >>> def MyMsg2( name ): "This prints a passed string into this function" print (name ,” is learning to define Python Function”) return 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
Calling function MyMsg1 () To call a function, use the function name followed by parenthesis: >>> MyMsg1() Output : >>> Learning to create function Calling Function MyMsg2() twice with different parameter >>> MyMsg2(‘Divyaditya’) >>> MyMsg2(‘Manasvi’) Output Divyaditya is learning to define Python Function Manasvi is learning to define Python Function Parameters 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior By: Sangeeta M Chauhan
www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
?????Arguments Vs Parameters 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior Function Arguments 1. Default 2. Positional 3. Keyword 4. Arbitrary(Variable no.) 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
Variable no. of arguments 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
Global and Local Variables in Python a=50 # Global Variable def MyFunc(): b=100 # Local Variable print("Function Called :",a) print("Function Called :",b) >>>MyFunc() # Function Call Output >>> Function Called 50 >>> Function Called 100 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
Local and Global Variable…….. 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior The globals() method returns the dictionary of the current global symbol table. A symbol table is a data structure maintained by a compiler which contains all necessary information about the program 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior MOST USEFUL BUILT-IN FUNCTIONS 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
Syntax :Filter(function, sequence) Returns an Object containing sequence of the items from the sequence for which function(item) is true Syntax :Filter(function, sequence) 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior By: Sangeeta M Chauhan
www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior Example Computes primes up to 20 output 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
Ex: map (factorial, list) Calls function(item) for each of the sequence’s items Syntax: map(function, sequence) Ex: map (factorial, list) 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
Example of Map function Computes the factorial of given values output 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
(Anonymous Functions) Python lambda (Anonymous Functions) A lambda function is a small anonymous function. A lambda function can take any number of arguments, but can only have one expression. Syntax lambda <arguments> : <expression> 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
Example 1 of lambda function output 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
Example 2 : lambda Function output 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
Example 3: lambda Function output 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
Calling of functions with ternary operator. OUTPUT 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
# Function argument unpacking output 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior Output 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior Thanks 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior