FUNCTIONS IN PYTHON By Mrs Sangeeta M Chauhan

FUNCTIONS IN PYTHON By : Mrs Sangeeta M Chauhan , Gwalior 9/19/2021 www. pythonclassroomdiary. wordpress. com © Sangeeta M Chauhan, Gwalior 1

FUNCTIONS IN PYTHON f e D 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) 9/19/2021 www. pythonclassroomdiary. wordpress. com © Sangeeta M Chauhan, Gwalior 2

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 9/19/2021 www. pythonclassroomdiary. wordpress. co m © Sangeeta M Chauhan, Gwalior 3

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. 9/19/2021 www. pythonclassroomdiary. wordpress. co m © Sangeeta M Chauhan, Gwalior 4
![Structure of Python Function def functionname ( parameters ): "function_docstring“ function_body return [expression] 9/19/2021 Structure of Python Function def functionname ( parameters ): "function_docstring“ function_body return [expression] 9/19/2021](http://slidetodoc.com/presentation_image_h2/b5dbcec1dbfba0d2d06bb0777b6a01d8/image-5.jpg)
Structure of Python Function def functionname ( parameters ): "function_docstring“ function_body return [expression] 9/19/2021 www. pythonclassroomdiary. wordpress. co m © Sangeeta M Chauhan, Gwalior 5

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. 9/19/2021 www. pythonclassroomdiary. wordpress. co m © Sangeeta M Chauhan, Gwalior 6

After execution of the program/function >>> Info. __doc__ 'This Function is to display info about author. ' 9/19/2021 www. pythonclassroomdiary. wordpress. co m © Sangeeta M Chauhan, Gwalior 7

Without execution of Program/function >>> use_doc. Info. __doc__ 'This Function is to display info about author. ' >>> 9/19/2021 www. pythonclassroomdiary. wordpress. co m © Sangeeta M Chauhan, Gwalior 8

Example of Function Definition • Example for Creating a Function without parameter • In Python a function is defined using the def keyword: >>> def My. Msg 1(): print("Learning to create function") 9/19/2021 www. pythonclassroomdiary. wordpress. co m © Sangeeta M Chauhan, Gwalior 9

Example of Function Definition • Example for Creating a Function with parameter • >>> def My. Msg 2( name ): "This prints a passed string into this function" print (name , ” is learning to define Python Function”) • return 9/19/2021 www. pythonclassroomdiary. wordpress. co m © Sangeeta M Chauhan, Gwalior 10

Calling function My. Msg 1 () To call a function, use the function name followed by parenthesis: • >>> My. Msg 1() Output : • >>> Learning to create function Calling Function My. Msg 2() twice with different parameter >>> My. Msg 2(‘Divyaditya’) >>> My. Msg 2(‘Manasvi’) Parameters Output Divyaditya is learning to define Python Function Manasvi is learning to define Python Function 9/19/2021 www. pythonclassroomdiary. wordpress. co m © Sangeeta M Chauhan, Gwalior 11

9/19/2021 www. pythonclassroomdiary. wordpress. co m © Sangeeta M Chauhan, Gwalior 12

9/19/2021 www. pythonclassroomdiary. wordpress. co m © Sangeeta M Chauhan, Gwalior 13

9/19/2021 www. pythonclassroomdiary. wordpress. com © Sangeeta M Chauhan, Gwalior 14

9/19/2021 www. pythonclassroomdiary. wordpress. co m © Sangeeta M Chauhan, Gwalior 15

9/19/2021 www. pythonclassroomdiary. wordpress. co m © Sangeeta M Chauhan, Gwalior 16

? ? ? Arguments Vs Parameter 9/19/2021 www. pythonclassroomdiary. wordpress. co m © Sangeeta M Chauhan, Gwalior Argument 17

Function Arguments 1. Default 3. Keyword 9/19/2021 2. Positional 4. Arbitrary(Variable no. ) www. pythonclassroomdiary. wordpress. co m © Sangeeta M Chauhan, Gwalior 18

Variable no. of arguments 9/19/2021 www. pythonclassroomdiary. wordpress. co m © Sangeeta M Chauhan, Gwalior 19

Global and Local Variables in Python a=50 # Global Variable def My. Func(): b=100 # Local Variable print("Function Called : ", a) print("Function Called : ", b) >>>My. Func() Output >>> Function Called 50 >>> Function Called 100 9/19/2021 # Function Call www. pythonclassroomdiary. wordpress. co m © Sangeeta M Chauhan, Gwalior 20

Local and Global Variable……. . 9/19/2021 www. pythonclassroomdiary. wordpress. co m © Sangeeta M Chauhan, Gwalior 21

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 9/19/2021 www. pythonclassroomdiary. wordpress. co m © Sangeeta M Chauhan, Gwalior 22

MOST USEFUL BUILT-IN FUNCTIONS 9/19/2021 www. pythonclassroomdiary. wordpress. co m © Sangeeta M Chauhan, Gwalior 23

Filter Function – Returns an Object containing sequence of the items from the sequence for which function(item) is true Syntax : Filter(function, sequence) 9/19/2021 www. pythonclassroomdiary. wordpress. co m © Sangeeta M Chauhan, Gwalior 24

Example Computes primes up to 20 output 9/19/2021 www. pythonclassroomdiary. wordpress. co m © Sangeeta M Chauhan, Gwalior 25

Map() – Calls function(item) for each of the sequence’s items Syntax: map(function, sequence) Ex: map (factorial, list) 9/19/2021 www. pythonclassroomdiary. wordpress. co m © Sangeeta M Chauhan, Gwalior 26

Example of Map function Computes the factorial of given values output 9/19/2021 www. pythonclassroomdiary. wordpress. co m © Sangeeta M Chauhan, Gwalior 27

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 > 9/19/2021 www. pythonclassroomdiary. wordpress. co m © Sangeeta M Chauhan, Gwalior 28

Example 1 of lambda function output 9/19/2021 www. pythonclassroomdiary. wordpress. co m © Sangeeta M Chauhan, Gwalior 29

Example 2 : lambda Function output 9/19/2021 www. pythonclassroomdiary. wordpress. co m © Sangeeta M Chauhan, Gwalior 30

Example 3: lambda Function output 9/19/2021 www. pythonclassroomdiary. wordpress. co m © Sangeeta M Chauhan, Gwalior 31

Calling of functions with ternary operator. OUTPUT 9/19/2021 www. pythonclassroomdiary. wordpress. co m © Sangeeta M Chauhan, Gwalior 32

# Function argument unpacking output 9/19/2021 www. pythonclassroomdiary. wordpress. co m © Sangeeta M Chauhan, Gwalior 33

Output 9/19/2021 www. pythonclassroomdiary. wordpress. co m © Sangeeta M Chauhan, Gwalior 34

9/19/2021 www. pythonclassroomdiary. wordpress. co m © Sangeeta M Chauhan, Gwalior 35

9/19/2021 www. pythonclassroomdiary. wordpress. co m © Sangeeta M Chauhan, Gwalior 36

9/19/2021 www. pythonclassroomdiary. wordpress. co m © Sangeeta M Chauhan, Gwalior 37

Thanks 9/19/2021 www. pythonclassroomdiary. wordpress. co m © Sangeeta M Chauhan, Gwalior 38
- Slides: 38