Today, you’ll learn how to:
This webpage looks like a Jupyter notebook. A notebook is a place to write programs and view their results. You could write programs in a text file if you want, but it’s nice to do it in a notebook like this because you can share it with others! To run this notebook, first download it here.
You can then upload it to Google Drive and run it there using Colaboratory.
Or, install python and then follow these steps to run locally:
pip3 install jupyter
Launch your notebook (opens in browser):
jupyter notebook [name_of_file.ipynb]
In a notebook, each rectangle containing text or code is called a cell.
Text cells (like this one) can be edited by double-clicking on them. Double click this cell!! This cell is a text cell, not code. If you want to make a new cell, click “+Code” or “+Text” at the top of the page for the cell type.
After you edit a text cell, click the “run cell” button on the left ▶ to re-run any changes.
I will use these text cells to give instructions. YOU will basically only work with code cells.
Try running the code cell below (remember shift + enter! or click the ▶ play button on the left).
CODE IS EXECUTED IN ORDER!
print("First this line is printed,") print("and then this one.")
Question 1.2.1. Change the cell above so that it prints out:
I'm dreaming of a white christmas, just like the ones I used to know
You can use Jupyter notebooks for your own projects or documents. When you make your own notebook, you’ll need to create your own cells for text and code.
To add a cell, click the ‘+ CODE’ button in the menu bar. It’ll start out as a text cell. You can change it to a code cell by double-clicking inside it.
Question 1.3.1. Add a code cell below this one. Write some code in it like:
2 + 2
Run your cell to verify that it works.
Python is a language, and like natural human languages, it has rules.
We have made an error in the next cell. Run it and see what happens.
print("This line is missing something."
OOPS! we are missing a parenthesis.
# use the hashtag to make a comment!
Notice that we didn’t have to
print(2) 3 4
Above, you should see that 4 is the value of the last expression, 2 is printed, but 3 is lost forever because it was neither printed nor last.
You don’t want to print everything all the time anyway. But if you feel sorry for 3, change the cell above to print it.
The line in the next cell subtracts. Its value is what you’d expect. Run it.
3.25 - 1.5
Remember order of operations applies in code!
If we want to save a value for later, we assign it a name!
ten = 3 * 2 + 4
When you run that cell, Python computes the value of
3 * 2 + 4, which is the number 10. Then it gives that value the name
After you run that cell, the value 10 is bound to the name
ten. Run the two cells below:
print(ten) print(ten * 5)
Question 3.1. Try defining a variable called
special_number that’s equal to your favorite number!
# delete this comment and put your answer here!
If we use a variable name that we haven’t made yet, we get an error! Run the following cell.
cruz + 5
Extra tip: Do “to the power of” in python using two stars:
Run the following cell:
Here’s a real-life example of a really useful variable to make:
pi = 355/113 pi
Compute the area of this circle, using the variable called
EXTRA: instead of using
14, define a variable called
radius = 14 first. Then use the variables
radius to compute the area.
# Put your answer below this comment!
Variable Names in python can have:
Names CANNOT have:
a = 840 b = 2 * a c = 12 d = c * Bob d
Hint: make names meaningful.
instead of random variable names, give it useful names like this: (for the computer, it’s the same thing!)
bimonthly_salary = 840 monthly_salary = 2 * bimonthly_salary number_of_months_in_a_year = 12 yearly_salary = number_of_months_in_a_year * monthly_salary yearly_salary
What do you thing the
abs function does?
Note that calling any function has the same format: name, paranthesis, values, end paranthesis
function_name ( put in your values here )
You can put in expressions as the value!
abs(1.21 - 1.688)
Some functions take in multiple values! If it does, you have to seperate them by commas
def func(x, y, hello): return x + y * hello
Now we can call this function later. Note that the name
func now refers to the function above. It takes in three values. We call these values that a function takes in arguments. So, when we call
func we have to input 3 arguments!
func(5, 6, 7)
What actually happens??? The values
5, 6, 7 are passed to the function
func and the values are assigned to the argument names.
Now, in the function the argument names have become variables:
x = 5
y = 6
hello = 7
Every time we call the function
x, y, hello values are reassigned.
For example, if we do
func(99, 4, -9) then:
x = 99
y = 4
hello = -9
Try calling the function again!
# call the function here
# Extra: try function for finding roots of a quadratic equation