Mapa mental
PYTHON Unit 4 - Functions and Modules
Conteúdo do mapa
PYTHON Unit 4 - Functions and Modules
SYNTAX
- def FUNCTION_NAME(parameters): statement(s)
- DEFAULT VALUE PARAMETERS & KEYWORD ARGUMENTS
- Default parameters
- def volume( l, b=5, h=10 ):
- b=5 and h=10 are default parameters meaning that if only one parameter is given in the function call eg: volume(2) the default values will be used for b and h while l will have the value of 2
- NOTE: volume(2, 4) -> l will be 2, b will be 4 and h will be its default parameter of 10
- Keyword Argument
- A value passed to a parameter by referring to its name is known as a keyword argument eg: volume(3, h=6). h=6 is a keyword argument
- Default parameters
LOCAL & GLOBAL VARIABLES
- GLOBAL
- Global variables are not bound to any particular function and can be accessed within the body of the function, outside the body of the function, or by any other function. The changes made to a global variable by any function will be visible by other functions.
- global indentifiers
- If a function needs global variables the first statement must be the global indentifiers
- def compute(): global x, y
- If a function needs global variables the first statement must be the global indentifiers
- LOCAL
- By default, any variable that is bound within a function body is a local variable. Each function has its own copy of a local variable, and its value is not accessible or modifiable outside the body of the function.
- def compute( x ): x += 5
- print ( x )
- x = 10
- compute( x )
- print( x )
- the output will be 15 then 10
- the value of x within the compute function at the end it 15 but outside the function x remains unchanged
- the output will be 15 then 10
- print( x )
- compute( x )
- x = 10
- print ( x )
- GLOBAL
LAMBDA FUNCTIONS
- For functions that are small (a single line expression) that are only going to be used once.
- SYNTAX
- g = lambda x : x * 2
- is the same as
- def f( x ): return x * 2
- NOTE: You can use a lambda function without even assigning it to a variable
- ( lambda x : x * 2 )( 3 ) will return 6
- is the same as
- g = lambda x : x * 2
APPLYING FUNCTIONS TO SEQUENCES
- NOTE: 3 types of sequences >> strings, lists, tuples
- filter( )
- SYNTAX: filter( function, sequence )
- EXAMPLE: evens = filter( evenval, range( 1, 11 ))
- range( 1, 11 ) generates integers from 1 to 10, each of which are passed into the evenval function, and if the evenval function returns true, the number is stored in evens
- map( )
- SYNTAX: map( function, sequence )
- The map method calls the included function for each of the elements in the sequence and returns a list of the returned values
- EXAMPLE: sqr = map( square, range( 1, 11 ))
- Where square is a function that calculates square of value passed in. range generates values from 1 to 10, each of which is is called with the square function, and results are stored in sqr
- reduce( )
- SYNTAX: reduce( function, sequence )
- the reduce( ) method returns a single value that is produced by calling the included function on the first 2 elements of the sequence. The function is then called on the result of the previous function call and the next element in the sequence and so on.
- EXAMPLE: r = functools . reduce( add, range( 1, 11 ))
- NOTE: To use reduce( ) when must import functools as reduce( ) is defined in that module.
- import functools
- Where add is a function that adds two numbers. So reduce calls the add function with the first 2 elements of the range( ) sequence and returns the sum. It then calls the add function again with the result of the previous function call with the next element of the sequence.
- NOTE: To use reduce( ) when must import functools as reduce( ) is defined in that module.
FUNCTION ATTRIBUTES
- Used to display specific information about a function
- called by giving the functionname . ATTRIBUTE
- EXAMPLE: sum.__doc__
- ATTRIBUTES
- __doc__
- __name__
- __module__
- __defaults__
- __code__
- __dict__
- represents the dictionary that defines the local namespace fro the attributes of the function
- represents the actual code object, the statements in the body of the function
- __dict__
- represents tuple with default values to be assigned to the default arguments of the function
- __code__
- represents name of module in which function is defined
- __defaults__
- represents functions name
- __module__
- represents the docstring from the first line of the function body
- __name__
- __doc__
RECURSION
- When a function calls itself. Creates infinite loop, so exit condition must be included in the function itself.
- EXAMPLE
- def addseq( x ):
- if x == 1: return 1
- esle: return x + addseq( x - 1 )
- print( addseq( 10 ))
- 10 + addseq( 9 )
- 9 + addseq( 8 )
- 8 + addseq( 7 )
- .......
- 1
- Output: 55
- 1
- .......
- 8 + addseq( 7 )
- 9 + addseq( 8 )
- 10 + addseq( 9 )
- print( addseq( 10 ))
- exit condtion
- esle: return x + addseq( x - 1 )
- if x == 1: return 1
- def addseq( x ):
ITERATORS
- Used for looping through collections of data. Every time you use a for loop in list, iterators are invoked.
- SYNTAX
- iter(object)
- EXAMPLE
- names=['John', 'Kelly', etc ]
- i = iter(names)
- print( i . __next__( ))
- print( i . __next__( ))
- etc.
- iterate over object using the next( ) method
- print( i . __next__( ))
- print( i . __next__( ))
- i = iter(names)
- names=['John', 'Kelly', etc ]
GENERATORS
- A GENERATOR is a function that creates an iterator. For a function to become a generator, it must return a value using the YIELD keyword.
- EXAMPLE
- def fruits( seq ):
- for fruit in seq:
- yield '%s' % fruit
- f = fruits( [ 'Apple', 'Orange', 'Mango', etc ] )
- print(f . __next__( ) )
- print(f . __next__( ) )
- ect.
- print(f . __next__( ) )
- function call assigned to variable f
- print(f . __next__( ) )
- f = fruits( [ 'Apple', 'Orange', 'Mango', etc ] )
- yield '%s' % fruit
- for fruit in seq:
- def fruits( seq ):
GENERATOR EXPRESSION
- A GENERATOR EXPRESSION is an expression in brackets that creates an iterator object.
- EXAMPLE
- def squarenum( x ):
- return x * x
- iteratorobj = ( squarenum( x ) for x in range ( 6 ) )
- print( iteratorobj . __next__( ) )
- print( iteratorobj . __next__( ) )
- etc.
- print( iteratorobj . __next__( ) )
- print( iteratorobj . __next__( ) )
- iteratorobj = ( squarenum( x ) for x in range ( 6 ) )
- return x * x
- def squarenum( x ):
MODULES
- A module is a file consisting of a few functions and variables used for a particular task. A module can be reused in any other program that imports it. The functions and variables of the module become available in the current program and can be used in it.
- Ways of importing modules
- import calendar
- calendar . prcal( )
- refers to function
- calendar . prcal( )
- calendar is a module that has function prcal( )
- from calendar import prcal
- prcal( )
- we can directly refer to the function because we specifically imported it
- prcal( )
- from calendar import *
- prcal( )
- imports all objects from calendar module,we can access any function of the module directly
- prcal( )
- import calendar as cal
- cal . prcal( )
- imports calendar module and makes it accessible through the term cal
- cal . prcal( )
- import calendar
- dir( ) function
- lists identifiers defined by the module. identifiers are the functions, variables, and classes defined in the module
- print( dir( calendar ) )
- lists identifiers defined by the module. identifiers are the functions, variables, and classes defined in the module
COMMAND-LINE ARGUMENTS
- Are used to pass arguments to a program while running it. Each command-line argument that you pass to the program will be stored in the sys . argv variable. This is a list that always has a length of at least 1. The item at index 0 is the name of the python program you are running.
Informações
- Descrição
- INF1511 Python Mapa Mental sobre PYTHON Unit 4 - Functions and Modules, criado por Chris Hawkridge em 04-05-2017.
- Criado por
- Chris Hawkridge
- Visualizações
- 2
- Avaliação