Skip to main content
Engineering LibreTexts

5.1: Mathematical Operations

  • Page ID
    39362
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    First, the easy part. Python has a number of built-in operators to do the familiar math stuff. Figure 5.1.1 has a table of the ones we’ll use. A few are mildly surprising (* instead of X for multiplication; / instead of ÷ for division, which I’ll bet you couldn’t find on your keyboard anyway), and you have to remember to use only bananas (not boxies [], curlies {}, or wakkas <>) for grouping sub-expressions within a larger expression. Otherwise, it’s a piece of cake.

    clipboard_e5d0f1363d04f6d8e672d18c73ed1efe2.png

    Figure \(\PageIndex{1}\): Python’s basic math operators.

    All this stuff has to appear on the right-hand side of an equals sign, by the way, never on the left. That may seem surprising, since in mathematics the equations “x = y + 3” and “y + 3 = x” mean the same thing. Why does it matter which order you write it in? The answer, you’ll recall, is that in a program the symbol “=” doesn’t mean “is equal to” but rather “make equal to.” It’s not an equation; it’s a command. And you can’t command “y + 3” to be equal to anything. Therefore the only thing permitted on the left-hand side of an equals sign is a single, plain-jane variable name.

    To test your understanding of the syntax, see if you agree that the following math expression:

    \[\mathrm{gpa}=\frac{\mathrm{creds_{1}\cdot gpts_{1}+creds_{2}\cdot gpts_{2}}}{\mathrm{creds_{1}+creds_{2}}}\nonumber\]

    should look like this in Python:

    Code \(\PageIndex{1}\) (Python):

    gpa = (creds1 * gpts1 + creds2 * gpts2) / (creds1 + creds2)

    and that this one:

    \[a=\frac{[x^{2}y(4-z)+(x+q).y]\times 2^{15y+2z}}{19x^{3}-(yz)^{(y-1)^{2}}}\nonumber\]

    should look like this:

    Code \(\PageIndex{2}\) (Python):

    a = (((x**2)*y*(4-z) + (x+q)*y) * 2**(15*y+2*z)) / (19*(x**3) - (y*z)**((y-1)**2))

    If so, you’re good to go. It’s tedious, but not complicated.

    Python also has plenty of functions for absolute value, sine and cosine, logarithms, square roots, and anything else you can think of. We’ll learn all those at the proper time (or they’re all eminently Google-able if you want to look them up now).

    A Common Pattern: Cumulative Totals

    Here’s a technique we’ll use over and over in our code, but which can seem a bit jarring the first time you see it. Check out this line of code:

    Code \(\PageIndex{3}\) (Python):

    balance = balance + 50

    Now there is no universe where that statement is true mathematically. (Think about it: can you come up with any number that is equal to itself plus fifty? I thought not.) But again, this is programming, not algebra. We’re commanding the balance variable to have a new value. And what is that new value? Simple: whatever its previous value was, plus 50.

    The net effect is to increase balance’s value by 50. Follow this:

    Code \(\PageIndex{4}\) (Python):

    balance = 1000

    print("In July, I had ${}.".format(balance))

    balance = balance + 50

    print("In August, I had ${}.".format(balance))

    balance = balance - 200

    balance = balance + 120

    print("In September, I had ${}.".format(balance))

    | In July, I had $1000.

    | In August, I had $1050.

    | In September, I had $970.

    You get the idea. This approach will become especially useful when we get to loops in Chapter 14, because we’ll be able to repeatedly increment a variable’s value by a desired amount in automated fashion.

    A couple other things. First, a very common special case of the above is to increment a variable by exactly one:

    Code \(\PageIndex{5}\) (Python):

    number_of_home_runs = number_of_home_runs + 1

    This allows us to count the occurrences of various things: every time somebody hits a home run (or whatever), the above line of code will increase the appropriate counter variable’s value by one.

    Second, Python has a special alternative syntax for this incrementing operation. It looks weird:

    Code \(\PageIndex{6}\) (Python):

    balance += 50

    number_of_home_runs += 1

    The two characters “+” and “=” (pronounced “plus-equals”) allow us to shorthand this operation and avoid typing the variable name twice. The above two lines of code are exact synonyms for these:

    Code \(\PageIndex{7}\) (Python):

    balance = balance + 50

    number_of_home_runs = number_of_home_runs + 1

    You can use whichever one you wish, although be aware that your fellow programmers may well choose the former one, so you need to understand what it means.


    This page titled 5.1: Mathematical Operations is shared under a CC BY-SA 4.0 license and was authored, remixed, and/or curated by Stephen Davies (allthemath.org) via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.

    • Was this article helpful?