Bird
Raised Fist0
Pythonprogramming~20 mins

Built-in scope in Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
๐ŸŽ–๏ธ
Built-in Scope Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of shadowing a built-in function
What is the output of this code?
len = 5
print(len([1, 2, 3]))
Python
len = 5
print(len([1, 2, 3]))
ANameError
B3
CTypeError
D5
Attempts:
2 left
๐Ÿ’ก Hint
Think about what happens when you assign a number to the name 'len'.
โ“ Predict Output
intermediate
2:00remaining
Using built-in function after local shadowing
What is the output of this code?
def f():
    sum = 10
    return sum([1, 2, 3])

print(f())
Python
def f():
    sum = 10
    return sum([1, 2, 3])

print(f())
ATypeError
BNameError
C6
D10
Attempts:
2 left
๐Ÿ’ก Hint
Inside the function, what does 'sum' refer to?
๐Ÿ”ง Debug
advanced
2:00remaining
Why does this code raise an error?
This code raises an error. What is the cause?
min = 5
numbers = [3, 1, 4]
print(min(numbers))
Python
min = 5
numbers = [3, 1, 4]
print(min(numbers))
Amin is an integer, so calling min(numbers) causes TypeError
Bmin is not defined, causing NameError
Cnumbers is not iterable, causing TypeError
DSyntaxError due to missing colon
Attempts:
2 left
๐Ÿ’ก Hint
Check what 'min' refers to after assignment.
โ“ Predict Output
advanced
2:00remaining
Output when using built-in after deleting local shadow
What is the output of this code?
max = 10
print(max)
del max
print(max([1, 5, 3]))
Python
max = 10
print(max)
del max
print(max([1, 5, 3]))
ATypeError\n5
B10\n5
CTypeError\nTypeError
D10\nTypeError
Attempts:
2 left
๐Ÿ’ก Hint
What happens after deleting the local variable 'max'?
โ“ Predict Output
expert
2:00remaining
Output of nested function with built-in shadowing
What is the output of this code?
def outer():
    list = "not a list"
    def inner():
        return list([1, 2, 3])
    return inner()

print(outer())
Python
def outer():
    list = "not a list"
    def inner():
        return list([1, 2, 3])
    return inner()

print(outer())
A[1, 2, 3]
BSyntaxError
CNameError
DTypeError
Attempts:
2 left
๐Ÿ’ก Hint
What does 'list' refer to inside inner()? Is it a function?

Practice

(1/5)
1. Which of the following best describes the built-in scope in Python?
easy
A. It is where user-defined variables are stored.
B. It is the first place Python looks for variable names.
C. It contains names of functions and variables Python always knows.
D. It only contains names defined inside functions.

Solution

  1. Step 1: Understand what built-in scope means

    Built-in scope contains names of functions and constants Python always knows, like print and len.
  2. Step 2: Identify where Python looks last

    Python looks in local, then global, then built-in scope last when searching for a name.
  3. Final Answer:

    It contains names of functions and variables Python always knows. -> Option C
  4. Quick Check:

    Built-in scope = Python's always known names [OK]
Hint: Built-in scope has Python's default functions and constants [OK]
Common Mistakes:
  • Thinking built-in scope is searched first
  • Confusing built-in scope with local or global scope
  • Assuming user variables are in built-in scope
2. Which of the following is the correct way to use a built-in function without causing a naming conflict?
easy
A. print('Hello, world!')
B. def print(): pass
C. len = 5
D. input = 'data'

Solution

  1. Step 1: Identify correct usage of built-in functions

    Using print('Hello, world!') calls the built-in print function correctly.
  2. Step 2: Recognize naming conflicts

    Defining print, len, or input as variables or functions overwrites built-ins and causes conflicts.
  3. Final Answer:

    print('Hello, world!') -> Option A
  4. Quick Check:

    Use built-ins directly without redefining [OK]
Hint: Avoid naming variables same as built-ins to prevent errors [OK]
Common Mistakes:
  • Redefining built-in function names as variables or functions
  • Calling built-ins after overwriting them
  • Assuming built-ins can be safely replaced
3. What will be the output of the following code?
len = 10
print(len('hello'))
medium
A. 5
B. 10
C. NameError
D. TypeError

Solution

  1. Step 1: Understand variable shadowing

    The variable len is assigned the integer 10, which hides the built-in len function.
  2. Step 2: Analyze the function call

    Calling len('hello') tries to call the integer 10 as a function, causing a TypeError.
  3. Final Answer:

    TypeError -> Option D
  4. Quick Check:

    Overwriting built-in function causes TypeError when called [OK]
Hint: Overwriting built-ins causes errors when called as functions [OK]
Common Mistakes:
  • Expecting output 5 (length of 'hello')
  • Confusing variable value with function behavior
  • Ignoring that built-in is overwritten
4. Find the error in this code snippet:
def input():
    return 'user input'

print(input())
print(len('abc'))
medium
A. SyntaxError due to function definition
B. No error, prints 'user input' and 3
C. Error because len is undefined
D. Error because input is redefined, blocking built-in input

Solution

  1. Step 1: Check function definition and shadowing

    Defining input() shadows the built-in input, but the code calls the custom function.
  2. Step 2: Verify execution flow

    print(input()) prints 'user input'; print(len('abc')) uses built-in len (unaffected) and prints 3. No errors occur.
  3. Final Answer:

    No error, prints 'user input' and 3 -> Option B
  4. Quick Check:

    Code executes fine despite shadowing [OK]
Hint: Avoid redefining built-in names to prevent unexpected behavior [OK]
Common Mistakes:
  • Thinking redefining input causes immediate runtime error
  • Assuming len is undefined or affected
  • Confusing bad practice with actual syntax or runtime error
5. You want to use the built-in max function inside a function that has a local variable named max. How can you still call the built-in max function?
hard
A. Use __builtins__.max() to call the built-in function.
B. Rename the local variable to max_builtin.
C. Call max() directly; Python will use the built-in automatically.
D. Use global max inside the function.

Solution

  1. Step 1: Understand local variable shadowing

    A local variable named max hides the built-in max function inside the function scope.
  2. Step 2: Access built-in function explicitly

    Using __builtins__.max() calls the original built-in max function despite the local variable.
  3. Final Answer:

    Use __builtins__.max() to call the built-in function. -> Option A
  4. Quick Check:

    Use __builtins__ to access hidden built-ins [OK]
Hint: Use __builtins__.function() to bypass local shadowing [OK]
Common Mistakes:
  • Assuming calling max() calls built-in despite local variable
  • Using global keyword incorrectly
  • Renaming variable without changing code