JV
PY

Java to Python

10 lessons

Progress0%
1Variables & Types2Classes & OOP3Collections4Exception Handling5File I/O6Functional Programming7Duck Typing and Protocols8Python Ecosystem9Type Hints and Static Analysis10Context Managers and Resources
All Mirror Courses
JV
PY
Variables & Types
MirrorLesson 1 of 10
Lesson 1

Variables & Types

Static typing vs dynamic typing

Introduction

In this lesson, you'll learn about variables & types in Python. Coming from Java, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.

Mirror Card
JV
From Java:

In Java, you're familiar with static typing vs dynamic typing.

PY
In Python:

Python has its own approach to static typing vs dynamic typing, which we'll explore step by step.

The Python Way

Let's see how Python handles this concept. Here's a typical example:

PY
Python Example
name = "Alice"
age = 30
score = 9.5
active = True
MAX = 100  # UPPER_CASE = constant by convention

# Optional type hints
city: str = "Istanbul"

# Types can change (though not recommended)
x = 42
x = "now a string"  # valid in Python

Comparing to Java

Here's how you might have written similar code in Java:

JV
Java (What you know)
String name = "Alice";
int age = 30;
double score = 9.5;
boolean active = true;
final int MAX = 100;

// var (Java 10+)
var city = "Istanbul"; // still String

// Cannot change type
int x = 42;
// x = "string"; // compile error
Mirror Card
JV
From Java:

You may be used to different syntax or behavior.

PY
In Python:

Python infers types dynamically; Java requires explicit type declarations

Mirror Card
JV
From Java:

You may be used to different syntax or behavior.

PY
In Python:

Java final = Python's UPPER_CASE constant convention

Mirror Card
JV
From Java:

You may be used to different syntax or behavior.

PY
In Python:

Python variables can change type; Java types are fixed

Mirror Card
JV
From Java:

You may be used to different syntax or behavior.

PY
In Python:

Python type hints are optional documentation; Java types are mandatory

Step-by-Step Breakdown

1. No Type Declarations

Python doesn't need type declarations. Just assign a value — the interpreter figures out the type at runtime.

JV
Java
String name = "Alice"; int age = 30;
PY
Python
name = "Alice"
age = 30

2. Type Hints

Python type hints (PEP 484) are optional annotations for documentation and static analysis tools, not enforced at runtime.

PY
Python
# Without hints
name = "Alice"
# With hints (mypy/pyright can check these)
name: str = "Alice"

3. Constants by Convention

Python has no final keyword. Constants are just UPPER_CASE variables — the convention signals 'don't change this'.

JV
Java
final int MAX_SIZE = 100;
PY
Python
MAX_SIZE = 100  # convention only

Common Mistakes

When coming from Java, developers often make these mistakes:

  • Python infers types dynamically; Java requires explicit type declarations
  • Java final = Python's UPPER_CASE constant convention
  • Python variables can change type; Java types are fixed
Common Pitfall
Don't assume Python works exactly like Java. While the concepts may be similar, the syntax and behavior can differ significantly.

Key Takeaways

  • No type declarations in Python
  • Java final → UPPER_CASE convention in Python
  • Types are dynamic in Python; static in Java
  • Type hints are optional in Python
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your Java code in Python to practice these concepts.
OverviewNext