Python

How to Use Python REPL

“The Python Interactive Terminal is known as REPL. Read, Evaluate, Print, and Loop are abbreviated as REPL. On a Windows operating system, we will open the command prompt. Python statements may be effectively validated using the interactive Python script. Press the “enter” key after typing the instruction on the terminal with no arguments to activate the Python interpreter.”

It proceeds like this:

  • Read means requesting user feedback.
  • Eval means analyzing the data.
  • Print means to display the results for the user.
  • Repeat in a loop.

Let’s talk more about python REPL.

Open the command prompt to set up the interactive Python language terminal. We type “python” as a command. By inputting the python command and then using the tab key rather than clicking the enter key from the keyboard, we would see what version of Python is currently installed. The latest Python versions on the system will be returned by this.

After running the “python” command, we receive the results depicted in the image below. The version of Python that is installed on the system is indicated by this information.

Now execute the following commands into the terminal.

Example no 1

Make sure the cursor is placed behind the >>> (three greater than) symbols and that it is accessible. Here, we are going to perform some mathematical operations. Mathematical operations include adding, subtracting, multiplying, and dividing numbers. Different arithmetic operators are used to carry out these tasks. We type the command of addition as we want to add two numbers.

Python assesses every command given at the terminal. We append the value “6” by “3”. We eventually tap the Enter key. The corresponding image shows the outcome, including the entered expressions.

In the next command, we do subtraction. We declare two random numbers, which include 9 and 4. We subtract 4 from 9 by using the – symbol. In this screenshot, we get the result of the subtraction. The corresponding image shows the outcome, including the entered expressions.

We will multiply the integer 3 with the integer 9. The “*” sign can be used to do multiplication. We receive the result depicted in the figure. The corresponding image shows the outcome, including the entered expressions.

Shape Description automatically generated with medium confidence

Here we divide. We specify two random numbers, which include 9 and 5. We will divide from 9 by 5 use of the / symbol. We get the result of the division that is depicted in the subsequent figure.

Shape Description automatically generated with medium confidence

Example no 2

As we can see from the instance above, Python Shell only runs one expression.  We will make a Python file with the .py suffix and add Python code that contains several statements within it to execute them. Using a text editor like Notepad is the initial step in writing several commands. Then we will write two lines in the notepad; the first one includes “Check out the sample Python Code”, and the other includes, “Welcome to the most amazing tech website.”

The next step is to save the notepad document as abc.py, navigate to the directory where we stored the file by utilizing the command prompt, and run the python abc.py command in the terminal. It will display the data that we have added to the relevant file.

Example no 3

Now we will type the statement “hello world, here is kalsoom” in the terminal. By executing this command, it will raise an exception of “SyntaxError”. In computer programming, a syntax error is an error created by a person while using the syntax of any programming language. A compiler is a software system that detects syntax errors, which the programmer will have to resolve just before the code has been compiled and executed.

Here this error will occur because the statement which we want to print on the screen will not be enclosed in double quotation marks. The corresponding image shows the outcome, including the entered expressions.

Graphical user interface, application Description automatically generated

Here we will again type the statement “hello world, here is kalsoom” inside the double quotation marks so it will successfully run. The corresponding image shows the outcome, including the entered expressions.

Example no 4

In this example, first, we will initialize two variables termed “w” and “h”. We have given values 12 and 20 to these variables, respectively. The “w” variable represents the value of the width, and the variable “h” represents the value of height. In the next line, we will declare a new variable “s”. We want to acquire the size. So we apply formulas of the size “s”. The size can be determined by multiplying the width by height. In the end, we will call the print() method to show the resultant value of the “s” variable.

Shape Description automatically generated with medium confidence

The Method Used to Quit the Command Prompt

We must terminate the command prompt after executing various statements on the terminal. The Python interactive shell can be terminated by using different methods. One approach is to type the exit() function on the terminal. The parenthesis in exit() must be provided because this is executing the method exit.

Shape Description automatically generated with medium confidence

Conclusion

We have covered how to use Python REPL in this article. The interactive shell is called REPL. An interpretive platform is Python. It indicates that the program will be implemented line by line. A Python statement can be run, and the output is displayed using the Python Terminal, which is provided by the language. Various expressions have been executed in this article in the command prompt. We carry out various computations. In Python, the sign used for adding two values is +. Two values will be added using it. The operator for subtracting different values is the symbol (-). In order to subtract the second number from the first, it is used. The operator for multiplying two values is *. To determine the product of two values, use this. The division method is denoted by the sign /. Whenever the first integer is divided by the other value, it is utilized to determine the quotient. After creating a file, we execute several commands. The command prompt exit method is also depicted in the last figure.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content