My good friend and collaborator, Shawn Cornally, recently asked for my opinion on what language to use to teach programming. He teaches science & math at Solon High School in eastern Iowa.
I’ll preface these comments by saying that choice of language is probably not as important as other things, like competence and enthusiasm of the instructor. I started learning to program, on my own, in Apple BASIC, then QBASIC. I can recommend neither of these, even though the B in BASIC stands for “beginner.” Picking a poor first language is unlikely to either put off an enthusiastic student, or hinder their ability to learn better languages later. Still, it’s worth thinking about, should you be thinking of learning to program, or are planning to teach it.
Java was the language chosen by my professor, Lynn Andrea Stein, for my first programming course at Olin, and I think it was a good one. It has a consistent, powerful object model, and its structure encourages good practice. And so, after a single term with Java, I was convinced.
But Java wasn’t the last language to wow me. Several years later, a new professor (Allen Downey) arrived, and decided to teach programming in Python. I didn’t take his course, but I did talk to him about it. Python made a big splash across our close-knit group of engineers, and quickly became my favorite language to program in. I’ll take the liberty to copy what he says in his course notes:
Why Python?
1) Python is a great first language
pleasant syntax
not too many exceptions
for many tasks, there is one (natural) solution, rather than an unnecessary choice
2) but it’s also powerful
can handle large programs
has lots of features and libraries
some friendly languages are limited to small, “toy” programs
By “pleasant” or “friendly” I think he means that Python syntax is often more intuitive (for English-speakers, anyway). It’s often easier to figure out what a Python code-fragment does, even if you don’t know the language well. This may not cause problems for professionals, who presumably do know the language well, but it’s an important consideration for beginners. Consider the following blocks of code:
Python:
phrase = "I am Jack's Colon." count = 0 for letter in phrase: if letter == "a": count = count + 1 print count
Java:
String phrase = "I am Jack's Colon."; int count = 0; for (Character letter : phrase.toCharArray() ) { if (letter == 'a') { count++; } } System.out.println(count);
The Python is just less cluttered with things which don’t have intuitive meaning (things like the “for ( : )” construction and “++,” and… you get the idea). Don’t get me wrong, I think Java is a great language too, but Python is certainly friendlier.
(July 28th: Edited above code based on comments; thanks Christoph & tgdavis. As other commenters have pointed out, there are more compact ways to accomplish this task in either language–but I’m just trying to explain what it means for syntax to be “friendly,” and so am trying to write an example that does the same thing in roughly the same way.)
There’s an intimidation factor involved, especially when teaching things that stretch students beyond what they know. For beginners, convincing them that understanding programming is within their ability is important. Programming is not difficult (at least, not at a beginner’s level), but our society is surprisingly tolerant, occasionally laudatory, of people who say things like “I can’t understand computers,” or “I’m not smart enough to do programming.” That makes it really easy for students to give up on programming if they get too frustrated at the early stage. I’m not saying that we need to make our courses easy—students are smart enough to see through that. They don’t want to be spoon-fed or patronized, they want to feel comfortable… most of all capable with programming. It’s important that the path from “Hello World” to some kind of real power is as uncluttered as possible. From that point, they’re hooked. Or at least realize that they can’t get off the hook by claiming they’re not smart enough.
Shawn specifically asked me if I thought it was a good idea for him to teach in PHP, since wanted to teach them to build internet applications. I think internet programming is really exciting, and a very relevant teaching ground for programming. Doing interesting things with web pages is accessible to beginners, and lets them do things that they might find actually useful. It’s not often that pedagogical and practical problems have as much overlap as they do in this sphere.
While I wholeheartedly endorse internet programming as a good place to do examples and assignments, I can’t really recommend PHP as a teaching language. In the first place, I don’t think PHP enjoys any particular claim to fame as the language of choice for internet programming, and in the second I think Java and Python have strengths as general languages which make them better suited for elementary concepts and examples. While PHP is used almost exclusively for internet programming, the reverse is definitely not true. I’d venture to say that there will be plenty of opportunity for internet programming (especially at a beginner’s level) almost irregardless of the language chosen.
Unfortunately, PHP is comparatively less capable for more general programming tasks. Java and Python code can be written and easily run on students own machines without the need for a webserver. They also both have large built-in libraries of well-documented, consistently designed classes which make it easy to incorporate relatively sophisticated behaviours like graphics and network communications. While there are plenty of PHP libraries out there, they are not all well-designed or well-documented. Again, I don’t think PHP is a bad language–just not a good choice for teaching.
Some colleagues in my research group also point out that FORTRAN, for example, has intuitive syntax. I can’t, however, recommend languages that are not object-oriented. Object-oriented programming (OOP) is simply too important a paradigm in modern programming to not introduce it from the start. It encourages good programming practice as well as scoping and modularization—all of which are fundamental concepts to writing elegant and maintainable software.
Allen Downey, the professor from Olin, has written and released textbooks for introductory programming in Java and Python. They are free, released under GNU Free Documentation License, so you can download PDFs of the books to distribute, or print your own copy. The Python one (Think Python) is also being published by Cambridge University Press, should you desire a hardcovers. The Java one was updated this year, and specifically addresses the AP syllabus.