Awhile back, I wrote an article about how to approach Python from a Java perspective. My general reason for writing that article was to get students in my Java classes interested in Python. After all, I love the language. Why shouldn’t they?
Of course, that article really only gets at some of the major differences like static and dynamic typing. In this article, I thought it would be fun to actually put a list of syntax elements in Java and sharing their Python equivalents.
Table of Contents
Article Layout
Usually, I use a bit of a narrative style to write my articles. I know folks in the coding community can hate this. After all, the general approach we have as a community is to use the internet for answers. That’s why sites like Stack Overflow are so popular. As a result, I want to take an approach in this article that will help the most folks.
Rather than outlining all the differences and sharing anecdotes, I’m going to layout the remainder of this article using clear headings and lookup tables. For example, if you want to know how to convert from &&
to the Python equivalent, you can find the answer in the boolean operators section below.
With that said, if you think anything is missing, let me know!
Mapping Java Syntax to Python Syntax
In the remainder of this article, you’ll find tables for all the major types of syntax elements in Java such as boolean and arithmetic operators as well as control flow structures.
Boolean Operators
Boolean operators are a set of operators used to make sense of boolean values. These operators include but are not limited to AND, OR, and NOT.
Operator | Java | Python |
---|---|---|
AND | && | and |
OR | || | or |
NOT | ! | not |
Often time, the comparison operators are included in this set as well. For example, it’s not uncommon for LESS-THAN to be considered a boolean operator because the return value is a boolean.
Operator | Java | Python |
---|---|---|
LESS-THAN | < | < |
LESS-THAN-OR-EQUAL-TO | <= | <= |
EQUALS | == | == |
GREATER-THAN-OR-EQUAL-TO | >= | >= |
GREATER-THAN | > | > |
NOT-EQUALS | != | != |
Arithmetic Operators
In addition to boolean operators, most programming languages support some set of arithmetic operators. Fortunately, these map more or less one-to-one for Java and Python.
Operator | Java | Python |
---|---|---|
ADDITION | + | + |
SUBTRACTION | - | - |
MULTIPLICATION | * | * |
DIVISION | / | / |
FLOOR DIVISION | N/A | // |
POWER | N/A | ** |
REMAINDER | % | N/A |
MODULUS | N/A | % |
I particularly like to lay out the operators like this because there are operators between the two languages that are similar but not the same. For example, the Java division operator depends on the types of numbers used around it. Meanwhile, the Python division operator behaves exactly as you’d expect. If you want an operator that behaves like integer division in Python, you can use the floor division operator.
Similar arguments can be made about the remainder and modulus operators. Python implements true modulus while Java only implements remainder. The difference often doesn’t matter, but it’s important to keep in mind for common tasks like testing even and odd. In true modulus, mod by two guarantees a positive number. That is not true for the remainder operator.
Assignment Operators
It might seem weird to consider more than one assignment operator, but many languages include multiple compound assignment operators for arithmetic. Let’s take a look.
Operator | Java | Python |
---|---|---|
ASSIGNMENT STATEMENT | = | = |
ASSIGNMENT EXPRESSION | = | := |
ADDITION ASSIGNMENT | += | += |
SUBTRACTION ASSIGNMENT | -= | -= |
MULTIPLICATION ASSIGNMENT | *= | *= |
DIVISION ASSIGNMENT | /= | /= |
REMAINDER ASSIGNMENT | %= | N/A |
MODULUS ASSIGNMENT | N/A | %= |
In addition, Java features increment and decrement assignment operators that are not included in Python:
Operator | Java | Python |
---|---|---|
INCREMENT | ++ | N/A |
DECREMENT | -- | N/A |
Control Flow
Because both Python and Java are imperative programming languages, we have access to various control flow structures. Let’s start with the various if statement structures.
Structure | Java | Python |
---|---|---|
IF STATEMENT |
|
|
IF/ELSE STATEMENT |
|
|
IF/ELSE IF/ELSE STATEMENT |
|
|
One thing I find really interesting about the if statement structures in Java and Python is that Python has a special keyword for “else if”. Java, on the other hand, does not have a special syntax for “else if” and instead relies on nesting of the else and the if keywords.
With that said, let’s take a look at loops.
Structure | Java | Python |
---|---|---|
WHILE LOOP |
|
|
FOR LOOP |
| N/A |
FOR EACH LOOP |
|
|
DO WHILE LOOP |
| N/A |
While I tend to argue that there are no traditional for loops in Python, it’s not impossible to make one. For example, many folks use range()
or enumerate()
to simulate traditional for loops.
Looking for More?
Like Java, Python is a language that has many features that aren’t demonstrated here. For example, here’s a list of some of my favorite Python language features.
If you’re interested in learning more about Python, I have several series that attempt to broach the topic from the lens of a beginner. For instance, if you’re interested in learning how to make a Discord bot and Python at the same time, I have a series for that. Likewise, I have a series for folks who want to learn Python in a more traditional way.
While you’re here, I’d appreciate it if you took a peek at some of these Python resources (#ad):
- Effective Python: 90 Specific Ways to Write Better Python
- Python Tricks: A Buffet of Awesome Python Features
- Python Programming: An Introduction to Computer Science
Otherwise, thanks again for hanging out! I appreciate your time.
Recent Posts
While creating some of the other early articles in this series, I had a realization: something even more fundamental than loops and if statements is the condition. As a result, I figured we could...
Today, we're expanding our concept map with the concept of loops in Python! Unless you're a complete beginner, you probably know a thing or two about loops, but maybe I can teach you something new.