| Python | Java |
|---|---|
# This is a comment x = 3 # This is also a comment |
// This is a comment int x = 3; // This is also a comment |
| Python | Java |
|---|---|
""" This is another comment. It extends over more than one line. """ |
/* This is another comment. It extends over more than one line. */ |
int x;
x = 3; double y = 12.3; x = x + 1; java.util.Scanner sc = new java.util.Scanner(System.out); System.out.println(x); String z = sc.nextLine(); System.out.println(z);
| Python | Java |
|---|---|
if x < y:
print('x is smaller than y')
elif x == y:
print('x is equal to y')
else:
print('x is larger than y')
|
if (x < y) {
System.out.println('x is smaller than y');
} else if (x === y) {
System.out.println('x is equal to y');
} else {
System.out.println('x is larger than y');
}
|
In the Java, note:
elif does not exist&&, || and !and, or and notwhile loops| Python | Java |
|---|---|
while x < y:
x = x + 1
print(x)
|
while(x < y) {
x = x + 1;
System.out.println(x);
}
|
In the Java, note:
for loops| Python | Java |
|---|---|
groceries = ['eggs', 'bread', 'milk']
for item in groceries:
print(item)
|
We're not ready for lists and arrays in Java yet! |
| Python | Java |
|---|---|
for i in range(10):
print(i)
|
for (int i = 0; i < 10; i = i + 1) {
System.out.println(i);
}
|
i is referred to as the loop variable (or 'loop counter')int i = 0 declares and initialises the loop variablei < 10 is called the loop test (or 'loop condition')for loops
break, return, importtry...catch...finally (Java), try...except...else...finally (Python)continuethrow (Java), raise (Python)switch, do...whilepass, yield,...from random import choice
secret_num = choice( range( 1, 101 ) )
while True:
guess = int( input( "What's your guess: " ) )
if guess < secret_num :
print( " ", guess, "is too low" )
elif guess > secret_num :
print( " ", guess, "is too high" )
else :
print( " ", guess, "is correct" )
break
import java.util.Scanner;
import java.util.Random;
class GuessingGame {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Random randy = new Random();
int secret_num = randy.nextInt(100) + 1;
while (true) {
System.out.print("What's your guess: " );
int guess = sc.nextInt();
if (guess < secret_num) {
System.out.println( " " + guess + " is too low" );
} else if (guess > secret_num) {
System.out.println( " " + guess + " is too high" );
} else {
System.out.println( " " + guess + " is correct" );
break;
}
}
}
}
int n = 3;or declare first and initialize later, e.g.:
int n; ... n = 3;
Which lines give compile-time errors?
class InitializationExercise {
public static void main(String[] args) {
java.util.Scanner sc = new java.util.Scanner(System.in);
int x = 3;
int y;
int z;
y = sc.nextInt();
System.out.println(x + y + z);
z = sc.nextInt();
int i;
if (z < 999) {
i = 100;
}
System.out.println(i);
int j;
if (z < 999) {
j = 100;
} else {
j = x;
}
System.out.println(j);
}
}
This is why Python is such a dangerous language to use for real-world software:
z = int( input() )
if z < 999 :
i = 100
print(i)
for-loops are a special case)
Draw the scope of each local variable onto your printout
public static void main(String[] args) {
java.util.Scanner sc = new java.util.Scanner(System.in);
int x = sc.nextInt();
if (x < 0) {
int y = sc.nextInt();
x = x + y;
} else {
double z = sc.nextDouble();
x = x * z;
}
System.out.println(x);
}
Draw the scope of each local variable onto your printout
def stupid_function() :
x = int( input() )
if x < 0 :
y = int( input() )
x = x + y
else :
z = float( input() )
x = x * z
print(x);
Draw the scope and find the compile-time errors
public static void main(String[] args) {
java.util.Scanner sc = new java.util.Scanner(System.in);
int x = sc.nextInt();
if (x < 0) {
int y = sc.nextInt();
x = x + y;
}
System.out.println(x);
System.out.println(y);
if (x < 100) {
int z = sc.nextInt();
x = x + z;
} else {
double z = sc.nextDouble();
x = x * z;
}
System.out.println(z);
}
Draw the scope. Are there errors?
def stupid_function :
x = int( input() )
if x < 0 :
y = int( input() )
x = x + y
print(x)
print(y)
if x < 100 :
z = int( input() )
x = x + z
else :
z = float( input() )
x = x * z
print(z)
Draw the scope and find the compile-time errors (if any)
public static void main(String[] args) {
int x = 2;
for (int i = 0; i < 10; i += 1) {
x = x * x;
}
System.out.println(i);
System.out.println(x);
}
Draw the scope and find the compile-time errors (if any)
public static void main(String[] args) {
int x = 2;
int i;
for (i = 0; i < 10; i += 1) {
x = x * x;
}
System.out.println(i);
System.out.println(x);
}