0
0
Pythonprogramming~5 mins

Local scope in Python - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AOutside any function
BIn the global space
CInside a function
DIn a class
What happens if you try to print a local variable outside its function?
AIt causes an error
BIt prints None
CIt prints the value
DIt prints the variable name
Which of these is true about local scope?
ALocal variables can be used anywhere in the program
BLocal variables are shared between functions
CLocal variables are global
DLocal variables exist only inside their function
If a variable is defined inside a function, can another function access it directly?
AYes, always
BOnly if passed as an argument
CNo, never
DOnly if declared global
What keyword is used to access a global variable inside a function?
Aglobal
Blocal
Cscope
Ddef
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.