Your First Steps with Python: Part II — Four Ways to Run your Code

This is the second in a series of posts to get you up and running on Python. In the first post I showed you which version of Python to install, how to check that the installation succeeded, and how to type in and run your first simple Python command.

In this tutorial I will show you four different ways of writing and running your code. For simplicity’s sake, each of these four methods will run the same typical beginners’ “Hello, world!” code. For the purposes of the tutorial I am assuming you are using a Mac. Instructions will vary slightly for PC or Linux.

Method One: Interactive Mode

The most basic way to run code is to enter and run lines of code in the Terminal. This was covered in Part I of the tutorial series. To recap, open /Applications/Utilities/Terminal.app, start Python by typing in python at the $ command prompt, type in print "Hello, world!" and hit enter. The expected output, “Hello world”, is seen in the below screenshot.

Screen Shot 2014-09-03 at 4.55.22 PM


This interactive mode works, but is really only helpful for basic coding. Your code isn’t saved, so you’ll want want to take a different approach.

Method 2: Running Python from a Code-Friendly Text Interpreter

Python code is written in plain text files, typically given a “.py” extension. Once a script (e.g., twitter.py) has been written, it can then be “run” by Python and perform whichever tasks are laid out in the script. So, you’ll need to find a good program for editing these text files. On my Mac I use TextWrangler. Vim is popular on other machines. Your choice here is not terribly important. These programs are all generally free and easy to install. One benefit you’ll get from these programs over say, the basic TextEdit app on the Mac is that they will highlight various elements of the Python code syntax, which helps in code formatting. Running the code is also easier through these specialized programs. Let’s say you download and install TextWrangler and enter your code. This is what is might look like.

Screen Shot 2014-09-04 at 7.13.03 PM


The first line in the code is the shebang — you’ll find this in all your Python scripts.

Lines 3 – 8 contain the docstring — also a Python convention. This is a multi-line comment demarcated by the docstrings """ that describes the code. Write whatever is helpful to you as well as anyone who might use your script in the future. For single-line comments, use the # symbol at the start of the line.

Line 10 contains the python code

OK, so you have written and saved your code in a file called hello_world.py. You can now run your code through TextWrangler and other text interpreters. Go to the #! menu and select “Run in Terminal” as in the following screenshot.

Screen Shot 2014-09-05 at 2.24.18 AM


What happens is that the Terminal will open another window and run your code. Below you’ll see the output on my MacBook Pro.


Screen Shot 2014-09-05 at 2.21.14 AM

Method 3: Running a Python Script in the Terminal

You don’t want to do it this way. If you want to run your scripts, use Method 2 instead. But I’ll show you quickly just so you know it’s possible. Open up a Terminal window. Let’s say you saved your script with the name hello_world.py in your Documents folder. Navigate to the Documents folder by typing in cd Documents and hit enter. Then to run your code type in python hello_world.py and hit enter. Your code will run and you’ll see the output as shown below.

Screen Shot 2014-09-05 at 2.47.46 AM

Method 4: iPython Notebook

This is the preferred way for running your code. I recommend that you familiarize yourself with the iPython Notebook, which comes included with Anaconda Python. The link provides an overview of the Notebook. Simply put, it has become a boon for interactive code development, that is, for “playing around” with the code. I now use the iPython Notebook for developing all of my code. In the same window it allows you to write blocks of code, run them, check whether they worked as intended and, if not, modify them. Highly recommended for learning as it allows for quick error checking. Annotation of your code is also facilitated.

Here is what you’ll do. Open up the Terminal and type in ipython notebook.

Screen Shot 2014-09-05 at 2.57.31 AM

You’ll see some lines of script running in your Terminal window indicating that the Notebook app is running. Wait a few seconds, and a browser window will open. This is your iPython Notebook interface. It will show all available notebooks, which are just fancy Python scripts you’ve developed in iPython. An example is below.

Screen Shot 2014-09-05 at 2.51.00 AM

Hit New Notebook and another browser window will open. This is where you will type in your print "Hello, world!" code in the first cell, as shown below.

Screen Shot 2014-09-05 at 2.51.33 AM


In iPython you can run each block of code, or cell, individually. Put your cursor anywhere in the first cell and hit Shift enter. You code will run. See below.

Screen Shot 2014-09-05 at 2.51.44 AM

One final step remains. You’ll want to rename and save your new notebook for future use. Click on ‘Untitled0’ and a dialogue box will open like you see below.

Screen Shot 2014-09-05 at 3.06.18 AM

Type in hello_world and hit OK and you’ll see that the code has been saved.

Screen Shot 2014-09-05 at 3.06.34 AM

Et voila! You now know 4 different ways of running Python code. In general, you won’t want to do methods 1 (interactive mode in the Terminal) or 3 (run scripts through Terminal). You may wish to use Method 2 from time to time by typing up entire scripts in TextWrangler, but that’s a topic for another day. Until you learn a bit more, concentrate on the last method and do your coding in the iPython Notebook.

Happy coding!




Your First Steps with Python: Part I — Running your First Python Code

For the beginning coder, every step involved in getting up and running the programming language presents a new (and potentially frustrating) learning experience. In a prior post I gave an overview of my preferred Python distributions and my general thoughts on how to approach learning Python. In this and subsequent posts I will walk you through some of the concrete first steps involved in actually using Python.

I am assuming you are using a Mac (slightly different instructions for PC or Linux) and that you are interested in developing code especially for processing social media data. Most of the steps are generalizable to other applications, but I’m tailoring the instructions to those who wish to run my code.

Step One: Download and Install Python

For Unix, Windows, and Mac users alike I recommend you install Anaconda Python 2.7. This distribution of Python is free and easy to install. Moreover, it includes most of the add-on packages necessary for scientific computing, including Numpy, Pandas, iPython, Statsmodels, Sqlalchemy, and Matplotlib.

So, download the software. Double-click on it and run the software. Easy as that!

Step Two: Verify Anaconda is Now Default Version of Python

To use Python you will occasionally have to do something in the Terminal. The Terminal provides a text-based interface to your computer’s operating system and is necessary for certain permissions changes, etc. If this is your first programming experience you may not even know it’s there. Now that you know, you may wish to you keep Terminal in the Dock to keep it handy.

Open /Applications/Utilities/Terminal.app and find out which version of Python is the default by typing in which python at the $ command prompt. It should look like the screenshot you see below. What you should get back is some indication that Anaconda Python is the default Python installation on your computer, such as //anaconda/bin/python

Screen Shot 2014-09-03 at 4.46.46 PM

Step Three: Verify Python is working

Start Python by typing in python at the $ command prompt. You should see some language letting you know which version of Python has started and then a >>> command prompt. This is the prompt for you to enter Python code. Below I’ve included a screenshot of what this looks like on my device.

Screen Shot 2014-09-03 at 4.51.12 PM

Step Four: Type in Some Code

Let’s type in the typical first command of a programmer, print "Hello, world!" and hit enter. The expected output, “Hello world”, is seen in the following screenshot.

Screen Shot 2014-09-03 at 4.55.22 PM


You’ve just executed your first Python command!

Note that this is just one way of running your code. Specifically, when you’re typing in commands in the Terminal you are working in the interactive mode. Your code is not saved, and thus the interactive mode is used only for simple tests. In the next tutorial I will walk you through two more powerful ways of creating and running Python code.

If you have found this helpful please share it, and I always welcome feedback.