Kaili Cao 曹开力

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:

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

Once you know the basics, get used to asking existing resources before asking people. The answers come faster, and they are more reliable:

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.

  1. Basicsstudy guide, exercises, weekly sessions
  2. Worked examplesproblems from real work, with a focus on thinking
  3. Personal projecta real problem of the student's own
The three stages of my Python tutorial (2022–2023). Made with AI assistance (Claude).

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.

Books and sites I recommended to my students: