Deck 4: Functions  

Full screen (f)
exit full mode
Question
Which of the following statements a), b) or c) is false?

A) If randrange truly produces integers at random, every number in its range has an equal probability (or chance or likelihood) of being returned each time we call it.
B) We can use Python's underscore (_) xe "_ (digit separator)"xe "digit separator (_)"digit separator to make a value like 6000000 more readable as 6_000_000.
C) The expression range(6,000,000) would be incorrect. Commas separate arguments in function calls, so Python would treat range(6,000,000) as a call to range with the three arguments 6, 0 and 0.
D) All of the above statements are true.
Use Space or
up arrow
down arrow
to flip the card.
Question
Which of the following statements is false?

A) A function definition begins with the xe "keyword:def"xe "function:def keyword"xe "def keyword for defining a function"def keyword, followed by the xe "function:name"function name, a set of parentheses and a colon (:).
B) Like variable identifiers, by convention function names should begin with a lowercase letter and in multiword names underscores should separate each word.
C) The required parentheses in a function definition contain the function's parameter list-a xe "comma-separated list of arguments[comma separated list of arguments]"comma-separated list of parameters representing the data that the function needs to perform its task.
D) The indented lines after the colon (:) are the function's suite, which consists of an optional xe "docstring"docstring followed by the statements that perform the function's task.
Question
Which of the following statements is false?

A) To view a list of identifiers defined in a module, type the module's name and a dot (.), then press Tab.
B) In the math module, pi and e represent the mathematical constants π\pi and e, respectively.
C) Python does not have xe "constants"constants, so even though pi and e are real-world constants, you must not assign new values to them, because that would change their values.
D) To help distinguish "constants" from other variables, the xe "Style Guide for Python Code:constants"Python style guide recommends naming your custom constants with a leading underscore (_) and a trailing underscore.
Question
Which of the following statements is false?

A) To use a tuple's values, you can assign them to a comma-separated list of variables, which xe "unpacking a tuple"unpacks the tuple.
B) Like a list, a tuple is a sequence.
C) To avoid name collisions, variables in different functions' blocks must have different names.
D) Each local variable is accessible only in the block that defined it.
Question
Which of the following statements a), b) or c) is false?

A) The following session defines a maximum function that determines and returns the largest of three values-then calls the function three times with integers, floating-point numbers and strings, respectively. In [1]: def maximum(value1, value2, value3):
)..: """Return the maximum of three values."""
)..: max_value = value1
)..: if value2 > max_value:
)..: max_value = value2
)..: if value3 > max_value:
)..: max_value = value3
)..: return max_value
)..:
In [2]: maximum(12, 27, 36)
Out[2]: 36
In [3]: maximum(12.3, 45.6, 9.7)
Out[3]: 45.6
In [4]: maximum('yellow', 'red', 'orange')
Out[4]: 'yellow'
B) You also may call maximum with mixed types, such as ints and floats: In [5]: maximum(13.5, -3, 7)
Out[5]: 13.5
C) The call maximum(13.5, 'hello', 7) results in TypeError because strings and numbers cannot be compared to one another with the greater-than (>) operator.
D) All of the above statements are true.
Question
Which of the following statements is false?

A) When defining a function, you can specify that a parameter has a default parameter value.
B) When calling the function, if you omit the argument for a parameter with a default parameter value, the default value for that parameter is automatically passed.
C) The following defines a function rectangle_area with xe "default parameter value"default parameter values: def rectangle_area(length=2, width=3):
"""Return a rectangle's area."""
Return length * width
D) The call rectangle_area() to the function in Part (c) returns the value 0 (zero).
Question
Which of the following statements a), b) or c) is false?

A) You can view a module's documentation in IPython interactive mode via tab completion-a discovery feature that speeds your coding and learning processes.
B) After you type a portion of an identifier and press Tab, IPython completes the identifier for you or provides a list of identifiers that begin with what you've typed so far.
C) IPython tab completion results may vary based on your operating system platform and what you have imported into your IPython session.
D) All of the above statements are true.
Question
Which of the following statements is false?

A) Experience has shown that the best way to develop and maintain a large program is to construct it from a small number of large, proven pieces-this technique is called divide and conquer.
B) Using existing xe "function:as building block"functions as building blocks for creating new programs is a key aspect of software reusability-it's also a major benefit of xe "object-based programming (OBP)[object based programming]"object-oriented programming.
C) Packaging code as a function allows you to execute it from various locations in your program just by calling the function, rather than duplicating the possibly lengthy code.
D) When you change a function's code, all calls to the function execute the updated version.
Question
Which of the following statements is false?

A) Function randrange actually generates pseudorandom numbers, based on an internal calculation that begins with a numeric value known as a seed.
B) When you're debugging logic errors in programs that use randomly generated data, it can be helpful to use the same sequence of random numbers until you've eliminated the logic errors, before testing the program with other values.
C) You can use the random module's seed function to seed the random-number generator yourself-this forces randrange to begin calculating its pseudorandom number sequence from the seed you specify. Choosing the same seed will cause the random number generator to generate the same sequence of random numbers.
D) In the following session, snippets [2] and [5] produce the same results purely by coincidence: In [1]: import random
In [2]: random.seed(32)
In [3]: for roll in range(10):
)..: print(random.randrange(1, 7), end=' ')
)..:
1 2 2 3 6 2 4 1 6 1
In [4] for roll in range(10):
)..: print(random.randrange(1, 7), end=' ')
)..:
1 3 5 3 1 5 6 4 3 5
In [5] random.seed(32)
In [6]: for roll in range(10):
)..: print(random.randrange(1, 7), end=' ')
)..:
1 2 2 3 6 2 4 1 6 1
Question
Which of the following statements is false?

A) Variables can be defined in a function's block.
B) A function's parameters and variables are all local variables. They exist only while the function is executing and can be used only in that function.
C) Trying to access a local variable outside its function's block causes a xe "NameError"NameError, indicating that the variable is not defined.
D) All of the above statements are true.
Question
Assuming the following function definition, which of the following statements is false? def rectangle_area(length=2, width=3):
"""Return a rectangle's area."""
Return length * width

A) You specify a xe "default parameter value"default parameter value by following a parameter's name with an = and a value.
B) Any parameters with xe "default parameter value"default parameter values must appear in the parameter list to the right of parameters that do not have defaults.
C) For the following call, the interpreter passes the xe "default parameter value"default parameter value 3 for the width as if you had called rectangle_area(3, 10): rectangle_area(10)
D) The following call to rectangle_area has arguments for both length and width, so IPython ignores the default parameter values: rectangle_area(10, 5)
Question
Which of the following statements is false?

A) The following return statement first terminates the function, then squares number and gives the result back to the caller: return number ** 2
B) Function calls can be embedded in expressions.
C) The following code calls square first, then print displays the result: print('The square of 7 is', square(7))
D) Executing a return statement without an expression terminates the function and implicitly returns the value None to the caller. When there's no return statement in a function, it implicitly returns the value None after executing the last statement in the function's xe "function:block"xe "block:in a function"block.
Question
Which of the following statements is false?

A) Each function should perform a single, well-defined task.
B) The following code calls function square twice. We've replaced each of the output values with ???. The first call produces the int value 49 and the second call produces the int value 6: In [1]: def square(number):
)..: """Calculate the square of number."""
)..: return number ** 2
)..:
In [2]: square(7)
Out[2]: ???
In [3]: square(2.5)
Out[3]: ???
C) The statements defining a xe "function"function are written only once, but may be called "to do their job" from many points in a program and as often as you like.
D) Calling square with a non-numeric argument like 'hello' causes a TypeError because the exponentiation operator (**) works only with numeric values.
Question
Which of the following statements is false?

A) You can introduce the element of chance via the Python Standard Library's random module.
B) The following code produces 10 random integers in the range 1-6 to simulate rolling a six-sided die: import random
For roll in range(10):
Print(random.randrange(1, 7), end=' ')
C) Different values are likely to be displayed each time you run the code in Part (b).
D) Sometimes, you may want to guarantee reproducibility of a random sequence-for debugging, for example. You can do this with the random module's repeat function.
Question
Which of the following statements is false?

A) An xe "statements:import"xe "import statement"import statement of the following form enables you to use a module's definitions via the module's name and a dot (.): import math
B) The following snippet calculates the square root of 900 by calling the math module's sqrt function, which returns its result as a float value: math.sqrt(900)
C) The following snippet calculates the absolute value of -10 by calling the math module's fabs function, which returns its result as a float value: math.fabs(-10)
D) The value of the expression floor(-3.14159) is -3.0.
Question
Which of the following statements is false?

A) The in operator in the following expression tests whether the tuple (7, 11) contains sum_of_dice's value. The operator's right operand can be any iterable: sum_of_dice in (7, 11)
B) There's also a not in operator to determine whether a value is not in an iterable.
C) The concise condition in Part (a) is equivalent to (sum_of_dice = 7) or (sum_of_dice = 11)
D) All of the above statements are true.
Question
Which of the following statements a), b) or c) is false?

A) The built-in xe "built-in function:max"max and xe "built-in function:min"min functions know how to determine the largest and smallest of their two or more arguments, respectively.
B) The built-in xe "built-in function:max"max and xe "built-in function:min"min functions also can receive an xe "iterable"iterable, such as a list but not a string.
C) Using built-in functions or functions from the Python Standard Library's modules rather than writing your own can xe "reducing program development time"reduce development time and increase program reliability, portability and performance.
D) All of the above statements are true.
Question
Which of the following statements is false?

A) A key programming goal is to avoid "xe "reinventing the wheel"reinventing the wheel."
B) A xe "modules"module is a file that groups related functions, data and classes.
C) A package groups related modules. Every Python source-code (.py) file you create is a module. They're typically used to organize a large library's functionality into smaller subsets that are easier to maintain and can be imported separately for convenience.
D) The xe "Python Standard Library"Python Standard Library module money provides arithmetic capabilities for performing monetary calculations.
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/18
auto play flashcards
Play
simple tutorial
Full screen (f)
exit full mode
Deck 4: Functions  
1
Which of the following statements a), b) or c) is false?

A) If randrange truly produces integers at random, every number in its range has an equal probability (or chance or likelihood) of being returned each time we call it.
B) We can use Python's underscore (_) xe "_ (digit separator)"xe "digit separator (_)"digit separator to make a value like 6000000 more readable as 6_000_000.
C) The expression range(6,000,000) would be incorrect. Commas separate arguments in function calls, so Python would treat range(6,000,000) as a call to range with the three arguments 6, 0 and 0.
D) All of the above statements are true.
D
2
Which of the following statements is false?

A) A function definition begins with the xe "keyword:def"xe "function:def keyword"xe "def keyword for defining a function"def keyword, followed by the xe "function:name"function name, a set of parentheses and a colon (:).
B) Like variable identifiers, by convention function names should begin with a lowercase letter and in multiword names underscores should separate each word.
C) The required parentheses in a function definition contain the function's parameter list-a xe "comma-separated list of arguments[comma separated list of arguments]"comma-separated list of parameters representing the data that the function needs to perform its task.
D) The indented lines after the colon (:) are the function's suite, which consists of an optional xe "docstring"docstring followed by the statements that perform the function's task.
D
3
Which of the following statements is false?

A) To view a list of identifiers defined in a module, type the module's name and a dot (.), then press Tab.
B) In the math module, pi and e represent the mathematical constants π\pi and e, respectively.
C) Python does not have xe "constants"constants, so even though pi and e are real-world constants, you must not assign new values to them, because that would change their values.
D) To help distinguish "constants" from other variables, the xe "Style Guide for Python Code:constants"Python style guide recommends naming your custom constants with a leading underscore (_) and a trailing underscore.
To help distinguish "constants" from other variables, the xe "Style Guide for Python Code:constants"Python style guide recommends naming your custom constants with a leading underscore (_) and a trailing underscore.
4
Which of the following statements is false?

A) To use a tuple's values, you can assign them to a comma-separated list of variables, which xe "unpacking a tuple"unpacks the tuple.
B) Like a list, a tuple is a sequence.
C) To avoid name collisions, variables in different functions' blocks must have different names.
D) Each local variable is accessible only in the block that defined it.
Unlock Deck
Unlock for access to all 18 flashcards in this deck.
Unlock Deck
k this deck
5
Which of the following statements a), b) or c) is false?

A) The following session defines a maximum function that determines and returns the largest of three values-then calls the function three times with integers, floating-point numbers and strings, respectively. In [1]: def maximum(value1, value2, value3):
)..: """Return the maximum of three values."""
)..: max_value = value1
)..: if value2 > max_value:
)..: max_value = value2
)..: if value3 > max_value:
)..: max_value = value3
)..: return max_value
)..:
In [2]: maximum(12, 27, 36)
Out[2]: 36
In [3]: maximum(12.3, 45.6, 9.7)
Out[3]: 45.6
In [4]: maximum('yellow', 'red', 'orange')
Out[4]: 'yellow'
B) You also may call maximum with mixed types, such as ints and floats: In [5]: maximum(13.5, -3, 7)
Out[5]: 13.5
C) The call maximum(13.5, 'hello', 7) results in TypeError because strings and numbers cannot be compared to one another with the greater-than (>) operator.
D) All of the above statements are true.
Unlock Deck
Unlock for access to all 18 flashcards in this deck.
Unlock Deck
k this deck
6
Which of the following statements is false?

A) When defining a function, you can specify that a parameter has a default parameter value.
B) When calling the function, if you omit the argument for a parameter with a default parameter value, the default value for that parameter is automatically passed.
C) The following defines a function rectangle_area with xe "default parameter value"default parameter values: def rectangle_area(length=2, width=3):
"""Return a rectangle's area."""
Return length * width
D) The call rectangle_area() to the function in Part (c) returns the value 0 (zero).
Unlock Deck
Unlock for access to all 18 flashcards in this deck.
Unlock Deck
k this deck
7
Which of the following statements a), b) or c) is false?

A) You can view a module's documentation in IPython interactive mode via tab completion-a discovery feature that speeds your coding and learning processes.
B) After you type a portion of an identifier and press Tab, IPython completes the identifier for you or provides a list of identifiers that begin with what you've typed so far.
C) IPython tab completion results may vary based on your operating system platform and what you have imported into your IPython session.
D) All of the above statements are true.
Unlock Deck
Unlock for access to all 18 flashcards in this deck.
Unlock Deck
k this deck
8
Which of the following statements is false?

A) Experience has shown that the best way to develop and maintain a large program is to construct it from a small number of large, proven pieces-this technique is called divide and conquer.
B) Using existing xe "function:as building block"functions as building blocks for creating new programs is a key aspect of software reusability-it's also a major benefit of xe "object-based programming (OBP)[object based programming]"object-oriented programming.
C) Packaging code as a function allows you to execute it from various locations in your program just by calling the function, rather than duplicating the possibly lengthy code.
D) When you change a function's code, all calls to the function execute the updated version.
Unlock Deck
Unlock for access to all 18 flashcards in this deck.
Unlock Deck
k this deck
9
Which of the following statements is false?

A) Function randrange actually generates pseudorandom numbers, based on an internal calculation that begins with a numeric value known as a seed.
B) When you're debugging logic errors in programs that use randomly generated data, it can be helpful to use the same sequence of random numbers until you've eliminated the logic errors, before testing the program with other values.
C) You can use the random module's seed function to seed the random-number generator yourself-this forces randrange to begin calculating its pseudorandom number sequence from the seed you specify. Choosing the same seed will cause the random number generator to generate the same sequence of random numbers.
D) In the following session, snippets [2] and [5] produce the same results purely by coincidence: In [1]: import random
In [2]: random.seed(32)
In [3]: for roll in range(10):
)..: print(random.randrange(1, 7), end=' ')
)..:
1 2 2 3 6 2 4 1 6 1
In [4] for roll in range(10):
)..: print(random.randrange(1, 7), end=' ')
)..:
1 3 5 3 1 5 6 4 3 5
In [5] random.seed(32)
In [6]: for roll in range(10):
)..: print(random.randrange(1, 7), end=' ')
)..:
1 2 2 3 6 2 4 1 6 1
Unlock Deck
Unlock for access to all 18 flashcards in this deck.
Unlock Deck
k this deck
10
Which of the following statements is false?

A) Variables can be defined in a function's block.
B) A function's parameters and variables are all local variables. They exist only while the function is executing and can be used only in that function.
C) Trying to access a local variable outside its function's block causes a xe "NameError"NameError, indicating that the variable is not defined.
D) All of the above statements are true.
Unlock Deck
Unlock for access to all 18 flashcards in this deck.
Unlock Deck
k this deck
11
Assuming the following function definition, which of the following statements is false? def rectangle_area(length=2, width=3):
"""Return a rectangle's area."""
Return length * width

A) You specify a xe "default parameter value"default parameter value by following a parameter's name with an = and a value.
B) Any parameters with xe "default parameter value"default parameter values must appear in the parameter list to the right of parameters that do not have defaults.
C) For the following call, the interpreter passes the xe "default parameter value"default parameter value 3 for the width as if you had called rectangle_area(3, 10): rectangle_area(10)
D) The following call to rectangle_area has arguments for both length and width, so IPython ignores the default parameter values: rectangle_area(10, 5)
Unlock Deck
Unlock for access to all 18 flashcards in this deck.
Unlock Deck
k this deck
12
Which of the following statements is false?

A) The following return statement first terminates the function, then squares number and gives the result back to the caller: return number ** 2
B) Function calls can be embedded in expressions.
C) The following code calls square first, then print displays the result: print('The square of 7 is', square(7))
D) Executing a return statement without an expression terminates the function and implicitly returns the value None to the caller. When there's no return statement in a function, it implicitly returns the value None after executing the last statement in the function's xe "function:block"xe "block:in a function"block.
Unlock Deck
Unlock for access to all 18 flashcards in this deck.
Unlock Deck
k this deck
13
Which of the following statements is false?

A) Each function should perform a single, well-defined task.
B) The following code calls function square twice. We've replaced each of the output values with ???. The first call produces the int value 49 and the second call produces the int value 6: In [1]: def square(number):
)..: """Calculate the square of number."""
)..: return number ** 2
)..:
In [2]: square(7)
Out[2]: ???
In [3]: square(2.5)
Out[3]: ???
C) The statements defining a xe "function"function are written only once, but may be called "to do their job" from many points in a program and as often as you like.
D) Calling square with a non-numeric argument like 'hello' causes a TypeError because the exponentiation operator (**) works only with numeric values.
Unlock Deck
Unlock for access to all 18 flashcards in this deck.
Unlock Deck
k this deck
14
Which of the following statements is false?

A) You can introduce the element of chance via the Python Standard Library's random module.
B) The following code produces 10 random integers in the range 1-6 to simulate rolling a six-sided die: import random
For roll in range(10):
Print(random.randrange(1, 7), end=' ')
C) Different values are likely to be displayed each time you run the code in Part (b).
D) Sometimes, you may want to guarantee reproducibility of a random sequence-for debugging, for example. You can do this with the random module's repeat function.
Unlock Deck
Unlock for access to all 18 flashcards in this deck.
Unlock Deck
k this deck
15
Which of the following statements is false?

A) An xe "statements:import"xe "import statement"import statement of the following form enables you to use a module's definitions via the module's name and a dot (.): import math
B) The following snippet calculates the square root of 900 by calling the math module's sqrt function, which returns its result as a float value: math.sqrt(900)
C) The following snippet calculates the absolute value of -10 by calling the math module's fabs function, which returns its result as a float value: math.fabs(-10)
D) The value of the expression floor(-3.14159) is -3.0.
Unlock Deck
Unlock for access to all 18 flashcards in this deck.
Unlock Deck
k this deck
16
Which of the following statements is false?

A) The in operator in the following expression tests whether the tuple (7, 11) contains sum_of_dice's value. The operator's right operand can be any iterable: sum_of_dice in (7, 11)
B) There's also a not in operator to determine whether a value is not in an iterable.
C) The concise condition in Part (a) is equivalent to (sum_of_dice = 7) or (sum_of_dice = 11)
D) All of the above statements are true.
Unlock Deck
Unlock for access to all 18 flashcards in this deck.
Unlock Deck
k this deck
17
Which of the following statements a), b) or c) is false?

A) The built-in xe "built-in function:max"max and xe "built-in function:min"min functions know how to determine the largest and smallest of their two or more arguments, respectively.
B) The built-in xe "built-in function:max"max and xe "built-in function:min"min functions also can receive an xe "iterable"iterable, such as a list but not a string.
C) Using built-in functions or functions from the Python Standard Library's modules rather than writing your own can xe "reducing program development time"reduce development time and increase program reliability, portability and performance.
D) All of the above statements are true.
Unlock Deck
Unlock for access to all 18 flashcards in this deck.
Unlock Deck
k this deck
18
Which of the following statements is false?

A) A key programming goal is to avoid "xe "reinventing the wheel"reinventing the wheel."
B) A xe "modules"module is a file that groups related functions, data and classes.
C) A package groups related modules. Every Python source-code (.py) file you create is a module. They're typically used to organize a large library's functionality into smaller subsets that are easier to maintain and can be imported separately for convenience.
D) The xe "Python Standard Library"Python Standard Library module money provides arithmetic capabilities for performing monetary calculations.
Unlock Deck
Unlock for access to all 18 flashcards in this deck.
Unlock Deck
k this deck
locked card icon
Unlock Deck
Unlock for access to all 18 flashcards in this deck.