24 - Reading Code

If you have stuck with Python up to this point, congratulations! You have learned the basics of Python scripting. Everything you have learned up to now has been an exercise to get you familiar with the syntax of Python as well as a few basic principles of programming and computer science. I mentioned when we introduced functions that there are a few things that are common to all programming languages. There are only a few more concepts common to many programming languages that we have not yet covered in Python. However, you now know enough to understand the majority of the language.

Reading Assignment

For this lesson, do the following steps:

  1. Go to a website that hosts open source code such as PyPI. (GitHub also hosts Python code but it can be harder to find there.)

  2. Search the website for Python code you can read.

    • You can select “browse projects” to find projects by category.
    • You can usually find the source code linked with the “Homepage” link for any project.

    • It really doesn’t matter what exactly you read.
  3. Read one or two scripts from a project and see if you can figure out what the scripts are doing and how they are doing them.

    • It is possible you will run into some things what we have not covered yet. Do not worry about understanding the code perfectly, just do your best to understand it. If you cannot follow some code at all, try another script until you can get a rough idea of what a script is doing. As you read, feel free to reference sections we have covered so far or search the Internet to help you understand. (Remember to type “Python” and then write what you are looking for into any search engine.)
    • Pay attention to how the code is documented as you do this. Is the documentation helpful in understanding the code?
  4. As you work, take notes on things you notice about the code. How can you apply what you see in other’s code to your own code?

Like all of these lessons, what you put in to this exercise is what you will get out of it. If you put forth the effort and really try to understand the code you will gain some valuable insight into how real code is written.

Hone Your Skills

  • Do this exercise for an entire Python library (via PyPI). (If you want you can try one I wrote here.)
    • While you do the exercise think about this: Could you write your own module? How? (Note: We will go into detail on this later.)