Python Foundations: Weeks 1–2

Java: java public class Main { public static void main(String[] args) { System.out.println("Hello, World!"); } }

TrackJava to Python Journey
Current SectionPython Basics
Progress5 of 19

Python Foundations: Weeks 1–2

1. Python Setup and Hello World

Java:

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

Python:

print("Hello, World!")
  • No class or main method needed; scripts run top-to-bottom.
  • Dynamic typing: no need to declare variable types.

2. Variables and Data Types

Java:

int x = 10;
double pi = 3.14;
String name = "Alice";
boolean isActive = true;

Python:

x = 10          # int
pi = 3.14       # float
name = "Alice"  # str
is_active = True # bool (capitalize True/False)
  • Python variables are dynamically typed; type is inferred at runtime.
  • Use snake_case for variable names by convention.

3. Control Flow

If-Else

Java:

if (x > 5) {
    System.out.println("Greater than 5");
} else {
    System.out.println("5 or less");
}

Python:

if x > 5:
    print("Greater than 5")
else:
    print("5 or less")
  • No parentheses needed.
  • Indentation (usually 4 spaces) defines code blocks instead of braces.

Elif (Else if)

Java:

if (score >= 90) {
    grade = "A";
} else if (score >= 80) {
    grade = "B";
} else {
    grade = "C";
}

Python:

if score >= 90:
    grade = "A"
elif score >= 80:
    grade = "B"
else:
    grade = "C"

4. Loops

For Loop (iterating numbers)

Java:

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

Python:

for i in range(5):
    print(i)
  • range(5) generates numbers 0 through 4.

For Loop (over lists)

Java:

int[] nums = {1, 2, 3, 4, 5};
for (int n : nums) {
    System.out.println(n);
}

Python:

nums = [1, 2, 3, 4, 5]
for n in nums:
    print(n)

While Loop

Java:

int count = 0;
while (count < 5) {
    System.out.println(count);
    count++;
}

Python:

count = 0
while count < 5:
    print(count)
    count += 1

5. Data Structures

List vs ArrayList

Java:

ArrayList<String> fruits = new ArrayList<>();
fruits.add("apple");
fruits.add("banana");

Python:

fruits = ["apple", "banana"]
fruits.append("cherry")

Tuple (Immutable sequence) — no direct Java equivalent, similar to final arrays or immutable objects

coordinates = (10, 20)

Dictionary vs HashMap

Java:

HashMap<String, Integer> ages = new HashMap<>();
ages.put("Alice", 25);
ages.put("Bob", 30);

Python:

ages = {"Alice": 25, "Bob": 30}
ages["Charlie"] = 35

6. Functions / Methods

Java:

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

Python:

def add(a, b):
    return a + b
  • No return type declarations.
  • Indent function body.
  • Call with add(3, 4).

7. String Handling

Java:

String message = "Hello";
System.out.println(message.length());
System.out.println(message.substring(0, 3));

Python:

message = "Hello"
print(len(message))
print(message[0:3])  # slicing, includes indices 0,1,2 but excludes 3
  • Strings are immutable in both.
  • Python slicing is powerful and concise.

8. Comments

Java:

// single line comment

/*
 Multi-line
 comment
*/

Python:

# single line comment

"""
Multi-line
comment
"""
  • Triple quotes denote multi-line strings, often used for docstrings or comments.

Sample Python Script Putting It Together

def greet(name):
    if name:
        print(f"Hello, {name}!")
    else:
        print("Hello, World!")

names = ["Alice", "Bob", None]

for n in names:
    greet(n)