0% found this document useful (0 votes)
18 views26 pages

Python: Functions - Print

python programming related lecture

Uploaded by

basitalikhan2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views26 pages

Python: Functions - Print

python programming related lecture

Uploaded by

basitalikhan2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Faisal Rehman [FR]

1
2
Summary
• Definition • Special Parameters
• Local, Non-Local, and Global • Arbitrary Arguments by Lists
• Pass by Value • Unpacking Argument Lists
• None Return • Lambda Expressions
• Callable • Documentation Strings
• Fruitful and Unfruitful • Function Annotations
• Default • Recursive Functions
• Keyword
Definition 3

• Functions are mini programs, that may take input as


arguments, process it and may return the result.

• In Python, the keyword def introduces a function definition.

• It must be followed by the function name and the


parenthesized list of formal parameters.

• The statements that form the body of the function start at


the next line and must be indented.
4
Definition
• The first statement of the function body can optionally be a
string literal.

• This string literal is the function’s documentation string,


or docstring.
5
Fibonacci Function

Output: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597


6
Local, Non-Local and Global Variables
• The execution of a function introduces a new symbol table used for
the local variables of the function.
• More precisely, all variable assignments in a function store the value
in the local symbol table.
• Global variables and variables of enclosing functions cannot be
directly assigned a value within a function.
• Unless, for global variables, named in a global statement, or, for
variables of enclosing functions, named in a nonlocal statement.
7
Pass by Value
• The actual parameters (arguments) to a function call are introduced
in the local symbol table of the called function when it is called; thus,
arguments are passed using call by value
• Pass by value is a method of passing arguments to a function where
the value of the argument is copied into the formal parameter of the
function.
• This means that any changes made to the parameter inside the
function have no effect on the argument.
8
None Return
• In fact, even functions without a return statement do return a value,
albeit a rather boring one. This value is called None

Output: None
9
Callable
• Function is also callable.
• In Python, a callable is an object that can be called.
• You can use built-in function callable that checks and returns True if
the object passed appears to be callable, otherwise False.
callable(fib)
Output: True
• Every callable object has the __call__() method implemented inside
its class definition.
• For example, functions, methods, and classes are callable in Python3.
10
Fruitful and Unfruitful
• The return statement returns with a value from a function.

• return without an expression argument returns None.

• Falling off the end of a function also returns None.


11
Default Argument
• The most useful form is to specify a default value for one or more
arguments.

• This creates a function that can be called with fewer arguments than
it is defined to allow. For example:
12
Default Argument
• This function can be called in several ways:
print(math_function(5))
Output: 30

print(math_function(5, 3))
Output: 45

print(math_function(5, 3, 2))
Output: 30
13
Keyword Arguments
• Functions can also be called using keyword arguments of the form
kwarg=value.
• Accepts one required argument (x) and two optional arguments (y, z).
• This function can be called in any of the following ways:
14
Special Parameters
• As guidance:

•Use positional-only if you want the name of the


parameters to not be available to the user.

•This is useful when parameter names have no real


meaning.

•If you want to enforce the order of the arguments when


the function is called.
15
Special Parameters

•If you need to take some positional parameters and


arbitrary keywords

•Use keyword-only when names have meaning.

•The function definition is more understandable by being


explicit with names.
16
Special Parameters

•You want to prevent users relying on the position of the


argument being passed.

•For an API, use positional-only to prevent breaking API


changes if the parameter’s name is modified in the future.
17
Arbitrary Argument Lists
• These arguments will be wrapped up in
a tuple (see Tuples and Sequences).

• Before the variable number of


arguments, zero or more normal
arguments may occur.

• Normally, these variadic arguments will


be last in the list of formal parameters.
18
Arbitrary Argument Lists
• Because they scoop up all remaining input arguments that are passed to
the function.

• Any formal parameters which occur after the *args parameter are
‘keyword-only’ arguments.

• You can have parameters as:


1. *args
2. required_arg, *args, **kwargs
3. *args, **kwargs
4. **kwargs
19
Unpacking Argument Lists
• The reverse situation occurs when the
arguments are already in a list or tuple but
need to be unpacked for a function call
requiring separate positional arguments.

• For instance, the built-in range() function


expects separate start and stop arguments.

• If they are not available separately, write the


function call with the *-operator to unpack the
arguments out of a list or tuple.

• In the same fashion, dictionaries can deliver


keyword arguments with the **-operator:
20
Lambda Expressions
• Small anonymous functions can be created
with the lambda keyword.
• This function returns the sum of its two
arguments: lambda a, b: a+b
• Lambda functions can be used wherever
function objects are required.
• They are syntactically restricted to a single
expression.
• Semantically, they are just syntactic sugar
for a normal function definition.
• Like nested function definitions, lambda
functions can reference variables from the
containing scope.
21
Documentation Strings
• Here are some conventions about
the content and formatting of
documentation strings.
• The first line should always be a
short, concise summary of the
object’s purpose.
• For brevity, it should not explicitly
state the object’s name or type.
• This line should begin with a capital
letter and end with a period.
22
Documentation Strings
• If there are more lines in the
documentation string, the second
line should be blank.
• To visually separating the
summary from the rest of the
description.
• The following lines should be one
or more paragraphs describing the
object’s calling conventions, its
side effects, etc.
23
Function Annotations
• Function annotations are completely optional metadata information
about the types used by user-defined functions.
• See PEP 3107 and PEP 484 for more information.
• Annotations are stored in the __annotations__ attribute of the
function as a dictionary and have no effect on any other part of the
function.
24
Recursive Function
• Self calling function is
called recursive function.

• In recursive function, stacks


of unknowns are added.

• Once the function return


value, the unknowns are
solved from top to bottom
in stacks.
25
Call Stack

1 5

2*factorial(1) 4
Return
and 3*factorial(2) 3
Recursive
Pop
Call and
4*factorial(3) 2 Push

5*factorial(4) 1

print(factorial(5))
Start Here

Function Stack
26

You might also like