We'll spend the beginning of this reading talking about Python in general, and then we'll dig into PyMySQL, which is the Python database API that we'll be using.

Plan for this Topic

Students in this class often have widely varying backgrounds. Some of you know Python better than I do, while others don't yet know what is meant by a script. Because of that, several sections will be linked to, making them easy to skip, but please don't skip them if you're shaky on the information in those links. You should err on the side of reading them, skimming if you're familiar with the content, and then returning.

We'll be covering Python as follows:

  • The first day we will learn the basics of Python scripts, and also how to connect to a database back-end from a Python script. We'll be running the scripts from the Unix command-line, not from the web. We'll be creating scripts that are useful in general, not just from the web. That is, we'll create normal scripts.
  • The second day, we will learn how to combine our use of the pymysql package with Flask.

Our goal for this topic is to learn how to connect Python with a database, because that is useful in itself, whether we connect that to a web application or not.

What is a script?

In typical Computer Science usage, a script is typically written in a language that doesn't require a separate compilation phase. There are many such languages; Python is only one. Furthermore, scripts are typically short, and often described as quick and dirty, so the scripting languages often strive to do what you mean rather than forcing you to be careful. But, of course, a script need not be quick or dirty; then can still be written carefully and robustly. You should strive for that.

Let's be more specific. A script is a plain text file that is executed by some appropriate interpreter. Sometimes, that interpreter is the shell, such as /bin/bash, in which case, the script is a shell script. Sometimes, the interpreter is another program such as Perl or Python.

The Python interpreter plays almost exactly the same role as the java command does, except that java requires you to run javac first and give it the .class file, while the python interpreter does that compilation for you.

In every case, the language you are writing in depends entirely on the interpreter. Bash scripts generally look very different from Perl scripts which look different from Python scripts, and so forth.

Why Python?

Since there are many scripting languages to choose from, why choose Python?

  • Python is a much more generally useful tool than, say PHP. Python is one of the first tools used for system administration, data manipulation and transformation, and anything quick and dirty.
  • Python is very powerful, thanks to thousands of extension modules (packages) that you can get from PyPI, the Python Package Index.

Python is a very likeable language. Some things that I like about it:

  • Its syntax, while bizarre compared to conventional Java-like languages, is spare and almost beautiful. It will take some getting used to, but it's quite readable, even for a beginner.
  • It has a lot of powerful, dynamic features: dynamic creation of objects, functions, and pretty much anything.
  • It has a passable attempt at closures.
  • It has a lot of powerful packages.
  • It is easily portable.
  • It has a read-eval-print-loop which makes experimentation and playing around a joy.
  • Its object-oriented programming is good but optional.
  • It has lots of good documentation and tutorials
  • It has a lot of mind-share, meaning many people use it.
  • It has spam, silly walks and a dead parrot.

There are lots of good tutorials for beginners online, so it would be foolish for me to try to write one. Here are some. If you know of a better one, let me know!

  • Think Python, by my friend Allen Downey. Read his textbook manifesto, and I think you'll find it irresistable to try his book. And it's free (though you can buy a bound copy if you want). I would suggest Chapter 2, Chapter 3, and Chapter 4. All these chapters are relatively short: 8–10 pages each.
  • The Python Tutorial. You can't get much more official than this. I would suggestion sections 1–5.
  • Categorized Python tutorials at Awaretek.com, including many for people without programming background (read those to feel really smart).

Nevertheless, I've written a crash course. If you are very familiar with Python, you can skip those notes, but I would recommend at least skimming them. If you need more information about Python, consult some of the references and tutorials above.

Versions of Python

Python is in active development, and new versions come out regularly. Two major series of versions are Python 2.x and Python 3.x. Python 2 is obsolete as of January 2020, so everyone should be switching to Python 3. The default python on Tempest is (as of 1/22/2020) Python3, and we'll use that in CS 304.