|
| 1 | +Statistics |
| 2 | +========== |
| 3 | + |
| 4 | +Writing all instructions into a single sequence creates programs that |
| 5 | +are hard to debug. Here, you learn to break the code down into smaller |
| 6 | +units: **functions**. |
| 7 | + |
| 8 | +In this chapter you learn: |
| 9 | +-------------------------- |
| 10 | + |
| 11 | +==== ============================================== |
| 12 | +area topic |
| 13 | +==== ============================================== |
| 14 | +🚀 calculate statistics |
| 15 | +⚙ implement a function |
| 16 | +⚙ call a function you defined |
| 17 | +💡 use the ``math`` module |
| 18 | +🔀 use a recursive function |
| 19 | +==== ============================================== |
| 20 | + |
| 21 | + |
| 22 | +Exercise 1: Sum up |
| 23 | +------------------ |
| 24 | + |
| 25 | +Write a function that calculates a sum from a list of numers. Insert |
| 26 | +into the gaps: ``numbers``, ``data``, ``def``, ``return``, |
| 27 | +``calc_sum',``\ +=\` |
| 28 | + |
| 29 | +.. code:: python3 |
| 30 | +
|
| 31 | + ____ calc_sum(data): |
| 32 | + total = 0 |
| 33 | + for n in ____: |
| 34 | + total ____ n |
| 35 | + ____ total |
| 36 | +
|
| 37 | + numbers = [12562, 2178, 342, 129, 384, 208, 164, 82, 41] |
| 38 | + s = ____(____) |
| 39 | + print(s) |
| 40 | +
|
| 41 | +
|
| 42 | +Exercise 2: Mean |
| 43 | +---------------- |
| 44 | + |
| 45 | +Write a function that calculates the arithmetic mean from the following numbers: |
| 46 | + |
| 47 | +.. code:: python3 |
| 48 | +
|
| 49 | + def mean(data): |
| 50 | + ... |
| 51 | +
|
| 52 | + numbers = [12562, 2178, 342, 129, 384, 208, 164, 82, 41] |
| 53 | +
|
| 54 | + ... |
| 55 | +
|
| 56 | +Don't forget about the ``return`` statement. |
| 57 | + |
| 58 | + |
| 59 | +Exercise 3: Standard Deviation |
| 60 | +------------------------------ |
| 61 | + |
| 62 | +The following program calculates the standard deviation from a list of |
| 63 | +numbers. You would like to generalize the code, so that it can be used |
| 64 | +with other data sets. Wrap the code for the calculation – but not the |
| 65 | +data – in a function. |
| 66 | + |
| 67 | +.. code:: python3 |
| 68 | +
|
| 69 | + import math |
| 70 | +
|
| 71 | + data = [12562, 2178, 342, 129, 384, 208, 164, 82, 41] |
| 72 | +
|
| 73 | + avg = mean(data) |
| 74 | +
|
| 75 | + stdsum = 0.0 |
| 76 | + for n in data: |
| 77 | + stdsum += (n - avg) ** 2 |
| 78 | + variance = stdsum / len(data) |
| 79 | + stdev = math.sqrt(variance) |
| 80 | +
|
| 81 | + print(f"Standard Deviation: {stdev:8.2f}") |
| 82 | +
|
| 83 | +
|
| 84 | +Exercise 4: Optional Parameters |
| 85 | +------------------------------- |
| 86 | + |
| 87 | +Explain the program: |
| 88 | + |
| 89 | +.. code:: python3 |
| 90 | +
|
| 91 | + def add(a=2, b=2): |
| 92 | + return a + b |
| 93 | +
|
| 94 | + print(add(3, 3)) |
| 95 | + print(add(3)) |
| 96 | + print(add()) |
| 97 | + print(add(b=4)) |
| 98 | +
|
| 99 | +
|
| 100 | +Exercise 5: Recursion |
| 101 | +--------------------- |
| 102 | + |
| 103 | +Explain the code: |
| 104 | + |
| 105 | +.. code:: python3 |
| 106 | +
|
| 107 | + def factorial(n): |
| 108 | + """Calculates the factorial of the given number.""" |
| 109 | + if n > 1: |
| 110 | + return n * factorial(n - 1) |
| 111 | + else: |
| 112 | + return 1 |
| 113 | +
|
| 114 | +
|
| 115 | + x = int(input('Please enter a number: ')) |
| 116 | + y = factorial(x) |
| 117 | + print (f"The result is:\n{x}! = {y}}") |
| 118 | +
|
| 119 | +
|
| 120 | +Reflection Questions |
| 121 | +-------------------- |
| 122 | + |
| 123 | +- Why is it useful to write functions? |
| 124 | +- What do you need to write in a function definition? |
| 125 | +- How do you call a function? |
| 126 | +- What does the ``return`` statement do? |
0 commit comments