Use Python Interactively with the REPL Console

Python includes an interactive REPL (Read - Evaluate - Print - Loop) console that allows you to easily test and interact with the Python interpreter. You will learn how to use it in this lesson to quickly write and execute code.

Code

You must be a Member to view code

Transcript

If I just type the Python command, it takes me into the REPL interface, or the interactive Python console. I can type 2 + 2, and that returns 4, and so REPL stands for Read Evaluate Print Loop.

When I typed 2 + 2, that was the read phase. I hit enter, it evaluated it, and then it printed 4 and looped back again, back to the cursor, waiting for my next input.

You can do anything here that you would do in a Python application. I can type print hello world, and it prints out hello, or I can use print as a function, and that works, too, but let me show you this real quick.

If I go into Python 3 -- this is one of the differences between Python 2 and Python 3 -- print is available only as a function in Python 3. You can also import modules like the datetime module, and use that.

Like I just mentioned, print is a function in Python 3, so that didn't work. There it prints out the current datetime, or the datetime is whenever we established that variable.

You can also create functions. We can say add number 1 and number 2, and then return the sum of number 1 + num 2. We can call that function by calling the function and passing in our parameters, and it returns a sum.

Here's the one caveat to using the REPL interface. Whenever I exit this, if I go back into it and I try to run my add function again, it doesn't exist, because whenever you close out the REPL interface, all the functions that you created, all the work that you've done has been destroyed unless you saved it out to a file.