global and local variables misconception
<p>I have a question regarding the output from the following code:</p>

<pre><code>def f():

     global s
     print(s)
     s = "That's clear."
     print(s)

s = "Python is great!"
f()
print(s)
</code></pre>

<p>The output is this:</p>

<blockquote>
  <p>Python is great!</p>
  
  <p>That's clear.</p>
  
  <p>That's clear.</p>
</blockquote>

<p>My question is:  Why is it the very last output (i.e. the third output) is also "That's clear" .<br>
How come the third output is not "Python is great!" . 
I thought the very last statement from the code (i.e. the print(s)) statement is outside the function f(). So shouldn't print(s) here looks at the s variable that is defined globally? in this case the globally defined variable s would refers to the value "Python is great!", isn't it?  Sorry there must be some concepts I have mis-understand. I am very newbie to python. Could someone kindly explain this simple concept. </p>

<p>Assuming you have declared the variable <code>s = "Python is great!"</code> globally.
You may not have indented the code properly, the below code:</p>

<pre><code>def f():
     global s
     print(s)
     s = "That's clear."
     print(s)

     s = "Python is great!"
f()
print(s)
</code></pre>

<p>Will give you the output</p>

<blockquote>
  <p>Python is great!</p>
  
  <p>That's clear.</p>
  
  <p>Python is great!</p>
</blockquote>

<p>The code you have written should essentially be giving an infinite recursion and should reach the max recursion depth.</p>

<p>Indentation is very important in python, when you indent the function call <code>f()</code> and the statement <code>print(s)</code>, python considers both of these statements to be a part of the function <code>f()</code>. When the statement <code>f()</code> is reached during the first function call, python will automatically call the function again and repeats the whole process. In reality, you will never be able to reach the third print statement.</p>

python global-variables