If you’d have asked me a couple of years ago to write an answer to this question, I would have said, without any hesitation, that you should learn Python as a first language.
After a number of extra years of programming and tutoring a wide range of students, I have changed my mind. Python is not a good language to teach as a first programming language; Java, on the other hand, is.
The reason is that Java is far more explicit and more strict than Python. Explicit is good, especially when you’re just starting out. Python gives a student the illusion of competence, but in practice, they often don’t know what is going on.
In Python, you assign a name to a value without specifying its type. In Java, in order to initialise a variable, you must specify its type.
In Python, you write:
first_name = "Ben"
while in Java, you write:
String firstName = "Ben"
This is a subtle difference, but the student is aware that what they just initialised is a string. That’s important; in many cases, students aren’t even fully aware of types.
If the student tries to do something ‘stupid’, like changing the value to an integer value, the compiler will warn them. Python won’t do this; these problems will only show up at runtime, which makes it much harder to debug, especially for a beginner.
A more explicit example would be to compare class declaration and instantiation.
In Python:
class MyClass:
def __init__(self, first_name):
self.first_name = "Ben"
def say_hello(self):
print("Hello, " + first_name)
my_class_instance = MyClass()
my_class_instance.say_hello()
In Java, this would be something like:
public class MyClass {
private String firstName = "Ben";
public void sayHello() {
System.out.println("Hello, " + firstName);
}
}
myClassInstance = new MyClass()
myClassInstance.sayHello()
In Java, we can see that the class is public
, we can see that firstName
is private
and a String
, and we see that sayHello
is a public
function with a void
return type, just to give a couple of examples. In Python, all of this is implicit and/or conventional. Ask a beginner what the return type is of the function say_hello
; many won’t be able to answer this. The Java student, however, is aware of the return type.
Python looks easy, but this is misleading. Java looks intimidating, but this is misleading. In reality, Java makes you conscious about certain design choices, while these same choices are much less explicit in Python — in many cases, you’re not even aware of it. The concept of private variables, for example, is very explicit in Java and baked into the language, but in Python, private variables are nothing more than conventions. This is not necessarily worse, and arguably ‘better’, but for an educational point of view, Java’s private variables are most certainly superior.
Python gives you a lot of freedom and responsibility (“we’re all adults here”), but that freedom is only valuable when the programmer has enough experience to deal with it. Without that experience, this will lead to poor design. Java gives you less freedom, but also less room to make mistakes and poor design choices.
Whether being more implicit is acceptable or even desirable is an interesting question and one that provokes a lot of debate, but from a pedagogical point of view, explicit is most certainly better than implicit, and for that reason, Java is a better choice than Python as a first language.