Getting Started in Python/Anaconda

You’ve now installed Anaconda/Python and Pyomo. What do you do now?

This post gives you the very basics of running Python in terms of line by line code in the IPython Console and running a file from the Editor window.

It is not a full tutorial on Python. There is so much excellent information available on the internet that we’d only be replicating what is already out there, probably in an inferior manner.

However, we’ve recommended you install this thing so we at least owe you to be able to run something.

Before we start, we’ll use the terms Anaconda and Python interchangeably here. Anaconda is our build of Python.

Firstly, open Anaconda/Spyder through your Start Menu (Windows only).

This image has an empty alt attribute; its file name is Anaconda-Start-1.png

You should get something like the image below come up. It may prompt you for an update but just click OK and ignore that for now. The exact windows shown in your Anaconda may vary from those below, but as long as you have the two main windows used here you’ll be fine. You can change the windows shown once you have a bit more experience.

The two Anaconda windows (Panes) we’ll use here are the Editor on the left and the IPython Console on the right (History is just above the Console, very handy).

If you don’t have the Console and Editor then from the main menu select View/Panes and make sure the IPython Console and Editor are checked. You can select other panes but these are the main two.

One of our existing Python files is shown in the Editor window in this post. It’s the code for our upcoming Disney Star Wars Finances post. You won’t have this and instead will have a blank file. This is fine for now.

Set your working directory from the menu on the top right of Anaconda as shown. We’ve chosen to create a directory C:\Python27\Practical_Economics for this post.

The simplest way to use Python is through the Console on a step by step basis. Python is a dynamic computer language that calculates each command as it occurs.

Let’s start by using the Console as a calculator – just type in 4+6 and hit Return.

Python, adhering to the laws of mathematics, returns the answer 10. No surprise here.

Try 4/6 or any other calculation. Have a guess what 6%4 means.

You should be bored of this after about 30 seconds, so let’s move on.

[As an aside, calculations do become a little more complicated on groups of numbers, such as lists (type [0.5,0.5]*2 and see what happens). This is why we recommend using pandas, which you get automatically with Anaconda, as it re-simplifies things]

OK, lets import our first array from the keyboard. Copy the following text and paste it into the Console.

import numpy as np
arry=np.array([[0.5,0.5],[0.7,0.3]])
print (arry)

Don’t worry about the exact form of what you’ve entered, technically you’ve read in two lists and specified it as an array, which we’ve named arry. Lists and arrays are specific Python data structures.

Hit Return on your keyboard and you should get the following result.

Take note of importing the package numpy, which has many handy mathematical functions and data structures. We’ve imported numpy and all of its functionality with our first line of code. We import packages when we need them rather than everything at once as it saves Python having to run the code for everything when we start it.

Convention is that you have an abbreviation from the package when you call the function (np.array in this case). It’s not strictly necessary, but helps people reading our code know where we got the array function from.

If we tried to create our array without first importing numpy we’d get the following error.

Once a package is imported in Python, it remains imported until we close the Python session.

Next we’ll turn our array into a pandas DataFrame. Copy and paste the following text into the Console and hit Return.

import pandas as pd
df=pd.DataFrame(arry)
print (df)

You should get the following output.

Congratulations, you’ve just created your first pandas DataFrame – df. Our guess is is won’t be the last.

Our DataFrame df is only small and the rows and columns aren’t named, but it’s a start.

The second way of running Python code is creating a file in the editor. Obviously, as your calculations become more complex you’ll gravitate towards this method as it will enable you to repeatedly run complex code quickly. It also provides an exact record of what you’ve done.

Click on the New File tab in the top left of the main Anaconda window.

You’ll get a blank file in the Editor called untitled0.py* for the first one you create in a session.

Next copy and paste our two sets of commands into the editor. Convention is to import all of the packages we need at the top of the script. We’ll also comment out the print (arry) command with the # key so that it doesn’t run.

Next, save your file into your working directory. We’ll call this one Test, make sure you select Python Files in the Save as Type menu.

This image has an empty alt attribute; its file name is image-6.png

Once you’ve saved it, just press the Run button (green arrow) on the top left of the main Anaconda window.

You should get the following output.

If you ever want to see a DataFrame, or any other Python data, you’ve created, just type the DataFrame name into the Console. In this case we type df.

You can now perform useful calculations on our DataFrame. For example, type in df.mean() or df.mean(axis=1) and see what happens. Keep in mind that this scales to just about any sized DataFrame and you can see the power at your disposal.

Once you’ve created something in Python it exists until you delete it or you close the program. This is actually pretty important. More than a few times the Proprietor has thought a program was working fine, only to find out it wasn’t when we opened Anaconda the next day.

Keyboard entry is but one method of importing data into Python. Methods exist to pull data from text files, spreadsheets and databases. Data can be outputted to these formats as well.

That’s it, you’re now off and running in Python.

Good luck.