title: What are the differences (if exist) in variable scope between Python and C++?
body: <p>I'm a bit confused with the variable scope of Python. Perhaps because of being used to the convention of C++, I always made some mistakes in variable scope of Python. For example:</p>
  
  <p>in C++:  </p>
  
  <pre><code>int main(){
      int i = 3;
      for (int j = 0; j &lt;= 3; ++j){
          i += 1;
      }
      cout &lt;&lt; "i = " &lt;&lt; i &lt;&lt; endl;
      cout &lt;&lt; "j = " &lt;&lt; j &lt;&lt; endl; //Error, out of 'for' scoping.
      return 0;
  }
  </code></pre>
  
  <p>But in Python:  </p>
  
  <pre><code>i = 3
  for j in range(1,4):
      i += 1
  print j               # j = 3, nothing wrong
  for i in range(5,7):
      j += 1
  print i               # i = 6, changed by operation in for loop
  </code></pre>
  
  <p>This is just a simple example, and I'm not going to list other differences. Could anyone please give a detailed explanation of their differences in scoping.</p>