Migrate from Java
Overview of Python:
Key Differences:
| Concept | Java | Python |
|---|---|---|
| Type System | Statically typed (e.g., int, String) |
Dynamically typed (types inferred at runtime) |
| Compilation | Compile into bytecode | Interpreted line by line |
| Curly Braces | Used to define blocks {} |
Indentation (4 spaces or tab) defines blocks; |
colons (:) indicate the start of controlled blocks (loops, functions, conditionals) |
||
| Semicolons | Required at the end of statements | Not required |
| Main Method | public static void main(String[] args) |
No main method needed |
In Java:
int number = 10;
String message = "Hello, World!";
In Python:
# Infer: Integer
number = 10
# Infer: String
message = "Hello, World!"
Java requires explicit data type declarations, while Python dynamically infers the type based on the value assigned.
In Java:
// Single-line comment
/*
Multi-line
comment
*/
In Python:
# Single-line comment
'''
Multi-line
comment
'''
In Java:
if (x > 10) {
System.out.println("x > 10");
} else if (x > 5) {
System.out.println("x > 5");
} else {
System.out.println("x < 5");
}
In Python:
if x > 10:
print("x > 10")
elif x >5:
print("x > 5")
else:
print("x < 5")
: is needed immediately after the condition e.g., x > 10. It is needed by for, while and def (function) and class definition.{} or semicolons, indentation defines code blocks.if, elif and else instead.