-4
def f():
    global s
    print s
    s = "That's clear."
    print s     

s = "Python is great!" 
f()
print s

Output:

Python is great!
That's clear.
That's clear.

as per program, the last print statement should return "s= Python is great" because I think here S should be referred Global variable.

Kevyn Meganck
  • 609
  • 6
  • 15

2 Answers2

2

You modified the global variable in your function (f) so the variable now has the value that you modified at last i.e. "That's clear."

Nimish Bansal
  • 1,719
  • 4
  • 20
  • 37
0

The output is correct. After s is defined as a global variable, you print the value assigned to s at that point which is Python is great, then you assign That's clear. to s - s is now global, so when you assign That's clear. to it inside your function, that becomes it's value in the outer scope as well.

Here is a good stackoverflow answer regarding Scoping Rules.

flevinkelming
  • 690
  • 7
  • 8