I am teaching Python to undergraduate math majors. I am interested in the optimal order in which students should be introduced to various Python concepts. In my view, at each stage the students should be able to solve a non-trivial programming problem using only the tools available at that time. Each new tool should enable a simpler solution to a familiar problem. A selection of numerous concepts available in Python is essential in order to keep students focused. They should also motivated and should appreciate each newly mastered tool without too much memorization. Here are some specific questions:
- For instance, my predecessor introduced lists before strings. I think the opposite is a better solution.
- Should function definitions be introduced at the very beginning or after mastering basic structured programming ideas, such as decisions (if) and loops (while)?
- Should sets be introduced before dictionaries?
- Is it better to introduce reading and writing files early in the course or should one use input and print for most of the course?
Any suggestions with explanations are most welcome.
Edit: In high school the students were introduced to computers. A few of them learned how to program. Prior to this they had a course, covering word, excel, powerpoint, html, latex, a taste of Mathematica, but no programming. 5 years ago I used Mathematica in this course and the follow-up course uses C and later Java. Now I teach introduction to Python and in the follow-up course my colleague teaches object-oriented programming in Python. Later a student may take special courses on data structures, algorithms, optimization and in some elective courses they learn on their own Mathematica, Matlab and R.
After some try / except as a teacher, I chose to stick to something like:
(starting from nothing, adjust to their level)
print().1 / 0,10 / 3but don’t bother them with details.%. Show only “%s”, it’s enough and always works. Always use a tuple (with an ending coma), even if it contains only one item.Forloop on strings, then lists, then tuples, then dictionaries.Forloop on nested types. Be nasty. Take your time. Knowing that part well changes everything.items(),values()andkeys().for, includingIOErrors.strip(),lower(),split(), etc. Don’t explain OOP, just how to use a method. Use the world “method” a lot from now.return, noprint(). Forbidprint()in functions).Try/Exceptand exceptions.Importand creation of your own directory modules. Show all the special cases (it takes way more time to explain it than you think).After that you can bring OOP on the table, but it a bit more complicated. Use strings, lists and files to introduce the notion of object. When they got it, start with classes. Then may the force be with you 🙂
It is tempting to use
printin functions to show how it works, and even more tempting to useraw_input. You should avoid it at all cost. The first one makes it very difficult to bring the concept of a “returned value”, the second hides the real flow of a program and students have a hard time to understand that you need to chain functions, not ask the user for every value you need.Generally, choose one method that works for something and stick to it. Don’t show alternative ways. E.g:
Show only string formating using
%, and ignore+and,. You can always add a little “going further” block in your lecture material for the ones who want to know more. Show onlyforand notwhile. You can code almost 90% of Python programs withoutwhile. Avoid+=. Don’t show that you can multiply strings/lists/dict with ints. This is not wrong, but will lead them to misconception. You need them focused on the main concepts.Don’t show sets. Sets are very useful but rarely used. Encourage them to code at home and to ask you if they can’t solve a problem. In that case show sets if they are the solution. Knowing sets take times and student brain resources that could be used for something more often used. They will have plenty of time to learn new tools later, without you: focus on what is hard or time consuming to learn alone.
Same goes for
enumerate. Students with a C or a Java background will use indexes to loop instead offorif you give themenumerate. For similar reasons, keeplen,fd.read,fd.realinesandrangefor one of the last courses entitles “advanced python” if you have any time for it.Don’t even think about generators, metaclasses and decorators. These can be apprehended by very few students, even after months of practice. List comprehensions,
withand ternary operations can be brought in some of the last courses if you feel your students are smart arses.Eventually, enforce good practices arbitrarily. PEP8 formating, good architecture, name conventions, no immutable default parameters, etc. They just can’t know about it right now. Don’t bother, you are the teacher, you have the right to say “this is just how it is” from time to times.
Oh, and they will be better programmers if they don’t start by learning things like bytecode, recursion, assembly, complexity, bubble sort, stack, implementation details, etc. You waste time teaching this to somebody that can’t code a decent Python program, he just can’t see what’s this is all about. Practice is your best tools to bring theory. And again, they will learn everything else by them-self later if you prepare them correctly, so prioritize and and don’t be afraid to skip concepts, even simple/important ones.