Pre-MAP Course Website | Pre-MAP GitHub | Google
Most of the coding in Python that we will do in class will be in this environment. This is an iPython Notebook. Let's define some things:
There are ways to write and execute Python code outside of the iPython Notebook. We'll learn some of those later.
A notebook is made up of cells. Below is an empty code cell, which is where you will write code.
Traditionally, the first line of code you write in a new language is one that commands the computer to say hello to you. In Python, the print
command is used to return some output to the screen for a user to read. In the cell below, type the following command:
print("Hello, world!")
To execute the code, press Shift+Enter
with the cursor in the cell that you want to run.
In [ ]:
You should now see the text Hello, world!
below the cell. That's the output that you generated with your first line of code!
You can print
anything to the screen if it's written between two quotes.
print("Here are some numbers and a smiley: 1, 2, 3, 4 =D")
Characters, numbers and spaces surrounded by quotes are called strings. Try printing your own string below, both single and double quotes will work:
In [ ]:
Now let's create a variable, which represents something. Let's write your name in a string and save it to the variable name
. Create a variable by typing a variable name, and putting an equals sign next to it, and the thing you want to save into the variable on the right of the equals sign. Note - order is important!
name = "Brett Morris"
Create a variable that contains your name in the cell below, and execute the cell with Ctrl+Enter
In [ ]:
Now you can use the variable name
anywhere in your code to access its contents (as long as you remembered to execute the cell above). For example, print your name in the cell below with:
print(name)
In [ ]:
We can print a string multiple times if we multiply the variable by the number of times we want to see the string. For example, here's how I'd calculate my name three times
print(10 * name)
Do the same with yours below:
In [ ]:
Typically we use programs to do things that are inconvenient to do by hand. We'll see a bunch of examples in class that do inconvenient things quickly. Here's our first one!
In Harry Potter and the Order of the Phoenix, Dolores Umbridge makes students write "I must not tell lies" on parchment (and therefore into their hands). If I had you write that quote one hundred times, you would likely get frustrated. Let's print that quote one hundred times, using Python!
*
.
In [ ]:
You may be wondering - how do I get a line break (newline) to show up between each quotes? Line breaks and tabs, for example, are special characters. When you're looking for some feature of Python that you're unfamiliar with, don't ever hesitate to Google your question.
Whenever you want to learn something in new, Google the word python
and the thing you want to learn about. In this case, the first few hits from searching "python special characters
" will give you some hints.
Use what you learned in the cell below to print the quote from above with line breaks in between each quote:
In [ ]:
Another resource you have for learning in Pre-MAP is your peers. Group work is encouraged (and sometimes required) in this class. If you haven't done so yet, turn to your neighbors to your left and right and introduce yourself! Compare your solutions to the previous exercises and see how they differ.
Code doesn't always work. When code fails, Python prints a message that's meant to help you figure out what went wrong. Try copy and pasting some of your code from above into the cell below, and see if you can break it. This should be easy. Try removing a parenthesis or a quote.
In [ ]:
If you forgot a parenthesis, you'll see:
File "<ipython-input-18-5a042a0f0398>", line 2
print(quote
^
SyntaxError: unexpected EOF while parsing
This a SyntaxError
- syntax is like the grammar of a program. See the caret symbol (^
) pointing to the place where the quote should be? That's your hint to fix the code, by putting in the missing parenthesis.
If you remove a quote, you'll see this:
File "<ipython-input-17-940964e7f8ab>", line 1
quote = "I must not tell lies
^
SyntaxError: EOL while scanning string literal
This is a SyntaxError
too, but now there's a more specific message telling us what's wrong. If you're like me, that message sounds technical, and it doesn't help. When this happens...
Google for the error message from above containing "EOL" (what does that mean!?), and the word "python". The first few hits will be from a website called StackOverflow
- read them! This is a website where people post code questions, and other people respond with answers. For most questions, there will be a few responses showing various approaches to solving the problem.
Can you figure out what the above error message is communicating now?
<tab>
completeThis iPython Notebook remembers what variables you've been working with. This is handy because it can save you some typing. In the cell below, type the characters na
and press the <tab>
button. You'll see that it completes the variable name name
(if you've used that variable above).
Name a few variables with long names below, like this (creativity welcome!):
favorite_color = 'blue'
this_is_a_long_variable_name = 'yes, it is'
i_really_wouldnt_want_to_type_this_out_again = "nope nope nope"
Execute the cell, and in the cell below, start typing the variable names. Before you complete the variable name, press <tab>
, and it will complete your variable name for you.
In [ ]:
In [ ]:
Make a conscious effort to remember to <tab>
complete, and it will save you lots of time in the future!
In [ ]: