Python functions and variable scope exam review
This study set covers essential concepts of Python functions and variable scope, including definitions, examples, and common questions to help students prepare for exams.
Quiz(24 questions)
1. What is the purpose of a function in Python?
Terms in this Study Set(24)
Functions in Python(12)
What is a function in Python?
A function is a block of reusable code that performs a specific task. It is defined using the 'def' keyword.
What are parameters in a function?
Parameters are variables listed inside the parentheses in a function definition. They allow you to pass information into the function.
True or False: Functions can return multiple values.
True. Functions can return multiple values as a tuple, allowing the caller to receive several results at once.
Fill in the blank: Use the ______ keyword to define a function.
'def'
How do you call a function?
You call a function by using its name followed by parentheses, e.g., 'my_function()'.
What is the return value of a function?
The return value is the output that a function sends back to the caller using the 'return' statement.
Compare 'return' vs. 'print' in functions.
'return' sends a value back to the caller, while 'print' displays output to the console without returning anything.
Cause → Effect: What happens if you omit the return statement?
The function will return 'None' by default, meaning no value is sent back to the caller.
What is a default parameter?
A default parameter is a parameter that assumes a default value if no argument is passed during the function call.
How to define a function with default parameters?
Example: 'def greet(name='User'):' sets 'User' as the default value for 'name' if not specified.
What is the purpose of *args in a function?
'*args' allows a function to accept any number of positional arguments, packing them into a tuple.
Explain the use of **kwargs.
'**kwargs' allows a function to accept any number of keyword arguments, packing them into a dictionary.
Variable Scope(12)
What is a local variable?
A local variable is defined within a function and only accessible within that function's scope.
True or False: Global variables can be accessed anywhere in the code.
True. Global variables are defined outside any function and can be used throughout the entire module.
Define a global variable.
A global variable is declared outside of all functions and is available to all functions within the same module.
What does the nonlocal keyword do?
The nonlocal keyword allows you to assign a value to a variable in the nearest enclosing scope that is not global.
Local vs Global: What's the difference?
- Local: Accessible only within the function. - Global: Accessible throughout the entire module.
Fill in the blank: A __________ variable exists only within a specific block of code.
Local
What happens if you use a global variable in a function without declaring it?
Python treats it as a local variable unless declared with the 'global' keyword, leading to an UnboundLocalError.
Cause: Assigning a value to a variable inside a function.
Effect: The variable is treated as local unless explicitly marked as global.
True or False: Nonlocal variables can be used in nested functions.
True. Nonlocal variables help to access variables from the nearest enclosing scope, excluding globals.
What is the scope of a variable defined inside a loop?
The variable has a local scope, accessible only within the loop unless declared otherwise.
Demonstrate local variable scope with an example.
def example(): x = 5 example() print(x) # Raises NameError: name 'x' is not defined.
Global variable example.
x = 10 def display(): print(x) display() # Outputs: 10
Questions in this Study Set(24)
1. What is the purpose of a function in Python?
2. What is the primary characteristic of a local variable?
3. Which of the following correctly defines a function named 'calculate'?
4. True or False: A global variable can be modified within a function without declaring it as global.
5. What happens if you do not provide a parameter when calling a function with default parameters?
6. What is the effect of using the nonlocal keyword in a nested function?
7. Which of the following is NOT a way to return multiple values from a function?
8. Which of the following variables is scoped locally?
9. What does the *args parameter allow in a function definition?
10. What happens if you refer to a variable before it is defined in a function?
11. In what scenario is it better to use **kwargs instead of individual parameters?
12. Identify the variable scope of a variable defined inside a for loop.
13. Which statement correctly calls a function named 'display_message'?
14. Which of the following is NOT true about global variables?
15. True or False: A function can have the same name as a built-in function.
16. Fill in the blank: A __________ variable is accessible in all functions throughout a module.
17. What is the effect of using the return statement in a function?
18. What occurs when a local variable is assigned a value within a function?
19. What is a key difference between 'print' and 'return' in a function?
20. True or False: A variable declared outside any function has local scope.
21. How does Python handle a function that does not include a return statement?
22. Which keyword is used to define a variable in the nearest enclosing scope that is not global?
23. What is the role of parameters in a function?
24. Consider the following code snippet: def func(): x = 10; return x. What is the scope of x?
Related Study Sets
Abitur Rekursion
Abitur: Abitur Klassen und Objekte
Was ist ein Algorithmus Schritt für Schritt
if und Schleifen Notizen
Wiederholung: Funktionen
Test: Binärzahlen
Listen Notizen
Schleife Alltag Beispiel Begriffe
Create Your Own Study Set
Upload a PDF, paste your notes, or describe a topic – AI generates flashcards, quizzes and more in seconds.

