introduction to python programming
Before getting into the specifics of the python language, let’s take a little time to understand the principles behind the python language and what it can and can’t be used for. let’s also look at who uses python and how python differs from other programming languages.
Python has a long history-noticeably longer than most people probably realize. In the year 2000, the Python development efforts underwent a great deal of reorganization as Guido van Rossum, the designer and primary developer of Python, and the rest of the Python team (including Tim Peters, Barry Warsaw, Jeremy Hylton, and Fred Drake) moved the development efforts first from CNRI (Centre for National Research Initiatives) to BeOpen, and ultimately, to Digital Creations.
Python Up to 1.5.2
Up until September 5, 2000. Python was developed and released through a public license supported by CNRL Python 152 contained most of the functionality of Python that we are familiar with today. The final 132 release was released on April 13, 1999.
Python 1.6 (September 2000)
In September 2000, after Guido left CN, two versions of Python 16 were released. The last version cattle from CNR and was their last official release of the Python language Python 1bt, the beta version of the language, had been in development and testing for some time, so it was not a complete surprise to see Python Lobl released by Guido van Rossum and the BeOpen development team. Version 1.6 included some minor improvements, including a change in the way list objects worked and some improvements in the socket and string-to-number conversion tools.
Python 2.0 (September 5, 2000)
Within 24 hours of CNRI’s announcement, Guido and the rest of the development team released version 2.0b1 (a beta release) of Python to steal a coup from CNRI’s 1.6 release. The entire Python team moved from CNRI to the BeOpen initiative (www.beopen.com) on May 5, 2000, to continue the development of Python outside of CNRI and under an open-source agreement.
Version 2.0 included some important updates to Version 1.6, including new operators, new list syntax, and better module-importing methods. Version 2.0 also included one of the most significant updates to the standard Python library for more than a year, fixing numerous bugs and adding several new features and an entirely rewritten suite of XML tools The final version of Python 2.0 was released on October 16, 2000).
Aside from the obvious language improvements, the move to BeOpen also enabled the development team to make other improvements for Python behind the scenes, such as moving the Python sources to SourceForge (www.sourceforge.net) and executing the final stages for moving jpython, the old Java-based Python interpreter, to the new Jython hybrid.
Python 2.0 (October 28, 2000)
On October 28, 2000, Guido announced on the newsgroups and the main Python website that the Python team had moved again, this time to Digital Creations. Digital Creations are the people behind Zope (Z-Objects Publishing Environment), one of the best-known Python projects.
No further releases have come out since the Python 2.0 release from BeOpen, but work is planned for the new Python 3000 product, due to be released sometime in 2002 Hopefully, given Digital Creations’ existing Python evangelism, there shouldn’t be any further moves for the Python team.
Python 3000 itself is expected to be a major update of the language, in much the same way that Perl 6.0 is expected to be a major update to Perl 5.6. Python 3000 is unlikely to be the final name of the new version of Python, it’s just a code name for the new version.
What Is Python?
Python is an interpreted language that employs an object-oriented approach. It’s a high-level programming language, which means that it separates the user from the underlying operating system as much as possible. However, unlike other languages, Python provides you with the ability to access the operating system at a lower level if you desire. Because of this ability, Python is often classified somewhere between such languages as Visual Basic or Perl and the system-level C language.
Although Python is considered an interpreted language like Perl, Tel, and some others, it employs a compilation stage that translates the raw-text Python script into a series of bytecodes, which are then executed by the Python Virtual Machine. The use of the compilation and bytecode stages helps to improve performance and makes Python much faster than pure interpreters such as BASIC, but slower than the truly compiled languages such as C and Pascal. However, unlike many other languages, the bytecode versions of modules can be saved and executed without having to recompile them each time they are required, thereby improving performance by eliminating the compilation stage. Note that the bytecode that is created is completely platform and operating-system-independent, much like the bytecode produced by Java.
Python also has the added benefit of providing rapid application development on the macOS, Windows (95/98/NT), and Unix platforms. Python is supplied with a module to the Tk interface libraries, and it’s possible to write an application on one platform and use it on all three platforms without making any modifications, In addition to the core platforms, Python also runs on MS-DOS, Amiga, BeOS, OS/2 VMS. QNX, and many other operating systems. You can even run Python on your Psion organizer!
Before going any further I should probably explain the name. The name Python is taken from the comedy group Monty Python, which is best known for the talents of Eric Idle, John Cleese, Terry Jones, Terry Gilliam, Michael Palin, and Graham Chapman.
Python Is Free
Although this is not that unique many of the most popular programming languages are available free-it does mean that you can write and deploy Python programs without having to purchase any software and without having to worry about licensing issues. You can even download the source code to the software if you want to take a closer look at how the Python language works.
Being free often means that there is little or no support. With Python, this is not the case. There is a huge following of Python programmers, and the people involved in t developing Python are always welcome to help new users get to know the language. There are also a number of commercial companies and individuals who provide custom programming and more in-depth support if you need it and are willing to pay for it.
Python Is Portable
Python is supported on a huge range of operating system platforms. It comes in ready compiled format for Windows and macOS and includes the Tk extensions so you can develop user interfaces. On Unix and all other platforms, Python is available as source code which you compile yourself You can also visit one of the websites offering precompiled binaries (see Appendix A). In all cases, compatibility is invisible. You can write a script on a Unix platform, and 95% of the time you can execute it without modification on a Mac or PC. Because of the Tk support, you can even run the same GLII-based application on all three platforms without any major modifications and have a consistent user interface in each case.
In addition to the native cross-platform capability, Python also supports some platform-native extensions to help ease the porting process or bridge gaps with other languages and environments. For example, the SunOS/Solaris implementation includes a driver for Sun audio devices, and the SGT version comes with tools for interfacing to the audio and video capabilities (including OpenGL) built into SGI workstations The Windows Python interpreter comes with toolkits to interface to the Visual C++ libraries and the Windows audio drivers. You can even communicate with COM (Component Object Model) objects.
Python Is Powerful
It is very little you cannot do with Python. The core of the language is very small, but it provides enough of the basic building blocks to allow you to design most applications. Furthermore, because the language can be extended using C, C++, and even Java in certain circumstances, you should be able to develop any type of program. The Python interpreter actually comes with a huge library of additional modules that extend the capabilities of the language to allow network communication, text processing (including extensive XML support in Python 2.0), and regular expression matching.
Although Python’s main objective is to hide much of the low-level complexity from the programmer, it also supports the necessary hooks, extensions, and functions to allow low-level access to certain areas of the operating system. By supporting both the high-level and low-level functionality, Python can be used at the same level as C or at the same level as Visual Basic, as well as at all the other levels in between You can even use Python as a macro or application extension language by embedding it into your applications, much like visual basic is the Macro language used in Microsoft office products.
Python Is Extensible
Because Python is written in C (some extensions are written in C++) and because you have access to the source code, you can also write extensions to the language Many of the standard modules supplied with the language are supported by a Cor C++ interface. This includes basic facilities such as networking and DBM database access, along with more advanced toolkits such as Tk.
In addition, Python can be embedded into Car C++ applications so you can provide a scripting interface to your application using the Python language. Because of the support for cross-language development, you can use Python to design and conceptualize an application and then port it, over time, to C. There is no need to rewrite the application in C before using it: Python and C can work together in tandem.
Finally, Jython is a complete Python interpreter written entirely in Java. This means that you can write a Python program that interfaces to Java objects, or you can write a Java application that uses Python objects. Better still, because the interpreter is written entirely in Java, you can deploy a Python application on any platform that supports lava-even a web browser can directly execute a Python script.
Python Is Easy
Once you understand the basic principles of the Python language, learning the rest is easy. The core of the language is very small, and its semantics and style are very simple. Since all the other components and extensions use exactly the same syntax and structure, you should be up to speed programming in Python like an expert in no time at all.
That’s not to say that there aren’t complexities in the Python language that you’ll need to leam. Many of the extensions and libraries require some careful thought to make effective use of the language. This is especially true when you start to integrate and interface with other languages such as Tk and SQL.
What Is Python Good For?
Not surprisingly, with such wide support and extensive features, Python is very effective for a large number of tasks. Here’s a quick list of the more common uses of the Python language.
Python supports an extension called NumPy, which provides interfaces to many standard mathematics libraries. The Python language also supports unlimited precision. If you want to add two 100-digit numbers together, you can do so with Python without requiring a third-party extension. If you need real speed in mathematical applications, the NumPy extension is written in C and as a result, operates faster than the native math supported by Python.
Python can split, separate, summarize and report on any data. It comes with modules that separate out the elements of a log file line. You can then use the built-in data types to record and summarize the information before writing it all out again. In addition, Python comes with expression libraries that allow you to use the same expressions as Emacs, Perl, and many other utilities. This means that Python can do all of the things that other languages can do. For example, a number of programmers have produced a complex SGML-processing tool using Python.
Python actually comes with SGML, HTML, and XML parsing modules for reading, writing, and translating the different formats With Python’s support for other text processing engines (regular expressions and the natural splitting/combining of information) and flexible variable and object handling, Python becomes a very useful tool for the text-processing programmer.
Rapid Application Development
Because Python is so straightforward to develop applications with, you can use Python to develop applications very quickly. The extensive module library that comes with Python provides direct interfaces to many of the protocols, tools, and libraries that you would otherwise have to develop yourself. Furthermore, because scripts are interpreted and don’t have to go through the normal compilation and linking stages, you can make modifications very quickly. If there’s something that you need to test quickly, you can easily check it with the interpreter shell before actually making changes to your program, and you can even interactively debug your script using the same shell and environment that you use for programming.
Also, because Python supports Tk, you can have not just an interface example, but a complete application in hours instead of days. I’ve written a cross-platform, Tk-based database query tool in less than a day, something that would have taken me two or three days with Perl, and a week or more in C.
You now know that Python supports a wide range of platforms in a completely neutral format. If you are deploying an application across a network that uses a variety of different platforms, you can use Python to develop the application. Using Python, you will never need to rewrite your software as you move between platforms Of course, you can also look at Python as an alternative when supplying software to end users. Instead of developing three separate applications, with operating system-specific compilation processes, testing systems, and interfaces, you only have to develop it once, saving significant time and money.
Although much of the ethos of Python is to hide from you the low-level parts of the operating system, the tools and extensions are there if you want to access the lowest levels. Because Python has access to the same set of functions as the operating system, you can use it to duplicate and extend the functionality of the operating system, while still retaining all of the compatibility and interface issues that you already know Python supports.
Python comes with a standard set of modules that allow you to communicate over the network sockets, both at a basic level and at a protocol level. If you want to read an e-mail from a POP server, for example. Python already comes with a library module that enables you to do that. In addition. Python also supports XML, HTML., and CGI libraries so you can parse user input and produce top-quality formatted output via a web server.
In fact, the combination of Python’s high-level module support and RAD powers gives you an enormous, but speedy, development toolkit. Most Internet modules allow you to communicate directly with an Internet server using a simple object class and a number of different methods. One programmer even managed to write a newsreader entirely in Python within a couple of afternoons. On my home network, I have a Web-based e-mail interface to my IMAP server that took me about an hour to write in Python.
You can even compile a module for Apache, Unix, and the Windows web server that embeds the Python interpreter. This means that when you want to execute a Python script the interpreter does not need to be loaded separately each time, thus providing the maximum possible performance from your CGI scripts.
There is a myriad of extension modules that interface to all of the common database systems, from Oracle to Informix, and free systems such as SQL, and MySQL. There is even a toolkit called Gadfly that provides a complete SQL environment within Python-no external modules or extensions are required. Because Python has strong text-and data-handling abilities, you can use Python to interface between databases and act as a better summary and report tool than many of the interfaces that come with the database systems themselves. Furthermore, because Python supports a number of different operating systems, instead of only one, you can use the same interface with any database. You can even use Tk to build the front end and then put it on any of the supported platforms-you’ll get an instant cross-platform. database independent query tool!
Python can be used for anything there are literally no limits to what this language can do. By supporting a small core set of functions, data types, and capabilities, Python provides an excellent base on which to build. Because you can extend the functionality with C and C++, you get the best of both worlds-unlimited and unfettered expansion to do whatever you want, but in a structured and manageable format.
What Isn’t Python Good For?
It’s very difficult to give a precise list of the problems that Python is unable to solve. Python provides most of its functionality in the extension modules that are supplied with the language, and this just demonstrates how easy it is to add functionality to the language. If you can’t do what you want to within Python, then it’s just as easy to write a C or C++ extension to do the job for you.
Some people criticize Python not because it’s not capable of doing a particular task but because they don’t understand how to do a particular task. One of the most common complaints relates to Python’s apparent lack of regular expression support, when in fact there are two modules (re and the older regex) that enable you to handle regular expressions, regex even supports the same syntax that is used in Perl. Regular expression handling may not be built into Python, and it probably isn’t as fast as Perl’s expression handling, which has been highly optimized over the years, but it’s still possible.
The advantage of Python over a language such as Perl, Rebol, or Java is that the core of the language is very small. This improves the execution time there’s less code to be loaded each time the script is run-and helps make the rest of the language easier to learn and more flexible.
Once you are familiar with the minimalist style of programming that Python supports, you’ll find that you still have all the power, but without the extra baggage. And you can read your code the next day, week, or year you look at it.
Who Uses Python?
Python is used by a large number of people for solving all sorts of tasks. Most of these are not well known, or at least not publicized, purely because the companies concerned do not normally divulge this sort of information. However, there are some larger companies that use Python within a commercial environment and that are proud to announce and even celebrate the fact.
•Red Hat (www.redhat.com), which makes the popular Red Hat Linux distribution, uses Python in combination with Tk to provide a visual interface for configuring and managing the Linux operating system. The configuration system gives you complete control over all aspects of the Linux operating system and automatically updates the configuration files according to your selections.
•Infoseek (www.infoseek.com) uses Python within certain parts of its public search engines. Python is also used for customizing the Infoseek software that can be downloaded from this site for use on end users’ machines.
•NASA (www.nasa.gov) uses Python in a number of different areas. The most significant use of Python is in the Mission Control Center, where Python is used in certain parts of the system for planning missions. Other uses take advantage of the strong numerical abilities of Python, which make it ideal for calculating the location of celestial objects and for plotting the paths taken by satellites.
•Industrial Light and Magic (www.ilm.com), famous for doing special effects on films such as Star Wars. The Abyss, Star Trek, and Indiana Jones use Python to produce commercial-grade animation. In fact, if you visit their website, you’ll see they have a number of vacancies for Python programmers!