Python 101

Intro to basic Python

Welcome to the first official lesson on Python! First things first - whitespace, punctuation, and capitalization all matter in Python. Vertical whitespace is okay (e.g. blank lines), but horizontal whitespace is used to block lines of code that belong together. If you are having trouble executing code, ensure that you have no typos, no extra whitespace, and that your capitalization matches the examples.

When looking at my example code in these lessons, pay attention to the In and Out markers right above the gray code boxes. The "In" boxes are the code that I submitted. The Out boxes contain the output of that code.

In the following examples, you'll also see that I use # to write little notes about what I'm doing. You can use this character to write reminders to yourself and your readers without it interfering with your code.

Let's get started. The easiest bit of code is using Python as a regular calculator.

In [1]:
1 + 2 #addition
In [2]:
2 * 2.5 #multiplication 

A note about division - in older versions of Python, the / does not actually divide like you would expect it to. In the version you installed (Python 3.x) that is no longer the case. But if you see any unexpected behavior in online tutorials, for example, that is why.

In [3]:

Python isn't just for numbers. You can also manipulate text, known as strings. Strings must have quotes around it; both single and double quotes work. You can do all sorts of things with strings, including multiplication.

In [4]:
'cat' * 4 #multiply the string

In this example, I am getting the first letter of the string, and in the second example I am capitalizing it. Don't worry about understanding or memorizing these examples - it's just a sneak peek.

In [5]:
'cat'[0] #get the first letter
In [6]:

The lines above were not stored in the computer, they were just printed and forgotten. If we want the computer to remember the output (the answer from a multiplication problem, for example), we can assign a variable name using an = sign. When you execute that code, nothing will be printed, but rest assured it worked. To access the value stored in the variable name, just type it again either by itself or inside a print() statement.

In [7]:
price_of_milk = 3.25
In [8]:
In [9]:
print(price_of_milk) #this does the same thing

If you want to store more than one thing under the same variable name, use a list. A list is just a pair of square brackets with a series of items inside, each separated by a comma. Once again, when you execute the code nothing will be printed. To see the list, you must type the variable name.

In [10]:
pets = ['cat', 'dog', 'ferret', 'canary']
In [11]:
['cat', 'dog', 'ferret', 'canary']

Lists can contain any kind of data, including a mix of different data types. Numbers, strings, or some of each are fine. You can even make lists within lists, which is known as nesting.

In [12]:
reps = [1, 2, 3, 'ant', 32, 'elephants', ['coffee', 'milk']]
In [13]:
[1, 2, 3, 'ant', 32, 'elephants', ['coffee', 'milk']]

One important thing to note - The first number in Python is 0, not 1. We will use this bit of knowledge to access the first element in our list using square brackets. This process of accessing elements is known as indexing, and the quirk of the first number in Python being a zero is known as zero-indexing.

In [14]:

In the example below, the 2nd element in the list is selected using 1 - because remember, the first number in Python is zero. I am sorry to say that this will trip you up for a long time.

In [15]:

It is also possible to obtain the last element (or last nth element) by using a negative index. Here we are getting the last item in our pets list.

In [16]:
In [17]:

If you would like to access a certain range in the list, you can put in the start and end indices separated by a colon, like so.

In [18]:
['ferret', 'canary']
In [19]:
['ant', 32]