Entries tagged with “Python 101”.


Python provides robust exception handing baked right into the language. Exception handing is something every programmer will need to learn. It allows the programmer to continue their program or gracefully terminate the application after an exception has occurred. Python uses a try/except/finally convention. We’ll spend some time learning about standard exceptions, how to create a custom exception and how to get the exception information in case we need it for debugging. (more…)

There are a lot of computer languages that include the ternary (or tertiary) operator, which is basically a one-line conditional expression in Python. If you’re interested, you can read about the various ways it’s rendered in other languages over on Wikipedia. Here we will spend some time looking at several different examples and why you might use this operator in real life. (more…)

Python provides a very powerful logging library in its standard library. A lot of programmers use print statements for debugging (myself included), but you can also use logging to do this. It’s actually cleaner to use logging as you won’t have to go through all your code to remove the print statements. In this tutorial we’ll cover the following topics:

  • Creating a simple logger
  • How to log from multiple modules
  • Log formatting
  • Log configuration

By the end of this tutorial, you should be able to confidently create your own logs for your applications. Let’s get started! (more…)

List comprehensions in Python are very handy. They can also be a little hard to understand when and why you would use them. List comprehensions tend to be harder to read than just using a simple for loop as well. We’ll spend some time looking at how to construct list comprehensions and learn how they can be used. By the end of this article, you should be competent enough to use them confidently. (more…)

There are lots of different ways to download a file from the internet using Python. One popular way is to connect to an FTP server and download your files that way. So that is what we will be looking at in this article. All you need is your standard installation of Python. It includes a library called ftplib, which has all the bits and pieces we need to accomplish this task. (more…)

Today we’re going to take a look at the controversial easy_install method of installing Python modules and packages. We will also learn how to create our own *.egg files. You will need to go get the SetupTools package to follow along. This package doesn’t support Python 3.x so if you need that, see pip or distribute. There will be articles on each of those projects in the future. For now, we’ll be starting with SetupTools and easy_install.

Why is it controversial? I’m not completely certain, but people were not happy with the way it would partially install packages because it doesn’t wait for the download to finish. Also I’ve heard that the author wasn’t very interested in updating it but wouldn’t allow anyone else to update it either. See the Ziade article at the end of this post.

SetupTools is the original mainstream method of downloading and installing Python packages from PyPI and other sources via the command line, kind of like apt-get for Python. When you install SetupTools, it installs a script or exe called easy_install that you can invoke on the command line to install or upgrade packages. It also provides a way to create Python eggs. Let’s spend a little time getting to know this utility. (more…)

Last time we learned how to create modules and packages. Today we’re going to take the package we created and use Python’s distutils to create a couple different ways to distribute our code. In this tutorial, we’ll be learning the following:

  • How to create a setup.py file
  • How to create a source distribution
  • How to create a Windows distribution

Let’s get started! (more…)

Creating Python modules is something that most Python programmers do every day. Any time you save a new Python script, you have created a new module. You can import your module into other modules. A package is a collection of modules. The things you import into your scripts from the standard library are modules or packages. In this article, we’ll be looking at how to create modules and packages. We’ll spend more time on packages since they’re more complicated than modules. (more…)

Whether you’re new to Python, been using it for a few years or you’re an expert, knowing how to use Python’s introspection capabilities can help your understanding of your code and that new package you just downloaded with the crappy documentation. Introspection is a fancy word that means to observe oneself and ponder one’s thoughts, senses, and desires. In Python world, introspection is actually kind of similar. Introspection in this case is to use Python to figure out Python. In this article, you can learn how to use Python to give yourself a clue about the code you’re working on or trying to learn. Some might even call it a form of debugging. (more…)

When I began learning Python, one of the first things I needed to know how to do was open a file. Now, the term “open a file” can mean different things depending on the context. Sometimes it means to actually open the file with Python and read from it, like with a text file. Other times, it means “to open the file in its default program”; and sometimes it means, “open the file in the program I specify”. So, when you go looking for how to do the latter two, you need to know how to ask Google just the right question or all you’ll end up with is learning how to open and read a text file.

In this article, we’re going to cover all three and we’ll also show how to open (or run) programs that are already installed on your PC. Why? Because that topic is also one of the first things I needed to learn and it uses some of the same techniques. (more…)