Jason Shepherd

from cerebral ephemera to digital permanence

  • Flickr Photostream

    Photos20090630 059

    Photos20090630 058

    Photos20090630 119

    More Photos
  • my del.icio.us

Archive for May, 2008

The case for (or against) Python in CS-I: Post 1

Posted by jbshep on May 29, 2008

At BV, we’re planning to switch from teaching Java in CS-I to teaching Python. In what may be a series of posts (depending on whether I actually see my good intentions through), I intend to investigate different approaches to problem solving using Python versus Java. Specifically, I plan to show how one would solve a problem in Python versus solving the problem in Java, and from there examining the stumbling blocks first-year CS students are likely to face in these solutions. These stumbling blocks might be something as simple as syntax or something more involved like data-type conversion.

I’d like to start with a fairly basic example: Euclid’s Greatest Common Divisor algorithm. This is a good first example (beyond “Hello, World!”) because it requires us to use several basic language features: looping, functions, variables, basic I/O, string parsing, and data-type conversion (from string to integer). Here is my solution in Python:

def gcd(m, n):
while n != 0:
r = m % n
m = n
n = r
return m

str = raw_input(“Enter two numbers separated by a space (or ‘quit’): “)
while str != “quit”:
x, y = str.strip().split()
print “gcd is”, gcd(int(x), int(y))
str = raw_input(“Enter two numbers (or ‘quit’): “)

And here is my solution in Java:

import java.io.Console;

public class EuclidGCD
{
public static int gcd( int m, int n )
{
while ( n != 0 )
{
int r = m % n;
m = n;
n = r;
}
return m;
}

public static void main( String[] args )
{
Console c = System.console();
if ( c == null )
{
System.err.println( “No console! Bailing….” );
System.exit( 1 );
}

String str = c.readLine( “Enter two numbers separated by a space (or ‘quit’): ” );
while ( str.equals( “quit” ) == false )
{
String[] numsStr = str.trim().split( ” ” );
System.out.println( “gcd is ” + gcd( Integer.parseInt( numsStr[0] ), Integer.parseInt(    numsStr[1] ) ) );
str = c.readLine( “Enter two numbers separated by a space (or ‘quit’): ” );
}
}
}

The first observation I would make is that these solutions are really very similar. I didn’t do anything in one language that was a radical departure from the other language. That’s good, I suppose. The next observation I would make is the greater prominence given to data-typing in Java. Everything must be explicitly declared to have a type in Java, whereas Python does not given its dynamically typed nature. If, in the Python version, students forget to cast their parsed strings for ‘m’ and ‘n’ to integers, Python will kindly remind them when the program runs. Java would alert students at compile time.  I thinking static versus dynamic data-typing is a matter of personal preference, especially for CS-I students (the argument is perhaps different for industrial software engineering).  One advantage I see to the dynamic typing nature of Python is that students can focus more on developing the algorithm and the logic and then have to focus less on making sure all the data types are correct.  That can come later.

Another observation is the greater relative “verbosity” of Java.  This is a consequence of Java’s lack of operator overloading and its insistence that everything be explicitly declared in a class context.  Verbosity is problematic for CS-I students.

Again, this is a fairly basic example that manages to test several different core programming concepts.  Hopefully, in future “installments,” I’ll be able to look at more involved examples to see how Python versus Java might impact the learning of CS-I students.

Posted in Uncategorized | Tagged: | 1 Comment »