Arguments and Parameters Flashcards

(51 cards)

1
Q
def agree():
    return True
if agree():
    print('Splendid!')
else:
    print('That was unexpected')
A

Splendid!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
def agree():
    return False
if agree():
    print('Splendid!')
else:
    print('That was unexpected')
A

That was unexpected

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
def echo(anything):
    return anything + ' ' + anything
print(echo("Rumplestiltskin"))
A

Rumplestiltskin Rumplestiltskin

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

The values you pass into a function when you call it are known as ___

A

arguments

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

Arguments are inside the function and ___ are outside the function

A

parameters

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
def commentary(color):
    if color == 'red':
        return "It's a tomato."
    elif color == "green":
        return "It's a green pepper."
    elif color == "bee purple":
        return "I dont know what it is, but only bees can see it."
    else:
        return "I've never heard of the color " + color + "."
    
comment = commentary('blue')
print(comment)
A

I’ve never heard of the color blue.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
thing = None
bool(thing)
print(bool(thing))
A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
thing = None
if thing:
    print("It's some thing")
else:
    print("It's no thing")
A

It’s no thing

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
thing = 0
print(bool(thing))
A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
thing = 0
print(thing == False)
A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
thing = 0
print(thing is False)
A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
thing = 1
print(bool(thing))
A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
thing = 1
print(thing == True)
A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
thing = 1
print(thing is True)
A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
def whatis(thing):
    if thing is None:
        print("None")
    elif thing:
        print("true")
    else:
        print("false")
whatis(None)
A

None

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

def whatis(thing):
if thing is None:
print(“None”)
elif thing:
print(“true”)
else:
print(“false”)
whatis(True)

A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q
def whatis(thing):
    if thing is None:
        print("None")
    elif thing:
        print("true")
    else:
        print("false")
whatis(False)
A

false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q
def whatis(thing):
    if thing is None:
        print("None")
    elif thing:
        print("true")
    else:
        print("false")
whatis(0)
A

false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q
def whatis(thing):
    if thing is None:
        print("None")
    elif thing:
        print("true")
    else:
        print("false")
whatis(0.0)
A

false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q
def whatis(thing):
    if thing is None:
        print("None")
    elif thing:
        print("true")
    else:
        print("false")
whatis('')
A

false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q
def whatis(thing):
    if thing is None:
        print("None")
    elif thing:
        print("true")
    else:
        print("false")
whatis("")
22
Q
def whatis(thing):
    if thing is None:
        print("None")
    elif thing:
        print("true")
    else:
        print("false")
whatis('''''')
23
Q
def whatis(thing):
    if thing is None:
        print("None")
    elif thing:
        print("true")
    else:
        print("false")
whatis(())
24
Q
def whatis(thing):
    if thing is None:
        print("None")
    elif thing:
        print("true")
    else:
        print("false")
whatis([])
25
``` def whatis(thing): if thing is None: print("None") elif thing: print("true") else: print("false") whatis({}) ```
false
26
``` def whatis(thing): if thing is None: print("None") elif thing: print("true") else: print("false") whatis(set()) ```
false
27
``` def whatis(thing): if thing is None: print("None") elif thing: print("true") else: print("false") whatis(0.00001) ```
true
28
``` def whatis(thing): if thing is None: print("None") elif thing: print("true") else: print("false") whatis([0]) ```
true
29
``` def whatis(thing): if thing is None: print("None") elif thing: print("true") else: print("false") whatis(['']) ```
true
30
``` def whatis(thing): if thing is None: print("None") elif thing: print("true") else: print("false") whatis(' ') ```
true
31
``` def menu(wine, entree, dessert): return {'wine':wine,'entree':entree,'dessert':dessert} print(menu('chardonnay','chicken','cake')) ```
{'wine': 'chardonnay', 'entree': 'chicken', 'dessert': 'cake'}
32
``` def menu(wine, entree, dessert): return {'wine':wine,'entree':entree,'dessert':dessert} print(menu('beef','bagel','bordeaux')) ```
{'wine': 'beef', 'entree': 'bagel', 'dessert': 'bordeaux'}
33
``` def menu(entree='beef',dessert='bagel',wine='bordeaux'): return {'wine':wine,'entree':entree,'dessert':dessert} print(menu(entree='burrito',dessert='ice cream',wine='red')) ```
{'wine': 'red', 'entree': 'burrito', 'dessert': 'ice cream'}
34
You can mix positional and keyword arguments ``` def menu(wine,entree,dessert): return {'wine':wine,'entree':entree,'desert':dessert} print(menu('frontenac',entree='fish',dessert='flan')) ```
{'wine': 'frontenac', 'entree': 'fish', 'desert': 'flan'}
35
Which argument is positional? ``` def menu(wine,entree,dessert): return {'wine':wine,'entree':entree,'desert':dessert} print(menu('frontenac',entree='fish',dessert='flan')) ```
frontenac
36
Notice the lack of dessert argument ``` def menu(wine,entree,dessert='pudding'): return {'wine':wine,'entree':entree,'desert':dessert} print(menu('chardonnay','chicken')) ```
{'wine': 'chardonnay', 'entree': 'chicken', 'desert': 'pudding'}
37
The argument is used instead of the default ``` def menu(wine,entree,dessert='pudding'): return {'wine':wine,'entree':entree,'desert':dessert} print(menu('dunkelfelder','duck','doughnut')) ```
{'wine': 'dunkelfelder', 'entree': 'duck', 'desert': 'doughnut'}
38
There are two functions. The goal is to run each time with a fresh empty result list, add the arg argument to it, and then print a single item list. Which function does this and why? ``` def buggy(arg, result=[]): result.append(arg) print(result) buggy('a') buggy('b') ``` ``` def works(arg): result = [] result.append(arg) return result print(works('a')) print(works('b')) ```
Works. default parameter values are calculated when the function is defined, not when it is run. Therefore the function buggy still had a list item the second time it was called. The works function fixes the issue by creating a fresh empty list each time the function is called.
39
When would you create a c-style pointer with ctypes module?
When integrating python and C code.
40
What does an asterisk inside a function with a parameter do? ``` def print_args(*args): print('Positional tuple:',args) print_args(3,2,1,'wait!','uh...') ```
Groups a variable number of positional arguments into a single tuple of parameter values. (3, 2, 1, 'wait!', 'uh...')
41
*args always goes at the end ``` def print_more(required1,required2,*args): print('Need this one:', required1) print('Need this one too:',required2) print('All the rest:',args) print_more('cap','gloves','scarf','monocle','mustache wax') ```
Need this one: (‘cap’) Need this one too: (‘gloves’) All the rest: ('scarf', 'monocle', 'mustache wax')
42
You can put keyword arguments into a dictionary where the arguments are the keys and their values are the corresponding dictionary values. What symbol do you use?
**
43
``` def print_kwargs(**kwargs): print('Keyword arguments:',kwargs) print_kwargs(wine='merlot',entree='mutton',dessert='macaroon') ```
Keyword arguments: {'wine': 'merlot', 'entree': 'mutton', 'dessert': 'macaroon'}
44
Where is ** syntax acceptable?
in a function call or definition
45
``` def print_data(data,/,start=0,end=100): for value in (data[start:end]): print(value) data = ['a','b','c','d','e','f','g','h'] print_data(data,2,4) ```
c d
46
``` def print_data(data,/,start=0,end=100): for value in (data[start:end]): print(value) data = ['a','b','c','d','e','f','g','h'] print_data(data,end=5,start=3) ```
d e
47
T/F start=0 and end=100 are optional ``` def print_data(data,/,start=0,end=100): for value in (data[start:end]): print(value) data = ['a','b','c','d','e','f','g','h'] print_data(data,end=5,start=3) ```
True
48
``` def print_data(data,/,start=0,end=100): for value in (data[start:end]): print(value) data = ['a','b','c','d','e','f','g','h'] print_data(data,end=4,start=0) ```
a b c d
49
What does the * mean? ``` def print_data(data,*,start=0,end=100): for value in (data[start:end]): print(value) data = ['a','b','c','d','e','f','g','h'] print_data(data,end=4,start=0) ```
Adding a * in the function definition means that all the following parameters (in this example, start and end) must be provided as named arguments if we don’t want their default values:
50
What does the / mean? ``` def print_data(data,/,start=0,end=100): for value in (data[start:end]): print(value) data = ['a','b','c','d','e','f','g','h'] print_data(data,end=4,start=0) ```
You may want an argument to be in a defined position, and that requires the use of a single slash (/) just after the position-only parameters:
51
If an argument is mutable its value can be changed from ___ the function via its corresponding parameter
inside