Functions Flashcards

(20 cards)

1
Q

To define a Python function, you type ____, the function name, parentheses enclosing any input parameters to the function, and then finally, a _____

A

def, colon

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Creating a function that returns a value

A

> > > def agree():
… return True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Example of passing in parameter

A

> > > def echo(anything):
… return anything + ‘ ‘ + anything

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

______ 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.

A

None
»> thing = None
»> if thing:
… print(“It’s some thing”)
… else:
… print(“It’s no thing”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

To distinguish None from a boolean False value, use Python’s ____ operator

A

is
»> thing = None
»> if thing is None:
… print(“It’s nothing”)
… else:
… print(“It’s something”)

It’s nothing

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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:

A

> > > menu(entree=’beef’, dessert=’bagel’, wine=’bordeaux’)
{‘wine’: ‘bordeaux’, ‘entree’: ‘beef’, ‘dessert’: ‘bagel’}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

You can specify _____ values for parameters. The default is used if the caller does not provide a corresponding argument.

A

default
»> def menu(wine, entree, dessert=’pudding’):
… return {‘wine’: wine, ‘entree’: entree, ‘dessert’: dessert}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

When used inside the function with a parameter, an asterisk groups a variable number of positional arguments into a ____ _____ of parameter values

A

single tuple

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Example of positional arguments with *

A

> > > def print_args(*args):
… print(‘Positional tuple:’, args)

print_args(3, 2, 1, ‘wait!’, ‘uh…’)
Positional tuple: (3, 2, 1, ‘wait!’, ‘uh…’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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.

A

dictionary
»> def print_kwargs(**kwargs):
… print(‘Keyword arguments:’, kwargs)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

If an argument is _____, its value can be changed from inside the function via its corresponding parameter

A

mutable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

You can use _____ as elements of lists, tuples, sets, and dictionaries. ______ are immutable, so you can also use them as dictionary keys

A

functions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

You can define a function within another function

A

True
»> def outer(a, b):
… def inner(c, d):
… return c + d
… return inner(a, b)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

A Python ______ function is an anonymous function expressed as a single statement. You can use it instead of a normal tiny function.

A

lambda

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Can you pass in functions as parameters in python?

A

Yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

> > > 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?

A

> > > edit_story(stairs, lambda word: word.capitalize() + ‘!’)
Thud!
Meow!
Thud!
Hiss!

17
Q

List of python tricks

A

*args and **kwargs
*Inner functions
*Functions as arguments

18
Q

The main part of a program defines the ____ ______; thus, the variables in that namespace are global variables.

A

global namespace

19
Q

To access the global variable rather than the local one within a function, you need to be explicit and use the _____ keyword

20
Q

To write empty functions in Python, we use ____ statement. ____ is a special statement that does nothing. It only works as a dummy statement.