Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Sangeeta M Chauhan, Gwalior

Similar presentations


Presentation on theme: "© Sangeeta M Chauhan, Gwalior"— Presentation transcript:

1 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
FUNCTIONS IN PYTHON By : Mrs Sangeeta M Chauhan , Gwalior 10/23/2019 © Sangeeta M Chauhan, Gwalior By: Sangeeta M Chauhan

2 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 © Sangeeta M Chauhan, Gwalior

3 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 © Sangeeta M Chauhan, Gwalior

4 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 © Sangeeta M Chauhan, Gwalior

5 Structure of Python Function
def functionname ( parameters ): "function_docstring“ function_body return [expression] 10/23/2019 © Sangeeta M Chauhan, Gwalior

6 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 © Sangeeta M Chauhan, Gwalior

7 After execution of the program/function
>>> Info.__doc__ 'This Function is to display info about author.' 10/23/2019 © Sangeeta M Chauhan, Gwalior

8 Without execution of Program/function
>>> use_doc.Info.__doc__ 'This Function is to display info about author.' >>> 10/23/2019 © Sangeeta M Chauhan, Gwalior

9 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 © Sangeeta M Chauhan, Gwalior

10 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 © Sangeeta M Chauhan, Gwalior

11 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 © Sangeeta M Chauhan, Gwalior

12 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
10/23/2019 © Sangeeta M Chauhan, Gwalior

13 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
10/23/2019 © Sangeeta M Chauhan, Gwalior

14 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
10/23/2019 © Sangeeta M Chauhan, Gwalior By: Sangeeta M Chauhan

15 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
10/23/2019 © Sangeeta M Chauhan, Gwalior

16 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
10/23/2019 © Sangeeta M Chauhan, Gwalior

17 ?????Arguments Vs Parameters
10/23/2019 © Sangeeta M Chauhan, Gwalior

18 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
Function Arguments 1. Default 2. Positional 3. Keyword 4. Arbitrary(Variable no.) 10/23/2019 © Sangeeta M Chauhan, Gwalior

19 Variable no. of arguments
10/23/2019 © Sangeeta M Chauhan, Gwalior

20 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 © Sangeeta M Chauhan, Gwalior

21 Local and Global Variable……..
10/23/2019 © Sangeeta M Chauhan, Gwalior

22 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 © Sangeeta M Chauhan, Gwalior

23 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
MOST USEFUL BUILT-IN FUNCTIONS 10/23/2019 © Sangeeta M Chauhan, Gwalior

24 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 © Sangeeta M Chauhan, Gwalior By: Sangeeta M Chauhan

25 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
Example Computes primes up to 20 output 10/23/2019 © Sangeeta M Chauhan, Gwalior

26 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 © Sangeeta M Chauhan, Gwalior

27 Example of Map function
Computes the factorial of given values output 10/23/2019 © Sangeeta M Chauhan, Gwalior

28 (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 © Sangeeta M Chauhan, Gwalior

29 Example 1 of lambda function
output 10/23/2019 © Sangeeta M Chauhan, Gwalior

30 Example 2 : lambda Function
output 10/23/2019 © Sangeeta M Chauhan, Gwalior

31 Example 3: lambda Function
output 10/23/2019 © Sangeeta M Chauhan, Gwalior

32 Calling of functions with ternary operator.
OUTPUT 10/23/2019 © Sangeeta M Chauhan, Gwalior

33 # Function argument unpacking
output 10/23/2019 © Sangeeta M Chauhan, Gwalior

34 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
Output 10/23/2019 © Sangeeta M Chauhan, Gwalior

35 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
10/23/2019 © Sangeeta M Chauhan, Gwalior

36 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
10/23/2019 © Sangeeta M Chauhan, Gwalior

37 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
10/23/2019 © Sangeeta M Chauhan, Gwalior

38 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior
Thanks 10/23/2019 © Sangeeta M Chauhan, Gwalior


Download ppt "© Sangeeta M Chauhan, Gwalior"

Similar presentations


Ads by Google