← Python for Data Analysis

PDA·02 · Chapter 2

Python Basics, IPython & Jupyter

Chapter 2 — the interpreter, notebooks, and the core of the language

The book's crash course in the Python you need before NumPy and pandas: how to run code, why notebooks, how names and objects really work, the basic types, and control flow. If you've done Python Crash Course, sessions 2.3 and 2.4 are where the new ideas are.

2.1

The Python Interpreter

  • Python is an interpreted language: the interpreter runs your program one statement at a time.
  • Type python in a terminal for the standard prompt (>>>); type exit() or press Ctrl-D (Ctrl-Z then Enter on Windows) to leave.
  • Run a whole file with python hello_world.py. Inside IPython, %run hello_world.py does the same and keeps its variables afterwards so you can inspect them.
  • Data analysts mostly live in IPython or Jupyter rather than the plain prompt — they add history, completion and rich output.
Interpreter
the program that reads and runs Python code
>>>
the standard Python prompt
%run
IPython command that runs a script in the current session
# hello_world.py
print("Hello world")
$ python hello_world.py
Hello world
Activity 1

Write hello_world.py, run it from a terminal, then run it again with %run inside IPython and check the variables it made still exist.

Activity 2

Open the plain python prompt and IPython side by side. Type the same three lines in each and list two things IPython does better.