More Expressive Functions
Meet Python’s REPL
Until now, you have mostly written a complete program in the editor and then clicked Run. That works well when several lines belong together. Sometimes, however, you only want to try one small piece of Python and see the result immediately.
The panel beside this lesson offers another way to work with Python: the REPL. The name is often pronounced “repple.”
REPL stands for:
Read the Python code you entered;
Evaluate it by running the code and working out its result;
Print that result when there is one;
back and wait for more code.
An 2 + 3 evaluates to the 5. The REPL displays that value and then waits for more code. There is no hidden trick: Python works out the result, and the REPL shows it.
Each complete line or block you submit is evaluated. Some code, such as an
You do not need to memorize the four words behind REPL. Think of it as a conversation: you give Python some code, Python evaluates it, shows any result, and then waits for whatever you enter next.
Read the prompts
When the REPL is ready, it shows this prompt:
>>>
The prompt means “Python is ready.” You type after it. Do not type the three > characters yourself.
Try entering this in the REPL:
2 + 3
The full exchange looks like this:
>>> 2 + 3
5
>>>
Python reads 2 + 3 and evaluates the expression. The result of that evaluation is the value 5, so the REPL displays 5 and then shows a new prompt.
Expressions show their values
In a program, you usually call print() when you want to see a value. The REPL automatically displays the value produced when it evaluates an expression. The Print in REPL describes this display step; it does not mean that the REPL secretly adds a print() call to your code.
Try these expressions one at a time:
"python land".title()
len([10, 20, 30])
10 > 4
The .title() returns a new string with each word beginning in uppercase. Here it gives you one concrete method to explore; the REPL displays its returned value immediately.
You can still use print() in the REPL, but you often do not need it for a quick experiment.
Names stay available
Now assign a value to a name:
course = "Python Fundamentals II"
The REPL displays no result for an assignment. That does not mean it failed. Enter the name on the next line:
course
The REPL remembers course and displays its value. Values and functions remain available while the REPL session is running. The Restart button starts a fresh session and clears them.
You can also press the up arrow to bring back a command you entered earlier. That is useful when you want to change and retry it.
Enter a function across several lines
A function definition needs more than one line. Enter the first line:
def double(number):
The prompt changes from >>> to .... This continuation prompt means that Python is waiting for the rest of the definition. Enter the indented body:
return number * 2
Then press Enter once more on the empty ... line. The empty line tells the REPL that the function definition is finished.
The whole exchange looks like this:
>>> def double(number):
... return number * 2
...
>>> double(6)
12
The >>> and ... prompts are part of the REPL display. They are not part of the function’s Python code.
An error does not end the session
If one instruction fails, the REPL shows the error and then returns to >>>. You can correct the instruction and continue experimenting. Values you created earlier remain available.
If you want to discard everything and begin again, use Restart.
What should you type when the REPL shows >>> and you want to calculate 4 + 5?
Explore before moving on
Use the REPL to calculate a value, store one in a name, and define one small function. Try the up arrow and Restart as well.
The next lesson keeps the REPL open and uses it to inspect the documentation stored inside functions.