Explain This To Me As If I Were a Small Child – Python Interactive Interpreter

I used to work with a guy who would, when trying to understand the mess of a design I just made, ask me to (facetiously), “explain it to me as if I were a small child.” I never liked teach things that way because it always felt insulting to the target audience. However, I sometimes find myself in situations where people I’m teaching say the following:

Him: “Wait, I didn’t know you could do that!”

Me: “Oh, I just thought you knew.”

Now I’m not insulting, I’m a terrible teacher.

So, let’s start from the beginning, as if you were a small child.

Python is an interpreted language. It means that the script that you write isn’t compiled directly into machine code. It get sent into an interpreter that then translates it into machine code on the fly. We’ll come back to this in a second.

What happens when you want to jump in and try a language? Usually, you start with your favorite editor (hopefully not Notepad), write your generic “Hello, World!” program, load that file into the interpreter, and marvel at what your fingertips hath wrought.

Let’s do one better. Let’s launch the Python interactive interpreter.

$ python3
Python 3.5.1+ (default, Mar 30 2016, 22:46:26) 
[GCC 5.3.1 20160330] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

Woah, what’s this? It’s an interactive Python shell. You can try Python code out live. Go ahead. Try it.

>>> print("Hello, world!")
Hello, world!
>>>

There. Hello, world, without having to touch Notepad. (You should really consider a better text editor).

What’s the point of this? You can, of course, learn about the language and try out new reserved keywords. It’s also good for trying out new libraries, seeing the exact format a function returns.

When you’re working with a new library, it would be nice to know what classes are in a package, or what methods are in a class. Traditionally, you would call dir() to see what is available.

>>> import json
>>> dir(json)
['JSONDecodeError', 'JSONDecoder', 'JSONEncoder', '__all__', '__author__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__version__', '_default_decoder', '_default_encoder', 'decoder', 'dump', 'dumps', 'encoder', 'load', 'loads', 'scanner']

It would be nice if we could get some tab completion in here. Enter ipython. You can get it via apt, yum, or pip.

$ ipython3
Python 3.5.1+ (default, Mar 30 2016, 22:46:26) 
Type "copyright", "credits" or "license" for more information.

IPython 2.4.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.

In [1]: import json

In [2]: json.
json.JSONDecodeError json.dump json.loads
json.JSONDecoder json.dumps json.scanner
json.JSONEncoder json.encoder 
json.decoder json.load 

In [2]: json.

Tab completion!

Now get out there and start exploring.