You will be working on a series of exercises for the material covered in lecture 1a. As a reminder, we discussed programming languages and how Python/Jupyter can be used as a powerful calculator.
The first set of exercises will focus on how to use Python/Jupyter as a calculator.
The following will be useful:
We will start simple. Calculate the following quantities using Jupyter, one per cell:
Remember to use shift + enter to run cells
Now that you're used to simple arithmetic, we'll make things a bit harder. Python/Jupyter executes statements like 1 + 2 * 3
in a specific order. This is called the order of operations.
The order of operation is very important! They can change the results dramatically.
Compute the following quantities by hand. Then, use Jupyter to check if you got the order of operations correct.
Now do the following:
Remember to use shift + enter to run cells
Beyond addition, subtraction, multiplication, and division, there are other, more complex operations. We will first review exponentiation.
The syntax for exponentiation is as a ** b
. IT IS NOT THE ^
!
Compute the following quantities by hand. Then, use Jupyter to check if you got them correct:
Remember to use shift + enter to run cells
In Python, there are some invalid operations. Invalid operations will cause errors. If you see an error, that means that there's a logical issue with your code. These will become increasingly important later in the course to help you debug your code
See what happens if you run the following:
Now that we've gone over the basic arithmetic operations, let's use them in interesting ways.
Let's say we have a mathematical function $f(x) = x^3 + 2 x^2 + 3 x + 4$. (Important note: we will see later in the course that programming function are NOT THE SAME as mathematical functions. You don't need to worry about this for now, but keep this in mind).
Using Jupyter, compute $f(5)$. Expand the computation so that $x$ is replaced by 5 and you do all of the arithmetic operatoins.
Do the same for $f(10)$.
You might notice that this process of substituting the variables is quite annoying. We will revisit this in future lectures.
Use this space to compute free-form arithmetic operations. What are the most interesting things you can compute? If you're willing, bring some to share to class.
Remember to use shift + enter to run the cells.
We will now review the modulus operation. The syntax for modulus is a % b
Compute the following quantities by hand. Then, use Jupyter to check if you got them correct:
Remember to use shift + enter to run cells
For this exercise, using only the operations we discussed in class, do the following:
Do you see a pattern?
If you've completed all of the exercises so far, congratulations! There are supplementary exercises you can do now. Ask a TA to get access to them.