There are several ways in which we can interact with Python's Interpreter so that we can run and generally interact with  python programs.

The command line interface comes in handy when it comes to interacting with Python. We will start by examining how we can run a Python Program directly from the Command Line  in the interactive mode and then later we will look at how we can use a text editor or an IDE to edit a python source code and then run the resulting file.

You will need to have Python installed in your computer. You can check  on Python Installation Guide  if you don't have Python running on your machine.

Starting Python.

On your computer open the command interface, there are various way that you can do this depending on the operating system your computer is running.

follow the steps shown below to open Windows Command Interface: 
  1. Press  "Windows Key + R ".
  2. On the dialog box which opens, type cmd then press okay

The run dialog box

You can alternatively  search cmd on  the Windows search   area and  follow through the search results to open the  command line.

To open the Linux Shell :

Press  "Ctrl+Alt+T" 

To open the terminal on Mac:
  1. Press "Command + Space Bar" On the keyboard
  2. Type "Terminal"
  3. Open  the Terminal from the search list

You can check the command interface, to see more about the  Command line interface in different operating system.

Type the following command on  the opened cmd/shell:

python

then press "Enter" to start the Python Interactive shell.

The command line interface should now change to show you that now you are on  Python's shell. You should ensure that the version of python shown is 3.x  for example in my computer I have python 3.11.2 as shown below.

Invoking the Python Interpreter

Running the first Python Program Interactively.

We will begin by running our very first  program interactively, this program simply outputs the words Hello, World!   to the console.

Type or paste the below python statement on the python's interactive shell and then press Enter.

print("Hello, World!")

You should see the words printed on the line just below  the one you have entered the above statement.

A  Hello world program on python interactive mode

When python programs are run this way, through the python interactive shell they  are said to be running on interactive mode. In this mode, we enters a statement, it is executed and we get the output, before entering another statement.

The Python interactive  shell lies between the operating system and the user so that the user doesn't have  to use complicated operations of the OS  to interact with the underlying Python language, they can instead use the shell which is simpler and easy to  understand and use. 

The process used by the shell is as outlined below:

  1. read a statement from user
  2. evaluate the statement entered
  3. Show the results to the user
  4. Repeat(loop)
Some things to note about Python's Interactive mode:
  • Statements are entered and evaluated one at a time.
  • Indentation is highly sensitive, and the statements should by default begin at the furthest left level of the shell.
  • Statements inside the same level of a block should be at the same level of indentation.
  • The print function can be foregone in statements and the results will still be displayed if the statement evaluates to a printable value.

Using Python Interactive shell as a calculator

Given some  features of the Python interactive shell outlined  above , the  shell forms a nice tool for use as a calculator.  When you enter a math expression, it is evaluated and the results are displayed immediately at the line just  below the statement. 

1 + 1
//2
5 + 7
//12
20 - 13
//7
15 * 10
//150
100 / 10
//10.0
57 / 7
//8.142857142857142

You can type more complicated mathematical expression using brackets to define the order of execution. The interpreter will use established evaluation hierarchy in math  such as bodmas or whatever way you want to call it. an example of such an expression is:

( 7 * (5 + 9) / (3 + 4) )

A math expression for interactive python

Exiting the Interactive mode

Sometimes you want to leave the Python interactive Shell without closing the Command interface , It is also a good practice to close the Python Interactive mode using a standard method so that the interpreter can do the necessary cleanups before ending the session.  There are two methods which we can use to close the interactive mode:

  • quit()
  • exit()

If you call any of the above builtin functions when  in the Python interactive Shell, the session will be terminated. 

Python Script Mode

The interactive mode has a lot of limitations when it comes to programming outside educational purposes. Consider some of these limitations as outlined below:

  • The interactive mode does not offer user friendly features such as syntax highlighting or auto indentation.
  • If a mistake occurs in a statement or a block , You will have to rewrite the whole statement/block.
  • The outputs are sandwiched between statements/commands making it hard to follow through the outputs of the program.
  • The commands you write and run  are not saved anywhere and if you want to run the same program again, you will have to rewrite the program from scratch.
  • It does not allow code reusability across different interactive programs, for example you cannot call a function you've declared in an interactive session outside that session.

A Python script is a file that  contains statements of a python program, which are meant to be executed by the python Interpreter. In reality, the python statements in the script  are also executed one line at a time  but the script mode offers far more flexibility and versatility  compared to the interactive mode. The process of using the script mode is as  outlined below: 

  1. Edit or modify  a python program's source code in an Editor/IDE
  2. Save the source code in a python file(s). 
  3. Run the program  in the Python Interpreter.
  4. repeat 

The script mode has a lot of advantages over the interactive mode, for example if you need to do modifications to the Program, you will only need to alter the file containing the source code instead of rewriting the whole program again.

Let us write and run the program to print Hello, World in script mode.

Create a python file and preferably name it hello.py , If you are on Windows  you can run the below command to create a Python file  from the command line :

type con > hello.py

Make sure you run the above command in a directory which you will be able to access the file  easily.

When the file is created open it in your favorite Editor/IDE,  in my case i will use the Sublime Text  editor.  You can check  top IDEs and Editors for python programing  to see more editors and IDEs.

type or paste the following statement in the editor:

print("Hello, World!")

Python hello world program in script mode

Hello, World!

Most IDEs and text Editors offers ways to invoke the   Python interpreter  within , meaning that you can edit and run the program without leaving the editor/IDE. For example I can run the above program in Sublime Text by pressing "ctrl + B"  . Sometimes that is not the case or one might be required to install other plugins in order to run the Program in the editor/IDE .  The most mechanical way  to run a program If you already have it saved in a file is to call Python from the terminal followed by the name of the Python file. For example to run the above program from the terminal you can run the command shown below:

python learn.py

This is provided that you are in the directory where the file is, otherwise you would need to provide the full path to where the file is located. The resulting output will be similar to the interactive version except that the output is not mixed with the python commands.

my learn.py file is located on desktop so I will need to change directory from User to  desktop 

running python hello word program in script mode from terminal