Learning Python¶
This document gives you a quick introduction to Python that aims to remind us of the Python knowledge we should have before taking CS 304. But for some of us, it's been a little while since we've done any Python programming, and our skills have faded a bit. So, this is intended as a refresher rather than a thorough introduction.
If you're still feeling rusty and uncomfortable with Python after reading this, please talk to me or the tutors. We can try to get you up to speed.
Plan for this Topic¶
Students in this class often have widely varying backgrounds. Below, this page links to some Python tutorials and such that will help you get back up to speed if your Python is a little rusty.
In CS 304, we'll be covering Python as follows:
- The first day we will learn the basics of Python scripts, running
Flask
, which is a Python package. We'll be running the python scripts from the Unix command-line, not from the web or via some friendly "run" button. - The second day, we will learn how to combine our use of the
pymysql
package withFlask
.
What is a script?¶
In 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.
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, machine learning, and lots of other coding tasks.
- 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 takes 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, though I have done so. 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).
Hopefully, you are not learning Python from scratch in CS 304.
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.