Compare With Java
Based on the knowledge of Java, it should be easier to learn Python. I don’t wanna learn from the beginning such as: “this is a variable, and now let’s see what we can do with a variable.”
So I mark this down here, to help me learn well when comparing with Java.
number
Java:
1 | int a = 1; |
Python:
1 | a = 1 |
String
Java:
1 | String s = "hello"; |
Python:
1 | s = 'hello' |
list
Java:
1 | List list = new ArrayList(); |
Python:
1 | list = [] |
if, for, while
Java:
1 | if(4 < 5){ |
Python:
1 | if 4 < 5: |
method/function
Java:
1 | public static void main(String[] args){ |
Python:
1 | def sum(a, b, c): |
OOD
Java:
1 | public class Dog{ |
Python:
1 | class Dog: |
others
1 | System.out.println(!(4 > 5)); |
1 | print(not (4 > 5)) |
Other Tricks
2d array
There is a methon like this:
1 | arr = [[0] * 4] * 3 |
However when we change a number, such as:
1 | arr[0][1] = 1 |
It will become:
1 | [[0, 1, 0, 0], |
So we have to do like this:
1 | arr = [([0] * 4 ) for i in range(4)] |
Or another way using numpy:
1 | import numpy as np |