Last updated: Monday 18th November 2024, 9:15 PT, AD

 

annedawson.net

 

 

 

How to use the Notepad editor to create Python programs by Dr Anne Dawson

 

 

Introduction

 

This web page explains how you can store several Python statements in one file to be executed in sequence by the Python interpreter.  This document assumes that the Python software is already installed and running correctly. For instructions on how to download, install and run the Python interpreter program on single line Python commands you go to this page: Python_Installation.

 

 

In order to create multi-line Python programs, you must have a text editor. Notepad.exe is a simple text editor supplied with all versions of the Windows operating system. This document explains how to use Notepad.exe to create a simple Python program file, and then goes on to explain how to execute (run) the program using the Python interpreter.

 

 

Once you've mastered how to create simple Python programs using Notepad, and run them using the Python interpreter, you're then ready to learn how to use a more sophisticated editor such as IDLE (supplied with the Python installation) and ConTEXT, designed for use with a number of programming languages including Python.

 

 

Both of these editors have a number of features to help you develop your Python programs including powerful syntax highlighting.

 

 

 

1.  Create a folder for your program files

 

Please note: the screen shots shown are generated from a computer running the Windows 98 operating system. If you have another version of Windows the screens may look slightly different, but the procedures you should follow are the same.

 

 

First, you should create a folder (directory) on your local drive C: drive to save your Python programs.  Go to the C:\temp folder and create a new folder named myprogs.  If you're unsure about how to do this, click on the  Start  button on the desktop taskbar, then click on   Help.

 

 

 

 

 

 

For the purposes of this document, it's assumed that the Python software is installed and a directory named myprogs exists in the temp directory of the C: drive:

 

 

C:\temp\myprogs

 

 

 

 

 

 

 

2.  Start up the Notepad program

 

 

On your desktop, click on the  Start   button, then click on   Programs , then   Accessories , then select    Notepad   from the list of options.

 

 

 

 

You will then see a Notepad window with a blank page. Type in the text exactly as shown on the next screen.

 

 

 

 

 

 

 

 

 

2.  Type in the program statements

 

 

 

 

 

 

 

 

 

3.  Save the program file

 

 

To save the file, select Save As from the File menu on the Notepad menu bar. Locate your folder C:\temp\myprogs folder by clicking the down-arrow button to the right of the Save in: text box. Type in the program name prog1.py in the File name: text box, then click the Save button.

 

 

 

 

Note that Python programs typically have a name which ends with the extension .py, in this case prog1.py, but other extensions (e.g. .pyw on Windows) can be used.

 

 

 

 

 

 

3.  Run the program

 

 

There are a number of ways to run the statements in a Python program file using the Python interpreter. Each of the following methods uses the file C:\temp\myprogs\prog1.py which contains these Python statements:

 

 

print "Hello World!"

print 1 + 1

print 2 + 2

print "Goodbye World!"

 

 

Running Python Programs - Method 1

 

 

On your desktop, click on the  Start  button, then click on   Run .

 

 

 

 

In the Run window, enter the following text in the text box, then click on the OK button.

 

 

c:\python23\python.exe -i c:\temp\myprogs\prog1.py

 

 

 

 

The Python interpreter then runs the program statements contained in

 

c:\temp\myprogs\prog1.py

 

leaving the interpreter in interactive mode:

 

 

 

 

As explained earlier, to exit from the Python interpreter you must hold down the Ctrl keyboard key and press the Z key at the same time. (Operating systems other than Windows may require a different key sequence, e.g. Ctrl-C).

 

 

 

 

 

Running Python Programs - Method 2

 

 

In the second method of running a Python program, you type a command at the MSDOS shell command line. From the desktop, click on the  Start  button, then click on  Programs , then   MS-DOS Prompt.  (In other versions of Windows, the MS-DOS prompt may have an alternative name, eg DOS Shell, Command shell, etc.)

 

 

 

 

 

 

 

 

Please note, other operating systems may default to a different folder than C:\WINDOWS. Notice that the prompt for MS-DOS is a single right arrow symbol >. The prompt for the Python interpreter is >>>.

 

 

Then type in the following text at the MS-DOS prompt, and press the Enter key to run the Python interpreter:

 

c:\python23\python.exe -i c:\temp\myprogs\prog1.py

 

 

 

 

 

 

 

The screen above shows the Python interpreter running in interactive mode. See Running Python - Method 1 for an explanation of interactive mode and instructions on the use of the Python interpreter with single Python commands.

 

 

As explained earlier, to exit from the Python interpreter you must hold down the Ctrl keyboard key and press the Z key at the same time. (Operating systems other than Windows may require a different key sequence, e.g. Ctrl-C).

 

 

If you run Python from an MS-DOS Prompt shell, exiting from Python returns you to the MS-DOS Prompt shell. To close the DOS shell and return to Windows, you must exit the MS-DOS Prompt environment by typing exit at the command prompt, then press the Enter key. An example of this is shown in the screen below.