Creating software is hard work. To make your software better, your application needs to keep working even when the unexpected happens. For example, let’s say your application needs to pull information down from the Internet. What happens if the person using your application loses their Internet connectivity?
Another common issue is what to do if the user enters invalid input. Or tries to open a file that your application doesn’t support.
All of these cases can be handled using Python’s built-in exception handling capabilities, which are commonly referred to as the try and except statements.
There are many times when you are writing code that you will need to find a way to iterate over something. Perhaps you’ll need to iterate over the letters in a string or the objects in a list. The process of iterating over something is done via a loop.
A loop is a programming construct that allows you to iterate over chunks. Those chunks could be the letters in the string or the lines of a file.
In Python, there are two types of loop constructs:
The for loop
The while loop
Besides iterating over sequences, you can use a loop to do the same thing multiple times. One example is a web server that is basically an infinite loop. A server waits, listening for a client to send it a message. When it receives the message, the loop will call a function in response.
Another example is the game loop. When you beat a game or lose a game, the game doesn’t usually exit. Instead, it will ask you if you want to play again. This is also done by wrapping the entire program in a loop.
Type checking or hinting is a newer feature of Python that was added in Python 3.5. Type hinting is also known as type annotation. Type hinting is adding special syntax to functions and variable declarations that tell the developer what type the argument or variable is.
Python does not enforce the type hints. You can still change types at will in Python because of this. However some integrated development environments, such as PyCharm, support type hinting and will highlight typing errors. You can also use a tool called Mypy to check your typing for you. You will learn more about that tool later on in this article.
You will be using strings very often when you program. A string is a series of letters surrounded by single, double or triple quotes. Python 3 defines string as a “Text Sequence Type”. You can cast other types to a string using the built-in str() function.
In this article you will learn how to:
Let’s get started by learning the different ways to create strings!
Dictionaries are another fundamental data type in Python. A dictionary is a key, value pair. Some programming languages refer to them as hash tables. They are described as a mapping object that maps hashable values to arbitrary objects.
A dictionary’s keys must be immutable, that is, unable to change. Starting in Python 3.7, dictionaries are ordered. What that means is that when you add a new key, value pair to a dictionary, it remembers what order they were added. Prior to Python 3.7, this was not the case and you could not rely on insertion order.
You will learn how to do the following in this chapter:
Tuples are another sequence type in Python. Tuples consist of a number of values that are separated by commas. A tuple is immutable whereas a list is not. Immutable means that the tuple has a fixed value and cannot change. You cannot add, delete or modify items in a tuple. Immutable objects are useful when you need a constant hash value. The most popular example is the key to a Python dictionary.
The original Python 101 was the first book I had ever written. In deciding to write a 2nd edition, I needed to decide what I should keep and what I should remove from the book. What I ended up doing was rewriting the book from the ground up.
In the original book, Python 101 was based on Python 3.5 and had the following sections:
Part I – Learning the basics
Part II – Learning from the library
Part III – Intermediate Odds and Ends
Part IV – Tips, tricks and tutorials
Part V – Packaging and Distribution
For the 2nd edition, I am dropping part II as most of that information can be covered in later chapters. Now that Python 3.8 is out, I am able to cover such things as
dictionaries being ordered by default
review questions for most chapters
And mention other neat things, like sub-interpreters, the futures module, and audit hooks, although those won’t be covered in detail. I am also planning on making the tutorials section into a more proof-of-concept section where you will have little useful scripts that you can take and use. That was already there to some degree, but I think these scripts will be even better than the original ones.
The distribution section will also get a makeover to cover the latest conventions for distributing your code to the Python Packaging Index. The original book’s coverage is out of date.
Also while the first book mentioned lots of different options for creating executables, the new version of the book will focus on only one or two, but it will show how to create a binary for Windows and Mac. I am investigating how to support Linux, but that may or may not be included.
I will be making quite a few chapters available in the free sample available on Leanpub so people can check it out before purchasing.
I hope you’ll check out the book. Feel free to ask questions in the comments or send me an email.
Python 101 2nd Edition will be released in September 2020
Lists are a fundamental data type in the Python programming language. A list is a mutable sequence that is typically a collection of homogeneous items. Mutable means that you can change a list after its creation. You will frequently see lists that contain other lists. These are known as nested lists. You will also see lists that contain all manner of other data types, such as dictionaries, tuples or objects.
Let’s find out how you can create a list!
There are several ways to create a list. You may construct a list in any of the following ways:
Using a pair of square brackets with nothing inside creates an empty list: 
Using square brackets with comma-separated items: [1, 2, 3]
Using a list comprehension: [x for x in iterable]
Using the list() function: list(iterable)
An iterable is a sequence, a container that supports iteration or an iterator object. Lists themselves are sequences as are strings.