Python for AI Development
Python Basics
Python for Java Developers
Coming from Java, Python will feel both familiar and strange. The core ideas of programming are the same, but the syntax is cleaner and less demanding. Let's start with the most obvious difference: Python doesn't use curly braces or semicolons. Instead, it uses indentation to define blocks of code.
Whitespace is meaningful in Python. Where you might use
{}in Java to define the body of a loop or function, Python uses a consistent level of indentation.
This might seem odd at first, but it enforces a clean, readable style across all Python code. You can't write messy, inconsistently indented code because it simply won't run.
Dynamic vs. Static Typing
Java is statically typed, which means you must declare a variable's type before you can use it. The type is then fixed for the life of that variable.
// Java code
String message = "Hello";
int number = 100;
// message = 100; // This would cause a compile error
Python is dynamically typed. You don't declare the type of a variable. A variable is just a name pointing to an object, and that name can point to a different type of object later.
# Python code
message = "Hello" # message is a string
print(message)
message = 100 # Now it's an integer
print(message)
This flexibility is a core feature of Python. The type is associated with the value, not the variable name.
Core Data Structures
Python has several built-in data types that are powerful and easy to use. For a Java developer, they map to familiar concepts.
| Python Type | Java Equivalent (Roughly) | Key Characteristic |
|---|---|---|
list | ArrayList | Ordered, mutable (changeable) collection. |
tuple | N/A (like an immutable array) | Ordered, immutable (unchangeable) collection. |
dict | HashMap | Unordered collection of key-value pairs. |
Here’s how they look in practice.
# A list of numbers
my_list = [1, 2, 3, 5]
my_list[3] = 4 # Lists are mutable, so this is fine
print(my_list) # Output: [1, 2, 3, 4]
# A tuple of coordinates
my_tuple = (10, 20)
# my_tuple[0] = 5 # This would cause an error
# A dictionary for a user profile
my_dict = {"name": "Alice", "id": 123}
print(my_dict["name"]) # Output: Alice
Control, Functions, and Modules
Control structures in Python are straightforward. Notice the use of colons : to start a block and the indentation that follows.
# Conditionals
score = 85
if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B")
else:
print("Grade: C")
# For loop
colors = ["red", "green", "blue"]
for color in colors:
print(f"The color is {color}")
The for loop is especially powerful. It works on any sequence, like a list, a tuple, or a string. This is much cleaner than a traditional C-style for loop with an index variable.
Defining functions is also simple, using the def keyword.
# A simple function definition
def greet(name):
return f"Hello, {name}!"
message = greet("World")
print(message)
Finally, Python organizes code into modules. A module is just a file containing Python definitions and statements. You use the import statement to bring code from one module into another, similar to how import works in Java.
# Import the entire math module
import math
print(math.sqrt(16)) # Output: 4.0
# Import just one function from a module
from random import randint
print(randint(1, 10)) # Output: a random integer between 1 and 10
With these basics, you can understand and write simple Python scripts. The transition from Java's explicit structure to Python's clean syntax is often a pleasant surprise.
How does Python define a block of code, such as the body of a loop or function?
In Python, a variable is just a name that points to an object. The type is associated with the object (the value), not the variable name itself.