Functions Flashcards
(20 cards)
To define a Python function, you type ____, the function name, parentheses enclosing any input parameters to the function, and then finally, a _____
def, colon
Creating a function that returns a value
> > > def agree():
… return True
…
Example of passing in parameter
> > > def echo(anything):
… return anything + ‘ ‘ + anything
______ is a special Python value that holds a place when there is nothing to say. It is not the same as the boolean value False, although it looks false when evaluated as a boolean.
None
»> thing = None
»> if thing:
… print(“It’s some thing”)
… else:
… print(“It’s no thing”)
To distinguish None from a boolean False value, use Python’s ____ operator
is
»> thing = None
»> if thing is None:
… print(“It’s nothing”)
… else:
… print(“It’s something”)
…
It’s nothing
To avoid positional argument confusion, you can specify arguments by the names of their corresponding parameters, even in a different order from their definition in the function:
> > > menu(entree=’beef’, dessert=’bagel’, wine=’bordeaux’)
{‘wine’: ‘bordeaux’, ‘entree’: ‘beef’, ‘dessert’: ‘bagel’}
You can specify _____ values for parameters. The default is used if the caller does not provide a corresponding argument.
default
»> def menu(wine, entree, dessert=’pudding’):
… return {‘wine’: wine, ‘entree’: entree, ‘dessert’: dessert}
When used inside the function with a parameter, an asterisk groups a variable number of positional arguments into a ____ _____ of parameter values
single tuple
Example of positional arguments with *
> > > 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 argument names are the keys, and their values are the corresponding dictionary values.
dictionary
»> def print_kwargs(**kwargs):
… print(‘Keyword arguments:’, kwargs)
If an argument is _____, its value can be changed from inside the function via its corresponding parameter
mutable
You can use _____ as elements of lists, tuples, sets, and dictionaries. ______ are immutable, so you can also use them as dictionary keys
functions
You can define a function within another function
True
»> def outer(a, b):
… def inner(c, d):
… return c + d
… return inner(a, b)
A Python ______ function is an anonymous function expressed as a single statement. You can use it instead of a normal tiny function.
lambda
Can you pass in functions as parameters in python?
Yes
> > > def enliven(word): # give that prose more punch
… return word.capitalize() + ‘!’
Mixing our ingredients:
»> edit_story(stairs, enliven)
Thud!
Since enlviven function is so short, what can we do instead?
> > > edit_story(stairs, lambda word: word.capitalize() + ‘!’)
Thud!
Meow!
Thud!
Hiss!
List of python tricks
*args and **kwargs
*Inner functions
*Functions as arguments
The main part of a program defines the ____ ______; thus, the variables in that namespace are global variables.
global namespace
To access the global variable rather than the local one within a function, you need to be explicit and use the _____ keyword
global
To write empty functions in Python, we use ____ statement. ____ is a special statement that does nothing. It only works as a dummy statement.
pass