Installing Python
To work through parts 2 and 3 or the book you need to be able to run Python 3 code and install Python packages.
If you can already do both those things and have a setup that works for you, great. If you do not, the easiest way to get one is from Anaconda.
(Note: this guide is just on installing Anaconda and running code — with Anaconda we don't need to worry about installing packages — it comes with everything we need.)
So go to:
Click on the green button to download the 3.x version (3.9 at time of this writing) for your operating system.
Note: Anaconda is the name for the free, user friendly Python bundle we're installing, but it's also a company. They didn't invent Python, but made it way easier for non-coders to install — especially on Windows — by bundling everything together. They make money by offering storage and add ons, but we won't need any of that.
Once it's installed, open up Anaconda Navigator and launch Spyder.
Spyder
Sypder is a place to write and run Python code. While there might be easier ways to get started (e.g. replit.com or Google Colab) I find Spyder is the best trade off when it comes to ease of use + writing real programs.
When you have Spyder open, go to View → Window layouts and select Horizontal split. Then make sure the bottom tab on the right side is set to IPython console (bottom red circle in the picture below).
Now you should be ready to code. Your editor is on left, and your Python console is on the right. Let's touch on each of these briefly.
Editor
If you're reading this you've probably worked with Microsoft Excel or Google Sheets, but not necessarily code. What's the difference?
A spreadsheet lets you manipulate a table of data as you look at. You can point, click, resize columns, change cells, etc. The coder term for this style of interaction is "what you see is what you get" (WYSIWYG).
In contrast, Python code is a set of instructions for working with data. You tell your program what to do, and Python does (aka executes or runs) it.
It is possible to tell Python what to do one instruction at a time, but usually programmers write multiple instructions out at once. These instructions are called "programs" or "code" and — for Python (each language has its own file extension) — are just plain text files with the extension .py.
When you tell Python to run some program, it will look at the file and run each line, starting at the top.
Your editor is the text editing program you use to write and edit these files. If you wanted, you could write all your Python programs in Notepad or Microsoft Word, but most people don't. An editor like Spyder will do nice things like highlight special Python related keywords and alert you if something doesn't look like proper code.
Console (REPL)
Your editor is the place to type code. The place where you actually run code is in what Spyder calls the IPython console. The IPython console is an example of what programmers call a read-eval(uate)-print-loop, or REPL.
A REPL does exactly what the name says, takes in ("reads") some code, evaluates it, and prints the result. Then it automatically "loops" back to the beginning and is ready for new code.
Try typing 1+1
into the REPL you have open in Spyder. You should see:
In [1]: 1 + 1
Out[1]: 2
The REPL "reads" 1 + 1
, evaluates it (it equals 2), and prints it. The
REPL is then ready for new input.
A REPL keeps track of what you have done previously. For example if you type:
In [2]: x = 1
And then:
In [3]: x + 1
Out[3]: 2
the REPL prints out 2. But if you quit and restart Spyder and try typing x + 1
again it will complain that it doesn't know what x
is.
In [1]: x + 1
...
NameError: name 'x' is not defined
By Spyder "complaining" I mean that Python gives you an error. An error —
also sometimes called an exception — means something is wrong with your
code. In this case, you tried to use x
without telling Python what x
was.
Get used to exceptions, because running into them is a big part of programming. If you are working interactively in a REPL and do something against the rules of Python it will alert you (in red) that something went wrong, ignore whatever you were trying to do, and loop back to await further instructions like normal.
Try:
In [2]: x = 1
In [3]: x = 9/0
ZeroDivisionError: division by zero
Since dividing by 0 is against the laws of
math,
Python won't let you do it and will throw (or raise) an error. No big deal —
your computer didn't crash, everything you were doing is still there. If you
type x
in the REPL again you'll see it's still 1
.
We'll mostly be using Python interactively like this, but know Python behaves a bit differently if you have an error in a file you are trying to run all at once. In that case Python will stop and quit, but — because Python executes code from top to bottom — everything above the line with your error will have run like normal.
Using Spyder and Keyboard Shortcuts
When writing programs (or following along with the examples in these posts or the book) you will spend a lot of your time in the editor. You will also often want to send (run) code — sometimes the entire file, usually just certain sections — to the REPL. You also should go over to the REPL to examine certain variables or try out certain code.
At a minimum, I recommend getting comfortable with the following keyboard shortcuts in Spyder:
Pressing F9 in the editor will send whatever code you have highlighted to the REPL. If you don't have anything highlighted, it will send the current line.
F5 will send the entire file to the REPL.
You should get good at navigating back and forth between the editor and the REPL. On Windows:
- control + shift + e moves you to the editor (e.g. if you're in the REPL).
- control + shift + i moves you to the REPL (e.g. if you're in the editor).
On a Mac, it's command instead of control:
- command + shift + e (move to editor).
- command + shift + i (move to REPL).