Opinions · Learning Python
Learning Python
Notes for anyone learning Python, from the free online tutorial I taught from 2022 to 2023.
These notes come from the tutorial I taught in 2022–2023, before AI began writing much of the code, and I have not updated them for it, so some of the advice may be out of date. What I think still matters now that AI writes code, and why, is on Python Philosophy.
Why learn Python
I think anyone who does not yet know a programming language will benefit from learning Python:
- If your work involves data, whether numbers or text, Python is a powerful tool.
- Once you can program, much of the work you do on a computer can be automated, which frees your time for what needs a person.
- Programming trains a way of thinking that helps you get by in an information age.
The AI era adds one more reason: you cannot check code you cannot read, and the ability to read code comes from having written some.
Python is not difficult. Anyone willing to learn it can reach the point of writing code that does what they need. For me, Python has become a way of using a computer. In research, I use it to manage files and run simulations, and to read, analyze and plot data; in my studies, for calculations and figures; and in daily life, for almost any calculation, and for small projects such as adding bookmarks to e-books or solving a sliding puzzle by brute force.
How to study
- Set up first. Install a development environment and learn to run Python in two ways: as a script, for real work, and in the interactive shell, for trying things out. Getting started is often the hardest part, so ask for help as soon as you are stuck. I do not recommend Jupyter notebooks to beginners, because the link between input and output is harder to follow.
- Try every example, then change it and see what happens. You can skip an example only if you can honestly predict what it does.
- Know which level you need. Some topics you need to write with, some only to read, and many only to know that they exist. Learn the details when your work calls for them.
- Use
help. Typinghelp(print), orhelp(str.split)for a method, shows the documentation that comes with the code. Make it a habit. - Watch code run. Python Tutor shows a program step by step. Before each step, guess what will happen next.
- Read teaching materials critically. Some examples in the course I used could have been written more simply or more Pythonically, and rewriting them was one of my exercises. The same goes for code from the web, and from AI.
- Do a project. A small project on a real problem of your own shows what all of this is for, and gives you confidence in what you can do.
Where to find answers
Once you know the basics, get used to asking existing resources before asking people. The answers come faster, and they are more reliable:
- the
helpfunction; - the documentation of Python and of the packages you use;
- programming forums. Almost every problem a beginner meets has been met and discussed before, and finding the discussion is a skill: it depends on how you phrase the search. If you can read English, searching in English often finds more discussions.
Anything ordinary you want to do, with a string, say, has certainly been written already, so do not write it by hand unless you are practicing. Today, an AI assistant is the fastest resource to ask, and it is very good at telling you which tool already exists. But an AI's answer is not the documentation. When it matters, check it against help and the official documentation, and try it yourself.
How I taught it
Most of the 46 students were first-time programmers from the humanities and social sciences, and most attended remotely; 28 took an active part, sharing notes, submitting homework or working on projects of their own. I kept revising the study guide from their feedback. The syntax was the means; the main aim was to learn how to think a problem through.
The tutorial had three stages. In the first, students learned the basics, following an open course from Nanjing University, with my study guide, exercises and weekly online sessions. In the second, I walked through worked examples drawn from my own work and my students', such as extracting web links from a PDF file, adding bookmarks to an e-book, counting votes, and a brute-force solver for Huarongdao, a Chinese sliding-block puzzle (try it here). In the third, each student took on a small project of their own, to solve a real problem with Python and see how much it can free up their time. As the poet Lu You wrote, 纸上得来终觉浅,绝知此事要躬行: what you learn from paper stays shallow; to truly know a thing, you must do it yourself.
- Basicsstudy guide, exercises, weekly sessions
- Worked examplesproblems from real work, with a focus on thinking
- Personal projecta real problem of the student's own
I named the modules with four-character idioms, from 锅碗瓢盆, "pots and pans," for Python's data types, to 庖丁解牛, after the cook in Zhuangzi who carves an ox along its natural joints, for the art of breaking a problem down. The exercises were meant to be fun: slicing a list of years to get those of the Tang dynasty, finding every year in the sixty-year cycle that shares 2022's name, or turning "abracadabra" into "obrocodobro" with string methods found through help.
There was no fixed pace, and the written documents, which I kept correcting, took precedence over the recordings. My approach to teaching in general is on Teaching Philosophy, and the tutorial is listed on About. At MIDAS, I hope to teach short workshops on high-performance Python, Git workflows and good practice in machine learning.
Further reading
Books and sites I recommended to my students:
- David Amos, Dan Bader, Joanna Jablonski and Fletcher Heisler, Python Basics: A Practical Introduction to Python 3 (Real Python, 2021): a clear first textbook. Its one-page section on choosing between lists, tuples and dictionaries is worth reading on its own.
- Dan Bader, Python Tricks: The Book (2017): for after the basics.
- Harold Abelson and Gerald Jay Sussman, Structure and Interpretation of Computer Programs, and John DeNero's Composing Programs, its Python counterpart: for going deeper.
- Aditya Bhargava, Grokking Algorithms (2016; the Chinese edition, 算法图解, 2017): an illustrated introduction to algorithms.
- Wu Jun, The Beauty of Mathematics (数学之美, 3rd ed., 2020): how mathematics powers information technology, especially natural language processing.
- The Missing Semester of Your CS Education (MIT), for the tools around the code.