x = 10 * 10
print(type(x)) # Output: <class 'int'>
x = 'X' * 10
print(type(x)) # Output: <class 'str'>
<class 'int'>
<class 'str'>
A function is a block of code that performs a specific task and may return a value. Functions are the building blocks of Python code. Functions are the building blocks of Python code.
User-defined functions are custom functions created by the user to perform specific calculations or tasks that are not available in Python’s built-in functions.
Modular programming is a software design technique that emphasizes breaking down programs into smaller, self-contained modules or procedures. Each module performs a specific task and can be reused in different parts of the program. This approach makes code more manageable, easier to understand, and less error-prone.
In Python, a function consists of the following parts:
return
statement is used to return a value from the function. If omitted, the function returns None
.In Python, you can define a function using the def
keyword followed by the function name and parameters. The function body is indented to indicate that it is part of the function. For example, in Listing 12.1, the function add_one_to
adds one to a number and returns the result. This function:
num
)result
result
using the return
statementreturn
statement will not be executed. The block of code inside the function is indented. The function is called with the argument 1
, and the return value is assigned to the variable value
. Then, the function is called two more times with the previous result as the argument.
def add_one_to(num):
# Assign result of calculation to variable "result"
result = num + 1
# Return the result
return result
# Everything you put here will NOT be executed!
# This is not part of the function
value = add_one_to(1) # Output: 2
value = add_one_to(value) # Output: 3
value = add_one_to(value) # Output: 4
In Python, the return
statement is used to return a value from a function. If the return
statement is omitted, the function will return None
by default.
To use a function, you need to:
In Listing 12.1, the function add_one_to
is called with the argument 1
, and the return value is assigned to the variable value
.
The difference between arguments and parameters is that arguments are the actual values passed to a function, while parameters are the variables that receive the values. These terms are often used interchangeably, but it’s important to understand the distinction. The parameters are placeholders for the arguments that will be passed to the function.
return
Statement)In Python, you can exit a function at any point using the return
statement. This statement is used to immediately exit the function and optionally return a value.
For example:
return
statement will not be executed.
Notice that since the return statement returns a value, you can use the function to assign the result to a variable. In Listing 12.3, the function power_two
calculates the square of a number. The result of the function is then used as an argument for the next call to the function, creating a chain of calculations:
power_two
calculates the square of a number. The result of the function is then used as an argument for the next call to the function. The result of the last call is assigned to the variable result
.
None
ValueIf a function does not contain a return
statement, it will return None
by default. In Listing 12.4, the function no_return
does not contain a return
statement. When called, the function will return None
. If the return
keyword is used without a value, it will also return None
.
no_return
does not contain a return
statement. When called, the function will return None
. The variable x
is assigned the return value of the print
function, which is None
. Also, the function return_keyword
contains a return
statement without a value, which will return None
.
The None
value is a special constant in Python of type NoneType
. It is often used to indicate the absence of a value or as a placeholder when a value is not needed. None
is useful when you want to explicitly return nothing from a function or assign a variable to an empty value.
None
If a function does not contain a return
statement, it will return None
by default. Be careful when assigning the return value of such functions to variables, as they may not behave as expected.
pass
Statement)Sometimes you may need to define a function without any code inside it. This can be useful when you are planning to implement the function later or when you want to create a placeholder for future functionality.
In Python, you can define an empty function using the pass
statement. The pass
statement is a null operation that does nothing when executed.
For example, in Listing 12.5, the function empty_function
does not contain any code. It is defined using the pass
statement. If you call this function, it will not perform any action and will return None
.
In Python, you can exit a function at any point using the return
statement. For example, in Listing 12.6, the function add_and_exit
adds two numbers and exits the function immediately after printing the result. The code after the return
statement will not be executed.
In Python, you can define default values for function parameters. If a default value is provided for a parameter, it becomes an optional argument, and the function can be called without providing a value for that parameter.
For example, in #lst-default-arguments, the function salute_recipient
has a default value for the parameter salutation
. If no value is provided for salutation
, the default value "Hello"
will be used.
In Python, the order of arguments passed to a function must match the order of parameters defined in the function signature. If the order is incorrect, the function may not work as expected.
For example, in #lst-order-of-arguments, the function calculate_speed
expects the parameters distance
and time
in that order. If the order is reversed when calling the function, the result will be incorrect.
In Python, you can use keyword arguments to specify the values of parameters by name when calling a function. This allows you to pass arguments in any order, as long as you specify the parameter names.
For example, in #lst-keyword-arguments, we adjust the calls to the function calculate_speed
(see Listing 12.8) using keyword arguments. This way, we can pass the arguments in any order.
calculate_speed
(see Listing 12.8). The function expects to receive first the distance and then the time. The order of the arguments is not important when using keyword arguments.
In Python, a function can return multiple values by separating them with commas. When multiple values are returned, they are packed into a tuple. You can then unpack the tuple into separate variables when calling the function.
For example, in #lst-return-multiple-values, the function get_name_and_surname
returns two values: the name and the surname. When calling the function, we unpack the tuple into two variables: name
and surname
.
split
is a built-in method that splits a string into a list of substrings at a specified separator (the space character by default). The function returns two values, which are unpacked into separate variables.
A tuple is an ordered collection of elements enclosed in parentheses ( )
. Tuples are similar to lists, but they are immutable, meaning their elements cannot be changed after creation. Tuples are often used to store related data that should not be modified.
In Python, functions that perform actions without returning a meaningful value are still defined using the def
keyword. If a function does not have a return
statement, it returns None
by default.
For example in #lst-display-message, the function display_message
prints a message without returning a value.
a
will be assigned None
.
Pre-defined functions are built-in functions that perform common tasks. They are used to simplify coding and avoid redundancy. Most programming languages have a set of built-in functions; therefore, before “reinventing the wheel,” check if there is a pre-defined function that can be used.
In the following, we will discuss some common pre-defined functions in Python:
print(objs, end="\n", sep=" ")
: Prints objects to the console. The end
parameter specifies the string that is printed at the end (default is a newline), and the sep
parameter specifies the separator between the objects (default is a space). For example:
Will output:
input(prompt="")
: Reads a line of input from the console and returns it as a string. The optional prompt
parameter is the message displayed to the user before input. For example:
Will prompt the user to enter their name and then display a greeting.
type(obj)
: Returns the type of an object. For example:
Will output:
len(obj)
: Returns the length of an object (e.g., a string, list, tuple, dictionary). For example:
Will output:
int(obj)
: Converts an object to an integer. For example:
Will output:
float(obj)
: Converts an object to a floating-point number. For example:
Will output:
str(obj)
: Converts an object to a string. For example:
Will output:
abs(num)
: Returns the absolute value of a number. For example:
Will output:
round(num, ndigits=None)
: Rounds a number to a specified number of decimal places. If ndigits
is not provided, the number is rounded to the nearest integer. For example:
Will output:
max(numbers)
, min(numbers)`: Returns the maximum or minimum value from a sequence of numbers. For example:
Will output:
When naming functions in Python, it is important to follow naming conventions to make the code more readable and maintainable. The rules for naming functions are similar to the rules for naming variables (see Variable Naming Conventions). Here are some aditional guidelines for naming functions:
snake_case
for function names: Function names should be in lowercase and words should be separated by underscores. For example, calculate_gpa
, display_results
, validate_input
.calculate_gpa
display_student_info
get_total_score
is_input_valid
is_capacity_reached
has_permission
validate_input
Starting from Python 3.5, we can use type hints (also known as type annotations) to specify the expected data types of variables, function parameters, and return values. This can help in code readability and IDEs can use this information for type checking.
Python is a dynamically typed language, which means that the data type of a variable is determined at runtime based on the value assigned to it. For example, in Listing 12.12, the variable x
is assigned an integer value, and later it is assigned a string value. The data type of the variable x
changes based on the assigned value.
x
is assigned an integer value and later a string value. The data type of the variable changes based on the assigned value.
Dynamic typing allows for flexibility but can lead to errors if the wrong type of value is assigned to a variable. For example, in Listing 12.13, the variable x
is assigned a string value, and the variable y
is assigned an integer value. When the code tries to add x
and y
, a TypeError
occurs because Python does not allow adding a string and an integer.
x
is assigned a string value, and the variable y
is assigned an integer value. When the code tries to add x
and y
, the error: TypeError: can only concatenate str (not "int") to str
occurs. Python will not prevent you from assigning different types of values to variables.
In contrast, statically typed languages, such as C++ and Java, require the data type of a variable to be declared explicitly. The data type of a variable is determined at compile time, and the compiler checks for type errors before the code is executed. Therefore, in statically typed languages, the code will not compile if the wrong type of value is assigned to a variable; the example in Listing 12.13 would not compile in a statically typed language.
Therefore, although dynamic typing provides flexibility, it can lead to errors that are not caught until runtime.
To add type hints to a function, you can specify the data types of the parameters using a colon (:
) after the parameter name, followed by the data type. You can also specify the return type of the function using the ->
arrow followed by the return type.
For example, in Listing 12.14, the function add_one_to_with_type_hint
adds one to a number and specifies the data types of the parameter num
and the return value result
.
num
is expected to be an integer, and the return value is an integer.
In Listing 12.15, we have three versions of a function to sum numbers. The first function func1
does not explicitly declare the types of variables. The second function func2
uses type hints to specify that the parameters and return value are integers. The third function func3
uses type hints to specify that the parameters and return value are floating-point numbers.
In Figure 12.1, you can see type hints in action in Visual Studio Code. The editor shows the expected types of the parameters and return values. Using the hints, a programmer can understand the expected types and write code accordingly.
Since the hints are not enforced by the Python interpreter (i.e., they are not used for type checking), the code will run even if the types do not match the hints. In Listing 12.16, the function func2
expects integer parameters and returns an integer, but it can receive even str
or float
values.
func2
with different types of arguments. The function expects integer parameters and returns an integer, but it can receive any type of value due to Python’s dynamic typing. (Note: due to precision issues, the output of the first call may not be exactly 7.7. In computers, floating-point numbers are stored in binary format, which can lead to small rounding errors.)
Type hints are not enforced by the Python interpreter, meaning that the code will run even if the types do not match the hints. Type hints are used for documentation and can be used by external tools for type checking.
The call stack is a data structure that stores information about the active subroutines (functions) of a program. When a function is called, a new frame is added to the top of the call stack to store information about the function call, such as the function’s parameters and local variables.
When a function returns, its frame is removed from the call stack, and the program continues execution from the point where the function was called. The call stack ensures that functions are executed in the correct order and that local variables are stored separately for each function call.
In Listing 12.17, we have a simple example of a call stack in Python. The function main
calls func1
, which in turn calls func2
. Inside each function, we print the value of a local variable x
. Every time a function is called, a new frame is added to the call stack, and when the function returns, its frame is removed from the call stack.
main
calls func1
, which in turn calls func2
. When a function is called, a new frame is added to the call stack. This frame has its own scope, that is, its own set of local variables. When a function returns, its frame is removed from the call stack.
When you run the code in Listing 12.17, you will see the following output:
The output shows the order of function calls and the values of the local variables x
in each function. The call stack ensures that functions are executed in the correct order and that local variables are stored separately for each function call.
In the example in Listing 12.17, the variable x
is defined in each function with the same name. However, each function has its own scope, and the variable x
is local to that scope. Therefore, the variable x
in func2
does not overwrite the variable x
in func1
or main
. An analogy is that each function is like a separate room in a house, and the variables are like items stored in each room. You can have items with the same name in different rooms without them interfering with each other because each room has its own storage space.
But what is at the bottom of the call stack, below main
? The bottom of the call stack is the initial frame that is created when the program starts. This frame contains information about the program itself and is the starting point for the call stack. Below, you can see a visualization of the call stack for the example in Figure 12.2.
main()
Stack (1)
func1()
main()
Stack (2)
func2()
func1()
main()
Stack (3)
func1()
main()
Stack (4)
main()
Stack (5)
Empty
Stack (6)
The lifetime of a variable refers to the period during which the variable exists in memory. In Python, the lifetime of a variable is determined by its scope, which is the region of the code where the variable is accessible.
In Listing 12.18, we have an example of local and global variables in Python. The function func
defines a local variable x
, and the program defines a global variable y
. When the function is called, the local variable x
is created, and when the function returns, x
is destroyed. The global variable y
exists throughout the program’s execution.
func
defines a local variable x
, and the program defines a global variable y
. The local variable x
is created when the function is called and is destroyed when the function returns. The global variable y
exists throughout the program’s execution.
When you run the code in Listing 12.18, you will see the following output:
When a local variable has the same name as a global variable, the local variable shadows the global variable. This means that the local variable takes precedence over the global variable within its scope. For example, in Listing 12.19, the function func
defines a local variable y
with the same name as the global variable y
. When the function is called, the local variable y
shadows the global variable y
.
func
defines a local variable y
with the same name as the global variable y
. The local variable y
shadows the global variable y
within the function’s scope.
When you run the code in Listing 12.19, you will see the following output:
The output shows that the local variable y
inside the function func
shadows the global variable y
. Inside the function, y
refers to the local variable, and outside the function, y
refers to the global variable.
global
KeywordIn Python, you can modify a global variable inside a function using the global
keyword. This allows you to change the value of a global variable from within a function.
In Listing 12.20, the function modify_global
uses the global
keyword to modify the global variable y
. The function increments the value of y
by 10.
global
keyword. The function modify_global
increments the value of the global variable y
by 10.
When you run the code in Listing 12.20, you will see the following output:
The output shows that the value of the global variable y
is incremented by 10 inside the function modify_global
.
While global variables can be useful, they can also lead to code that is difficult to understand and maintain. But why are global variables evil? Since global variables can be accessed and modified from anywhere in the program, they can introduce hidden dependencies and side effects that make the code more complex, error-prone, and difficult to debug. If a function relies on global variables, you cannot easily determine the function’s behavior by looking at its code alone. You need to know the state of the global variables at the time the function is called, which can be challenging in a large codebase.
Therefore, using global variables is generally discouraged as it can quickly lead to Spaghetti code (code that is difficult to understand and maintain due to its complex and tangled structure).
To avoid global variables, you can use function parameters to pass values between functions or return values from functions. This makes the code more modular, easier to test, and less error-prone. A function can be considered a black box that takes input (parameters) and produces output (return value) without relying on external state. When a function is self-contained and does not depend on global variables, it is easier to reason about its behavior and test it in isolation. When you use built-in functions like print
, len
, or abs
, you are using functions that do not rely on global variables. They take input (arguments) and produce output without modifying external state. As users, we do not need to know how these functions work internally; we only need to know how to use them.
While it is generally recommended to avoid global variables, there are situations where they can be useful:
When using global variables, it is important to document their purpose and usage to make the code easier to understand and maintain.
A cache is a temporary storage area that stores frequently accessed or recently used data to speed up access to that data. Caching is used to reduce the time it takes to access data by storing a copy of the data in a faster storage location (e.g., memory) so that it can be retrieved more quickly.
In Python, function arguments can be passed by reference or by value, depending on the type of the argument. Understanding the difference between passing by reference and passing by value is important for writing correct and efficient code.
In Python, immutable objects (e.g., numbers, strings, tuples) are passed by value, while mutable objects (e.g., lists, dictionaries, objects) are passed by reference.
In Listing 12.21, the function modify_value
takes an integer as an argument and increments the value by 10. When the function is called, a copy of the integer x
is passed to the function, and the original value of x
is not modified.
modify_value
takes an integer as an argument and increments the value by 10. The original value of x
is not modified.
In Listing 12.22, the function modify_list
takes a list as an argument and appends a new element to the list. When the function is called, a reference to the list lst
is passed to the function, and changes made to the list inside the function affect the original list.
modify_list
takes a list as an argument and appends a new element to the list. Changes made to the list inside the function affect the original list.
When writing functions, it is important to provide documentation that describes what the function does, its parameters, and its return value. This documentation helps other programmers understand how to use the function and what to expect from it.
In Python, you can add documentation to a function using docstrings. A docstring is a string literal that appears as the first statement in a function and provides information about the function. Docstrings are enclosed in triple quotes ("""
) and can span multiple lines.
In Listing 12.23, we provide two versions of the function calculate_area
using different styles of docstrings (NumPy and Google). The function calculates the area of a circle given its width and height and an optional parameter precision
to specify the number of decimal places in the result.
calculate_area
with different styles of docstrings (NumPy and Google). The function calculates the area of a rectangle given its width and height.
def calculate_area_circle(radius, precision=2):
"""
Calculate the area of a circle.
Parameters
----------
radius : float
The radius of the circle.
precision : int, optional
The number of decimal places in the result (default is 2).
Returns
-------
float
The area of the circle.
Examples
--------
>>> calculate_area_circle(5)
78.54
>>> calculate_area_circle(5, 3)
78.540
"""
return np.pi * radius ** 2
def calculate_area_circle(radius, precision=2):
"""
Calculate the area of a circle.
Args:
radius (float): The radius of the circle.
precision (int, optional): The number of decimal
places in the result (default is 2).
Returns:
float: The area of the circle.
Examples:
>>> calculate_area_circle(5)
78.54
>>> calculate_area_circle(5, 3)
78.540
"""
return np.pi * radius ** 2
If you are using an IDE like Visual Studio Code, you can hover over a function to see its docstring. This can help you understand what the function does and how to use it without looking at the function’s implementation. Also, by using extensions like AutoDocstring in VS Code, you can generate docstrings automatically by typing """
and pressing Enter
above a function definition. This can save you time and ensure that your functions are well-documented.
For a more comprehensive guide on writing docstrings, you can refer to the NumPy Docstring Guide (see example) and the Google Python Style Guide.
Create functions to test the following built-in string functions applied to a str
string:
str.strip()
: Removes leading and trailing spaces from a string.str.upper()
: Converts a string to uppercase.str.lower()
: Converts a string to lowercase.str.replace(old, new)
: Replaces occurrences of the substring old
with the substring new
in a string.str.split(sep)
: Splits a string into a list of substrings based on a separator sep
.str.find(sub)
: Returns the index of the first occurrence of the substring sub
in a string.str.count(sub)
: Returns the number of occurrences of the substring sub
in a string.str.isalpha()
: Returns True
if all characters in a string are alphabetic.str.isdigit()
: Returns True
if all characters in a string are digits.Create a function for each of the above functions and test them with different input strings. Below, you can see an example of testing the len()
and count()
functions:
Create functions to test each one of the following built-in or math
module functions:
abs()
: Returns the absolute value of a number.math.sqrt()
: Returns the square root of a number.round()
: Rounds a number to a specified number of decimal places.int()
: Converts a number to an integer (truncates the decimal).math.pi
: Returns the mathematical constant Pi (approximately 3.14159).random.random()
: Generates a (pseudo) random number between 0 and 1.math.exp()
: Returns the exponential value of a number.The Mayer Multiple is a ratio used to evaluate the price of Bitcoin in relation to its historical performance. It is calculated by dividing the current price of Bitcoin by the 200-day moving average (200 DMA) of its price:
\[ \text{Mayer Multiple} = \frac{\text{Current Price}}{\text{200 DMA}} \]
The logic for using the Mayer Multiple in trading decisions is simple:
Create a function calculate_mayer_multiple(bitcoin_price, dma_200)
to calculate the Mayer Multiple given the current price of Bitcoin and the 200-day moving average. Your function should:
Then, create three functions to determine the trading decision based on the Mayer Multiple:
sell_signal(mayer_multiple)
: Returns True
if the Mayer Multiple is greater than 2.4.buy_signal(mayer_multiple)
: Returns True
if the Mayer Multiple is less than 1.0.hold_signal(mayer_multiple)
: Returns True
if the Mayer Multiple is between 1.0 and 2.4.Test your functions with different values to ensure they are working correctly.
Which of the following functions will return the square of a number passed as an argument?
Choose the correct option:
Answer: Listing 12.24 (c)
The function must return the square of the number passed as an argument. The correct syntax is to assign the result to the function name (SquareNumber = num ^ 2
).
Which of the following will display the message "Hello, World!"
in the console? Assume that the function main
is the entry point for the program.
Choose the correct option:
main()
.
Answer: Listing 12.25 (a)
hello_world
prints the message, and the main
function calls it.hello_world
prints the message, but the main
function tries to print the return value of hello_world
, which is None
.hello_world
returns the message, but the main
function does not print it.hello_world
returns None
before printing the message, so the message is not displayed.Which of the following flowcharts correctly represents the algorithm of a function called draw_eye_array
that generates a symmetrical array (eye pattern) in a 2D list?
The algorithm should create a square array of size N x N
and set the value of the diagonal cells to 1
and the rest to 0
.
An array is a data structure that stores a collection of elements, typically of the same type. A 2D array is an array of arrays, where each element is itself an array. In this case, the 2D array represents a matrix or grid of values.
For example, for a 5x5 array, the output should be:
\[ \begin{array}{ccccc} 1 & 0 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 0 & 1 \\ \end{array} \]
The function draw_eye_array
should read the dimension N
and generate the array.
Consider that the command array[row][col] = 1
sets the value of the cell in the row
and col
position to 1
. For example, array[0][0] = 1
sets the value of the first cell to 1
.
Select the correct flowchart from the following options:
graph LR A((Start)) --> B[/"N"/] B --> R["Set\nrow = 0"] R --> C{"Is\nrow > N?"} E -- No --> J["Increment\nrow by 1"] C -- No --> E{"Is\ncol > N?"} E -- Yes --> EYE{"Is\nrow\nequal to\ncol?"} EYE -- Yes --> F[/"array[row][col] = 1"/] EYE -- No --> G[/"array[row][col] = 0"/] F --> I["Increment\ncol by 1"] J --> C G --> I I --> E C -- Yes --> L((End))
graph LR A((Start)) --> B[/"N"/] B --> R["Set\nrow = 0"] R --> C{"Is\nrow >= N?"} E -- No --> J["Increment\nrow by 1"] C -- No --> D["Set\ncol = 0"] D --> E{"Is\ncol >= N?"} E -- Yes --> EYE{"Is\nrow\nequal to\ncol?"} EYE -- Yes --> F[/"array[row][col] = 1"/] EYE -- No --> G[/"array[row][col] = 0"/] F --> I["Increment\ncol by 1"] J --> C G --> I I --> E C -- Yes --> L((End))
graph LR A((Start)) --> B[/"N"/] B --> R["Set\nrow = 0"] R --> C{"Is\nrow > N?"} E -- No --> J["Increment\nrow by 1"] C -- No --> D["Set\ncol = 0"] D --> E{"Is\ncol > N?"} E -- Yes --> EYE{"Is\nrow\nequal to\ncol?"} EYE -- Yes --> F[/"print(1)"/] EYE -- No --> G[/"print(0)"/] F --> I["Increment\ncol by 1"] J --> C G --> I I --> E C -- Yes --> L((End))
graph LR A((Start)) --> B[/"N"/] B --> R["Set\nrow = 0"] R --> C{"Is\nrow >= N?"} C -- No --> D["Set\ncol = 0"] D --> E{"Is\ncol >= N?"} E -- Yes --> J["Increment\nrow by 1"] E -- No --> EYE{"Is\nrow\nequal to\ncol?"} EYE -- Yes --> F[/"array[row][col] = 1"/] EYE -- No --> G[/"array[row][col] = 0"/] F --> I["Increment\ncol by 1"] J --> C G --> I I --> E C -- Yes --> L((End))
Answer: Flowchart in Figure 12.4 (d)
Explanation:
N
, sets the initial row and column values, and then iterates over the rows and columns to set the cell values based on the condition “Is row equal to col”.N
. Also, row > N
should be row >= N
, since the last row is N-1
.N
(i.e., is col >= N?
evaluates to Yes
), the algorithm should Increment row by 1
because all columns in the row have been set. However, the flowchart inverted the logic and increments the column instead.1
and 0
instead of setting the values in the array. The correct action is to set the values in the array based on the condition “Is row equal to col”.In the correct version, the algorithm reads the dimension N
, sets the initial row and column values, and then iterates over the rows and columns to set the cell values based on the condition “Is row equal to col”.
The Python code is as follows:
draw_eye_array
to generate an eye pattern array in a 2D list. The code is here for reference only. You do not need to know the Python syntax to answer the question.
def draw_eye_array(N):
array = [[0 for _ in range(N)] for _ in range(N)]
for row in range(N):
for col in range(N):
if row == col:
array[row][col] = 1
else:
array[row][col] = 0
return array
# Example usage
n = 5
eye_array = draw_eye_array(n)
for row in eye_array:
print(row)
[1, 0, 0, 0, 0]
[0, 1, 0, 0, 0]
[0, 0, 1, 0, 0]
[0, 0, 0, 1, 0]
[0, 0, 0, 0, 1]
What is the output of the code in Listing 12.27?
x
and a function func
.
Choose the correct option:
15
and 10
.10
and 15
.15
and 15
.UnboundLocalError
.Answer: The code will raise a UnboundLocalError
.
More specifically, the error is UnboundLocalError: local variable 'x' referenced before assignment
. The variable x
is defined outside the function, so it is considered a global variable. To modify a global variable inside a function, you need to use the global
keyword.
What is the output of the code in Listing 12.28?
x
and a function func
.
Choose the correct option:
15
and 10
.10
and 15
.15
and 15
.UnboundLocalError
.Answer: The code will print 15
and 15
.
The global
keyword is used to modify the global variable x
inside the function func
. The value of x
is incremented by 5 inside the function and outside the function.
What is the output of the code in Listing 12.29?
x
and a function func
.
Choose the correct option:
15
and 10
.10
and 15
.15
and 15
.UnboundLocalError
.Answer: The code will print 15
and 10
.
The variable x
inside the function func
is a local variable that shadows the global variable x
. The local variable x
is assigned the value 15
, and the global variable x
remains unchanged.
What is the output of the code in Listing 12.30?
x
and a function func
.
Choose the correct option:
15
, 10
, and 15
.10
, 15
, and 15
.SyntaxError
.UnboundLocalError
.Answer: The code will raise a SyntaxError
.
More specifically, the error is SyntaxError: name 'x' is used prior to global declaration
. The global
keyword must be used before the variable is referenced in the function. Placing the global
keyword after the variable assignment will raise a SyntaxError
.
What is the output of the code in Listing 12.31?
x
and y
and a function func
.
Choose the correct option:
A
, B
, A
, and B
.A
, B
, 10
, and 15
.10
, 15
, A
, and B
.SyntaxError
.Answer: The code will print A
, B
, A
, and B
.
The global
keyword is used to modify the global variables x
and y
inside the function func
. The values of x
and y
are changed inside the function and outside the function.
What is the output of the code in Listing 12.32?
add_one_to
with type hints.
Choose the correct option:
6
and raise a TypeError
.6
and 4
.TypeError
and print 4
.6
and 4.14
.Answer: The code will print 6
and 4.14
.
Since type hints are not enforced by the Python interpreter, the code will run even if the types do not match the hints. Therefore, the function add_one_to
can accept both integer and floating-point numbers as input.