#!/usr/bin/env python # coding: utf-8 # # Exercises for day 1a # # 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: # - Jupyter consists of a series of cells # - Use shift + enter to run a cell # - Remember the order of operations! # ## Exercise 1 # # We will start simple. Calculate the following quantities _using Jupyter_, one per cell: # - 10 + 20 # - 72 - 123 # - 23 * 67 # - 90 / 30 # - 90 / 27 # - 90 // 27 # # Remember to use _shift + enter_ to run cells # In[ ]: # In[ ]: # In[ ]: # In[ ]: # In[ ]: # In[ ]: # ## Exercise 2 # # 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. # - 1 + 2 * 3 # - 1 * 2 + 3 # - 1 / 2 + 3 # - 1 - 2 * 3 # - 1 * 2 - 3 # # Now do the following: # - 10 * 23 / 5 # - (1 + 2) * 3 # - 1 + (2 * 3) # - 12 * 5 / 4 # - 12 * 5 // 7 # # Remember to use _shift + enter_ to run cells # In[ ]: # In[ ]: # In[ ]: # In[ ]: # In[ ]: # In[ ]: # In[ ]: # In[ ]: # In[ ]: # In[ ]: # ## Exercise 3 # # 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: # - 2 ** 2 # - 2 ** 3 # - 5 ** 2 # - 10 ** 5 # - 25 ** 0.5 # - 3 ** -1 # # Remember to use _shift + enter_ to run cells # In[ ]: # In[ ]: # In[ ]: # In[ ]: # In[ ]: # In[ ]: # ## Exercise 4 # # 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: # - 10 / 0 # - 1 + # - 2 * * 4 # - 3 x 4 # - 3 a # In[ ]: # In[ ]: # In[ ]: # In[ ]: # In[ ]: # ## Exercise 5 # # 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. # In[ ]: # ## Exercise 6 # # 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. # In[ ]: # ## Exercise 7 (medium) # # 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: # - 1 % 2 # - 10 % 2 # - 10 % 5 # - 67 % 3 # # Remember to use _shift + enter_ to run cells # In[ ]: # In[ ]: # In[ ]: # In[ ]: # ## Exercise 8 (Hard) # # For this exercise, using _only the operations we discussed in class_, do the following: # - Extract the lowest digit from the number 48756378291 # - Extract the second lowest digit from the number 48756378291 # - Extract the third lowest digit from the number 48756378291 # # Do you see a pattern? # In[ ]: # In[ ]: # In[ ]: # # Supplementary exercises # # 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. # In[ ]: