Python for Java programmers

 

 

Java

 

Python

 

 

compiled:

 

javac MyClass.java

java MyClass 

 

 

interpreted:

 

python MyProgram.py

      

 

variables must be declared (static typing)

no declarations (dynamic typing)

indentation encouraged

indentation necessary

comments start with //

comments start with #

filename must match class name

no

all statements followed by ;

no

program must be embedded in class

no

 

 

print and string formatting

 

System.out.printf(“Student: %s ~ ID: %d%n”, name, id);

 
or

System.out.println(“Student: ” + name + ” ~ ID: ” + id); 

 

Student: Arthur ~ ID: 731137

 

 print “Student: %s ~ ID: %d” %(name, id)

 

 or

 

print “Student:”, name, “~ ID:”, id

 

 

 

if-statement

 

if (cond) {

a = a + 1;

cond = false;

}

else if (!cond) {

a = a - 1;

}

q = 17;      

 

 

 

 

if cond:

    a = a + 1

    cond = 0

elif not cond:

    a = a - 1

q = 17

 

block statement surrounded by { }

block statement indented, follows a :

condition surrounded by ( )

no

Boolean type values true and false

Boolean types True and False

 

logical operators && and || and !

 

if (a<b) && (b<c) {..}

if (a==2) || (a==3) {..}

if (!found_item) {..}

 

 

 

if a<b and b<c:

 ..

if a==2 or a==3:

 ..

if not found_item:

 ..

 

 

while-statement

 

s = “”;

while (counter > 0) {

s = s + “O”;

counter--;

}

mystring = s;

 

 

 

s = “”

while counter > 0:

    s = s + “O”

    counter -= 1

mystring = s

 

 

NB: a string with 5 O’s can be created with

 

5 * “O”

 

 

 

for-statement

 

int s = 0

for (int i=0; i<10; i++) {

s = s + i;

}

 

 

 

 

s = 0

for i in range(10):

   s += i

 

NB: can’t use ++ , use += 1 instead

 

 

 

Classes and objects

 

public class DNA {

 public static int dna_objects; //class var.

 private int length;            //object var.

 

 public DNA() {                 //constructor

  length = 0;

 }

 ..

 

 public int getLength() {

  return length;

 }

}

 

DNA dna = new DNA();

 

 

 

 

class DNA:

  dna_objects = 0        # class variable

 

  def __init__(self):    # constructor

     self.length = 0     # object variable

  ..

 

  def getLength(self):

     return self.length

 

 

 

dna = DNA()

 

private, public fields and methods

everything is public

class variables declared static 

 

object variables must be prefixed with self. and should be initialized in constructor,

class variables do not have prefix and are initialized in class definition

 

keyword this refers to the object itself

 

self must be first parameter in all methods (ignore when calling a method)

 

constructor has same name as class

constructor is called __init__

new keyword used when creating objects

only ( )

 

 

 

           

 

// silly Java example                 

public class MyClass {

 public static void main(String[] a) {

  int a,  x = 5;                                        # analogous Python example

  String s = “Java is træls”;                           x = 5

  if (x == 5) {                                         s = “Python is neat”

   x = x + 1;                                           if x == 5:

   System.out.print(s);                                       x = x + 1

  }                                                           print s

  else if (x == 3) {                                    elif x == 3:

    x = x + 2;                                                x = x + 2

    }                                                   else:

  else {                                                      x = x + 3

    x = x + 3;                                           a = x

    }

  a = x;

  }

}