6 ms·
I have taught an introductory course for programming in Python, and there certainly are a few areas that are less than ideal for beginners. 1) Setting up Pytho
by Derbasti 13y ago
I have taught an introductory course for programming in Python, and there certainly are a few areas that are less than ideal for beginners.
1) Setting up Python, a text editor and a shell is easy on Windows (using WinPython), but pretty confusing for Mac users.
2) It is actually surprisingly difficult to teach object oriented programming in Python, since most simple problems are much more easily solved in a functional manner than with classes.
3) It is more or less impossible to peek under the hood. There is no way to teach integer logic really (Python has auto-growing arbitrary precision integers) and floats can lead to strange errors (x=1; for i in range(10): x+=0.1 does not yield 2).
4) It can be very difficult for students to grasp the difference between different types and when or how they are to be used. I suspect that these kinds of problems might be more easily explained in C.
All that said, I feel that the students did learn quite a few things that would have been more or less impossible with C or Javascript. In many ways, Python provides a happy medium between full-on dynamicism and strictness. Four stars, would teach again.
- csense 13y ago> introductory > object-oriented programming Unless I was mandated to make my syllabus match the language reference's table of contents, I would defer most of object-oriented programming to a second semester. How to use particular objects from the standard library would likely be covered on an as-needed basis in an introductory class -- for example, "you use the syntax d.keys() to get a list of a dictionary's keys" without attempting to explain that "keys" is a function attribute of a class of which d is a member, and thus by MRO rules d.keys triggers a lookup of the "keys" member from the built-in dict class, and d will be passed as the first parameter when the function call operator denoted by the parentheses is executed ...You may be tempted to include such detail for various reasons, but trust me, your students will thank you if you save that content for a more advanced course. > There is no way to teach integer logic really (Python has auto-growing arbitrary precision integers) You can still teach binary representation and use bit-wise operators. Also, I once wrote a storage-efficient bit vector class with the built-in bytearray module, which is a good exercise to tie together object-oriented programming and binary representation. However, a student at the level of doing that exercise is no longer beginner, but firmly intermediate or advanced. > floats can lead to strange errors These are a natural consequence of the floating-point representation and this problem exists in each of the article's top three language choices.
- Derbasti 13y agoRe floats: True, this problem is universal. I still wish there was a way to circumvent it, much like the arbitrary precision integers circumvent integer problems. Rationals and fractional integers come to mind, though I don't know their performance characteristics.
- sdevlin 13y agoPython has a decimal package in stdlib. It provides an arbitrary-precision floating point type. The performance will obviously be worse than with floats, but I doubt it matters in an intro programming class. The only real drawback is that there are no decimal literals. Actually, several languages I know of have decimal types in stdlib. Two that do not are C and JavaScript.
- lhl 13y agoWhile obviously it's a fundamental thing to teach/cover the various data types (or at least how '1', 1, and 1.0 are different since they'll be encountering this over and over if they continue programming), on a practical level, frequent casting can be sidestepped by using string formatting when printing numeric output. In all my production code (my string printing is not performance critical) I pretty much always use string substitution when printing as a matter of style/convenience. It's much easier to add/replace substitutions, and I often need formatting anyway. If students get used to printing w/ string substitution, they can then simply use %d or %f to control how they want their numbers to display, which is probably the right way of thinking about rendering text output.
- csense 13y agoPython has the "fractions" module which provides a more powerful abstraction: It's not restricted to denominators that are powers of 10. > I don't know their performance characteristics It's easy to make some good guesses. Since (I'm guessing) it's implemented with pairs of integers, adding probably uses a / b + c / d = (ad + bc) / (cd) which is 3 multiplications and an addition, and multiplication probably uses (a / b) (c / d) = (ac) / (bd) so only two multiplications. So it's a small constant factor times the cost of integer multiplication, plus a bit of overhead for the wrapper class. So my back-of-the-envelope guess is that the fractions module is less than 10x slower than the same operation with integers, or fast enough for just about anything you do in a beginner level class. These numbers will change significantly if fractions are automatically reduced by gcd of numerator and denominator; I'm not sure about this point.
- ama729 13y ago> 2) It is actually surprisingly difficult to teach object oriented programming in Python, since most simple problems are much more easily solved in a functional manner than with classes. Do you have an example? From the outside, it doesn't seem to be that difficult, for example, in the C++ course I followed, the teacher introduced classes with the implementation of a RPG character. That was pretty simple.
- jeorgun 13y agoWhile I don't know that it's difficult to teach OOP in the sense of 'objects exist' in Python, I do think it's more difficult to teach a lot of tthe concepts associated with OOP, because the way it's done in Python is so unusual (explicit self, no real encapsulation, etc., etc.). In the introductory programming course I took, the way OOP was introduced involved writing incredibly un-idiomatic Python. So in that sense it works fine, but if you're going to ignore the 'spirit' of a language in order to use it as a teaching tool, it seems to me what language you choose doesn't really matter, so long as it has the features to shoe-horn in whatever style you want to teach people in.
- Derbasti 13y agoI explained it using a bank account. That works well enough. It's just that I couldn't come up with many real-world problems and exercises that are naturally solved with classes. Classes are a rather high-level abstraction, and most problems in introductory classes are decidedly low-level.
- zhemao 13y agoCould you explain how setup is more difficult on a Mac than on Windows? Other than a decent beginner's text editor, everything you mentioned comes pre-installed on OSX. Trying to install 3rd-party Python libraries on OSX is kind of a pain, but if you're just using the standard library (which will get you pretty far in an intro class). Finding a good text editor for Mac also isn't that difficult. When I've taught beginners on a Mac, I usually just download Textmate.
- KC8ZKF 13y agoDoesn't Emacs and Vim come preinstalled on OS X? Or has that changed with Mavericks?
- zevyoura 13y agoThose are exceptionally poor choices for a beginner's class.
- KC8ZKF 13y agoI disagree. Why do you say they are?
- zevyoura 13y agoBecause it's a class to learn programming, not a new text editor.
- KC8ZKF 13y agoBut they don't know any text editor. They have to learn something.
- zhemao 13y agoMost new students have seen a text editor before (like Notepad or Microsoft word). They are used to things like using the arrow keys for navigation (instead of HJKL), not having to switch modes, and using the mouse to manipulate text. There are plenty of serviceable programmers' text editors which follow these idioms. Better to let students use something familiar so you can focus on the actual subject (programming) and not frustrate them with learning a completely different set of keyboard shortcuts.
- hcarvalhoalves 13y ago> 2) It is actually surprisingly difficult to teach object oriented programming in Python, since most simple problems are much more easily solved in a functional manner than with classes. That's actually a good thing. I'm afraid too many people learn programming early on with e.g. Java and end believing programming equates defining classes and mutating objects. I've worked with people with such background (actually, I routinely do), and it's not pretty. I notice a difficulty in these people to grasp fundamentals, like data structures, idempotency and high-order functions, which leads to awful bad practices.
- Derbasti 13y agoI completely agree. Still, OOP is an important tool to have in your belt. I would say though, that a difficulty to grasp data structures or higher order functions is pretty universal in beginners regardless of background. Those are just hard concepts.
- NAFV_P 13y ago> It is more or less impossible to peek under the hood. There is no way to teach integer logic really (Python has auto-growing arbitrary precision integers) and floats can lead to strange errors (x=1; for i in range(10): x+=0.1 does not yield 2). I believe the same problem applies to C, and any program that employs a float register. 0.1 cannot be defined accurately in a float, there is incompatibility between a base 10 system and a base 2 system. This should be true no matter the size of the register. The larger the register, the more precision you get, but you don't get more accuracy.
- Fomite 13y ago> 1) Setting up Python, a text editor and a shell is easy on Windows (using WinPython), but pretty confusing for Mac users. How is this even possible? TextWrangler (or other choices) come from the App store, Terminal is right there and Python is already installed.