Python Programming from First Principles: Learn to Code, Think Computationally, and Solve Real-World Problems
PDFEPUB
CHAPTER GUIDE · PYTHON EDITORS

IPython exploration — return after the language basics

On this pageIPython shellTab CompletionIntrospection

These retained examples explore the IPython shell, completion and object help. They use concepts introduced later in the book. Return here after the language basics. Version banners and random values are illustrative.

IPython shell

IPython adds interactive commands to Python. Examples containing ?, ?? or a leading % belong in an IPython console or Jupyter code cell, not in a normal .py script. A transcript labelled In [20]: shows a prompt: type only what follows the prompt; an Out[...] line is a displayed result, not another instruction.

To launch the IPython shell, use the command ipython in your terminal:

$ ipython
IPython starts and displays an input prompt; the version banner depends on your installation.
Listing 24 — IPython shell

You can input Python statements directly and execute them by pressing Return (or Enter). IPython automatically renders a more readable representation of Python objects. For example:

In [1]: 2 + 3
Out[1]: 5
Listing 25 — IPython shell

The In prompt marks what you enter; Out marks the displayed result. You do not type these labels. Here IPython evaluates an addition and displays 5.

Tab Completion

One of the helpful features of IPython is tab completion, which makes coding faster and easier to explore. By pressing the Tab key, IPython automatically searches for names, functions, or paths that match what has been typed so far and then displays possible completions.

For example:

In [1]: age = 15
In [2]: area = "New York City"

# Typing 'a' followed by Tab shows all variables beginning with 'a'
In [3]: a<Tab>
Listing 26 — Tab Completion

Tab completion is not limited to variable names. It also works for object methods, attributes of imported modules, and even file paths.

x = [1, 2, 3]

# Typing 'x.' followed by Tab lists all methods available for lists,
# such as append, pop, or sort
x.<Tab>

# Typing part of a folder path followed by Tab expands or suggests
# matching file and directory names 
datasets/medical/<Tab>
Listing 27 — Tab Completion

In summary, tab completion is a convenient way to discover what objects, functions, or files are available in the current environment, without needing to recall their exact names. It enhances productivity and reduces typing errors, especially in interactive sessions.

Introspection

Using a question mark (?) before or after a variable provides general information about the object:

x = [1, 2, 3]
x?
Listing 28 — Introspection

Output of x?:

Type:        list
String form: [1, 2, 3]
Length:      3
Docstring:
Built-in mutable sequence.
Listing 29 — Introspection
print?
Listing 30 — Introspection

Output of print?:

Signature: print(*args, sep=' ', end='\n', file=None, flush=False)
Docstring: Prints the values to a stream, or to sys.stdout by default.

sep
  string inserted between values, default a space.
end
  string appended after the last value, default a newline.
file
  a file-like object (stream); defaults to the current sys.stdout.
flush
  whether to forcibly flush the stream.
Type:      builtin_function_or_method
Listing 31 — Introspection

This is known as object introspection. If the object is a function or instance method, the docstring, if defined, is also shown.

Using ?? will also display the function’s source code if available:

def add_numbers(a, b):
    """
    Sum two numbers 
    Returns
    -------
    the_sum : type of arguments
    """
    return a + b

add_numbers??
Listing 32 — Introspection

Output of add_numbers??:

Signature: add_numbers(a, b)
Source:
def add_numbers(a, b):
"""
Sum two numbers 
Returns
-------
the_sum : type of arguments
"""
return a + b
Listing 33 — Introspection

The question mark also serves for searching the IPython namespace similar to the standard Unix or Windows command line. Using a wildcard (*) will show all names matching the expression:

str.*upper*?
Listing 34 — Introspection

The matching names include str.isupper and str.upper. This inspects available names; it does not call the methods.