Python crash course 8-10
<p>I'm currently reading Python Crash Course by Eric Matthes and doing some of the problem sets. One of them is giving me some difficulty and I was hoping someone could help me with it. </p>

<blockquote>
  <p>8-9. Magicians: Make a list of magician's names. Pass the list to a function called show_magicians(), which prints the name of each magician in the list.</p>
  
  <p>8-10. Great Magicians: Start with a copy of your program from exercise 8-9. Write a function make_great() that modifies the list of magicians by adding the phrase <em>the great</em> to each magician's name. Call show_magicians() to see that the list has actually been modified. </p>
</blockquote>

<p>And this is what I have for 8-9:</p>

<pre><code>magician_names=['Alice', 'Alex', 'Blake']
def show_magicians(n):
    for magician in n:
        print (magician)

show_magicians(magician_names)
</code></pre>

<p>I just don't really know what to do for 8-10</p>

<p>To do this, you can append to a string and reassign to add words to the end of each element:</p>

<pre><code>magician += " the great"
</code></pre>

<p>Here, the <code>+=</code> operator appends a string, then reassigns, which is equivalent to the following:</p>

<pre><code>magician = magician + " the great"    
</code></pre>

<p>Now, you can add this to a function like so:</p>

<pre><code>def make_great(list_magicians):
    for i in range(len(list_magicians)):
        list_magicians[i] += " the great"

make_great(magician_names)
show_magicians(magician_names)
</code></pre>

<p>The output is:</p>

<pre><code>Alice the great
Alex the great
Blake the great
</code></pre>

<p>How this works is that we use a 'counter' for loop, similar to the ones in languages like C, (you can also use <code>enumerate</code> here), that loops through each element by using subscripts. Then it appends the string " the great" to all elements.</p>

<hr>

<p>The reason why you cannot just do a simple <code>for-in</code> loop and modify those values is because <code>for-in</code> copies the value in the variable before <code>in</code>:</p>

<pre><code>for magician in list_magicians: # magician is a temporary variable, does not hold actual reference to real elements
</code></pre>

<p>If they don't hold an actual reference, you cannot modify the reference, hence the reason you need to use counter loops to access the actual reference through subscript.</p>

python