Python For Java Developers Cheat Sheet
Python For Java Developers Cheat Sheet
1 Basic Syntax You declare multiple variables by separating each variable name
with a comma.
JAVA b = 2
c = "john"
if (0 == value) {
System.out.print("Number is Zero");
} else {
System.out.print("Number is non-Zero."); 3 Data Types
}
Python sets the variable type based on the value that is assigned to
System.out.print("All done!");
it. Unlike JAVA, Python will change the variable type if the variable
value is set to another value.
Python
var = 123 # This will create a number integer assignment
if 0 == value:
var = ’john’ # the ‘var‘ variable is now a string type.
print(’Number is Zero’)
else:
print(’Number is non-Zero.’)
3.1 Numbers
print(’All done!’)
Most of the time using the standard Python number type is fine.
Python will automatically convert a number from one type to an-
To indicate a block of code in Python, you must indent each
other whenever required. We don’t require to use the type casting
line of the block by the same amount. The two blocks of code in
like JAVA.
our example if-statement are both indented four spaces, which is
a typical amount of indentation for Python.
Type Java Python Description
2 Variables int int a = 11 a = 11 Signed Integer
long long a = 1712L a = 1712L (L) Long integers
2.1 Declaration float float a = 19.91 a = 19.91 (.) Floating point values
complex --- a = 3.14J (J) integer [0 to 255]
Variables are created the first time a value is assigned to them.
There is no concept of the declaration of the data type in python.
number = 11
string = "This is a string"
Create string variables by enclosing characters in quotes. Python list = new ArrayList() list=[]
uses single quotes 0
double quotes ” and triple quotes ””” list.add(object) list.append(object)
to denote literal strings. Only the triple quoted strings ””” will list.get(i) list[i]
automatically continue across the end of line statement. list.size() len(list)
list2= list.clone() list2=list[:]
firstName = ’Jane’
lastName = "Doe"
message = """This is a string that will span across multiple
lines. Using newline characters and no spaces for the
next lines."""
3.5 Tuple
Key Methods:
A tuple is another useful variable type similar to list and can contain
Java Python
heterogeneous values. Unlike the list, a tuple is immutable And is
charAt() find() like a static array.
indexOf() index()
A tuple is fixed in size and that is why tuples are replacing
length() len()
array completely as they are more efficient in all parameters.
replace() replace()
toString() str() Tuple can be used as an alternative of list(python/Java) OR
trim() rstrip(), lstrip() Array(Java) with respect to the use cases. i.e, If you have a
dataset which will be assigned only once and its value should not
Python String comparison can be performed using equality (==)
change again, you need a tuple.
and comparison (<, >, !=, <=, >=) operators. There are no special
methods to compare two strings. Tuple variables are declared by using parentheses () follow-
ing the variable name.
3.3 Boolean A = () # This is a blank tuple variable.
B = (1, 23, 45, 67) # creates a tuple of 4 numbers.
Python provides the boolean type that can be either set to False or
C = (2, 4, ’john’) # can contain different variable types.
True. In python, every object has a boolean value. The following D = ("A", B, C) # can contains other tuple objects as well.
elements are false:
• None
• False
3.6 Dictionary
• 0
• Empty collections: “”, (), [], Dictionaries in Python are lists of Key: Value pairs. This is a very
powerful datatype to hold a lot of related information that can be
All other objects are True. Note that, In JAVA null is not false associated through keys. Dictionary in Python can be the Alter-
while in python None is. native of the Map in JAVA. But again a Dictionary in python can
contain heterogeneous key as well as value.
3.4 List room_num = {’john’: 121, ’tom’: 307}
room_num[’john’] = 432 # set the value associated with the
The List is one of the most powerful variable type (data structure!)
’john’ key to 432
in Python. A list can contain a series of values. Unlike JAVA, the print (room_num[’tom’]) # print the value of the ’tom’ key.
list need not be always homogeneous. A single list can contain room_num[’isaac’] = 345 # Add a new key ’isaac’ with the
precedence.
|| or Conditional OR
if skill1 == "java" or skill2 == "python":
Comparison operators all have equal precedence; that is, they print ("At least One condition satisfies")
Note: == in python is actually .equals() of Java. Increase or decrease the counter variable by the value you
specify.
You can exit any for loop before the counter reaches its end That’s all Folks.
value by using the break statement. This ebook is made using Overleaf.
The source can be found on .
6.2 While Loop Thanks to ¯ Piyush Joshi for all the guidance for this project.
var1 = 3
while var1 < 37:
var1 = var1 * 2
print (var1)
print ("Exited while loop.")
while True:
n = raw_input("Please enter ’hello’:")
if n.strip() == ’hello’:
break
7 Iterations
The above will simply loop over the keys in the dictionary, rather
than the keys and values.
To loop over both key and value you can use the following: