Disclosure: Your support helps keep the site running! We earn a referral fee for some of the services we recommend on this page. Learn more

Python is a high-level, general-purpose, interpreted scripting language used by scientists, mathematicians, penetration testers, spies, cryptographers, musicians, stock brokers, and network administrators. 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 any type of application.

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. However, if you want to learn how to use computers to do interesting new things, Python is the language for you.

Key takeaways:

  • If you need a quick guide to learning Python, there are plenty of free resources available online.
  • If you’re a more advanced user looking to use Python in a very specific application, there are many books you can reference as a guide.
  • Exceptions are an important part of how Python operates — familiarizing yourself with common exceptions can help you to Python proficiency.
Need a great website builder?
Save time and money with these top-rated website builder deals.
Hostinger
Best website builder
Hostinger
Best website builder
$2.99/mo
Web.com
Best website builder for small businesses
Web.com
Best website builder for small businesses
$4.17/mo
GoDaddy Website Builder
Best website builder for beginners
GoDaddy Website Builder
Best website builder for beginners
$9.99/mo

Online Python Resources

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

Learning Python

Recommended Courses and Tutorials

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.
  • Pillow — Python Imaging Library
  • 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.

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 been 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

  • 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.)
  • 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 Python

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 has been widely used in math and science for a few key reasons. Importantly, there are great math and science tools for the language, such as SciPy and NumPy. The language also 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

The Following are a few great desk references for Python. 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 there are always people out there who prefer to have hard copies.

Python Exceptions

Since exceptions are critically important to Python programming, the following tutorial 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… well… then 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.

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 … 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.

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.

AttributeError

Raised on failure of attribute reference or assignment.

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.

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

IndexError

Raised when a referenced index is invalid.

KeyError

Raised when a dictionary key is not found.

KeyboardInterrupt

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

MemoryError

Raised when an operation runs out of memory.

NameError

Raised when a variable name can not be found.

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.

IndentationError

Raised on indentation errors.

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.

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.)

ZeroDivisionError

Raised when division by zero is attempted.

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 an 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 a raise. You can pass in a string to be shown to the user.

Handling Exceptions

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

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.

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

Frequently Asked Questions About Python

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.

Is it worth it to learn Python?

If you are hoping to build typical web applications, you should probably learn PHP or Ruby (and Rails), along 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 as 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 specialized field or domain — such as math, science, finance, music, or cryptography — Python is an excellent language for these sorts of projects as well.

Need a great web host?
Save time and money with these top-rated web hosting deals.
Hostinger
Best web hosting service
Hostinger
Best web hosting service
$1.99
Bluehost
Best hosting for new sites
Bluehost
Best hosting for new sites
$2.75
SiteGround
Best for small businesses
SiteGround
Best for small businesses
$2.99