164k views
1 vote
What is the output?

x = 5
def f1():
-> x = 7
-> print(x)
f1()
print(x)

1 Answer

6 votes

Final answer:

The code prints the number 7, which is the local variable inside the function f1, and then prints the number 5, which is the global variable x.

Step-by-step explanation:

The student's question is related to the output of a segment of Python code that includes a global variable x and a function f1 that creates a local variable also named x. When the function f1 is called, it defines a new local variable x with the value of 7 and then prints it. After the function call, the original global variable x remains unchanged and is printed as well. Therefore, the output will be two lines: the first line with the number 7 (from the function print) and the second line with the number 5 (from the global print).

The output is:

  • 7
  • 5
User MarkWalczak
by
8.9k points