Hands-on Day

Expressions


Note: All of these problems are for practice. You are freely encouraged to work together and you do not need to submit any of this work for a grade.


To further understanding of Python expressions and the precedence of operators, you will explore two styles of questions:

When we use variables within the expressions, you can assume that they are well-defined in the Python workspace at the time of evaluation.


Evaluation Trees

  1.    n / j + k 

    Answer:

  2.    a * b - c + d 

    Answer:

  3.    (a + b) / c*d

    Answer:

  4.    k = 3 * (a + b / c)

    Answer:

  5.    k = 3 * (a5 + b42 / c1)

    Answer:

  6.    a + b - c * d / e ** f

    Answer:

  7.    3 + 4 == 7 and 1 + 1 != 4

    Answer:

  8.    A and not B or C and D

    Answer:

  9.    A or not B and C or D

    Answer:

  10.    not A or B and C or not D

    Answer:


Crafting Python Expressions

Note: although we have not (yet) introduced Python's sqrt function for calculating square roots, note that the square root is equivalent to raising an expression go the power 0.5.

For each of these, you can test the general formula by first making assignments to the variables involved, and then evaluating the general expression.

  1. Area of a triangle
    \(\frac{1}{2}bh\)

    Some sample values:

    bhresult
    4 10 20.0
    10 4 20.0
    3 5 7.5

    Answer:

  2. Area of a trapezoid with bases b1 and b2
    \(\frac{1}{2}h(b1+b2)\)

    Some sample values:

    b1b2hresult
    4 6 10 50.0
    6 10 4 32.0
    0 6 10 30.0

    Answer:

  3. A typical quadratic
    \(8x^2 - 12x + 5\)

    Some sample values:

    xresult
    0 5
    1 1
    -1 25

    Answer:

  4. A typical product of terms
    \((a+b)(a-b)\)

    Some sample values:

    abresult
    5 3 16
    10 4 84
    4 10 -84

    Answer:

  5. Volume of a sphere:
    \(\frac{4}{3} \pi r^3\)

    Some sample values:

    xresult
    1 4.1888
    1.25 8.1812
    4 268.0823

    Answer:

  6. An interesting way to approximate π with continued fractions (though need more terms for better approximation)
    \(3 + \cfrac{1^2}{6 + \cfrac{3^2}{6 + \cfrac{5^2}{6}}}\)

    For this finite fraction, the result should be 3.145238 (which is slightly bigger than π. If you want to extend the pattern with one more level of denominator, you're approximation will go below π at 3.13968. Add yet another level, the result will be 3.143206.

    Answer:

  7. The formula to calculate the hypotenuse of a right triangle:
    \(\sqrt{a^2 + b^2}\)

    Some sample values:

    abresult
    3 4 5.0
    12 5 13.0
    5 3 5.83

    Answer:

  8. The formula to calculate one of the roots of a quadratic equation:
    \(\frac{-b + \sqrt{b^2 - 4ac}}{2a}\)

    Some sample values:

    abcresult
    1 4 4 -2.0
    1 4 -4 0.8284
    4 1 4 -0.125 + 0.992j

    (Yes, Python has support for complex numbers!)

    Answer:

  9. A more precise formula for energy (rather than \(E=mc^2\)), based on Einstein's general relativity:
    \(\frac{mc^2}{\sqrt{1 - \frac{v^2}{c^2}}}\)

    Some sample values:

    mcvresult
    1 4 0 16.0
    2 4 0 32.0
    1 4 2 18.4752
    1 299792458 0 8.98755e+16
    1 299792458 1e6 8.9876+16

    Answer:

  10. This one isn't a self-contained formula, but a great multistep way to compute the area of a triangle with side lengths a, b, and c, known as Heron's formula. First computer the intermediate value
    \(s = \frac{a + b + c}{2}\)
    and then the area is computed as
    \(\sqrt{s(s-a)(s-b)(s-c)}\)

    Some sample values:

    abcresult
    3 4 5 6.0
    13 5 12 30.0
    10 10 10 43.30

    Answer:


Last modified: Sunday, 22 December 2019