Course Home | Class Schedule | Assignments | Git Submission | Perusall | Python | Tutoring

Saint Louis University

Computer Science 1300/5001
Introduction to Object-Oriented Programming

Michael Goldwasser

Fall 2018

Computer Science Department

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
    41020.0
    10420.0
    357.5

    Answer:

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

    Some sample values:
    b1b2hresult
    461050.0
    610432.0
    061030.0

    Answer:

  3. A typical quadratic
    $8x^2 - 12x + 5$

    Some sample values:
    xresult
    05
    11
    -125

    Answer:

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

    Some sample values:
    abresult
    5316
    10484
    410-84

    Answer:

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

    Some sample values:
    xresult
    14.1888
    1.258.1812
    4268.0823

    Answer:

  6. An interesting way to approximate π with continued fractions (though need more terms of 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
    345.0
    12513.0
    535.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
    144-2.0
    14-40.8284
    414-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
    14016.0
    24032.0
    14218.4752
    129979245808.98755e+16
    12997924581e68.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
    3456.0
    1351230.0
    10101043.30

    Answer:


Michael Goldwasser
CSCI 1300/5001, Fall 2018
Last modified: Wednesday, 29 August 2018
Course Home | Class Schedule | Assignments | Git Submission | Perusall | Python | Tutoring