3
$\begingroup$

I am preparing to teach students python, in addition to java.

Usually, I test a proof of concept (POC) of my teaching with a handful of students, so as to know if my lesson plans are good (I teach the lesson to 4-6 students of varying skill and then I ask them for feedback). This time, the students were introduced to Python, after they were taught Java (They were taught OOP and basic I\O).

For the POC lesson in python, the feedback I received showed the difficulty in understanding the difference between Python and Java. Python felt so different (and rightly so) to the students, and they couldn't quite understand how it works, when compared to java. They know java has the runtime environment which runs compiled code, but they don't know how python runs the code they write. This difference I'm referring to. That isn't the only difference they had difficulty with.

So, in an attempt to make the introductory lesson in python to the rest of the students, I am trying to explain those differences beforehand.

How can make the transition\introduction from java to python more smooth?

$\endgroup$
  • 4
    $\begingroup$ "Compiled" and "interpreted" are properties of implementations. CPython inteprets Python, but it first compiles Python to bytecode before actually interpreting it. PyPy compiles Python using JIT. Jython compiles Python to JVM bytecode, as does javac. As for Java, the Java bytecode needs to be eventually intetpreted, but there are implementations like gcj which compile Java to native code. Conclusion: don't complicate things; the bigger difference is that Python is dynamically typed while Java is statically typed. $\endgroup$ – xuq01 Jun 24 '17 at 8:44
  • $\begingroup$ @xuq01 fair point, I edited the question. Thanks! $\endgroup$ – ItamarG3 Jun 24 '17 at 8:47
  • $\begingroup$ You might be interested in reading this paper (dl.acm.org/citation.cfm?id=2831459) on why one educator switched back to Java after switching to Python. $\endgroup$ – Brett Becker Jun 26 '17 at 8:40
3
$\begingroup$

How can make the transition\introduction from java to python more smooth?

That's an interesting question, because usually it's done the other way around - you learn Python, which IMO is the simpler language to learn, and then get introduced to Java.

They know java has the runtime environment which runs compiled code, but they don't know how python runs the code they write. This difference I'm referring to

I don't think that's the real problem when switching from Java to Python. Python's model is actually simpler to understand - write some source code, run it, done. The compilation step is nicely hidden, even though it's there, too. There is no difference between Java and Python in that respect - Java compiles to the Java Virtual Machine, and Python compiles (if you're using CPython) to the CPython Bytecode Interpreter. Both are virtual machines, although the JVM is targeted by other languages (Python, for example, in the Jython variant), while the CPython Bytecode Interpreter is a virtual machine that only exists for Python, and AFAIK doesn't do JIT - but PyPy does.

Using Jython to transition from Java to Python

You could use Jython to make the transition smoother. Jython will allow your students to use the full Java API (all the classes they already know, including I/O) in Python. The full Java API is callable from Python source code if you use Jython.

However, I would discourage using Jython to that purpose, because your students will have trouble separating out the two languages. They will have difficulties identifiying what is Python and what is Java, and they'll question the use of Python if all it offers (from their viewpoint) is another syntax to access the same library functions.

Focus on the differences

I'd suggest you build your python lessons by focusing on the differences between Python and Java:

  • Don't start by introducing classes and OOP in Python, for example, because unlike Java, you don't need them to write simple programs.

  • Instead of OOP, you could discuss other programming styles. Start with simple procedural style, or baffle the students with functional programming.

  • If you've taught the students the standard c-like for-loop that increases a variable in Java, explain to the students how Python's for loop is fundamentally different from that because it's not there to increase a counter; rather it iterates over a collection of things (or something that can be enumerated). Then show them how lists can be iterated over using Python's for loop, but so can strings. That could be a good way to start a discussion about python's data model.

  • You'll need to talk about static/dynamic typing. Explain to the students that while in Java it's variables that are typed, in Python only values have types. Show them how that makes it very easy to build prototypes, and allows for a degree of freedom unparalleled by statically typed languages, but also makes it easy to make mistakes the compiler can't catch.

Note that this approach only works if your students already have a good understanding of what programming is, and how to do it in Java. If they're still struggling with learning the basics, introducing a second language IMO would be a bad idea, but also this approach wouldn't work very well - in that case, I'd suggest focusing on the similarities instead.

$\endgroup$
  • $\begingroup$ This is a great suggestion, thanks! $\endgroup$ – ItamarG3 Jun 25 '17 at 10:24
  • $\begingroup$ Hi Pascal! Welcome to Computer Science Educators! This is a really useful answer, well fleshed out and informative. I hope to see you around the site more! $\endgroup$ – thesecretmaster Jun 25 '17 at 11:43
3
$\begingroup$

TL;DR Use the Python module dis to show students Python's bytecode alongside Java's bytecode. Avoid the "compiled v. interpreted" rabbit hole to start (because it may lead to more confusion than clarity) and instead show students what's actually happening underneath the hood.


Begin by defining "bytecode" in the context of Python. Here is the Python 3 documentation:

Python source code is compiled into bytecode, the internal representation of a Python program in the CPython interpreter. The bytecode is also cached in .pyc files so that executing the same file is faster the second time (recompilation from source to bytecode can be avoided). This “intermediate language” is said to run on a virtual machine that executes the machine code corresponding to each bytecode. [emphasies added] Do note that bytecodes are not expected to work between different Python virtual machines, nor to be stable between Python releases.

That might sound to students not too dissimilar from their experience with Java and its use of a virtual machine. Also, take a close look at the documentation for the dis module.

Let's consider this very short Python program test.py:

print("Hello, CSEducators.SE!")

If I run the command python -m dis test.py, I see the following Python bytecode:

  1           0 LOAD_NAME                0 (print)
              3 LOAD_CONST               0 ('Hello, CSEducators.SE!')
              6 CALL_FUNCTION            1 (1 positional, 0 keyword pair)
              9 POP_TOP
             10 LOAD_CONST               1 (None)
             13 RETURN_VALUE

Now, let's write Test.java:

public class Test {
    public static void main(String args[]) {
        System.out.println("Hello, CSEducators.SE!");
    }
}

I run javac Test.java then javap -v Test.class. Here's a small part of what I see:

  public static void main(java.lang.String[]);
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=2, locals=1, args_size=1
         0: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
         3: ldc           #3                  // String Hello, CSEducators.SE!
         5: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
         8: return        
      LineNumberTable:
        line 3: 0
        line 4: 8

This is only a small part of it; the full output leaves a lot to explore. Nonetheless, at this point, students will see fewer differences than similarities (hopefully).

Additionally, consider this SO question: "Compiled vs. Interpreted Languages". It's not quite so black-and-white, and forcing it into a pure dichotomy may do more harm than good.

Let it be a bit messy. That's what learning is all about.

$\endgroup$
  • $\begingroup$ I wasn't familiar with dis. Thanks! $\endgroup$ – ItamarG3 Jun 24 '17 at 8:16
  • 1
    $\begingroup$ I wish I'd been exposed to the mechanics of programming languages earlier - I think I'd have really benefitted from something like this. It's only when I started looking at reverse engineering that I began to look at how the JVM worked, how I could disassemble code etc. $\endgroup$ – Adam Williams Jun 25 '17 at 17:02
3
$\begingroup$

If the students are getting caught up on the difference between a compiled vs. interpreted language (the difference you've described), I would say that they're missing the forest for the trees somewhat. While this is an important difference in the languages, there is a lot more to the comparison of the two besides this. They both have a lot of the same common concepts, especially with the streaming API's introduced in Java 8 which are similar to Python's comprehensions.

I think it can be an interesting discussion to understand how two languages that both reduce source code to bytecodes can be different, but in terms of learning to program in both of them that is probably not something that needs to be emphasized.

$\endgroup$
  • $\begingroup$ They have trouble adapting to how you program things in python. They write programs as though it's java. I'm trying to explain that the languages are different, and so programming in each of them in the same way isn't a good thing to do. $\endgroup$ – ItamarG3 Jun 24 '17 at 14:38
  • $\begingroup$ What do they have difficulty with? Is it the syntax? Things like comprehensions? Maybe certain toolkits? Can you provide more details or examples of things they have problems with? $\endgroup$ – Uncle Long Hair Jun 24 '17 at 22:22
2
$\begingroup$

Java and Python are actually more similar than you've probably thought, as long as students get over the concrete syntax. As I've said in the comments, "compiled" and "interpreted" are properties of specific implementations, not of languages; in case of Java (Oracle JDK/OpenJDK) and Python (CPython), this distinction is very blur.

However, the key difference is that Java is statically typed, but Python is dynamically typed. This means that there are no type errors unless you actually run the code. Your students will know that this method wouldn't work in Java:

public _____ foo (int x) 
{
     if (x > 0) {
         return 1;
     } else {
         return "non-positive";
     }
}

They would know that this code wouldn't compile; foo must return an int or a String, but not "either" or "both". This is because values must have predefined types in Java, i.e. static typing.

However, the Python counterpart to this would work perfectly, because Python values do not have types that are predefined, and values get types only when we actually get them. This, I believe, is really what you meant by "interpreted" vs "compiled".

Finally, don't let students miss out on Python's awesome features, such as simultaneous assignment, lambda expressions, and list comprehension. Enjoy!

$\endgroup$

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.