Functions And Modules Flashcards
Function arguments?
Required Keyword Default Variable-Length (Positional Argument assigned based on its position and keyword: based on parameter name)
Keyword arguments
PRO:
1. change the order of parameters when calling than while defining.
The argument should not receive value more than once and no argument should be left behind.
Default arguments
Assumes a default value if no value assigned when calling. c is an example. Default arguments should not be followed by non-default while declaring the function. Syntax def func(a,b,c= 'value'):
Variable-length Arguments
Syntax: def func(a,*b):
- The arbitrary no. of arguments passed, form a tuple.
- Should be last in the list of a formal parameter only keyword arguments can be after it.
Lambda Functions
- can take any no. of args.
- no explicit return statement but always returns an expression.
- can be passed as arg to a func.
(also look at example 5.28, 204)
Nested lambda functions
name = lambda args: exp name2 = lambda x,y,z: x*(name(y,z))
print ( ( lambda x,y,z : x*(lambda x,y: x+y)(y,z) ) (1,2,3))
The occurrence of recursion and runtime error of recursion depth may occur.
The number of expressions in lambda functions.
1
Use of lambda function?
usually when created only, hence called thrown away.
Syntax of lambda
lambda arguments: expression
passing args to lambda
(lambda x:x2) (9) # 9 being the arg passed.
OR
twice = lambda x:x2
print ( twice(9) )
Documentation Strings
Explain code, same as a comment but are retained throughout the runtime of the program. So users can inspect during program execution.
Docstring declaration
def func(): "Func \_\_docdtring\_\_"
- triple quotes to extend it to multiline.
- in case of multiline second line should be blank.
- first non blank line after first line of docstring determines indentation’s amt for rest of docstring.
Docstring good rules
- First line: short n concise highlighting summary n purpose.
- objects name n type info NOT needed.
- begin with capital letter n end with period.
- describe objects calling conventions, its side effects.
how to know what a function does?
functionName.__doc__
Recursive function
A function that calls itself to solve a smaller version of its task until a final call is made which does not require a call to itself.
parts of recursive function:
- A base case: here problem in its simplest to solve without calling itself.
- Recursive Case: Problem here divided into subparts and calls itself with subparts and then the result is obtained by combining the result from simpler subparts.
what error if no base case in recursive function?
infinite stack.
The technique utilisez by recursion?
Divide n conquer.
Base case significance:
It acts as a terminating condition so it doesn’t call itself infinitely.
factorial by recursive:
Base case: if n==0 or n ==1:
return 1
Recursive case: factorial = n * factorial (n-1)
Greatest common denominator by recursive:
return b, if a%b==0
GCD( b , a%b )
Exponent ( x, y ) by recursive:
if n == 0, return 1
else:
x*exp(x,y-1)
The Fibonacci Series:
if n <2: return 1 return fib(n-1)+fib(n-2)
then use for i in range: print fib(i) to print out the series.
Recursion Vs Iteration
approach type:
R is a top-down, original problem divided in smaller problems.
Itr is bottom-up.