This is a practical translation guide for students who already know Python 3 and are starting to write sketches in Processing’s Java mode. It focuses on the syntax changes you’ll meet most often, not on teaching programming from scratch.

Contents


Quick translation table

Task In Python In Processing Java mode
Defining blocks Indentation defines blocks Braces { ... } define a block; indentation is still important for readability
Statements A newline ends a statement A semicolon ; ends a statement
Declaring a variable x = 5 Declare a type when creating a variable: int x = 5;
Boolean values True, False true, false
Boolean operators and, or, not &&, ||, !
If if x > 0: if (x > 0) {
Else if elif else if
Loop over a range for i in range(n): for (int i = 0; i < n; i++) {
Loop over each item for x in items: for (int x : items) {
While loop while x < 10: while (x < 10) {
Define a function def area(w, h): float area(float w, float h) {
Print text print(value) println(value); (or print(value); without a newline)
Length of an array or list len(items) items.length for arrays; items.size() for ArrayList
Append to a list items.append(x) items.add(x); on an ArrayList
Create an object b = Ball() Ball b = new Ball();
Call an object’s method b.move() b.move();

The most important habits to build: declare each variable’s type, and use braces and semicolons. The next sections show what that looks like in context.

Statements, blocks, and comments

Python uses a colon followed by an indented body to group statements inside functions, conditionals, loops, etc. Processing uses curly braces { ... }. A semicolon ends most statements, but not a block’s closing brace.

Python

if score >= 60:
    print("pass")
else:
    print("try again")

Processing

if (score >= 60) {
  println("pass");
} else {
  println("try again");
}

A simple rule: If a Python line ends with a colon : and is followed by an indented body, in Processing you will use curly braces { ... } around the body. But if a Python line does not end with a colon, then in Processing you should end that line with a semicolon ;.

Indent blocks in your Processing code too, even though the compiler uses braces—not indentation—to determine which statements belong in a block. A missing } can make several later lines look wrong, so check the braces around the whole block.

For comments, Python’s # becomes // for a one-line Processing comment. For a longer comment, you can wrap it in /* ... */.

Python

# A one-line comment

Processing

// A one-line comment
/* A comment
   that spans lines */

Variables and types

Python gives a variable a type based on the value assigned to it. In Processing, write the type when you declare the variable. Common types are int (whole numbers), float (decimal numbers), boolean (true or false), and String (text).

Python

age = 19
speed = 2.5
is_moving = True
name = "Ada"

Processing

int age = 19;
float speed = 2.5;
boolean isMoving = true;
String name = "Ada";

After declaration, a variable keeps its type: you can give age another integer value, but not a string. You can declare a variable first and assign it later:

int score;
score = 0;

Names are case-sensitive in both languages: score, Score, and SCORE are different names. Processing commonly uses camelCase (mouseX, frameRate, doSomethingFunction), so copy capitalization carefully.

A Python division gotcha

In Python 3, / produces a decimal result. In Processing, when both operands are integers, / does integer division and drops the fractional part:

Python

print(5 / 2)       # 2.5

Processing

println(5 / 2);      // 2
println(5.0 / 2);    // 2.5

If you need a fractional result, make at least one operand a float before dividing.

Conditions and comparisons

Use parentheses around a Processing condition, braces around its body, and Java-style logical operators:

Meaning Python Processing
Both conditions a and b a && b
Either condition a or b a || b
Negate not ready !ready
Otherwise-if elif else if
Equal values a == b a == b
Not equal a != b a != b

if, else if, and else are written like this:

Processing

if (temperature > 30) {
  println("hot");
} else if (temperature > 20) {
  println("comfortable");
} else {
  println("cool");
}

One important exception to the simple comparison table: compare strings with .equals(), not ==.

if (answer.equals("yes")) {
  println("You said yes.");
}

Use lowercase true and false for Boolean values. (True and False are Python spellings.)

Loops

Python’s range(n) produces 0 through n - 1. The equivalent counted loop in Processing has an initialization, a condition, and an update:

Python

for x in range(5):
    print(x)

Processing

for (int x = 0; x < 5; x++) {
  println(x);
}

The three parts inside the parentheses mean: start x at 0; keep going while x < 5; add one to x after each pass. Use <= only when you really do want to include the endpoint.

A while loop has the same basic structure in both languages, with Processing’s parentheses, braces, and semicolon:

while (x < 5) {
  x++;
}

x++ adds one, like x += 1 in Python. The ++ operator is optional; x += 1; works in Processing too.

Iterating over items

Python

for x in items:
    print(x)

Processing

for (int x : items) {
  println(x);
}

This works with both arrays and ArrayList. Note that the loop variable has to be declared with a type.

Functions

In Python, def introduces a function. In Processing, put the return type before the function name, and declare a type for every parameter. Use void as the return type when the function does not return a value.

Python

def area(width, height):
    return width * height

Processing

float area(float width, float height) {
  return width * height;
}

Calling the function is nearly the same; Processing adds a semicolon when the call is a statement:

Python

result = area(3, 4)

Processing

float result = area(3, 4);

Lists, arrays, and None

Python lists can grow and shrink, and can hold values of different types. A basic Processing array has a fixed length and a declared element type:

Python

scores = [90, 85, 100]
scores.append(95)
first_score = scores[0]
number_of_scores = len(scores)

Processing

int[] scores = {90, 85, 100};
int firstScore = scores[0];
int numberOfScores = scores.length;

Array indexes still start at 0. The array’s .length is a field, so write scores.length, not scores.length().

For a collection that can grow, Java provides ArrayList; its syntax is a little more involved than a Python list:

ArrayList<Integer> scores = new ArrayList<Integer>();
scores.add(90);
int firstScore = scores.get(0);
int numberOfScores = scores.size();

None is Python’s special value for “no value.” In Processing, null is used for a missing object reference (for example, a String or an object). Primitive values such as int and float cannot be null.

Choosing between an array and an ArrayList

  • Use an array when the size is fixed and known, such as a grid or a fixed set of samples.
  • Use an ArrayList when items are added or removed while the program runs, such as particles, bullets, or bouncing balls that appear over time.

A Processing sketch has a built-in rhythm

The syntax is only part of the transition. A Processing sketch also has two special functions: setup() runs once when the sketch starts, and draw() runs repeatedly to make animation. Put setup code such as the canvas size in setup(), and put the code for each frame in draw().

int x = 20;
int speed = 3;

void setup() {
  size(400, 200);
}

void draw() {
  background(255);
  x += speed;
  ellipse(x, height / 2, 30, 30);
}

This sketch declares x and speed outside the functions so they are global and their values persist from one call to draw() to the next. background(255) clears the previous frame by filling the window with white; remove the background() call to leave a trail. Processing calls draw() for you, so you do not write your own animation loop.

ellipse(x, y, width, height) draws an ellipse, and fill(255, 0, 0) sets the fill color. The canvas origin (0, 0) is at the upper-left; x increases to the right and y increases downward.

Common translation errors to check

When Processing reports an error, check these Python-to-Processing differences first:

  1. A type is missing: declare variables and specify function return and parameter types.
  2. A semicolon is missing: add ; after statements and declarations, but not after an if, loop, or function block.
  3. A brace is missing or misplaced: braces—not indentation—define the block.
  4. Python spelling slipped in: use true, false, else if, &&, and ||.
  5. Division dropped the decimal: make one operand a float.
  6. A string comparison uses ==: use text.equals("...").
  7. An array length was called like a function: use array.length.
  8. A name’s capitalization differs: ellipse and Ellipse are different.

For details on drawing functions, see the Processing Language Reference. For more background on classes and objects in Processing, see A Brief Guide to Objects.