A Comparison of The Syntax of Python and Java PDF
A Comparison of The Syntax of Python and Java PDF
Python Java
Python supports many (but not all) aspects of Java supports only object-oriented
object-oriented programming; but it is programming.
possible to write a Python program without
making any use of OO concepts.
Programs written in Java must be explicitly
Python is designed to be used interpretively. compiled into bytecodes (.class files),
A Python statement may be entered at the though an IDE may do this automatically in a
interpreter prompt (>>>), and will be executed way that is transparent to the user. Java does
immediately. (Implementations make some not support direct execution of statements -
use of automatic compilation into bytecodes though there are tools like Dr. Java that
(.pyc files). support this.
Python supports the following built-in data Java has two kinds of data types: primitive
types: types and reference types. Java supports the
following primitive data types:
Plain integers (normally 32-bit integers in
the range -2147483648 through byte - 8-bit integers
2147483647). short - 16-bit integers
Long integers (size limited only by memory int - 32-bit integers
size of the machine running on) long - 64-bit integers (Java also supports a
Booleans (False and True). class java.math.BigInteger to represent
Real numbers. integers whose size is limited only by
Complex numbers. memory)
float - 32-bit real numbers.
In addition, Python supports a number of
double - 32-bit real numbers.
types that represent a collection of values -
including strings, lists, and dictionaries. boolean - (false and true).
char - a single character.
1
A Comparison of the Basic Syntax of Python and Java
Python is line-oriented: statements end at the Statements in Java always end with a
end of a line unless the line break is explicitly semicolon (;). It is possible for a statement to
escaped with \. There is no way to put more run over more than one line, or to have
than one statement on a single line. multiple statements on a single line.
Examples: Examples:
this is a statement this is a statement;
this is another statement this is another statement;
this is a long statement that extends over more \ this is a long statement that extends over more
than one line than one line;
a statement; another; another;
Python comments begin with # and extend to Java has two kinds of comments. A comment
the end of the line. Example: beginning with // extend to the end of the
line (like Python comments). Comments can
# This is a comment
also begin with /* and end with */. These
A new statement starts here
can extend over multiple lines or be
embedded within a single line. Examples:
// This is a comment
A new statement starts here
/* This is also a comment */
/* And this is also a comment, which is
long enough to require several lines
to say it. */
Statement starts /* comment */ then continues
Python strings can be enclosed in either single Java strings must be enclosed in double
or double quotes (' or ""). A character is quotes (""). A character is a different type of
represented by a string of length 1. Examples: object and is enclosed in single quotes (').
'This is a string' Examples:
"This is also a string" # Equivalent
"This is a String"
'c' # A string
'c' // A character, but not a String
"c" # An equivalent string
Python uses the following operators for Java uses the following operators for
constructing compound boolean expressions: constructing compound boolean expressions:
and, or and not. Example: &&, ||, ! and ^ (meaning exclusive or)
Example:
not(x > 0 and y > 0) or z > 0
! (x > 0 && y > 0) || z > 0 ^ w > 0
In Python, the comparison operators (>, <, >=, In Java, most of the comparison operators
<=, == and !=) can be applied to numbers, ( >, <, >=, and <=) can be applied only to
strings, and other types of objects), and primitive types. Two (== and !=) can be
compare values in some appropriate way (e.g. applied to any object, but when applied to
numeric order, lexical order) where possible. reference types they test for same (different)
object rather than same (different) value.
2
A Comparison of the Basic Syntax of Python and Java
Python definite looping statements have the Java has two kinds of definite looping
form for variable in expression: Example: statements. One has the form
for (variable in collection) Example:
for p in pixels:
something for (p in pixels)
something;
Python uses the built-in function range() with Java uses a different form of the for to loop
for to loop over a range of integers. over a range of integers. Examples:
Examples: for (int i = 1; i < 10; i ++)
for i in range(1, 10) something;
something (i takes on values 1, 2, 3, 4, 5, 6, 7, 8, 9)
(i takes on values 1, 2, 3, 4, 5, 6, 7, 8, 9)
for (int i = 1; i < 10; i += 2)
for i in range(1, 10, 2) something;
something (i takes on values 1, 3, 5, 7, 9)
(i takes on values 1, 3, 5, 7, 9)
Python indefinite looping statements have the Java has two forms of indefinite looping
form while condition: Example: statement. One has the form
while (condition) Example:
while x > 0:
something while (x > 0)
something;
do
something;
while(x > 0);
3
A Comparison of the Basic Syntax of Python and Java
Python conditional statements have the form Java conditional statements have the form
if condition: and an optional else part has the if (condition) and an optional else part has
form else:. The form elif condition: is the form else (no colon) There is no elif
allowed as an alternative to an else: form - else if is used directly. Examples:
immediately followed by an if. Examples:
if (x < 0)
if x < 0: something;
something
if (x < 0)
if x < 0: something;
something else
else: something different;
something different
if (x < 0)
if x < 0: something;
something else if (x > 0)
elif x > 0: something different;
something different else
else: yet another thing;
yet another thing
Java also has another form of conditional
statement (switch) which has no analog in
Python. Example:
switch(i)
{
case 0:
something 1;
something 2;
...
break;
case 1:
something else;
break;
....
default:
yet something else;
}
4
A Comparison of the Basic Syntax of Python and Java
The scope of a Python conditional or looping The scope of a Java conditional or looping
statement is denoted by indentation. (If statement is normally just the next statement.
multiple lines are to be included, care must be Indentation is ignored by the compiler
used to be sure every line is indented (though stylistically it is still highly desirable
identically). Examples: for the benefit of a human reader). If
multiple lines are to be included, the scope
if x < 0:
do something must be delimited by curly braces ({ , }).
do another thing regardless of the value of x (Optionally, these can be used even if the
scope is a single line.) Examples:
if x < 0:
if (x < 0)
do something
do something else do something;
do yet a third thing do another thing regardless of the value of x;
do another thing regardless of the value of x
if (x < 0)
do something; // Bad style-don't do this!
do another thing regardless of the value of x;
if (x < 0)
{
do something;
do something else;
do yet a third thing;
}
do another thing regardless of the value of x;
if (x < 0)
{
do something;
}
do another thing regardless of the value of x;
5
A Comparison of the Basic Syntax of Python and Java
A Python function definition has the form A Java function definition always occurs in
def function-name(formal-parameter-list): the context of some class. It has the form
body type function-name(formal-parameter-list)
Example: {
body
def disc(a, b, c): }
return b * b - 4 * a * c Example:
A Python function is called by specifying its A Java function is called by specifying the
name followed by a list of actual parameters object or class to which it is to be applied
(that must be the same length as the list of followed by a dot, then its name followed by
formal parameters) Example: a list of actual parameters (that must be the
same length as the list of formal parameters,
discr(1, 2, 1) with each actual parameter compatible with
the declared type of the corresponding formal
parameter) Example:
solver.discr(1, 2, 1)