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.

Are Python Scripts Programs?

Python scriptWe have often heard Python referred to as a scripting language; in fact, often the terms “Python program” and “Python script” used interchangeably. So is Python a true scripting language?

Python Script: What a Scripting Language Is

In order to answer this question, we must first determine what a scripting language is. A scripting language is a language that incorporates support for scripts, and by scripts we mean programs written for a run-time environment (as opposed to programs that are compiled) and that automate the execution of tasks that could otherwise be executed one-by-one by a human operator. Examples of environments that incorporate support for scripting include software applications (programs like Excel and mIRC come to mind), web pages within a web browser, the shells of operating systems, and embedded systems. A scripting language can be viewed as a domain-specific language for a specific environment. Scripting languages can also be viewed as very high-level programming language (as opposed to a low-level programming language such as Assembly), as they operate at a high level of abstraction, or as control languages, particularly for job control languages on mainframes. Thus, we have at least two somewhat distinct ideas of what a scripting language is: [1] a run-time language that only works in a specific environment, such as a software application, or [2] a “glue” or control layer used to control and direct other application components. These categories are not mutually exclusive, but hopefully, the distinction will serve its purpose. To cover the first case, Python programs can indeed serve the role of a domain-specific language, and therefore such a program could be called a Python script. Python programs can be launched from console command lines and perform such tasks as processing text files and launching other programs, which is what we would expect a shell script to be able to do. But this is just one of dozens of common Python application domains, and Python is more than just a shell-script language. To cover the second case, Python programs are indeed often used as the control layer. To test hardware devices, Python programs may call out to components that give low-level access to a device; thus, changes made to the low-level drivers need not affect the control layer. In addition, programs may run Python code at certain points to support end-user product customization. This way, software can be tailored to a customer’s needs without having to recompile and ship the entire system’s source code. However, this is just a common Python role and a subset of what Python can do. Although we could call such code a Python script, Python is more than just a control language. Another sense in which we think of a scripting language is as a simple language for quickly coding tasks. In this sense, perhaps we can think of Python as a scripting language and a program as a Python script. It allows much faster program development than compiled languages like C/C++. But again, Python is not just for simple tasks, and programs can scale up in sophistication as required. That brings us back to the question posed in the beginning of this article: is Python a scripting language? Clearly it can be used as a scripting language, and it supports a rapid and flexible mode of development, but it is much more than just a scripting language. In future articles, I will explore some of the basics of writing a Python script, while also unleashing some of the more powerful functionality of this programming language.

External Links:

Python documentation at python.org