This web page contains some hints for folks who are making the transition from:
The main target audience is students at UC Santa Barbara that took CS5nm during Fall 2008, and are now planning to take CS10 during Winter 2009, or Spring 2009. If others find it useful, then great—if not, then so be it.
This isn't intended to be a complete tutorial—rather just some hints about things that may be particularly troublesome. I'm building this page "in my spare time" as I come across things that I think may be confusing for students on this particular journey.
If you have ideas to contribute, email them to me.
Python comparison operators include: ==, !=, <, <=, >=, and >.
Java has all these same operators, so at first you may think you are home free.
However, there are a few differences you should be aware of.
Suppose we want to test whether three integers, a, b and c are equal. We can write this:
| Python | Java |
|---|---|
def threeEqual(a,b,c):
return (a==b) and (b==c) |
bool threeEqual(int a, int b, int c) {
return (a==b) && (b==c);
} |
Note that in both cases, we don't have to test whether a==c, becuase if a==b and b==c, it has to be true that a==c by transitivity.
But in Python, we can also write a==b==c as a boolean expression to test whether a, b and c are equal. This is NOT permitted in Java:
| This will work in Python | This will not work in Java! |
|---|---|
def threeEqual(a,b,c):
return a==b==c |
boolean threeEqual(int a, int b, int c) {
return a==b==c;
} |
The reason is that in Python, you can string any number of relational operators together. The rule in Python is that:
x relop y relop z
is equivalent to
(x relop y) and (y relop z)
And, this applies to any number of relational operators. So,
| This expression in Python | is equivalent to this, in Python |
|---|---|
a==b==c |
a==b and b==c |
a==b==c > 0 |
a==b and b==c and c > 0 |
0 < x <= 10 |
0 < x and x <=10 |
However, in Java, expressions like this usually result in either a syntax or a logic error:
| This expression in Java | is equivalent to this, in Java | and likely is an error, because: |
|---|---|---|
a==b==c |
(a==b)==c |
(a==b) is of type boolean, and will be compared with c |
0 < x <= 10 |
(0 < x) <= 10 |
(0<x) is of type boolean, and will be compared with 10 |
In fact, Python's ability to string relational operators together like this is rare among current programming languages. C, C++, JavaScript, and most other C-related language behave like Java in this respect.
So, while Python's ability to string relational operators together (a==b==c > 0) is nice in one sense, because it matches our understanding of relational operators from Math class, it could be a problem if you end up programming in languages other than Python. So, it may be best to avoid it, and use and when you need more than one relation to be true.
| If you'll be coding in Python, plus languages like C, C++ and Java, then to avoid confusion: |
|
|---|---|
| instead of writing this in Python... |
... get in the habit of writing this in Python: |
a==b==c |
a==b and b==c |
a==b==c > 0 |
a==b and b==c and c > 0 |
0 < x <= 10 |
0 < x and x <=10 |
One last thing: in Python, these two expressions are not the same.
| This expression in Python | is NOT equivalent to: |
|---|---|
a==b==c |
(a==b)==c |
If you understand why, that's a good start towards the understanding you'll need to work with expressions like this in Java.