← All posts

Introduction to Python

python\_1

I’m currently learning Python and decided to share some notes about the language, much as I did when I wrote about Java. I’ll still be writing notes on Java, so don’t worry…

Right now, my priorities simply put Python first! To get some serious practice, I’m going to use it together with Django to develop Liburg (a catalog of IT books with reviews and essays).

These notes are based on Learning Python, a book I bought recently; you could even call them study notes. Let’s get down to business and start with an introduction to the language and the technology in general.

Introduction

Python was originally optimized for rapid development. Its simple, intuitive syntax (I find its constructs very logical, and I’ll demonstrate this with concrete examples in future posts), dynamic typing, lack of a compilation step (Python uses dynamic compilation), and built-in tools allow developers to create programs in less time than with other tools.

It offers features such as modularity and object-oriented programming, but doesn’t force you to use OOP exclusively: you can also take a procedural approach.

Its object model supports concepts such as polymorphism, operator overloading, and multiple inheritance, but thanks to the simplicity of Python’s syntax and typing, OOP is easy to use. With the appropriate glue code, Python programs can inherit from classes implemented in C++, Java, and C#.

Key Factors and Advantages of the Language

Many companies around the world use Python for a variety of projects. Here’s a short list: NASA (scientific computing), CERN, STSCI (Space Telescope Science Institute), NSA (encryption and intelligence analysis), Apple, Google (including its search engine), Yandex, YouTube, BitTorrent (a peer-to-peer client), Intel, Cisco, HP, Seagate, Nokia, Qualcomm, IBM, DreamWorks, Pixar (for animated films), and many other organizations worldwide.

Software Quality

Python code is clearer and easier to read, which undoubtedly improves code quality compared with other scripting languages (think of Perl). We’ll see this for ourselves in upcoming posts when we move on to the practical part.

Rapid Development

Compared with other compiled programming languages, such as C, C++, and Java, Python significantly improves developer productivity, since equivalent code is three or more times smaller. Moreover, Python code runs immediately, skipping the compilation and linking stages.

Program Portability

The standard implementation of Python is written in portable ANSI C, allowing it to compile and run on virtually every major platform. Python programs can run on a wide range of devices, from handheld computers (PDAs) to supercomputers.

Most Python code runs unchanged on any supported platform:

  • MS Windows and DOS;
  • Mac OS and Mac OS X;
  • FreeBSD, Linux, Solaris;
  • BeOS, QNX, OS/2, VMS, and VxWorks;
  • PalmOS and PocketPC;
  • Symbian and Windows Mobile;
  • iPods and game consoles;
  • and many others…

What’s more, it offers a wealth of options for creating portable graphical interfaces, database access programs, web applications, and so on.

Library Support

Python has a large number of standard libraries you can use in your programs, such as a “library for working with regular expressions” or a “networking library”. You can also write your own libraries and use them alongside the standard ones.

Third-party libraries include:

  • NumPy - an alternative to Mathlab;
  • PyGame - game development and animation;
  • PIL - image processing;
  • PyRo - robot control;
  • NLTK - analyzing natural-language phrases;
  • libraries for working with serial ports, and so on.

Component Integration

Today, Python can call functions from C/C++ libraries, be called from programs written in C/C++, integrate with Java-components, and interact with other platforms such as SOAP, XML-RPC, COBRA, and so on.

Python’s ability to be extended with and embedded in systems written in C and C++ makes it a convenient, flexible language for describing the behavior of other systems and components. Python’s C API allows C programs to call and be called by Python programs.

Tools such as SWIG and SIP, which generate code automatically, can automate the work of connecting compiled components to Python for subsequent use in scripts.

Large platforms such as COM support in Windows, Jython—an implementation of Python in Java—and IronPython—an implementation based on .NET—offer alternative ways to organize interactions with components. For example, on Windows, Python programs can control applications such as Word or Excel.

Software Quality and Maintenance

Python’s philosophy calls for a minimalist approach. This means that even when there are several ways to solve a problem, the language usually has just one obvious way, a small number of less obvious alternatives, and several related ways to organize interactions.

The statement

import this

activates an Easter egg that displays some of the design principles underlying the language.

Let’s think back to Perl, where every programmer devised their own way of solving a problem, after which the code was difficult for other programmers to understand (and, in most cases, for its own author as well). These days, the most important thing is maintaining code, not writing it quickly!

What Can You Do with Python?

As a general-purpose programming language, Python is practically unlimited: it can be used to implement anything from websites and games to controlling robots and spacecraft.

Systems Programming

Python’s built-in interfaces for accessing operating system services make it an ideal tool for creating portable programs and system administration utilities. Python programs can find files and directories, launch other programs, perform parallel computations using multiple processors and threads, and much more. It also supports all the usual OS tools: files, sockets, pipes, processes, multithreaded execution, pattern matching using regular expressions, command-line arguments, and much more.

Python’s system interfaces are designed to be portable, so you don’t have to change your code for each OS.

Graphical Interfaces

Python includes a standard OO interface to the Tk GUI API, called Tkinter, which lets you implement a portable graphical interface with the operating system’s native look and feel. The freely distributed PMW extension provides additional visual components for the Tkinter toolkit.

There is also the wxPython GUI API, based on a C++ library, which offers an alternative toolkit. The PythonCard and Dabo tools are built on wxPython and Tkinter. By choosing the appropriate library, you can use other tools to build graphical interfaces, such as Qt, GTK, MFC, and Swing.

Web Scripting

Python comes with standard Internet modules that let programs perform a variety of network operations. Scripts can communicate through sockets; extract data received from HTML forms; transfer files using FTP; process XML files; send, receive, create, and parse email messages; communicate using XML-RPC, SOAP, Telnet, and so on.

The mod_python package for Apache lets you run Python scripts, and it also supports templates using the Python Server Pages mechanism.

The Jython system provides seamless Python/Java integration and supports server applets that run on the client side.

In addition to all this, Python has comprehensive web development packages such as Django, Zope, TurboGears, Pylons, and WebWare, designed for quickly creating full-featured, high-quality websites.

Database Applications

Python supports Sybase, Oracle, ODBC, MySQL, PostgreSQL, Informix, SQLite, and many others. There is also a “portable database application programming interface” for accessing SQL databases from Python scripts, which unifies access to different databases—something like PDO for PHP.

The standard Pickle module implements a simple object storage system that lets programs save and restore Python objects in files or in objects specialized for this purpose. You can also use ZODB, a fully OO database. There is another tool called SQLObject, which maps relational tables to Python’s class model.

Python Is Powerful

Python provides the simplicity and ease of a scripting language along with the power usually found in compiled languages. Going beyond the capabilities of other scripting languages, this combination makes Python a convenient tool for developing large-scale projects.

  • Dynamic typing In Python, there is no need to explicitly declare object types; it keeps track of them itself.
  • Automatic memory management Python automatically allocates memory for objects and frees it (garbage collection) when the objects are no longer needed.
  • Modular programming To build large systems, Python provides features such as modules, classes, and exceptions. These let you break a system into components, use OOP to create reusable code, and handle events and errors gracefully.
  • Built-in object types Python provides the most common data structures, such as numbers, lists, dictionaries, and strings, as features intrinsic to the programming language itself. Built-in objects can be combined to represent more complex data structures.
  • Built-in tools To work with all these built-in data types, Python has tools for concatenation, slicing, sorting, mapping, and much more.
  • Utility libraries Python also includes a large collection of library tools that support practically anything you might need, from regular-expression searches to networking.
  • Third-party utilities Since Python is open, many third-party libraries are developed for it to handle tasks that cannot be solved using its built-in facilities. Online, you can find freely available implementations of COM support, image-processing tools, COBRA distributed objects, XML, database access mechanisms, and much more.

Despite its broad capabilities, Python has an extremely simple syntax and architecture. The result is a powerful programming tool with the simplicity and convenience of a scripting language.

Python Is Free

You can find the complete source code of the Python implementation online. There are no restrictions on copying it, embedding it in your systems, or distributing it as part of your products. In fact, you can even sell Python’s source code if you wish.

Python Is Named After Monty Python

Although Python’s logo depicts a reptile, the truth is that Python’s creator, Guido van Rossum, named his creation after the comedy series “Monty Python’s Flying Circus,” which aired on the BBC.

He’s a big fan of the series, as are many programmers around the world! As a result, the traditional variable names “foo” and “bar” are often replaced with “spam” and “egg”. Python conferences are also commonly called “The Spanish Inquisition.”

What Advantages Does Python Have over Other Languages?

  • Python has broader capabilities than Tcl. Python supports “programming in the large,” making it suitable for developing large systems.
  • Python has clearer syntax and a simpler architecture than Perl, making code more readable and easier to maintain.
  • Python is simpler and more convenient than Java. Python is a scripting language, while Java inherited complex syntax from programming languages such as C++.
  • Python is simpler and more convenient than C++, but it often cannot compete with C++, because, as a scripting language, Python is intended for a different range of tasks.
  • Python is more mature and has clearer syntax than Ruby. Unlike Ruby and Java, an OO programming style is not mandatory in Python—it doesn’t force you to use OOP in projects where that style is inappropriate.
  • Python has the dynamic characteristics of languages such as SmallTalk and Lisp, but with simpler, more traditional syntax.

Summary

That wraps up our introduction to this wonderful language, but we’re not finished with the theory yet. In the next post, I’ll explain how Python and the PVM work, discuss alternative interpreters based on Java and .NET and what they’re for, and cover much more besides.

Comments are closed. Read the original discussion in Russian →