Fall 2006»Pythagorean Theorem In Java And Python

Pythagorean Theorem In Java And Python

Programming Effort Comparison

Alan Kay, in his paper "The Early History of Smalltalk" states:

"When teaching Smalltalk to 20 nonprogrammer adults, they were able to get through the initial material faster than children, but, just as it looked like an overwhelming success was at hand, they started to crash on problems that didn't look to me to be much harder than the ones they had just been doing so well on. ... E.g., make a little database system that would act like a card file or rolodex. They couldn't even come close to programming it. Such a project was well below the mythical 'two pages' for end-users we were working with. After showing them the solution, I realized this little program contained 17(!) nonobvious ideas. And some of them were like the concept of the arch in building design: very hard to discover, if you don't already know it."

In this context, here are two typical programs, in Java and in Python, to calculate length of the hypotenuse of a triangle given the length of the two legs.

Consider the number of new concepts/"nonobvious ideas" required to master in order to comprehend/write each of these programs.


Pythagorean Theorem in Java.
import java.util.*;

public class Triangle
    public static void main (String[] args)
        Triangle t1 = new Triangle();
        Scanner input = new Scanner(System.in);
        int a = input.nextInt();
        int b = input.nextInt();
        double c = t1.hypotenuse(a, b);

    public double hypotenuse(int a, int b)
       return (Math.sqrt( (a*a) + (b*b) ));

For the Java program, a programmer needs to master the following concepts/"nonobvious ideas":

  1. libraries (i.e., import java.util.*;)
  2. class
  3. encapsulation and information hiding (e.g., public vs. private, etc.)
  4. class name must be the same as program file name
  5. blocks (i.e., { ... })
  6. method
  7. special method main() (i.e., program entry point by Java VM)
  8. static vs. non-static methods
  9. void vs. value returning methods
  10. parameter passing
  11. arrays (i.e., String[] args)
  12. command-line parameter passing (i.e., String[] args)
  13. class instantiation (class vs. object)
  14. you may instantiate an object within its own class (i.e., chicken-and-egg paradox)
  15. statement terminator (i.e., ;)
  16. data types
  17. primitive data types vs. non-primitive data types
  18. variable
  19. assignment
  20. variable declaration
  21. input
  22. special class Scanner
  23. input streams, e.g., System.in
  24. class member method invocation (e.g., input.nextInt())
  25. iterators (e.g., nextInt())
  26. output (i.e., System.out.println)
  27. hierarchical composition (i.e., System constains object out)
  28. algebraic expressions
  29. Math library (i.e., sqrt())
  30. return statement


Pythagorean Theorem in Python.
from math import *

def hypotenuse(a, b):
    return(sqrt( (a*a) + (b*b) ))

x = input()
y = input()

print hypotenuse(x, y)

For the Python program, a programmer needs to master the following concepts/"nonobvious ideas":

  1. module (i.e., from math import *)
  2. function
  3. blocks (i.e., indentation)
  4. statement terminator (i.e., newline)
  5. functions may or may not return a value
  6. parameter passing
  7. variable
  8. assignment
  9. input (i.e., input())
  10. output (i.e., print)
  11. algebraic expressions
  12. return statement


The above is a generic, simple (rather typical?) programming task. Given human cognitive limitations (e.g., the 7 plus-or-minus 2 rule), it is not surprising that programmers report at least a 3 times increase in productivity when they move from Java (or C/C++) to Python.

Also, the above comparison does not capture the amount of effort required to master each of the required concepts. Some concepts are easier to master than others (e.g., "statement terminator" vs. "class"). Also, a single concept may be easier to master in one language vs. another (e.g., "input" or "method/function").

It would be interesting to generate a relative index of difficulty by, say, counting the number of lines used in a typical CS book to explain each of these concepts.


  1. Kay, A. "The Early History of Smalltalk", ACM SIGPLAN Notices, Volume 28, Number 3, March 1993.
  2. Miller, G.A. (1956), "The Magical Number Seven, Plus or Minus Two: Some Limits on Our Capacity for Processing Information", The Psychological Review, vol. 63, pp. 81-97.
  3. McConnell, J.J. and Burhans, D.T. (2002), "The Evolution of CS1 Textbooks", 32nd ASEE/IEEE Frontiers in Education Conference, November 6-9, 2002, Boston, MA, pp. T4G1-T4G6. "The average size for the Java books in our study is 2.25 times the average size of the Fortran books and 1.95 times the average size of the Pascal books. From the perspective of a course, the authors of three of the four Java books we examined expect that their entire book (averaging 880 pages) will be covered in one semester. This amounts to 22 pages per class or about 66 pages per week that a student is expected to prepare."
  4. Manaris, B. (2006), "Python vs. Java in Intro to CS Courses", a collection of resources and incomplete thoughts.