Chapter 9: Functions Flashcards
Define a function that does nothing
Def do_nothing():
Pass
Define a function that has no parameters but returns a value
Def agree():
Return True
You can also pass ____ into function
Parameter
____ is a specifically Python value that holds a place when there is nothing to say. It is not the same as Boolean value False.
None
Example using None to distinguish from Boolean False value
Thing = None
If thing is None
Print(‘It is nothing’)
You can specify _____ values for parameters. The default is used in the caller does not provide a corresponding argument.
Default
Ex:
Def menu(wine, entree, dessert = ‘pudding’)
When an asterisk (*) is used in a function with a parameter, an asterisk groups a variable number of positional arguments into a single tuple of parameter values.
Def print)args(*args)
Print(‘Positional tuple’, args)
Print_args(3,2,1,’wait’,’uh…’)
Positional tuple: (3,2,1,’wait’,’uh..’)
You can use two asterisks (**) to group keyword arguments into a _____ where the arguments are the keys, and their values are the corresponding values
Dictionary
If an argument is mutable, its value can be changed from ____ the function
Inside
Example of something you can do with python
Def run_something_with_args(func, ar1, arg2)
Func(arg1,arg2)
> > > run_something_wiith_args(add_args,5,9)
14
Another example
Def sum_args(*args)
Return sum(args)
//sum is a built in python function that calculate the sum of the value in its iterable numeric argument
In python you can define function inside of function
True
Using lambda
Def edit_story(words, func)
For word in words:
Printf(func(word))
Edit_story(stairts, lambda word: word.capitalize() + ‘!’)
A _______ is a Python sequence creation object
Generator
> > > sum(range(1,101))
5050
Generator comprehensions
Get obj = (pair for pair in zip([‘a’,’b’], [‘1’,’2’]))