title: Python changing variables vs arrays in functions?
body: <p>I am a bit confused about, changing (mutating or appending) variables from within functions.<br />
  For reference, i found this question about <a href="https://stackoverflow.com/questions/26027694/correct-style-for-python-functions-that-mutate-the-argument">functions that mutate the argument</a>.  which describes doing just that on an array:</p>
  <pre><code>def add_thing(a):
      a.append(2)
  
  my_a = [1]
  
  append_two(my_a)
  my_a
  &gt;&gt;&gt; [1,2]
  </code></pre>
  <p>and we get the same results using a += [2]</p>
  <p>However, if we try the same thing with a string, or an integer:</p>
  <pre><code>def add_world (s):
      s += &quot; world&quot;
  
  my_str = &quot;hello&quot;
  
  add_world(my_str)
  my_str
  &gt;&gt;&gt; &quot;hello&quot;
  </code></pre>
  <p>It doesn't change, and the same goes for integers, like:</p>
  <pre><code>def add_one(i):
      i += 1
  
  x = 1
  add_one(x)
  x
  &gt;&gt;&gt; 1
  </code></pre>
  <p>My question is:</p>
  <ol>
  <li><p>How should I recognize which objects I can mutate in a function like an array, and which ones I need to directly assign?</p>
  </li>
  <li><p>why is the += operator not working as I'd expect?  I was fairly sure that it was short hand for my_var = my_var + thing, which aught to work just fine within a function.</p>
  </li>
  </ol>