Recall & Review
beginner
What is local scope in Python?
Local scope refers to variables that are created inside a function and can only be used within that function.
Click to reveal answer
beginner
Where can a variable defined inside a function be accessed?
A variable defined inside a function can only be accessed inside that same function, not outside it.
Click to reveal answer
beginner
What happens if you try to use a local variable outside its function?
You get an error because the variable does not exist outside the function's local scope.
Click to reveal answer
beginner
Example: What will this code print?
def greet():
message = 'Hello!'
print(message)
greet()It will print: Hello! because 'message' is defined and used inside the function greet.
Click to reveal answer
beginner
Can a local variable be accessed by another function?
No, each function has its own local scope. Variables inside one function are not visible to others.
Click to reveal answer
Where is a local variable created?
✗ Incorrect
Local variables are created inside functions and exist only there.
What happens if you try to print a local variable outside its function?
✗ Incorrect
Local variables are not accessible outside their function, so trying to print them causes an error.
Which of these is true about local scope?
✗ Incorrect
Local variables exist only inside the function where they are defined.
If a variable is defined inside a function, can another function access it directly?
✗ Incorrect
Another function can access a local variable only if it is passed as an argument.
What keyword is used to access a global variable inside a function?
✗ Incorrect
The 'global' keyword allows a function to access or modify a global variable.
Explain what local scope means in Python and why it is important.
Think about where variables live and who can use them.
You got /4 concepts.
Describe what happens if you try to use a local variable outside its function.
Consider what Python does when it can't find a variable.
You got /3 concepts.