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.
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
- The official Python site
- Beginner’s Guide to Python — The official place to start learning.
- Learn Python the Hard Way — An excellent Python course for people who want to really learn the language without taking any shortcuts.
- Google’s Python Class — Google uses Python for many projects. Want to learn it the way they teach it? You can.
- Guide to the Standard Library
- Python Koans — An amazing, philosophical approach to Python programming and thinking like a developer using Test Driven Development.
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.
- Python Videos — Huge list of videos about all sorts of Python Topics
- Python Programming for Beginners — Bare bones tutorial from Linux Journal.
- A Python Book — An in-depth online book that covers beginning Python, advanced topics, and many exercises.
- Python Course — A free and extensive set of tutorials for Python.
- Building Skills in Python — 42 chapters of Python exercises designed
- Python 101 — An introduction to Python.
- Python 201 — Intermediate Python.
- Learn Python in 10 Minutes — A very quick introduction to the language.
- Checkio — Learn to improve your code with a game. Playing a game, not writing one.
- Python Programming Wiki Book
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.
- PyDev — Python IDE for Eclipse.
- Komodo — Python IDE
- PyCharm — Python IDE
- Wing IDE — Commercial Python IDE.
- Spyder — Python IDE for Science and Mathematics.
- NetBeans
- PyScripter
- µ.dev — Microdev, a weird little IDE from Sakura studio.
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.
- PyChecker — Code checker (linter).
- Rope
- Bicycle Repair
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.
- Django — By far, the most popular web application framework for Python. It is conceptually similar to Ruby on Rails. see our Django intro
- Python Paste — Not a framework, but a “framework for frameworks.” Provides low-level tools for building Python web frameworks.
- CherryPy — Minimalist Python web framework.
- TurboGears
- Web2Py
- CubicWeb
- Giotto
- Reahl
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.
- SchoolTool — Student information system
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.
- Learn Python the Hard Way: A Very Simple Introduction to the Terrifyingly Beautiful World of Computers and Code
- Python for Kids
- Python Crash Course: A Hands-On, Project-Based Introduction to Programming
- Python Programming for Beginners: An Introduction to the Python Computer Language and Computer Programming
- Python Programming: An Introduction to Computer Science
- Automate the Boring Stuff with Python: Practical Programming for Total Beginners
- Learning Python
- Hello! Python
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.
- Expert Python Programming
- Python High Performance Programming
- Python Parallel Programming Cookbook
- Python in Practice: Create Better Programs Using Concurrency, Libraries, and Patterns
- Advanced Python 3 Programming Techniques
- Mastering Object-oriented Python
- Effective Python: 59 Specific Ways to Write Better Python
- Python Machine Learning
- Think Python
- Professional Python
- High Performance Python: Practical Performant Programming for Humans
- Web Scraping with Python: Collecting Data from the Modern Web
- Test-Driven Development with Python
- Fluent Python
- Python Playground: Geeky Projects for the Curious Programmer
- Foundations of Python Network Programming: The comprehensive guide to building network applications with Python
- Guide To: Learning Iteration and Generators in Python
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.
- Doing Math with Python
- Python for Data Analysis: Data Wrangling with Pandas, NumPy, and IPython
- Python Data Science Essentials
- Data Science from Scratch: First Principles with Python
- Learning Data Mining with Python
- Advanced Python for Biologists
- Python for Finance: Analyze Big Financial Data
- Data Structure and Algorithmic Thinking with Python: Data Structure and Algorithmic Puzzles
- Python for Informatics: Exploring Information
- Derivatives Analytics with Python
- ArcPy and ArcGIS: Geospatial Analysis with Python
- Learning Pandas – Python Data Discovery and Analysis Made Easy
- Learning Geospatial Analysis with Python
- Statistics, Data Mining, and Machine Learning in Astronomy: A Practical Python Guide for the Analysis of Survey Data
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.)
- Violent Python: A Cookbook for Hackers, Forensic Analysts, Penetration Testers and Security Engineers
- Black Hat Python: Python Programming for Hackers and Pentesters
- Python for Secret Agents
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
- Be sure to read the official documentation on errors, and the full list of built-in exceptions.
- Python developer Julien Danjou wrote the definitive guide to Python Exceptions.
- Codementor.io has Writing and Using Custom Exceptions in Python.
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.