Python Introduction, Resources and FAQs

Python is a high-level, general purpose, interpreted scripting language. The language’s design has a strong emphasis on code readability, as well as flexibility and ease of use.

Like most similar languages (Ruby and Perl, for example), Python can be used for anything, for any type of application. There are a handful of web apps built using Python, and some excellent web development frameworks. With tools like Py2Exe, you can package Python scripts as stand-alone desktop applications.

But where Python really shines, and where it gets most of its use, is as a tool for data analysis, number crunching, scientific research, hacking, scripting, and task automation. Python is used by scientists, mathematicians, penetration testers, spies, cryptographers, musicians, stock brokers, and network administrators for a wide range of applications.

If you want to learn how to build nice web applications, you probably want to start with Ruby or (if you’re into WordPress), PHP. But if you want to learn how to use computers to do interesting new things, Python is the language for you.

Online Python Resources

There is a lot of Python material on the web — some of it free, some of it paid. These are some of the best resources for learning Python.

Learning Python

Other Great Python Tutorials and Learning Resources

Here are some very good resources for learning Python that almost made it into the above “Recommended” list. Everyone has a different learning style, so maybe one of these will suit you better than the tutorials above.

Additional Python Tutorials

These are “Honorable Mention” tutorials on beginning Python. We didn’t find them quite up to our (very high) standards, but they are all fairly popular — so maybe one of them will work well for you.

Python Development Tools

Libraries, Plugins, and Add-ons

A big part of the strength of Python is the ecosystem of excellent tools for accomplishing a number of different types of tasks in the language. From graphics processing to mathematical analysis, there’s probably a Python module for just about any domain you are working in.

  • Shrapnel — Python Library for high-volume concurrency.
  • MatPlotLib — Graphics and data visualization.
  • Mako — Web templating engine.
  • PIL — Python Imaging Library
    • Pillow — Fork of PIL. (PIL seems to have ceased active development. Pillow is picking up the slack on this popular project.)
  • Pyx — Python Graphics package
  • Beautiful Soup — Tools for screen scaping and then handling the parse-tree and content.
  • Scrappy — Web scraping tools.
  • Gooey — Tools for providing a GUI for command-line Python programs.
  • Peewee — A minimalist SQL ORM for connecting a Python application to MySQL, PostgreSQL, or SQLite.
  • SQL Alchemy — A more fully-featured SQL ORM.
  • PyGame — Platform for building video games in Python.
  • SciPy — Science and math tools for Python; very important to for scientific computing.
  • Pandas — Data analysis tools.
  • sh — Library for calling other programs from within Python scripts.

The Python wiki also maintains a list of some of the most useful and popular Python modules.

IDEs

An IDE is an Integrated Development Environment, a tool for managing the development of a large or complex application. Most Python users tend to work in a simple code editor, but a number of excellent Python-focused IDEs are available for those using Python for larger projects.

Also see this list of Python editors.

Refactoring and Code Checking

Python developers have a culture that tends to prefer clean and efficient code. At the same time, they also value speed, and often plunge into coding quickly in order to solve immediate problems. A number of tools have developed to help Python programmers automate the task of checking code and making it more efficient.

Build Tools

Python excels at task automation, so it should be no surprise that there are a number of tools for doing just that, and for speeding up build and deploy cycles.

Also included in this list are specialized development tools that are used for packaging and distributing Python apps.

  • Invoke - Tasks execution and scripting tool.
  • Microbuild — Lightweight build tool.
  • Paver — Task scripting.
  • Pynt — Build tool.
  • VirtualEnv — Tool for building isolated Python environments.
  • Bitten — Continuous integration tool for Python and Trac.
  • iPython — Interactive Python shell and development library; too many cool features to list.
  • Py2Exe — Compiles Python scripts into Windows executables.
Web Frameworks

If you want to use Python to build a web application, there are a number of low-level tasks you’ll need to take care of first — or you could just start from step 10 and use a web application development framework.

Applications Built in Python

Python is used by a lot of people, for a lot of different tasks and purposes, but is not hugely popular for building apps to be distributed as code to consumers and end users (the way, for example, PHP is). Still, it is sometimes used for this purpose. Here are some examples of some applications built in Python.

Content Management Systems
  • Plone — The most popular Python-based CMS.
  • Django CMS — Not as popular as Plone for CMS, but built on top of the most popular Python framework.
  • MoinMoin — Python wiki-engine that powers the Python wiki. (For other Python-based Wiki CMSes, see this page.)
  • CPS-CMS
  • Silva
  • ZMS

Online Python Reference

These are some of the most important single-source Python reference sites, which you should probably bookmark.

Books

Beginning Python

If you prefer to learn using a printed book, there is no shortage of excellent ones available. Here are some of the best Python books for beginners. If you don’t prefer books (or don’t prefer paying for things) scroll down to the Online Resources section for a list of excellent online (and mostly free) tutorials.

Advanced

Many of the more advanced concepts in Python programming are not covered in online tutorials, and can only be found in printed books.

Python for Math, Science, and Data

Python is widely used in math and science for a at least a couple of reasons: - There are great math and science tools for the language, such as the SciPy and NumPy. - The language lends itself well to quick programming tasks, so it is easy to use Python for ad hoc data analysis without building fully-featured apps.

As with general Advanced topics, if you are looking for information on specialized topics in advanced Python programming, you will find a lot more excellent books than free websites and online tutorials.

Python for Hacking

Because of its suitability for ad hoc programming, and for task automation, Python also gets used quite a bit by people who like to break into things, and also by the people who try to stop them. (We assume you are one of the good guys, of course.)

Reference

Here are a few great desk references for Python, for people who like that sort of thing. While some of the advanced topics mentioned above are book-only, most of the basic reference material here is easier to find online with a good search engine — but some people prefer to have hard copies.

Python Exceptions

Since exceptions are critically import to Python programming, we have provided the following tutorial, which will get you up to speed on this aspect of Python.

When something goes wrong in the execution of a Python program, what happens? Well, if the next thing to happen isn't an exception... two things have gone wrong.

Python raises exceptions in response to all sorts of error conditions. As a Python developer, exceptions tell you what is going wrong with the code and where. You can also define and raise your own exceptions.

In this article, we'll take a look at Python's built-in exceptions, and explore exception handling.

How exceptions are built

Following the principles of object-oriented programming, exceptions are defined in exception classes. These classes are organized in an inheritance hierarchy. For example, IndentationError is a subclass of SyntaxError.

When an exception is raised, an instance of the particular exception class is created. If you catch the error (as is done in the except clause below), you can inspect it.

>>> try:
...     raise ValueError
... except ValueError as e:
...     print(str(type(e)) + "\n" + str(dir(e)))
...
<class 'ValueError'>
  ['__cause__', '__class__', '__context__',
  '__delattr__', '__dict__', '__dir__',
  '__doc__', '__eq__', '__format__', '__ge__',
  '__getattribute__', '__gt__', '__hash__',
  '__init__', '__le__', '__lt__', '__ne__',
  '__new__', '__reduce__', '__reduce_ex__',
  '__repr__', '__setattr__', '__setstate__',
  '__sizeof__', '__str__', '__subclasshook__',
  '__suppress_context__', '__traceback__',
  'args', 'with_traceback']

Note about code samples: Code samples with the >>> prompt can be tried out using the interactive interpreter. Just type python3 into the terminal. Everything not preceded by >>> or ... those is output. Code samples without the prompt are examples of code you might actually write in a .py module.

Abstract Exceptions

These exception classes are used as the base class for other exceptions.

BaseException

This is the base class from which all other exceptions are derived.

Exception

All built-in, non-system-exiting exceptions are derived from this class. All user-defined exceptions should also be derived from this class.


class MyNewException(Exception):

    def __str__(self):
        return "MyNewException has occured."

ArithmeticError

Inherited by exceptions related to arithmetic:

BufferError

Raised when a buffer related operation cannot be performed.

LookupError

Inherited by exceptions related to invalid keys or indexes. For example, a bad key on a dict or an out of range index on a list.

Concrete exceptions

AssertionError

Raised on failed assertions.

>>> assert 1 > 2
Assertion Error

AttributeError

Raised on failure of attribute reference or assignment.

>>> x = 1

>>> x.name
AttributeError: 'int' object has no attribute 'name'

>>> x.name = "one"
AttributeError: 'int' object has no attribute 'name'

EOFError

Raised when input() reaches end-of-file (EOF) without reading any data.

FloatingPointError

Raised when a floating point operation fails. Note that this exception will normally not be raised unless configured, and that handling floating point exceptions is discouraged for most non-expert users..

GeneratorExit

Raised when a generator or coroutine closes. This is not actually an error, since closing is normal behavior.

ImportError

Raised when the import statement fails.

>>> import ModuleThatDoesNotExist
ImportError: No module named ModuleThatDoesNotExist

In Python 3.6 and later, there is additionally the subclass ModuleNotFoundError.

IndexError

Raised when a referenced index is invalid.

>>> l = ["zero", "one", "two"]
>>> l[4]
IndexError: list index out of range

KeyError

Raised when a dictionary key is not found.

>>> d = {'TOS':'Kirk','TNG':'Picard','DS9':'Sisko','VOY':'Janeway'}
>>> d['ENT']
KeyError: 'ENT'

KeyboardInterrupt

Raised when interrupt key is hit (CTRL-C).

>>> while True
... pass

^C
KeyboardInterrupt

MemoryError

Raised when an operation runs out of memory.

NameError

Raised when a variable name can not be found.

>>> while True:
...     pass

^C
KeyboardInterrupt

NotImplementedError

This is intended to be written into classes; it is not raised by any built-in features. It has two uses:

  • Raised by abstract methods, to indicate they need to be overwritten in derived classes.
  • Raised by derived classes, to indicate that an implementation needs to be added. This allows the class to be loaded without raising an exception.

OSError

Raised when a system error is returned. See below for more information on OSError.

OverflowError

Raised when the result of a math operation is too large.

RecursionError

Raised when the maximum recursion depth is exceeded.

ReferenceError

Raised when a weak reference proxy is used to access an object after it has been garbage collected.

RuntimeError

Raised when an error is detected that doesn't fall in any of the other categories.

StopIteration

Raised by next() and __next__() when no further items will be produced by an iterator.

StopAsyncIteration

Must be raised by anext () method of an asynchronous iterator object to stop the iteration.

This is new in version 3.5.

SyntaxError

Raised on a syntax error.

>>> 1 = 2
SyntaxError: can't assign to literal

IndentationError

Raised on indentation errors.

>>> if 1 == 1:
...     x = 1
IndentationError: expected an indented block

TabError

A subclass of IndentationError, this is raised when indentation uses tabs and spaces inconsistently.

SystemError

Raised on non-serious internal errors.

SystemExit

Raised by the sys.exit() function.

TypeError

Raised when an operation or function is called on an inappropriate type of object.

>>> 1 > "one"
TypeError: unorderable types: int() > str()

User code should raise a TypeError for inappropriately typed function inputs.

UnboundLocalError

Raised when a local variable is referenced in a function or method, but the variable hasn't been defined.

UnicodeError

Base class used for errors that occur while handling Unicode strings. It has three subclasses:

  • UnicodeEncodeError
  • UnicodeDecodeError
  • UnicodeTranslateError

ValueError

Raised when a function or operation is called with an argument of the right type but an inappropriate value, unless amore specific error applies. (For example, an out of range index raises IndexError.)

>>> f = open("name-of-file.txt", "m")
ValueError: invalid mode: 'm'

ZeroDivisionError

Raised when division by zero is attempted.

>>> 1/0
ZeroDivisionError: division by zero

>>> 1.0/0
ZeroDivisionError: float division by zero

>>> 1%0
ZeroDivisionError: integer division or modulo by zero

More on OSError

OSError was reworked in Python 3.3. There are now three aliases for OSError, as well as a number of derived classes for various error cases.

Aliases of OSError

  • EnvironmentError was originally the base class for OSError and IOError.
  • IOError was originally raised for errors occurring during any I/O operation, including printing or reading from a file.
  • WindowsError was originally raised for any Windows-specific errors.

All three of these were retained solely for compatibility purposes, but are actually aliases of OSError.

OSError Subclasses

These are all derived from OSError, and are raised depending on the error code returned by the operating system. Below each description is the related system errno.

BlockingIOError

Raised when an operation would cause blocking on an object set for non-blocking.

  • EAGAIN
  • EALREADY
  • EWOULDBLOCK
  • EINPROGRESS
ChildProcessError

Raised when an operation on a child process fails.

  • ECHILD
ConnectionError

Base class for errors related to connections.

Subclasses:

  • BrokenPipeError, raised when write is attempted on a closed pipe or or socket.
    • EPIPE; ESHUTDOWN
  • ConnectionAbortedError, raised then an attempted connection is aborted by the peer.
    • ECONNABORTED
  • ConnectionRefusedError, raised when an attempted connection is refused by the peer.
    • ECONNREFUSED
  • ConnectionResetError, raised when a connection is reset by the peer.
    • ECONNRESET
FileExistsError

Raised when attempting to create a file or directory which already exists.

  • EEXIST
FileNotFoundError

Raised when a requested file or directory does not exist.

  • ENOENT
InterruptedError

Raised when am incoming signal interrupts a system call.

  • EINTR

Note that since Python 3.5, interrupted system calls will be retried, unless the signal handler raises an exception.

IsADirectoryError

Raised when a file-only operation (such as os.remove()) is attempted on a directory.

  • EISDIR
NotADirectoryError

Raised when a directory-only operation (such as os.listdir()) is attempted on a file or other non-directory object.

  • ENOTDIR.
PermissionError

Raised when trying to run an operation without the sufficient permissions.

  • EACCES
  • EPERM
ProcessLookupError

Raised when a referenced process doesn't exist.

  • ESRCH
TimeoutError

Raised when a system function times out.

  • ETIMEDOUT

Warnings

These exception classes are used as base classes for warning exceptions.

Warning

Inherited by all warning subclasses.

UserWarning

Inherited by warnings generated by user code.

DeprecationWarning

Inherited by warnings about deprecated features.

PendingDeprecationWarning

Inherited by warnings about features which will be deprecated in the future.

SyntaxWarning

Inherited by warnings about problematic syntax.

RuntimeWarning

Inherited by warnings about problematic runtime behavior.

FutureWarning

Inherited by warnings about constructs that will change in the future.

ImportWarning

Inherited by warnings about possible mistakes in package and module imports.

UnicodeWarning

Inherited by warnings related to Unicode.

BytesWarning

Inherited by warnings related to bytes and bytearray.

ResourceWarning

Inherited by warnings related to resource usage.

Raising Built-in Exceptions in Your Code

You may wish to raise exceptions in response to various user actions. This is as easy as invoking raise. You can pass in a string to be shown to the user.

def ObnoxiousFavoriteColorAssigner(color):
    if type(color) is not str:
        raise TypeError("I need a string.")
    if color in ["red", "green", "blue"]:
        favorite_color = color
    else:
        raise ValueError("That's not *really* a color.")

Handling Exceptions

You can handle exceptions that occur when your code is run using try and except.


def EverybodyLovesRed():
    while True:
        try:
            ObnoxiousFavoriteColorAssigner(input())
            break
        except TypeError as err:
            print(err)
        except ValueError:
            ObnoxiousFavoriteColorAssigner("red")

Defining Custom Exceptions

You can extend any of the built-in exceptions to create your own custom exceptions. This is most often done in large frameworks.


class ColorError(ValueError):
    """
    Raised when someone suggests there are colors
    other than red, blue, and green.
    """

    def __str__():
        return "Colors only exist in your eyes."


def ObnoxiousFavoriteColorAssigner(color):
    if type(color) is not str:
        raise TypeError("I need a string.")
    if color in ["red", "green", "blue"]:
        favorite_color = color
    else:
        raise ColorError

It is good practice, when creating your own custom exception class, to include in the docstring relevant information about when or how the exception might be raised. Beyond that, though, this logic is not included in the code defining the exception.

More on Python Exceptions

Python FAQ

What does it mean that Python is a “scripting language”?

A scripting language is a language that is interpreted at run time, rather than compiled into a binary executable.

Some people use the phrase “scripting language” to indicate that the language is particularly good at writing short “scripts,” or miniature ad hoc programs used to automate tasks.

Python fits both descriptions — it is an interpreted language, and it is also highly useful for writing short, ad hoc scripts.

Are scripting languages like Python good for writing full-scale applications?

There are some people who have a bias against the use of scripting/interpreted languages for entire applications. The wisdom of this bias is entirely up to individual context.

Scripting languages tend to run a little slower than compiled languages, and in some instances this difference in performance is a huge issue. In most contexts, though, it is a negligible concern.

Python is perfectly well suited for writing applications of all kinds. Using Django or another web framework allows you to build web-based applications. There is nothing deficient about Python in terms of the tools and capabilities needed to write full-scale applications. In fact, Python is arguably much better suited to such work than either PHP or JavaScript, both of which are frequently used for large, complex web applications.

Should I learn Python?

That depends on your goals.

If you are hoping to build typical web applications, you should probably learn PHP or Ruby (and Rails), along with with JavaScript, HTML, and CSS. There’s no reason you could not use Python for this work, but it is not typical to do so. PHP and Ruby would give you access to a lot more existing web applications, frameworks, and web development tools.

If you are looking to use programming skills to directly accomplish tasks, such automation or analysis, Python is an excellent language for that sort of work, and is where it gets most of its use.

If you are building apps that need to manipulate data in a a specialized field or domain — such as math, science, finance, music, or cryptography — Python is an excellent language for these sorts of projects as well.


Further Reading and Resources

We have more guides, tutorials, and infographics related to programming and statistics:

What Code Should You Learn?

Confused about what programming language you should learn to code in? Check out our infographic, What Code Should You Learn? It not only discusses different aspects of the languages, it answers important questions such as, "How much money will I make programming Java for a living?"