Chapter 9: Functions Flashcards

1
Q

Define a function that does nothing

A

Def do_nothing():
Pass

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

Define a function that has no parameters but 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

You can also pass ____ into function

A

Parameter

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

____ is a specifically Python value that holds a place when there is nothing to say. It is not the same as Boolean value False.

A

None

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

Example using None to distinguish from Boolean False value

A

Thing = None
If thing is None
Print(‘It is nothing’)

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

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

A

Default

Ex:
Def menu(wine, entree, dessert = ‘pudding’)

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

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.

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
8
Q

You can use two asterisks (**) to group keyword arguments into a _____ where the arguments are the keys, and their values are the corresponding values

A

Dictionary

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

If an argument is mutable, its value can be changed from ____ the function

A

Inside

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

Example of something you can do with python

A

Def run_something_with_args(func, ar1, arg2)
Func(arg1,arg2)

> > > run_something_wiith_args(add_args,5,9)
14

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

Another example

A

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

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

In python you can define function inside of function

A

True

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

Using lambda

A

Def edit_story(words, func)
For word in words:
Printf(func(word))

Edit_story(stairts, lambda word: word.capitalize() + ‘!’)

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

A _______ is a Python sequence creation object

A

Generator

> > > sum(range(1,101))
5050

17
Q

Generator comprehensions

A

Get obj = (pair for pair in zip([‘a’,’b’], [‘1’,’2’]))