Welcome to Python 101! I wrote this book to help you learn Python 3. It is not meant to be an exhaustive reference book. Instead, the object is to get you acquainted with the building blocks of Python so that you can actually write something useful yourself. A lot of programming textbooks only teach you the language, but do not go much beyond that. I will endeavour to not only get you up to speed on the basics, but also to show you how to create useful programs. Now you may be wondering why just learning the basics isn’t enough. In my experience, when I get finished reading an introductory text, I want to then create something, but I don’t know how! I’ve got the learning, but not the glue to get from point A to point B. I think it’s important to not only teach you the basics, but also cover intermediate material.
Thus, this book will be split into five parts:
- Part one will cover Python’s basics
- Part two will be on a small subset of Python’s Standard Library
- Part three will be intermediate material
- Part four will be a series of small tutorials
- Part five will cover Python packaging and distribution
Let me spend a few moments explaining what each part has to offer. In part one, we will cover the following:
- Python types (strings, lists, dicts, etc)
- Conditional statements
- List and dictionary comprehensions
- Exception Handling
- File I/O
- Functions and Classes
Part two will talk about some of Python’s standard library. The standard library is what comes pre-packaged with Python. It is made up of modules that you can import to get added functionality. For example, you can import the math module to gain some high level math functions. I will be cherry picking the modules I use the most as a day-to-day professional and explaining how they work. The reason I think this is a good idea is that they are common, every day modules that I think you will benefit knowing about at the beginning of your Python education. This section will also cover various ways to install 3rd party modules. Finally, I will cover how to create your own modules and packages and why you’d want to do that in the first place. Here are some of the modules we will be covering:
- smtplib / email
- thread / queues
- time / datetime
Part three will cover intermediate odds and ends. These are topics that are handy to know, but not necessarily required to be able to program in Python. The topics covered are:
- the Python debugger (pdb)
- the lambda function
- code profiling
- a testing introduction
Part four will be made up of small tutorials that will help you to learn how to use Python in a practical way. In this way, you will learn how to create Python programs that can actually do something useful! You can take the knowledge in these tutorials to create your own scripts. Ideas for further enhancements to these mini-applications will be provided at the end of each tutorial so you will have something that you can try out on your own. Here are a few of the 3rd party packages that we’ll be covering:
- pip and easy_install
- pylint / pychecker
Part five is going to cover how to take your code and give it to your friends, family and the world! You will learn the following:
- How to turn your reusable scripts into Python “eggs”, “wheels” and more
- How to upload your creation to the Python Package Index (PyPI)
- How to create binary executables so you can run your application without Python
- How to create an installer for your application
The chapters and sections may not all be the same length. While every topic will be covered well, not every topic will require the same page count.
A Brief History of Python¶
I think it helps to know the background of the Python programming language. Python was created in the late 1980s. Everyone agrees that its creator is Guido van Rossum when he wrote it as a successor to the ABC programming language that he was using. Guido named the language after one of his favorite comedy acts: Monty Python. The language wasn’t released until 1991 and it has grown a lot in terms of the number of included modules and packages included. At the time of this writing, there are two major versions of Python: the 2.x series and the 3.x (sometimes known as Python 3000) . The 3.x series is not backwards compatible with 2.x because the idea when creating 3.x was to get rid of some of the idiosyncrasies in the original. The current versions are 2.7.12 and 3.5.2. Most of the features in 3.x have been backported to 2.x; however, 3.x is getting the majority of Python’s current development, so it is the version of the future.
Some people think Python is just for writing little scripts to glue together “real” code, like C++ or Haskell. However you will find Python to be useful in almost any situation. Python is used by lots of big name companies such as Google, NASA, LinkedIn, Industrial Light & Magic, and many others. Python is used not only on the backend, but also on the front. In case you’re new to the computer science field, backend programming is the stuff that’s behind the scenes; things like database processing, document generation, etc. Frontend processing is the pretty stuff most users are familiar with, such as web pages or desktop user interfaces. For example, there are some really nice Python GUI toolkits such as wxPython, PySide, and Kivy. There are also several web frameworks like Django, Pyramid, and Flask. You might find it surprising to know that Django is used for Instagram and Pinterest. If you have used these or many other websites, then you have used something that’s powered by Python without even realizing it!
As with most technical books, this one includes a few conventions that you need to be aware of. New topics and terminology will be in bold. You will also see some examples that look like the following:
>>> myString = "Welcome to Python!"
The >>> is a Python prompt symbol. You will see this in the Python interpreter and in IDLE. You will learn more about each of these in the first chapter. Other code examples will be shown in a similar manner, but without the >>>.
You will need a working Python 3 installation. The examples should work in either Python 2.x or 3.x unless specifically marked otherwise. Most Linux and Mac machines come with Python already installed. However, if you happen to find yourself without Python, you can go download a copy from http://python.org/download/. There are up-to-date installation instructions on their website, so I won’t include any installation instructions in this book. Any additional requirements will be explained later on in the book.
I welcome feedback about my writings. If you’d like to let me know what you thought of the book, you can send comments to the following address: