Answers.Body: <p><strong>TL;DR</strong> Use the Python module <code>dis</code> 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.</p>
  
  <hr>
  
  <p>Begin by defining "bytecode" in the context of Python. Here is the <a href="https://docs.python.org/3/glossary.html#term-bytecode" rel="nofollow noreferrer">Python 3 documentation</a>:</p>
  
  <blockquote>
    <p>Python source code is compiled into bytecode, the internal
    representation of a Python program in the CPython interpreter. The
    bytecode is also cached in <code>.pyc</code> files so that executing the same file
    is faster the second time (recompilation from source to bytecode can
    be avoided). <em>This “intermediate language” is said to run on a virtual
    machine that executes the machine code corresponding to each bytecode.</em> [emphasies added]
    Do note that bytecodes are not expected to work between different
    Python virtual machines, nor to be stable between Python releases.</p>
  </blockquote>
  
  <p>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 <code>dis</code> module.</p>
  
  <p>Let's consider this very short Python program <code>test.py</code>:</p>
  
  <pre><code>print("Hello, CSEducators.SE!")
  </code></pre>
  
  <p>If I run the command <code>python -m dis test.py</code>, I see the following Python bytecode:</p>
  
  <pre><code>  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
  </code></pre>
  
  <p>Now, let's write <code>Test.java</code>:</p>
  
  <pre><code>public class Test {
      public static void main(String args[]) {
          System.out.println("Hello, CSEducators.SE!");
      }
  }
  </code></pre>
  
  <p>I run <code>javac Test.java</code> then <code>javap -v Test.class</code>. Here's a small part of what I see:</p>
  
  <pre><code>  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
  </code></pre>
  
  <p>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).</p>
  
  <p>Additionally, consider this SO question: <a href="https://stackoverflow.com/questions/3265357/compiled-vs-interpreted-languages">"Compiled vs. Interpreted Languages"</a>. It's not quite so black-and-white, and forcing it into a pure dichotomy may do more harm than good.</p>
  
  <p><strong>Let it be a bit messy. That's what learning is all about.</strong></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-06-24T08:16:15.873Z
Answers.Text: I wasn't familiar with `dis`. Thanks!
Answers.UserDisplayName: ItamarG3
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-06-25T17:02:54.460Z
Answers.Text: 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.
Answers.UserDisplayName: Adam Williams
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-06-24T08:12:01.253Z
Answers.LastActivityDate: 2017-06-24T08:12:01.253Z
Answers.UserDisplayName: Peter
Answers.Body: <p>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.</p>
  
  <p>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.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-06-24T14:38:05.533Z
Answers.Text: 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.
Answers.UserDisplayName: ItamarG3
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-06-24T22:22:04.443Z
Answers.Text: 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?
Answers.UserDisplayName: Uncle Long Hair
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-06-24T14:35:53.927Z
Answers.LastActivityDate: 2017-06-24T14:35:53.927Z
Answers.UserDisplayName: Uncle Long Hair
Answers.Body: <p>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.</p>
  
  <p>However, the key difference is that Java is <em>statically typed</em>, but Python is <em>dynamically typed</em>. 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:</p>
  
  <pre><code>public _____ foo (int x) 
  {
       if (x &gt; 0) {
           return 1;
       } else {
           return "non-positive";
       }
  }
  </code></pre>
  
  <p>They would know that this code wouldn't compile; <code>foo</code> must return an <code>int</code> or a <code>String</code>, but not "either" or "both". This is because values must have predefined types in Java, i.e. static typing.</p>
  
  <p>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".</p>
  
  <p>Finally, don't let students miss out on Python's awesome features, such as simultaneous assignment, <code>lambda</code> expressions, and list comprehension. Enjoy! </p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-06-25T08:40:03.563Z
Answers.LastActivityDate: 2017-06-25T08:41:52.187Z
Answers.LastEditDate: 2017-06-25T08:41:52.187Z
Answers.UserDisplayName: xuq01
Answers.Body: <blockquote>
    <p>How can make the transition\introduction from java to python more smooth?</p>
  </blockquote>
  
  <p>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.</p>
  
  <blockquote>
    <p>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</p>
  </blockquote>
  
  <p>I don't think that's the real problem when switching from Java to Python. Python's model is actually <em>simpler</em> 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.</p>
  
  <h3>Using Jython to transition from Java to Python</h3>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <h3>Focus on the differences</h3>
  
  <p>I'd suggest you build your python lessons by focusing on the differences between Python and Java:</p>
  
  <ul>
  <li><p>Don't start by introducing classes and OOP in Python, for example, because unlike Java, you don't need them to write simple programs.</p></li>
  <li><p>Instead of OOP, you could discuss other programming styles. Start with simple procedural style, or baffle the students with functional programming.</p></li>
  <li><p>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 <em>iterates</em> 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.</p></li>
  <li><p>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 <em>values</em> 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. </p></li>
  </ul>
  
  <p>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.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-06-25T11:43:43.697Z
Answers.Text: Hi Pascal! Welcome to [cseducators.se]! This is a really useful answer, well fleshed out and informative. I hope to see you around the site more!
Answers.UserDisplayName: thesecretmaster
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-06-25T10:22:43.673Z
Answers.LastActivityDate: 2017-06-25T10:22:43.673Z
Answers.UserDisplayName: Pascal
Body: <p>I am preparing to teach students python, in addition to java. </p>
  
  <p>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).</p>
  
  <p>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.</p>
  
  <p>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. </p>
  
  <p>How can make the transition\introduction from java to python more smooth?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-06-24T08:44:20.213Z
Comments.Text: "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.
Comments.UserDisplayName: xuq01
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-06-24T08:47:00.087Z
Comments.Text: @xuq01 fair point, I edited the question. Thanks!
Comments.UserDisplayName: ItamarG3
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-06-26T08:40:10.883Z
Comments.Text: You might be interested in reading this paper (http://dl.acm.org/citation.cfm?id=2831459) on why one educator switched back to Java after switching to Python.
Comments.UserDisplayName: Brett Becker
Tags: <lesson-ideas><secondary-education><introductory-lesson>
Title: Transition from Java to Python
ThreadText: # Transition from Java to Python
  
  (Asked by: username_0 on 2017-06-24)
  
  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?
  
  **Tags:** lesson-ideas, secondary-education, introductory-lesson
  
  > **Comments:**
  > **username_1**: "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.  
  > **username_0**: @username_1 fair point, I edited the question. Thanks!  
  > **username_2**: You might be interested in reading this paper (http://dl.acm.org/citation.cfm?id=2831459) on why one educator switched back to Java after switching to Python.  
  
  ---
  
  ## 4 answers
  
  ### Answer by username_6 on 2017-06-25. Score: 4 | Accepted answer
  
  > 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.
  
  > **Comments:**
  > **username_7**: Hi username_6! Welcome to [cseducators.se]! This is a really useful answer, well fleshed out and informative. I hope to see you around the site more!  
  
  ---
  
  ### Answer by username_3 on 2017-06-24. Score: 4
  
  **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](https://docs.python.org/3/glossary.html#term-bytecode):
  
  > 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"](https://stackoverflow.com/questions/3265357/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.**
  
  > **Comments:**
  > **username_0**: I wasn't familiar with `dis`. Thanks!  
  > **username_4**: 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.  
  
  ---
  
  ### Answer by username_5 on 2017-06-24. Score: 3
  
  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.
  
  > **Comments:**
  > **username_0**: 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.  
  > **username_5**: 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?  
  
  ---
  
  ### Answer by username_1 on 2017-06-25. Score: 2
  
  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!
  
  
  ---
  
  