跳到主要内容

Python for Java Developers: Modern Syntax Crash Course - I

· 阅读需 12 分钟

🚀 From Java to Python: The Essential Mindset Shift​

Java Mindset → Python Mindset​

  • Verbose → Concise (Python is ~5-10x less code)
  • Explicit types → Duck typing ("If it walks like a duck...")
  • Compile-time → Runtime (More flexibility, less safety)
  • Class-heavy → Function-friendly (Functions are first-class)
  • public static void main → if __name__ == "__main__"

📦 1. Package & Module Structure​

Java vs Python Structure​

// Java
com/
example/
project/
Main.java
utils/
StringHelper.java
models/
User.java
# Python
project/
├── __init__.py # Makes directory a package
├── main.py # Entry point
├── utils/
│ ├── __init__.py
│ └── string_helper.py # snake_case, not camelCase!
└── models/
├── __init__.py
└── user.py # lowercase module names

Import System​

# Java: import com.example.project.models.User;
# Python:

import models.user # Full module
from models.user import User # Specific class
from models.user import User as U # Alias
from models import user # Package import

# Special imports
import models.user as mu # Module alias
from .utils import helper # Relative import (same package)
from ..parent import something # Parent package

🎯 2. Basic Syntax Comparison​

Variables & Types​

// Java
String name = "Alice";
int age = 30;
final double PI = 3.14159;
List<String> names = new ArrayList<>();
Map<String, Integer> scores = new HashMap<>();
# Python (NO semicolons, NO type declaration needed)
name = "Alice" # str - Unicode by default!
age = 30 # int - arbitrary precision!
PI = 3.14159 # Constants by convention (uppercase)
names = [] # list - mutable array
scores = {} # dict - hash map

Type Hints (Python 3.5+)​

# Optional type hints (like Java generics, but not enforced at runtime)
from typing import List, Dict, Optional, Union, Any

name: str = "Alice"
age: int = 30
PI: float = 3.14159
names: List[str] = []
scores: Dict[str, int] = {}
maybe_value: Optional[int] = None # int or None
flexible: Union[str, int] = "hello" # str or int
anything: Any = 42 # Like Java's Object

🔄 3. Control Flow​

If-Else (No Parentheses!)​

// Java
if (score >= 90) {
grade = 'A';
} else if (score >= 80) {
grade = 'B';
} else {
grade = 'C';
}
# Python
if score >= 90:
grade = 'A' # INDENTATION MATTERS! (4 spaces)
elif score >= 80: # elif, not else if
grade = 'B'
else:
grade = 'C'

# One-liner (use sparingly)
grade = 'A' if score >= 90 else 'B' if score >= 80 else 'C'

Loops​

// Java
for (int i = 0; i < 10; i++) {
System.out.println(i);
}

for (String name : names) {
System.out.println(name);
}

int i = 0;
while (i < 10) {
i++;
}
# Python
# Range loop (0 to 9)
for i in range(10): # range(start, stop, step)
print(i)

# For-each loop
for name in names: # Direct iteration
print(name)

# With index
for i, name in enumerate(names, start=1): # Gets (index, value)
print(f"{i}: {name}")

# While loop
i = 0
while i < 10:
i += 1 # No ++ operator!

Switch-Case Alternative (Python 3.10+)​

// Java
switch (day) {
case 1: System.out.println("Monday"); break;
case 2: System.out.println("Tuesday"); break;
default: System.out.println("Other");
}
# Python 3.10+ - Pattern Matching (like switch)
match day:
case 1:
print("Monday")
case 2:
print("Tuesday")
case _: # Default case
print("Other")

# Advanced pattern matching
match point:
case (0, 0):
print("Origin")
case (x, 0):
print(f"On x-axis at {x}")
case (0, y):
print(f"On y-axis at {y}")
case (x, y):
print(f"Point at ({x}, {y})")

📝 4. Functions vs Methods​

Function Definition​

// Java
public static int add(int a, int b) {
return a + b;
}

// Overloading
public static int add(int a, int b, int c) {
return a + b + c;
}
# Python (indentation defines function body)
def add(a: int, b: int) -> int: # Type hints optional
"""Docstring - describes function""" # Like JavaDoc
return a + b

# Default parameters (instead of overloading)
def add(a: int, b: int, c: int = 0) -> int:
return a + b + c

add(1, 2) # 3
add(1, 2, 3) # 6

# Variable arguments
def sum_all(*args: int) -> int: # *args = variable positional args
return sum(args)

sum_all(1, 2, 3, 4) # 10

# Keyword arguments
def create_user(name: str, **kwargs) -> dict: # **kwargs = keyword args
user = {"name": name, **kwargs}
return user

create_user("Alice", age=30, email="alice@example.com")

Lambda Functions (Like Java Lambdas)​

// Java
Function<Integer, Integer> square = x -> x * x;
Comparator<String> comparator = (a, b) -> a.compareTo(b);
# Python
square = lambda x: x * x # Anonymous function
square(5) # 25

# Use with map/filter (like Java Streams)
numbers = [1, 2, 3, 4]
squared = list(map(lambda x: x * x, numbers)) # [1, 4, 9, 16]
evens = list(filter(lambda x: x % 2 == 0, numbers)) # [2, 4]

# Better: Use comprehensions (see below)

🏗️ 5. Classes & Objects​

Basic Class​

// Java
public class Person {
private String name;
private int age;

public Person(String name, int age) {
this.name = name;
this.age = age;
}

public String getName() { return name; }
public void setName(String name) { this.name = name; }

public String greet() {
return "Hello, I'm " + name;
}
}
# Python
class Person:
"""Python class - much simpler!"""

def __init__(self, name: str, age: int): # Constructor
self.name = name # Public by default
self.age = age # No getters/setters needed usually

def greet(self) -> str: # self = this
return f"Hello, I'm {self.name}"

# Special methods (like toString, equals)
def __str__(self) -> str:
return f"Person(name={self.name}, age={self.age})"

def __eq__(self, other) -> bool:
if not isinstance(other, Person):
return False
return self.name == other.name and self.age == other.age

# Usage
alice = Person("Alice", 30)
print(alice.name) # Direct access (no getter)
alice.name = "Alicia" # Direct modification
print(alice) # Uses __str__

Properties (Getters/Setters Pythonic Way)​

class Temperature:
def __init__(self, celsius: float):
self._celsius = celsius # Private by convention (_prefix)

@property # Getter
def celsius(self) -> float:
return self._celsius

@celsius.setter # Setter
def celsius(self, value: float):
if value < -273.15:
raise ValueError("Too cold!")
self._celsius = value

@property # Computed property
def fahrenheit(self) -> float:
return self._celsius * 9/5 + 32

temp = Temperature(25)
print(temp.celsius) # 25
print(temp.fahrenheit) # 77.0
temp.celsius = 30 # Uses setter

Dataclasses (Python 3.7+ - Like Lombok!)​

from dataclasses import dataclass, field
from typing import List, ClassVar

@dataclass
class User:
"""Auto-generates __init__, __repr__, __eq__"""
name: str
age: int = 18 # Default value
emails: List[str] = field(default_factory=list) # Mutable default
id: int = field(init=False) # Not in __init__

# Class variable (like static)
count: ClassVar[int] = 0

def __post_init__(self):
"""Runs after auto-generated __init__"""
self.id = hash(self.name)
User.count += 1

def greet(self):
return f"Hi, I'm {self.name}"

# Auto-generated: __init__, __repr__, __eq__
user = User("Alice", 30)
print(user) # User(name='Alice', age=30, emails=[], id=...)

📊 6. Collections: List, Dict, Set, Tuple​

Lists (Like ArrayList)​

# Creation
numbers = [1, 2, 3, 4, 5]
mixed = [1, "hello", 3.14, True] # Can mix types!

# Access
first = numbers[0] # 1
last = numbers[-1] # 5 (negative index from end)
sublist = numbers[1:4] # [2, 3, 4] (slice)
sublist = numbers[::2] # [1, 3, 5] (step)

# Modification
numbers.append(6) # [1, 2, 3, 4, 5, 6]
numbers.insert(0, 0) # [0, 1, 2, 3, 4, 5, 6]
numbers.extend([7, 8]) # Extend with another list
popped = numbers.pop() # Remove and return last
removed = numbers.pop(0) # Remove at index

# List Comprehensions (VERY IMPORTANT!)
squares = [x * x for x in range(10)] # [0, 1, 4, ..., 81]
evens = [x for x in range(10) if x % 2 == 0] # [0, 2, 4, 6, 8]
pairs = [(x, y) for x in range(3) for y in range(3)] # Nested loops

Dictionaries (Like HashMap)​

# Creation
person = {"name": "Alice", "age": 30}
person = dict(name="Alice", age=30) # Alternative

# Access
name = person["name"] # Alice
age = person.get("age", 0) # 30, with default 0
keys = person.keys() # dict_keys(['name', 'age'])
values = person.values() # dict_values(['Alice', 30])
items = person.items() # dict_items([('name', 'Alice'), ...])

# Modification
person["email"] = "alice@example.com" # Add/update
person.update({"city": "NYC", "age": 31}) # Multiple updates
email = person.pop("email") # Remove and return

# Dict Comprehensions
squares = {x: x * x for x in range(5)} # {0: 0, 1: 1, 2: 4, ...}
swapped = {v: k for k, v in person.items()} # Swap keys/values

Sets (Like HashSet)​

# Creation
primes = {2, 3, 5, 7}
evens = set([2, 4, 6, 8]) # From list

# Operations
union = primes | evens # {2, 3, 4, 5, 6, 7, 8}
intersection = primes & evens # {2}
difference = primes - evens # {3, 5, 7}
sym_diff = primes ^ evens # {3, 4, 5, 6, 7, 8}

# Set Comprehensions
unique_chars = {c for c in "hello world"} # {'h', 'e', 'l', 'o', ' ', 'w', 'r', 'd'}

Tuples (Immutable Lists)​

# Creation
point = (10, 20)
person = ("Alice", 30, "NYC") # Heterogeneous
single = (42,) # Note comma for single element

# Access
x, y = point # Unpacking: x=10, y=20
name, age, city = person # Multiple unpacking
first = point[0] # 10

# Named Tuples (Python 3.6+)
from typing import NamedTuple

class Point(NamedTuple):
x: int
y: int

p = Point(10, 20)
print(p.x, p.y) # 10 20

🔧 7. Error Handling​

Try-Catch-Finally​

// Java
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Division by zero");
} catch (Exception e) {
System.out.println("Other error");
} finally {
System.out.println("Always runs");
}
# Python
try:
result = 10 / 0
except ZeroDivisionError as e: # Specific exception
print(f"Division by zero: {e}")
except (TypeError, ValueError) as e: # Multiple exceptions
print(f"Type or value error: {e}")
except Exception as e: # General catch
print(f"Other error: {e}")
else: # Runs if NO exception
print("Success!")
finally: # Always runs
print("Cleanup")

# Raise exceptions
if age < 0:
raise ValueError("Age cannot be negative")

# Custom exception
class ValidationError(Exception):
"""Custom exception with additional info"""
def __init__(self, message, field):
super().__init__(message)
self.field = field

raise ValidationError("Invalid value", "email")

📁 8. File Operations​

Reading Files​

# Java: BufferedReader, FileReader, try-with-resources
# Python: Context managers (automatic cleanup)

# Read entire file
with open("data.txt", "r", encoding="utf-8") as file: # Auto-closes
content = file.read() # Entire file as string
lines = file.readlines() # List of lines

# Read line by line (memory efficient)
with open("large_file.txt", "r") as file:
for line in file: # Iterate directly
process(line.strip()) # strip() removes newline

# Read binary file
with open("image.jpg", "rb") as file: # 'b' for binary
data = file.read()

Writing Files​

# Write text
with open("output.txt", "w") as file: # 'w' overwrites, 'a' appends
file.write("Hello\n")
file.writelines(["Line 1\n", "Line 2\n"])

# Write with print
with open("log.txt", "a") as file:
print("Error occurred", file=file) # Redirect print to file

🎨 9. String Formatting (Modern Approaches)​

name = "Alice"
age = 30

# 1. f-strings (Python 3.6+ - BEST!)
message = f"Hello, {name}. You are {age} years old."
expression = f"Next year: {age + 1}"
formatting = f"Price: ${price:.2f}"
multiline = f"""
Name: {name}
Age: {age}
"""

# 2. str.format() (Python 2.7+)
message = "Hello, {}. You are {} years old.".format(name, age)
named = "Hello, {name}. You are {age} years old.".format(name=name, age=age)

# 3. Template strings (safe for user input)
from string import Template
template = Template("Hello, $name. You are $age years old.")
message = template.substitute(name=name, age=age)

🛠️ 10. Essential Built-in Functions​

Commonly Used Functions​

# Type checking
isinstance(obj, Person) # Like instanceof
type(obj) # Get type
callable(func) # Check if callable

# Math
abs(-5) # 5
round(3.14159, 2) # 3.14
min([1, 2, 3]) # 1
max([1, 2, 3]) # 3
sum([1, 2, 3]) # 6

# Collection operations
len([1, 2, 3]) # 3
sorted([3, 1, 2]) # [1, 2, 3]
reversed([1, 2, 3]) # Reverse iterator
enumerate(["a", "b"]) # (0, 'a'), (1, 'b')
zip([1, 2], ["a", "b"]) # (1, 'a'), (2, 'b')

# Boolean
any([False, True, False]) # True (any true)
all([True, True, False]) # False (all true)

# Conversion
int("42") # 42
float("3.14") # 3.14
str(42) # "42"
list((1, 2, 3)) # [1, 2, 3]
dict([("a", 1), ("b", 2)]) # {'a': 1, 'b': 2}

🎯 Quick Reference Cheat Sheet​

Java → Python Mapping​

JavaPythonNotes
ArrayList<String>List[str]Type hints, use []
HashMap<K,V>Dict[K, V]Use {}
HashSet<T>Set[T]Use {}
String[]Tuple[str, ...]Immutable
System.out.printlnprint()No semicolon
public static void mainif __name__ == "__main__"
try-catch-finallytry-except-else-finally
for (int i=0; i<n; i++)for i in range(n)
foreachfor item in collection
switchmatch (Python 3.10+)
interfaceProtocol or ABC
abstract classABC
finalNo direct equivalentUse _ prefix convention
private_prefix (convention)Actually public

Must-Know Python Idioms​

# Swap variables
a, b = b, a

# Multiple assignment
x, y, z = 1, 2, 3

# Unpacking
first, *middle, last = [1, 2, 3, 4, 5]

# Dictionary merge (Python 3.9+)
dict1 = {"a": 1}
dict2 = {"b": 2}
merged = dict1 | dict2 # {'a': 1, 'b': 2}

# Walrus operator (Python 3.8+)
if (n := len(items)) > 10: # Assign and compare
print(f"Too many items: {n}")

# Chained comparisons
if 0 <= x <= 100: # Instead of: 0 <= x && x <= 100
print("In range")

📚 Practice Exercises​

Convert these Java snippets to Python:

Exercise 1: Calculate Average​

// Java
public static double average(int[] numbers) {
int sum = 0;
for (int num : numbers) {
sum += num;
}
return numbers.length > 0 ? (double)sum / numbers.length : 0;
}
# Python solution
def average(numbers: List[int]) -> float:
return sum(numbers) / len(numbers) if numbers else 0.0

# One-liner with walrus
def average(numbers: List[int]) -> float:
return sum(numbers) / (n if (n := len(numbers)) else 1)

Exercise 2: Filter Students​

// Java
public List<Student> filterByGrade(List<Student> students, String grade) {
List<Student> result = new ArrayList<>();
for (Student s : students) {
if (s.getGrade().equals(grade)) {
result.add(s);
}
}
return result;
}
# Python solution
def filter_by_grade(students: List[Student], grade: str) -> List[Student]:
return [s for s in students if s.grade == grade]

🚀 Next Steps​

In Chapater 2, we'll cover:

  • Advanced OOP (inheritance, abstract classes, interfaces)
  • Decorators and context managers
  • Async/await (like CompletableFuture)
  • Type hints and mypy
  • Testing with pytest
  • Popular libraries you should know

Key Takeaways for Java Developers​

  1. Less is more: Python is concise - embrace it
  2. Duck typing: Focus on behavior, not type
  3. Indentation matters: No braces, consistent indentation
  4. Everything is an object: Even functions and classes
  5. Batteries included: Rich standard library
  6. Community conventions: Follow PEP 8 style guide

Quick Start Commands​

# Create virtual environment (like Maven/Gradle but simpler)
python -m venv venv
source venv/bin/activate # Linux/Mac
venv\Scripts\activate # Windows

# Install packages
pip install package_name
pip install -r requirements.txt

# Run Python script
python script.py

# Interactive shell (REPL)
python -i script.py

# Type checking
pip install mypy
mypy script.py

# Format code (like Google Java Format)
pip install black
black script.py

Remember: Python is designed to be readable and concise. If your Python code looks like Java (lots of classes, getters/setters everywhere), you're probably not writing idiomatic Python!

Start by writing small scripts, then gradually incorporate more Pythonic patterns. The transition will feel liberating once you embrace Python's philosophy!