Guide for Java developers
Python for Java Developers: Learn Python Using What You Already Know
Already know Java? Discover how familiar programming concepts translate into Python, what works differently, and the fastest path to getting productive.
Java vs Python, side by side
Each pair below does the same job. Read the Java you know, then see how Python expresses it.
Variables, types and functions
int total = 0;
String name = "Ada";
static int add(int a, int b) {
return a + b;
}total = 0
name = "Ada"
def add(a: int, b: int) -> int:
return a + bPython has no type declarations: a name simply refers to a value. Type hints document intent and help editors and tools, but Python does not enforce them while the program runs.
Lists, maps and dictionaries
List<String> langs = new ArrayList<>(List.of("Java", "Python"));
langs.add("SQL");
Map<String, Integer> ages = new HashMap<>();
ages.put("Ada", 36);
int age = ages.get("Ada");langs = ["Java", "Python"]
langs.append("SQL")
ages = {"Ada": 36}
age = ages["Ada"]Lists and dictionaries are built into the language, with literal syntax and no imports or generics. Reading a missing key with [] raises KeyError; use ages.get(name) to get None instead, like Java's Map.get.
Loops and iteration
for (int i = 0; i < names.size(); i++) {
System.out.println(i + ": " + names.get(i));
}for i, name in enumerate(names):
print(f"{i}: {name}")Python loops over items directly. When you also need the position, enumerate gives you the index and the value together, so there is no counter to manage.
Classes and objects
public class Point {
private final int x;
private final int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public double distanceToOrigin() {
return Math.sqrt(x * x + y * y);
}
}import math
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def distance_to_origin(self):
return math.hypot(self.x, self.y)self is an explicit first parameter instead of an implicit this, and you create objects without new. Python has no access modifiers; a leading underscore, as in _x, marks something as internal by convention.
What Java developers need to unlearn
A few habits carry over badly. These shifts matter most.
Indentation is the syntax
Blocks start with a colon and are defined by indentation instead of braces. Use four spaces and stay consistent.
Types are checked at runtime
Dynamic typing means a name can refer to any value, and type errors appear when the line runs. Optional type hints plus a checker such as
mypybring back some of Java's safety.Iterate over things, not indexes
Loop over the collection itself. Reach for
enumerateorzipbefore writingrange(len(items)).Lean on built-in collections
Lists, dictionaries, sets and tuples cover most of what you import from
java.util. A comprehension such as[len(w) for w in words]replaces a simple loop that builds a list; add anifat the end to filter.Write Python, not Java in Python
Skip getters, setters and one-method classes. Plain functions and modules are normal, and names use
snake_case.
Your Java-to-Python learning roadmap
Translate familiar concepts
Map Java concepts to Python.
Learn Python's differences
Collections, functions, idiomatic style.
Practice with real code
Solve small problems, not just read.
Build confidence
Harder, more realistic challenges.
Hintor's Python for Developers track is designed for developers switching languages, with hands-on practice built into the lessons. Explore the Python for Developers track
Mini challenge
Given a list of integers, write Python code that creates a new list containing the squares of only the even numbers.
- Input
- [1, 2, 3, 4, 5, 6]
- Expected output
- [4, 16, 36]
Hint: a list comprehension can include an if filter, and n % 2 == 0 tests whether n is even.
numbers = [1, 2, 3, 4, 5, 6]
even_squares = [n * n for n in numbers if n % 2 == 0]
print(even_squares) # [4, 16, 36]The comprehension reads left to right: compute n * n for each n in numbers, keeping only the even ones. A regular for loop with append works too; the comprehension is the idiomatic shortcut.
Ready to Turn Your Java Knowledge into Python Skills?
You already know how to program. Now learn to think in Python with guided lessons and hands-on practice.
Start Learning Python on Hintor