Loading, please wait...

What is python shell?

May 21, 2020 Python shell, 1756 Views
This article will discuss the python shell.

Python is a language that is interpreted which means that it executes the code line by line. Python provides a Python Interactive Shell which is used to execute a single Python command to get the result. The python interpreter is usually installed by default in “/usr/local/bin/python3.8” when installing Python in the machine whereas, in Unix, if the shell’s search path is set to “/usr/local/bin”  it can be easily started by typing the python3.8 command.

Now, as the choice of the directory is an installation option to where the interpreter exists, other places are also possible. You can check on it with your System Administrator. (e.g., /usr/local/python).

How to open Python Shell on Windows?

Simply open the Command Prompt, write python, and press Enter.

As a result, a Python Prompt with three greater than symbols (>>>) appears in front of you. Now, you can enter a single statement to get the result. For e.g., calculate a simple statement like 5+2, press enter and the result will be displayed.

It can be used as a simple calculator to perform various arithmetic operations.

As you’ve seen in the above examples, Python Shell executes a single statement at a time. To come out of the python shell or to end it you have to type the command exit() or quit() and press enter after which you’ll see the three greater-than signs (>>>) will be gone and that’s how you’ll know you’ve come out of the shell.

Now, to execute multiple statements, you have to create a Python file with .pyextension and write Python scripts (multiple statements).

For e.g., Enter the following statement in a text editor (Notepad, Sublime Text, etc):

Save it with .py extension like the example above with the name test.py, navigate the command prompt to the folder where you have saved this file. If you have saved it in a separate folder or in a specific drive, right-click on the saved .py file, click on Properties and copy the Location as shown in the figure below highlighted in blue.

Type command “cd (file destination)” if your folder or file exists in the C: drive else you have to change the drive first by simply typing the drive. In my case, it’s the D: Drive so I’ll type D:and press enter and then I’ll proceed with the cd command. According to the example above “cd D:\python files” and press enter to change the directory.

Then, execute the script by writing the “python test.py” command as shown below.

And that’s how you can execute Python statements and commands using Python Shell.

Related Article