0
0
Pythonprogramming~5 mins

Built-in scope in Python - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the built-in scope in Python?
The built-in scope is the area where Python's built-in functions and names like <code>print()</code>, <code>len()</code>, and <code>int()</code> live. These are always available anywhere in your code without needing to import or define them.
Click to reveal answer
beginner
Where does Python look first when you use a name in your code?
Python first looks in the local scope (inside the current function or block), then in the enclosing scopes (like outer functions), then in the global scope (the main script), and finally in the built-in scope if it doesn't find the name anywhere else.
Click to reveal answer
intermediate
Can you override a built-in function name in your code? What happens?
Yes, you can create a variable or function with the same name as a built-in. This will hide the built-in inside your current scope, so when you use that name, Python uses your version instead of the built-in. This can cause confusion, so it's best to avoid it.
Click to reveal answer
beginner
Example: What will this code print?
print(len([1, 2, 3]))
It will print 3 because len() is a built-in function that returns the number of items in a list.
Click to reveal answer
intermediate
How can you see all the names in the built-in scope?
You can import the <code>builtins</code> module and use <code>dir(builtins)</code> to list all built-in names available in Python.
Click to reveal answer
Which scope does Python check LAST when looking for a variable name?
ALocal scope
BGlobal scope
CBuilt-in scope
DEnclosing scope
What happens if you define a variable named print in your code?
AYour variable hides the built-in print function in that scope.
BThe built-in print function is still used.
CPython throws an error.
DThe built-in print function is deleted.
How can you list all built-in functions and names in Python?
AUse <code>dir(builtins)</code> after importing <code>builtins</code>.
BUse <code>dir()</code> with no arguments.
CUse <code>help()</code>.
DUse <code>list_builtins()</code>.
Which of these is NOT a built-in function in Python?
Alen()
Bprint()
Cinput()
Dmy_function()
If a name is not found in local, enclosing, or global scopes, what does Python do?
ARaises a syntax error.
BLooks in the built-in scope.
CCreates a new variable.
DIgnores the name.
Explain the order Python uses to find a variable name when you use it in your code.
Think about the LEGB rule.
You got /5 concepts.
    Describe what happens if you create a variable with the same name as a built-in function.
    Consider how Python decides which name to use.
    You got /4 concepts.