In our first notebook, we will investigate:
Python and Jupyter Notebooks. Python is a programming language, a way for us to give commands to a computer. Jupyter Notebooks are a convenient way to play with Python.
We can run programs by clicking on the play button next to them:
print("Hello world!")
print("It is a good day!")
Hello world! It is a good day!
print("You can edit me!")
You can edit me!
print("I forgot a closing quotation mark!)
print("This instruction won't run!")
my_favorite_color = "red"
print(my_favorite_color)
my_favorite_color = "green"
print(my_favorite_color)
my_favorite_color = 1
print(my_favorite_color)
my_favorite_color += 1
print(my_favorite_color)
Python is a programming language. Programming languages are a lot like regular languages, except we use them to communicate with a computer rather than with other people.
Every sentence in a programming language is an instruction to the computer. A program is a list of instructions. When the computer runs your program, it will read instructions, from top to bottom, and perform each one after the other.
Let me show you what I mean:
print("Hello world!")
Above is a Python program! Click the play button to the left of the word "print" to make the computer run it.
This program is one instruction long, and it just causes the words "Hello world!"
to appear below. It is an example of the print
command, which just tells the computer to say something back to us.
In these notebooks, you can edit any of the programs by simply clicking on them and typing. This is most of how we'll be learning in these notebooks; editing code together.
print("Pierce")
If you make a mistake editing any of these programs, you will notice the code is underlined in red or yellow.
It turns out that computers are not very smart. They are very picky about how you communicate with them, and they want you to talk to them in exactly the way they expect. This is one reason why writing code can be very challenging.
This following program has a mistake:
print(Pierce)
Run it, and notice what happens. The computer shows you the instruction that caused the error and tries to describe what it was (which may not make sense to you yet!) In this case, it says name 'Pierce' is not defined
. We will see what that means later in this notebook.
When writing programs, you will likely make mistakes. This OK. It is a normal and unavoidable part of writing code. Because the computer is so specific in what it wants, it is difficult to write correctly all of the time. Don't feel bad if you make errors; again, it is a natural part of the process.
Fixing errors is usually called debugging.
Let's explore some other things the print command can do. We can print numbers, and we can do math with them. Try running this program:
print(5 + 2)
Programs can have more than one instruction! This program prints out three words:
print("What's")
print("good")
print("world")
Python isn't all just print
instructions. Another type of command we can give is to make variables.
Variables allow our program to remember something while it is running. For instance, if we want to use your name multiple times in a program, it is easier to have the computer simply remember what it is rather than type it out in full every time!
To create a variable, we put the name we want on the left side, and the value on the right, and then an equals sign in between. Like this:
my_color = "red"
This creates a variable with the name my_color
and the value "red"
.
One way to think of this is we have put the word "red" in a box with the label my_color
. If we say my_color
in another program, it will open the box to see what is inside.
There are no print
instructions this time, so our program doesn’t output anything at all! However, that doesn't mean nothing happened - the name my_color
now refers to our variable, and we can use it in any code that comes after this. Try running the following program, and note how it uses the variable:
print("My favorite color is:")
print(my_color)
If you favorite color isn't red, you can change it, of course! Go back to the program that creates my_color
and write something else, then run it again. Now, if you run the program that prints my_color
again, it will have updated to your new favorite color!
You'll notice that quotation marks are used to tell the computer when we want to simply print a word or when we want to use a variable.
print("my_color")
When we use quotation marks, we are telling the computer we want the exact word we typed, and not to look for any variables. So, it prints, literally, "my_color".
If we remove the quotation marks, the computer will know we want to use a variable and print the value of the variable instead.
Variables can store lots of different things. Here we are storing a number:
the_best_number = 14
print(the_best_number)
Once again, the first line of this program creates a variable - we have stored the number 14 in a box with the name my_favorite_number
. In the second line, we print out what is in the box.
the_best_number
contains 14, What do you think the program below will print?print(the_best_number * 2)
print(my_number)
It is an error! Why? Think about it for a moment, and read the message it gives you.
The computer says name 'my_number' is not defined
. In other words, there is no variable with the name my_number
. This means there is no value for my_number
, so the computer has no idea what to do!
Here's a program that creates my_number
. Go ahead and run it:
my_number = 4
Then, run the program that prints my_number
again. It should work now!
Variables can be changed after they are created - we just need to create it again, using the same name. This program changes the value of my_number
, and then prints it:
my_number = 8
print(my_number)
The old value of my_number
is completely gone, replaced with 8.
We can also add onto variables rather than replacing them outright. The following program increases the value of my_number
by 7, then prints it!
my_number += 7
print(my_number)
Notice that if you keep running this program, the value of my_number
keeps going up!
You can also add words in the same way. Run these programs, and watch what happens:
word = "super"
word += "duper"
print(word)
Every time you run the bottom program, it adds another "duper" to "super"! Every time you run the top program, it resets word
back to just "super".
If you've gotten to this point, you've learned everything we have for day 1. Well done!
It is okay if you struggled. Learning a programming language can be extremely challenging, since you have to get all of the details exactly right for things to work correctly.
The best way to learn something is to play and experiment. Let's try some more challenges using what you have read about. You do not need to complete all of these, but please try them!
If you need help, go back and look at the previous parts of the notebook or ask an instructor. We are here for you!
Below is a program that is meant to print a greeting, but the variable it uses doesn't exist. Can you fix it?
print("Hello", name)
Here is a program that is supposed to print out 2000 - however, it prints out 2001 instead. Can you fix it?
two_thousand = 20 * 100
two_thousand += 1
print(two_thousand)
This program prints out a greeting and a name, but in the wrong order. Can you fix it?
greeting = "Hello"
name = "Bob"
print(name, greeting)
This program is meant to print out "Fort Collins", but it doesn't work. Can you fix it?
city = "Fort "
city += ""
print(city)
Fort
These programs have mistakes, and cause errors! Can you fix them?
Note that you should run them first to see what the computer says might be wrong - sometimes, but not always, it will be helpful. If you don't understand what it says, don't worry too much - just compare it with programs you've seen before, or ask an instructor for help.
print "Hello World!"
name "Bob"
print(name)
print(Hello friends)
If you managed to fix some or all of these, give yourself a pat on the back and show the instructors your awesome work!