Python 😮🐍

To work on this notebook interactively, download it here: first_lesson.ipynb.

Don't have Anaconda installed? You can run Jupyter Notebooks interactively with Google Drive.
Download file above > Upload to Google Drive > right click file > "Open with" > 'Colaboratory' (click 'connect more apps' if Colaboratory isn't an option).

Today, you'll learn how to:

  1. navigate Jupyter notebooks (like this one);
  2. write and evaluate some basic expressions in Python, the computer language that many people use!
  3. call functions to use code other people have written; and
  4. break down Python code into smaller parts to understand it.

1. Jupyter notebooks

This webpage is called 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!

1.1. Text cells

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.

To run a cell:

- click on the cell

- hold down 'SHIFT' and then tap 'ENTER/RETURN' once.

1.2. Code cells

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).

In [1]:
print("Hello, World!")


  • first line is printed first, then second, etc.
In [3]:
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

1.3. Writing Jupyter notebooks

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.

1.4. Errors

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.

In [4]:
print("This line is missing something."

OOPS! we are missing a parenthesis.

In [2]:
# use the hashtag to make a comment!

2. Numbers

In [ ]:

Notice that we didn't have to print to see the number displayed. When you run a notebook cell, if the last line has a value, then Jupyter helpfully prints out that value for you. However, it won't print out prior lines automatically.

In [ ]:

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.

2.1. Arithmetic

The line in the next cell subtracts. Its value is what you'd expect. Run it.

In [ ]:
3.25 - 1.5

Remember order of operations applies in code!

In [ ]:
In [ ]:

3. Names

If we want to save a value for later, we assign it a name!

In [1]:
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 ten.

After you run that cell, the value 10 is bound to the name ten. Run the two cells below:

In [ ]:
print(ten * 5)

Question 3.1. Try defining a variable called special_number that's equal to your favorite number!

In [4]:
# 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.

In [ ]:
cruz + 5
In [ ]:

Extra tip: Do "to the power of" in python using two stars:

$$x^{y} = x ** y$$

Run the following cell:

In [ ]:

Here's a real-life example of a really useful variable to make:

In [ ]:
pi = 355/113

Compute the area of this circle, using the variable called pi.

EXTRA: instead of using 14, define a variable called radius = 14 first. Then use the variables pi AND radius to compute the area.

In [3]:
# Put your answer below this comment!

Variable Names in python can have:

  • letters
  • numbers
  • underscore _

Names CANNOT have:

  • no spaces allowed
  • names can't start with a number
In [ ]:
a = 840
b = 2 * a
c = 12
d = c * Bob

Hint: make names meaningful.

instead of random variable names, give it useful names like this: (for the computer, it's the same thing!)

In [ ]:
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

Calling functions!

What do you thing the abs function does?

In [ ]:
In [ ]:

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!

In [ ]:
abs(1.21 - 1.688)

Some functions take in multiple values! If it does, you have to seperate them by commas

In [ ]:
max(17, 16)
In [ ]:
min(590, -590)

Writing your own function!

In [9]:
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!

In [ ]:
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 func the 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!

In [10]:
# call the function here
In [1]:
# Extra: try function for finding roots of a quadratic equation
In [ ]: