Python in a Command Window

A computer program is a collection of instructions that a computer runs to perform a task. In our case, the instructions will be written in the Python language. For now, let us see how we can work with Python using the command line in Microsoft Windows (On a Mac it is called the Terminal window). The command line in Windows is an environment where the user types commands rather than clicking with the mouse. In “old school” terms, it is a shell system that uses text from the keyboard to provide instructions to the operating system. An advantage of the shell window (or command window) is that the interface can be faster, depending on how fast you type, but the disadvantage is that you must learn a whole new set of commands to type.

To open the command window, click on the Windows 10 Start button and type cmd and press the ENTER key. A window like the one below will be displayed.

You now have access to the Python programming environment! Simply type Python and press the ENTER key. This begins the Python environment. Notice that this version is running Python 3.8.3? The >>> is the command prompt indicating that it is waiting for you to type Python statements.

To exit the Python shell window type exit() and press ENTER. Another way to exit is to press CTRL Z and press ENTER. Control will be passed back to the command window. To leave the command window type exit and press ENTER.

Watch the following video for how you can use Python in a command window.

If you had a file that contained Python source code, you could execute that file at the command line. For example, if the file name were MyScript.py you could execute the file by typing the following at the command line prompt

python MyScript.py

Then press ENTER. That would execute the source code contained in the MyScript.py file.

NOTE: the py extension indicates the file’s contents contain python source code. Much like .JPG, .CSS, or .HTML indicate the contents of a specific file

What does this mean to you? Do you really think you are going to type in all your python source code in the command window? That would defeat the purpose of having a visual programming IDE.