No history yet

Python Basics

A Different Syntax

Coming from Java, the first thing you'll notice about Python is its clean look. Java uses curly braces {} to define blocks of code, like the body of a loop or an if statement. Python throws them out entirely. Instead, it uses indentation.

That's right, the whitespace at the beginning of a line is not just for readability, it's a rule. A block of code is defined by its level of indentation. This might feel strange at first, but it forces code to be written in a clean, consistent style. You start a block with a colon : and then indent the following lines. To end the block, you simply un-indent.

# Python
if temperature > 30:
    print("It's a hot day!")
    print("Drink plenty of water.")

# Java
if (temperature > 30) {
    System.out.println("It's a hot day!");
    System.out.println("Drink plenty of water.");
}

Types on the Fly

Java is statically typed, meaning you must declare a variable's type before you can use it, like int count = 10;. The type is locked in.

Python is dynamically typed. You don't declare types. The interpreter figures it out at runtime. You can create a variable by simply assigning a value to it. The same variable can even hold different types of data at different times, though this is not always a good practice.

# Python - no type declaration needed
x = 10
print(x)

x = "Hello, Python!"
print(x)

# Java - must declare the type
int x = 10;
System.out.println(x);
// x = "Hello, Java!"; // This would cause a compile error

With dynamic typing, your code becomes more concise, but you trade some of the compile-time error checking you get with Java.

Powerful Data Structures

Python comes with a set of powerful, built-in data types that are more flexible than Java's arrays. They are your primary tools for storing collections of data.

Python TypeJava Equivalent (roughly)Key Feature
listArrayListAn ordered, mutable collection. Can hold items of different types.
tuple(none)An ordered, immutable collection. Once created, it cannot be changed.
dictHashMapA collection of key-value pairs. Unordered and mutable.
setHashSetAn unordered collection of unique items.

Creating these is straightforward. For a list, you use square brackets []. For a dictionary, you use curly braces {} with key-value pairs.

# A list of numbers
primes = [2, 3, 5, 7, 11]

# A list with mixed types
mixed_list = [1, "apple", 3.14]

# A dictionary mapping names to ages
ages = {"Alice": 30, "Bob": 25}

# Accessing a value by its key
print(ages["Alice"])  # Outputs: 30

Functions and Modules

In Python, functions are defined with the def keyword. They are also "first-class citizens," which is a fancy way of saying you can treat them like any other variable. You can pass them as arguments to other functions or return them from functions.

For simple, one-line functions, Python offers lambda expressions. These are anonymous functions that you can define on the fly, which is useful for tasks that require a quick, throwaway function.

# A regular function definition
def add(x, y):
    return x + y

# The same function as a lambda expression
add_lambda = lambda x, y: x + y

print(add(5, 3))         # Outputs: 8
print(add_lambda(5, 3))  # Outputs: 8

To organize your code, Python uses modules and packages, which are similar to Java's packages. A module is simply a file containing Python code. You use the import statement to bring that code into your current program.

# Import the entire math module
import math

# Use a function from the module
print(math.sqrt(16)) # Outputs: 4.0

# Import just one specific function
from math import pi

# Use it directly
print(pi) # Outputs: 3.14159...

Let's review these new concepts with some flashcards.

Now, let's test your understanding of these core differences between Python and Java.

Quiz Questions 1/5

How does Python define a block of code, such as the body of an if statement or a loop?

Quiz Questions 2/5

Python is a dynamically typed language, which means you don't need to declare a variable's type before assigning it a value.

These are the fundamental building blocks of Python. Understanding how its syntax, typing, data structures, and functions differ from Java will give you a solid base for writing Python code effectively.