How to write a function with a list as parameters
<p>Here is the question, I'm  trying to deï¬ne a function <code>sample_mean</code> that takes in a list of numbers as a parameter and returns the sample mean of the the numbers in that list. Here is what I have so far, but I'm not sure it is totally right. </p>

<pre><code>def sample_mean(list):
    """ (list) -&gt; number
     takes in a list of numbers as a parameter and returns the sample mean of the the numbers in that list
     sample_mean =
     sample_mean =
    """

    mean = 0
    values = [list]
    for list in values:
        print('The sample mean of', values, 'is', mean(list))
</code></pre>

<p>Firstly, don't use <code>list</code> as a name because it shadows/hides the builtin <a href="https://docs.python.org/3/library/functions.html#func-list" rel="nofollow"><code>list</code></a> class for the scope in which it is declared. Use a name that describes the values in the list, in this case <code>samples</code> might be a good name. The function could be implemented with something like this:</p>

<pre><code>def sample_mean(samples):
    total = 0
    for value in samples:
        total = total + value
    return total / float(len(samples))
</code></pre>

<p>Or a shorter version which avoids writing your own loop by making use of Python's <code>sum()</code> function :</p>

<pre><code>def sample_mean(samples):
    return sum(samples) / float(len(samples))
</code></pre>

<p>Call the function like this:</p>

<pre><code>&gt;&gt;&gt; print(sample_mean([1,2,3,4,5]))
3.0
</code></pre>

<p>Note the use of <code>float()</code> to ensure that the division operation does not lose the fractional part. This is only an issue in Python 2 which uses integer division by default. Alternatively you could add this to the top of your script:</p>

<pre><code>from __future__ import division
</code></pre>

<p>If you are sure that you only need to support Python 3 you can remove the <code>float()</code> and ignore the above.</p>

python