Use Python Interactively with the REPL Console

Will Button
InstructorWill Button
Share this video with your friends

Social Share Links

Send Tweet
Published 7 years ago
Updated 6 years ago

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.

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.

egghead
egghead
~ 39 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today