Question #181891

Create your own Python code examples that demonstrate each of the following. Do not copy examples from the book or any other source. Try to be creative with your examples to demonstrate that you invented them yourself.



  • Example 5: Show what happens when a variable defined outside a function has the same name as a local variable inside a function. Explain what happens to the value of each variable as the program runs.

184 words


Expert's answer

def test():
    example_variable = "local value"
    print(example_variable)
example_variable = "global value"
"""
result of the operation print(example_variable)
In this case, the variable has a global scope will be displayed:
global value
"""
print(example_variable)
"""
test() function call result
A local variable is defined in the body of the function :
example_variable
execution print(example_variable) inside a function
In this case, the variable has a local scope and the local value 
of the function will be displayed:
local value

"""
test()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS