Trying Code in the Interactive Shell

Concepts covered: Scope

Running python with a filename executes a script: Python reads the file, runs it top to bottom, and exits. Running python with no filename opens the interactive shell. It shows a >>> prompt, runs each line the moment you press Enter, and waits for the next one. Everything in this lesson works in both, with one difference that changes how each is used.

You type at >>>The shell shows
2 + 35
total = 10 * 4(nothing; an assignment has no value to show)
total40
"ab" * 3'ababab'
print(total)40
import math(nothing)
math.sqrt(2)1.4142135623730951

The shell prints the value of any expression you type, with no print call, and shows a string with quotes around it so that '40' and 40 can be told apart. A script does none of that. Put the same lines in a file and run it, and only the print line produces output.

2 + 3
total = 10 * 4
total
"ab" * 3
print(total)

Output:

40

Lines 1, 3 and 4 were computed and their results thrown away. In a script, output comes from print alone; in the shell, output also comes from the expression echo. A value you saw in the shell goes silent in a script until you wrap it in print.

You paste these 3 lines from a shell session into a file and run it as a script. What is printed?

import math
math.sqrt(49)
print(math.floor(7.0 / 2))

Blocks in the shell

A header line in the shell switches the prompt to ..., meaning a block is open. Type the indented lines, then press Enter on an empty line to close the block and run it. That blank line is what the shell needs, unlike a script, where dedenting alone closes a block.

PromptYou typeShell shows
>>>for i in range(3):
... print(i * 10)
...(empty line)0, then 10, then 20
>>>i2

In the last row, i still holds its final value after the loop, because the shell remembers every name you assign until you close it. Close the shell with exit() or Ctrl-D and all of it is gone, which is why anything worth keeping goes in a file.

Which one, when

Interactive shell
  • Checking what an expression evaluates to
  • Seeing what a function returns before relying on it
  • Looking around a module: dir(math) lists every name in it
  • Trying one line of a bigger script on its own
  • Reproducing a traceback with the exact values that caused it
Script
  • Anything you will run twice
  • Programs longer than a handful of lines
  • Work that reads input() or prints for a user
  • Code someone else will read or run
  • Anything that must survive closing the terminal

The 2 work together. Try an expression in the shell until it gives the value you want, paste that line into the script, and run the script. When a traceback points at a line, type that line into the shell with the same values to watch the failure up close. dir(math) and help(math.floor) answer most questions about a module faster than a search does.

Capstone

The capstone pulls the 5 sections into one short script: an import, an indented block, a value standing in for user input, and a bug that produces a traceback. Predict the traceback first, then decide the fix.

The variable typed stands in for what input() returned. Where does this script fail, and what fixes it?

import math

typed = "12.25"
if typed > 0:
    root = math.sqrt(typed)
    print("root:", root)
print("done")

A traceback reports only the first failure, because the program stops there. Fix that line and rerun, and the program gets further, which can expose a second bug of another kind, such as a print whose indentation keeps it from ever running.

About this lesson

This section is part of the Python Foundations: Intermediate lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.