# Variables and Values

## Contents

Printed copies of Elements of Data Science are available now, with a full color interior, from Lulu.com.

# Variables and Values#

The topics in this chapter are:

• Basic programming features in Python: variables and values.

• Translating formulas from math notation to Python.

You don’t need a lot of math to do data science, but and the end of this chapter I’ll review one topic that comes up a lot: logarithms.

## Numbers#

Python provides tools for working with many kinds of data, including numbers, words, dates, times, and locations (latitude and longitude). Let’s start with numbers. Python can work with several types of numbers, but the two most common are:

• int, which represents integer values like 3, and

• float, which represents numbers that have a fraction part, like 3.14159.

Most often, we use int to represent counts and float to represent measurements.

Here’s an example of an int:

3

3


When you run a cell that contains a value like this, Jupyter displays the value.

Here’s an example of a float:

3.14159

3.14159


float is short for “floating-point”, which is the name for the way these numbers are stored. Floating-point numbers can also be written in a format similar to scientific notation:

1.2345e3

1234.5


This value is equivalent to $$1.2345 \times 10^{3}$$, so the result is 1234.5. The e in 1.2345e3 stands for “exponent”.

If you are not familiar with scientific notation, you can read about it at https://en.wikipedia.org/wiki/Scientific_notation.

## Arithmetic#

Python provides operators that perform arithmetic. The operators that perform addition and subtraction are + and -:

3 + 2 - 1

4


The operators that perform multiplication and division are * and /:

2 * 3

6

2 / 3

0.6666666666666666


And the operator for exponentiation is **:

2 ** 3

8


Unlike math notation, Python does not allow implicit multiplication. For example, in math notation, if you write $$3 (2 + 1)$$, that’s understood to be the same as $$3 \times (2+ 1)$$. In Python, that’s an error.

NOTE: The following cell uses %%expect, which is a Jupyter “magic command” that means we expect the code in this cell to produce an error.

For more about this magic command, see the Jupyter notebook introduction.

%%expect TypeError

3 (2 + 1)

<>:1: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma?
/tmp/ipykernel_12818/978361997.py:1: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma?
3 (2 + 1)

TypeError: 'int' object is not callable


In this example, the error message is not very helpful, which is why I am warning you now. If you want to multiply, you have to use the * operator.

The arithmetic operators follow the rules of precedence you might have learned as “PEMDAS”:

• Parentheses before

• Exponentiation before

• Multiplication and division before

• Addition and subtraction.

So in this expression:

1 + 2 * 3

7


The multiplication happens first. If that’s not what you want, you can use parentheses to make the order of operations explicit:

(1 + 2) * 3

9


Exercise: Write a Python expression that raises 1+2 to the power 3*4. The answer should be 531441.

## Math Functions#

Python provides functions that compute mathematical functions like sin and cos, exp and log. However, they are not part of Python itself, but they are available from a library, which is a collection of values and functions. The one we’ll use is called NumPy, which stands for “Numerical Python”, and is pronounced “num pie”. Before you can use a library, you have to import it. Here’s how we import NumPy:

import numpy as np


This line of code imports numpy as np, which means we can refer to it by the short name np rather than the longer name numpy. Names like this are case-sensitive, which means that numpy is not the same as NumPy. So even though the name of the library is NumPy, when we import it we have to call it numpy.

%%expect ModuleNotFoundError

import NumPy as np

ModuleNotFoundError: No module named 'NumPy'


This error message might be confusing if you don’t pay attention to the difference between uppercase and lowercase. But assuming we import np correctly, we can use it to read the value pi, which represents the mathematical constant $$\pi$$.

np.pi

3.141592653589793


The result is a float with 16 digits. As you might know, we can’t represent $$\pi$$ with a finite number of digits, so this result is only approximate.

NumPy provides log, which computes the natural logarithm

np.log(100)

4.605170185988092


NumPy also provides exp, which raises the constant e to a power.

np.exp(1)

2.718281828459045


Exercise: Use these functions to check the identity $$\log(e^x) = x$$. Mathematically, it is true for any value of $$x$$. With floating-point values, it only holds for values of $$x$$ between -700 and 700. What happens when you try it with larger and smaller values?

Floating-point numbers are finite approximations, which means they don’t always behave like math. As another example, let’s see what happens if we add up 0.1 three times:

0.1 + 0.1 + 0.1

0.30000000000000004


The result is close to 0.3, but not exact. When you work with floating-point numbers, remember that they are only approximately correct.

## Variables#

A variable is a name that refers to a value. The following statement assigns the value 5 to a variable named x:

x = 5


The variable we just created has the name x and the value 5.

If we use x as part of an arithmetic operation, it represents the value 5:

x + 1

6

x ** 2

25


We can also use a variable when we call a function:

np.exp(x)

148.4131591025766


Notice that the result from exp is a float, even though the value of x is an int.

Exercise: If you have not programmed before, one of the things you have to get used to is that programming languages are picky about details. Natural languages, like English, and semi-formal languages, like math notation, are more forgiving.

As an example, in math notation, parentheses and square brackets mean the same thing, you can write $$\sin (\omega t)$$ or $$\sin [\omega t]$$ – either one is fine. And you can leave out the parentheses altogether, as long as the meaning is clear, as in $$\sin \omega t$$. In Python, every character counts. For example, the following are all different, and only the first one works:

np.exp(x)
np.Exp(x)
np.exp[x]
np.exp x


While you are learning, I encourage you to make mistakes on purpose to see what goes wrong. Read the error messages carefully. Sometimes they are helpful and tell you exactly what’s wrong. Other times they can be misleading. But if you have seen the message before, you might remember some likely causes.

Exercise: The NumPy function that computes square roots is sqrt. Use it to compute a floating-point approximation of the golden ratio, $$\phi = \frac{1}{2}(1 + \sqrt{5})$$. Hint: The result should be close to 1.618.

## Calculating with Variables#

Now we’ll use variables to solve a problem involving compound interest. It might not be the most exciting example, but it uses everything we have done so far, and it reviews exponentiation and logarithms, which we are going to need.

If we invest an amount of money, $$P$$, in an account that earns compounded interest, the total accumulated value, $$V$$, after an interval of time, $$t$$, is:

$$V=P\left(1+{\frac {r}{n}}\right)^{nt}$$

where $$r$$ is the annual interest rate and $$n$$ is the compounding frequency. For example, if you deposit \$2,100 in a bank paying an annual interest rate of 3.4% compounded four times a year, we can compute the balance after 7 years by defining these variables:

P = 2100
r = 0.034
n = 4
t = 7


And computing the total accumulated value like this.

P * (1 + r/n)**(n*t)

2661.6108980682593


Exercise: Continuing the previous example, suppose you start with the same principle and the same interest rate, but interest is compounded twice per year, so n = 2. What would the total value be after 7 years? Hint: we expect the answer to be a bit less than the previous answer.

Exercise: If interest is compounded continuously, the value after time $$t$$ is $$V=P~e^{rt}$$. Translate this equation into Python and use it compute the value of the investment in the previous example with continuous compounding. Hint: we expect the answer to be a bit more than the previous answers.

## Summary#

This chapter introduces variables, which are names that refer to values, and two kinds of values, integers and floating-point numbers.

It presents mathematical operators, like + for addition and * for multiplication, and mathematical functions like log for logarithms and exp for raising e to a power.

In the next chapter, we’ll see additional data types for representing letters and words, dates and times, and latitude and longitude.