Python Programming: Part One

Python programmingPython, as mentioned earlier, is an interpreted language. An interpreter is a program that executes other programs, and when you are doing Python programming, the Python interpreter reads your program and carries out the instructions it contains.

In its simplest form, a python program is just a text file containing Python statements. For example, the initial “Hello, world” program from the previous article is a very simple Python script, but it passes for a fully functional Python program:

print(‘Hello, world!’)

This program contains a single Python print statement, which simply prints a string to the output stream, and thus is a very simple example of Python programming. We can also output the results of numeric expressions if we want:

print(3+5)

This will print an “8” to the output stream.

Python Programming: Running Python Programs

Perhaps the easiest way to begin Python programming is to type in programs at Python’s interactive command line. There are many ways to do this: start it in an IDE, from a system console, and so on. Assuming that Python is installed on your system (we covered installation in a previous article), the simplest and most platform-neutral way to start the Python interpreter is usually just to type python at your operating system’s prompt, without any other arguments. The notion of a system shell prompt is cross-platform, but how you access it varies by platform:

  • In Windows, you can type python in a DOS console window (the Command Prompt), found in the Accessories section of the Start -> Programs menu, by right mouse-clicking on the window icon on the lower left corner of the screen in Windows 8, or in the Start -> Run dialog box (run “cmd.exe”).
  • On Unix/Linux/Mac OS X, you might type this command in a shell or terminal window.
  • On handheld devices, you generally click the Python icon in the home or application window to launch an interactive session.

If you have not set your shell’s PATH environment variable to include Python’s install directory, you may need to type the full path to the Python executable on your machine (e.g. c:\Python34\python in Windows for version 3.4). The installer should update the path, however, and I have not had to type the full path under Windows or Linux. Of course, you can always use the change directory command to go to the Python install directory.

In Windows, you can also begin interactive sessions for Python programming by starting IDLE’s main window or by selecting the “Python (command line)” menu option from the Start button menu (in Windows 7 and earlier). I made a desktop shortcut for Python in Windows 8. Both the command line Python interpreter and IDLE spawn a Python interactive prompt with equivalent functionality.

When coding at the Python interactive prompt, we can type as many Python commands as we want, and each command will be run immediately after it is entered:
>>> print(‘Hello, world!’)
Hello, world!
>>> a = 3+4
>>> print(a)
7
The first command prints: Hello, world!. The second command assigns the sum of 3+4 to the variable a, and the third command prints out the value of a. Note that because the interactive session automatically prints the results of expressions you type, so you do not even need to say “print” at the prompt:
>>> a
7

We did not do much with the code in this article, but hopefully it served as an adequate introduction to Python programming, and the main point we want to remember here is that the interpreter executes the code entered on each line immediately, when the [ENTER] key is pressed. There is no need to create a source code file, and no need to run the code through a compiler and linker first, as you would do with a language such as C or C++. As we will see in future articles, you can also run multi-line statements at the interactive prompt. Such statements run immediately after you’ve enter all of its lines and pressed [ENTER] twice to add a blank line.

External Links:

The official Python site – contains many useful resources for Python programming.