>>>

Dane Hillard
Easy as Python
Published in
3 min readJun 16, 2016

--

There it is, staring you in the face. You’re fresh off of some very theoretical and optimistic reading that says Python is one of the easiest languages to learn, but you’ve finally typed “python” at the command line and you’re met with the cryptic triple-angle-bracket, might-be-an-ancient-heiroglyph prompt once again.

>>>

What now? Last time you were here, you printed “Hello, world!” That was fun and all, but that’s a far cry from the data analysis you were supposed to have on your PI’s desk yesterday.

First, let’s remember that the Python prompt isn’t all that scary.

>>> 1 + 1
2
>>>

It follows the laws of integer mathematics, so how exciting can it even get?

>>> print(‘freakin awesome’)
freakin awesome

There are plenty of things to tinker with at the prompt. However, I’d like to introduce a better long-term way of working.

Python’s prompt is what’s known as a REPL, which stands for read-eval-print loop. This means that when you enter something at the prompt, it’s read and evaluated, followed by the printing of the result, if any. Then you’re returned to the prompt again (the “loop”). However, there’s already something we’ve glossed over. What does the reading and evaluating? You’re sitting there saying “Python, you ninny.”

Python is, of course, the correct — though not very specific — answer. The more specific answer is that the Python interpreter is doing that work. The interpreter is the piece that executes (“runs”) your code.

I mention this because, just as the interpreter is happy to churn away on anything you type at the prompt, it’s also happy reading code from files. As you start learning more Python and thinking up bigger functionality, you’ll probably want to start storing your code this way.

Creating a Python file is pretty easy. Open a new file in your favorite text editor, put some Python code in it, and save it with a .py extension. Done!

“Done” means “but wait, there’s more!”

RIP

Let’s try creating a simple Python file. Save the following code as two.py:

1 + 1

Since we’re working with a file now, let’s use the python command to run the file instead of opening the REPL:

$ python two.py
$

Pikachu used Python! It’s not very effective…Why don’t we see a “2” anywhere?

Unlike the REPL, running code in files with Python requires you to explicitly print anything you want to see in the output. The code was executed, but the result vanished into thin air. Let’s try again, this time printing out the result of the addition explicitly:

print(1 + 1)

When we run the file this time, we see:

$ python two.py
2
$

Heck yes.

Let’s recap. Python can execute code stored in plain-text files. The Python interpreter behaves slightly differently when used to execute code in files as compared to being used as a REPL. As you think up more and more software, storing it in files will prove immensely useful.

I’ve received feedback that a post specifically about setting up a computer for use with Python would be pertinent; look for one soon! Meanwhile, comment here about any Python-y thing you want to learn more about!

--

--

Publishing Python Packages 🐍📦 ⬆️ Practices of the Python Pro 🐍📘 Technical architect at ITHAKA