1/112
Looks like no tags are added yet.
What is the purpose of functions in programming?
To prevent re-writing the same code repeatedly and to conceptually separate parts of the code.
What is a function call?
A function call is when a function is invoked to execute its code.
What is meant by 'return value' in functions?
A return value is the data that a function sends back to the place it was called from.
What is a tuple?
A tuple is an immutable sequence of values, similar to a list but cannot change values.
How do you define a function in Python?
Using the syntax: def
What is the difference between mutable and immutable data types?
Mutable data types can be changed after creation (like lists), while immutable data types cannot (like tuples).
What is the significance of parentheses in a function call?
Parentheses are required after the function name and may contain arguments.
How can you return multiple values from a function?
By returning a tuple, which can then be unpacked into multiple variables.
What does it mean to pass data to a function?
Passing data to a function means providing input values through arguments.
What happens if you try to change an item in a tuple?
You will receive a TypeError because tuples are immutable.
What is a function header?
The first line of a function definition that starts with 'def' and includes the function name and parameters.
What is the purpose of arguments in a function?
Arguments allow you to pass data into the function for it to use during execution.
What are built-in functions in Python?
Functions that are included by default in Python, such as print() and input().
What is the role of the return statement in a function?
The return statement ends the function execution and specifies the value to be returned.
What is the syntax to call a function with arguments?
What is meant by 'functions as a black box'?
Functions can be viewed as a black box that takes input, processes it, and returns output without needing to understand the internal workings.
What is the output of the function call sqrt(16)?
4, since it returns the square root of 16.
How can you define a tuple in Python?
Using parentheses, e.g., my_tuple = (1, 2, 3).
What happens when you try to assign a new value to a tuple element?
You will encounter a TypeError because tuples do not support item assignment.
What is the significance of the 'def' keyword?
'def' is used to define a function in Python.
What is the output of print(my_tuple[1:3]) given my_tuple = (1, 2, 3, 4)?
(2, 3), as it slices the tuple from index 1 to 2.
What is the difference between a list and a tuple in terms of mutability?
Lists are mutable (can change), while tuples are immutable (cannot change).
How do you unpack a tuple in Python?
By assigning the tuple to multiple variables, e.g., a, b = my_tuple.
What is an example of a function that does not take any arguments?
input() is an example of a function that can be called without arguments.
What is the output of len(my_tuple) if my_tuple = (1, 2, 3)?
3, since it counts the number of elements in the tuple.
What is the purpose of using commas in function arguments?
Commas separate multiple arguments when passing them to a function.
What is the general structure of a function definition in Python?
def
How many spaces are commonly used for indentation in Python?
4 spaces (equivalent to 1 tab)
What is the purpose of a docstring in a function?
To document what the function does, its purpose, inputs, and outputs.
How do you access a function's docstring?
Using the help command: help(
What is an example of a simple function that prints a warning?
def warn(): print(' WARNING!!! ')
What must you do before calling a function in Python?
You must define the function before calling it.
What is the output of the function call warn() if it is defined as follows: def warn(): print(' WARNING!!! ')?
WARNING!!!
What happens if you try to call a function that has not been defined?
A NameError will occur indicating that the function is not defined.
What is a common practice regarding function definitions in a program?
List all functions near the beginning of the program, before the main code.
What is the purpose of the main code in a program?
To execute function calls after defining them.
How can a function be called multiple times in a program?
By defining the function once and calling it as needed with different parameters.
What is the output when calling the function area_of_circle(radius) defined as: def area_of_circle(radius): return pi * radius ** 2?
It returns the area of a circle based on the given radius.
Can one function call another function in Python?
Yes, a function can call another function.
What happens when a function is called within another function?
The program executes the called function's body and then returns to the calling function.
What will happen if a function is defined after it is called?
A NameError will occur because the function is not defined at the time of the call.
What is the output of the following code? def doublewarn(): warn(); warn() and then calling doublewarn()?
It will print the warning message twice.
What is the significance of the return statement in a function?
It specifies the value that the function will output when called.
What does the interpreter do when it encounters a function definition?
It remembers the function's name and parameters but does not execute the body until the function is called.
What is the output of the following function call? def warn(): print('Warning!'); warn()?
Warning!
What is the result of calling a function that does not exist?
The program will complete without error if the call is not reached.
What is the purpose of the print statement within a function?
To output information to the console when the function is called.
What is the correct syntax for defining a function with parameters?
def
What is a common error when calling functions in Python?
Calling a function before it has been defined.
What is the purpose of the import statement in a Python program?
To include external modules or libraries that provide additional functionality.
What does the function 'warn()' do?
It prints a warning message to the console.
What happens when 'doublewarn()' is called?
'doublewarn()' calls a nonexistent function, but it is never executed, so no error occurs.
What is the scope of a variable?
The scope of a variable refers to the regions of the program where that variable is valid and can be accessed.
What is a local variable?
A local variable is defined within a function and is not accessible outside of that function.
What is a global variable?
A global variable is defined throughout the program and can be accessed in any function unless shadowed by a local variable.
What happens to the variable 'a' in the main program when 'my_function()' is called?
'my_function()' creates a new variable 'a' in its own memory, which is separate from the main program's 'a'.
What is the output of 'print(a)' after calling 'my_function()' if 'a' is set to 5 in the main program?
The output will be 5, as the function's 'a' does not affect the main program's 'a'.
What does the 'my_function(x)' do when called with an argument?
It creates a new variable 'x' in the function's memory and prints its value.
What happens to the variable 'x' after the function 'my_function(x)' completes?
The variable 'x' is released from memory after the function finishes.
What is the correct way to access data from a function?
By passing data through arguments/parameters in the function call.
What does 'x += 1' do in the context of 'my_function(x)'?
It increments the value of 'x' by 1 within the function's local scope.
What will happen if a variable is referenced in a function without being defined first?
An UnboundLocalError will occur, indicating that the variable was referenced before assignment.
What is the output of 'print(a)' after calling 'F(a)' if 'a' is set to 5 in the main program?
The output will be 5, as 'F(a)' does not modify the main program's 'a'.
What is the result of trying to modify a variable in the main program from within a function?
It is considered bad programming style and can lead to confusion; functions should be self-contained.
What does the function 'F(a)' do when called?
It prints the value of the parameter 'a' passed to it.
What happens when a local variable in a function has the same name as a global variable?
The function will use the local variable, and the global variable will be shadowed.
What is the output of 'print(a)' after calling 'F()' if 'a' is not defined in 'F()'?
An UnboundLocalError will occur because 'a' is referenced before assignment.
What is the purpose of parameters in a function definition?
Parameters allow the function to accept input values that can be used within the function.
What happens to the memory of a function after it completes execution?
The memory allocated for the function's local variables is released.
What is the output of 'print(x)' in 'my_function(x)' if 'x' is passed the value 5?
The output will be 6, as 'x' is incremented by 1 within the function.
What does the statement 'a += 3' do in the context of a function?
It attempts to increase the value of 'a' by 3, but will raise an error if 'a' is not defined locally.
What is the correct way to define a function that takes two parameters?
Use the syntax 'def my_function(param1, param2):'.
What is the output of 'print(a)' after calling 'F(a)' if 'a' is defined globally as 5?
The output will be 5, as the function does not modify the global variable.
What does 'my_function(3, "Joe")' do?
It calls 'my_function' with 3 and 'Joe' as arguments for its parameters.
What is the purpose of the 'global' keyword in a function?
It allows the function to access and modify a global variable.
What happens when a function uses the 'global' keyword?
The function can read and write to the global variable specified.
Why should global variables generally be avoided?
They make code harder to maintain and can introduce bugs.
What is the syntax to declare a global variable inside a function?
Use 'global
What does the 'return' statement do in a function?
It ends the function and returns a specified value.
What will the function 'thirty()' return?
It will return the value 30.
What does the function 'is_three_more(a, b)' return if a is 3 greater than b?
It returns True.
What is the output of 'print(is_three_more(10, 7))'?
True.
What happens to the lines of code after a 'return' statement in a function?
They are never executed because the function ends at 'return'.
How can default parameters be defined in a function?
By assigning values using the assignment operator '=' in the parameter list.
What will the function 'F(a="Bryan", b="Texas", c="USA")' print if called with no arguments?
It will print 'Bryan Texas USA'.
What error occurs if a function is called without required parameters?
A TypeError occurs, indicating missing required positional arguments.
What is the rule regarding default parameters in a function?
All parameters after a default parameter must also have default values.
What is the output of 'print(x)' after calling 'dosomething(x, y)' with x = 1 and y = 2?
It will print 1, as x is immutable.
How do lists behave differently than other data types when passed to functions?
Lists are mutable, so changes within the function affect the original list.
What is the output of 'print(x)' after calling 'dosomething(x, y)' with x = [1] and y = [2]?
It will print [1, 2], as the list x is modified.
What does the function 'F(a)' do if a is passed the value 4?
It increases a by 1 and returns 5.
What will happen if a function has both required and default parameters?
Required parameters must be specified, while default parameters can be omitted.
What is the significance of mutable vs immutable data types in Python?
Mutable types can be changed in place, while immutable types cannot.
What will the output be when 'F(
TypeError: F() missing 1 required positional argument: 'a'.
What type of data structure is a list in Python?
A mutable data type.
What are examples of immutable data types in Python?
Tuples and strings.
Can the elements of a list be modified?
Yes, the values of elements in a list can be changed.
Can a list be reassigned to a new value?
No, a list cannot be assigned a brand-new value.
What happens when you assign a new list to a parameter in a function?
It does not change the original list; it only changes the local reference.
What happens when you modify an element of a list inside a function?
It changes the corresponding element in the original list.