How to Run Python Programs on a Mac

Hello, World!

This tutorial will teach you to write and run your first Python program on a Mac.

To start, we need to get some things straight.

Programs are recipes you write to make your computer do things. These recipes are written in a programming language, which is a language close enough to human for you to write, and organized enough for a computer to read. Python is one of the most popular programming languages.

When you write a program, you’re just writing a text file (similar to a word document, or an email, or a short story).

 

Code without syntax highlighting

 

When you run a program, you’re giving that text file to Python, and Python is making your computer do stuff.

Programs can do all kinds of stuff, like printing, calculating with numbers, reading a website, or sending a file.

The first program we’re going to write in this tutorial is the classic “Hello World”. Our version will print the words “Hi, Everybody!” onto the screen.

When you’re beginning to learn to program, it’s hard to do all the fancy graphics necessary to make a website or a phone app. So, to start out, most programmers learn to program using the command line. The command line is a program where you type commands and the computer outputs results. It looks like something used by old school hackers on television:

 

Screen Shot 2014-09-15 at 6.27.32 PM

 

The command line is where you’ll run your first few programs. But it’s not where you’ll write them. Just as there are good places to write documents (Microsoft Word) and emails (Gmail), and good places to edit photos (Photoshop) and draw pictures (Illustrator), there are good places to write programs.

Since programs are written in text, it’s good to write them in a text editor. A text editor is just a program for writing. Microsoft Word and Gmail are text editors, but they have special features which get in the way of writing programs.

Programming has its own text editors. They range from the very light weight (some programmers just use a basic notepad) to the very heavyweight (big development environments like Eclipse and Visual Studio will automatically complete some of your code for you).

We will use a middleweight program called Sublime Text. Sublime looks like a simple notepad, but it provides what is called syntax highlighting. Programs are organized text, so showing them in colors helps highlight the important parts:

 

No Highlighting

 

Highlighting

 

Sublime Text is available at http://www.sublimetext.com/2. Download the OS X copy, open the dmg file you downloaded, and drag the Sublime icon to your Applications folder. It is also a good idea to add this program to your dock.

The command line program in OS X is called Terminal. You can find it in your Applications folder inside another folder called Utilities.

Open Sublime to a blank file. In that file, write:

Save this file to your home directory as “helloworld.py”.

Later we will learn how to navigate files at the terminal, but for now, saving to your home directory will allow use to run the program without worrying about where it is located.

Notice how after you save the file, Sublime starts highlighting it, turning the word “print” one color, and the text another color. This is because, by naming the file with a “.py” at the end, you’ve helped Sublime to guess that this is a python file, and it has applied Python language highlighting.

Open the Terminal program. Some text is printed to say who you are and when you last logged in, but we’ll worry about that later. Right now, we’re only concerned with the prompt. The prompt is the line you’re currently typing on. You can type anything you want, but when you press enter, your computer will assume you have given it a command and try to execute that command.

We will command the computer to run our new Python program. At the prompt, type the following and press enter:

On the line below the prompt, the terminal should print:

Below this line, it should print another prompt, indicating that it is waiting for you to give another command.

And there you go! You made the computer print something out to the terminal.

In the Python language, the word “print” tells the computer to output text to the terminal. The text you want to print is wrapped in quotes. So,

tells the computer to print the text “Hi, Everybody!”.

In the next post, you will learn how to use Python to do calculations and print numbers.