How To Use Python Interactively

As we said in the “How to install Python” article, Python gives us a platform that we can use to do some fast calculations and run small code fragments and also for quick testing. This interactive platform is called REPL, which means Read, Evaluate, Print, and Loop.

You can open it by typing python 3 or python in terminal or command prompt. In the REPL, at the top, you will see the version of Python, which version you are using at the time. In it, you will see the three arrow prompt in which you can write your code snippet.

In this three arrow prompt, Python reads whatever you type, then evaluates it, prints the result, and returns to the waiting position to take the next command or we can say it back to the three arrows prompt.

I have given some uses of REPL below. Knowing which you can use Python REPL very easily. Believe it is very funny and easy.

Calculations

As I mentioned, you can use Python REPL for fast calculations. I am showing you some simple calculations here. Well, you can also do big calculations here, but for that, you have to take care of some things that we will learn in the coming articles.

python Calculations

I have shown some basic arithmetic operations here and you can do more operations here. When we write our code in a three arrow prompt and press enter, Python immediately reads the code, evaluates its result, prints the result, and comes back. You can also say that it returns the result of the command. But it is not necessary that it always returns the result. 

Variable Calculations

You can also assign a variable in Python and use it in calculations. This makes it very easy to do large calculations. I am showing you a calculation using the variables below.

variable calculations

Here I have assigned the value 7 to the variable x and the value 9 to the variable y and then added the x and y which gives us the 16 which is obtained by adding 7 and 9. We can also use the last returned value as a variable with the help of _ (underscore) in REPL. One thing to note here is that this trick can only be used in Python REPL. Underscore does not have any special significance in Python programs.

underscore

Side – Effects

There are some commands that do not return results at the time of the evaluation but give side effects. For example, if we assign a value to a variable x, then it does not yield any result, but there is a side effect of assigning a value to x.

side effect variable assign

You see in the picture that when we assign a value 7 to the variable x it does not return us anything. Instead, the value 7 is assigned to the variable x as a side effect. Which you can try by just typing x in the three arrow prompt.

variable assign call

I show another command or code which has a very visible side effect. Which is the print command. You can show any line on the terminal screen using the print command.

python

Here, after applying this print command nothing results, but the given line comes as a side effect on the screen. We can come out from the REPL  with the help of CTRL + Z in Windows operating system and CTRL + D in Unix (Linux and macOS) operating systems.

Do you know?
How To Use Python Interactively This article is contribute by Yash Goyal If you want share your knowledge on Study4Geeks then go to login section.