Lab: Setting Up Your Python Environment

Part 1, Chapter 1: Welcome, ML for Earth and Subsurface Data

Learning objectives

  • Write and run basic Python statements
  • Use variables, data types, and arithmetic
  • Work with strings, lists, and basic data structures
  • Write loops and conditionals
  • Define and call functions

What Is Python?

Python is a programming language, a way to give instructions to a computer. Think of it as learning a new language, except your audience is a machine that follows your instructions exactly. Python is the most popular language for machine learning and data science because it is easy to read, has a huge ecosystem of scientific libraries, and has a gentle learning curve.

When you write Python code, the computer reads it line by line, from top to bottom, and executes each instruction in order.

Your Very First Python Code

The simplest thing you can do in Python is print something to the screen. The print() function displays whatever you put inside the parentheses:

%%%python print("Hello, Geoscience!") print(42) print(3.14) %%%

This code produces three lines of output:

Hello, Geoscience! 42 3.14

Notice: text must be in quotes ("..." or '...'), but numbers do not need quotes.

Variables and Assignment

A variable is a name that stores a value, like a labeled box. You create a variable using the = sign (the assignment operator):

%%%python depth = 1500 # depth in meters (an integer) porosity = 0.23 # porosity as a decimal (a float) rock_name = "granite" # rock type (a string) is_igneous = True # boolean (True or False) print(depth) print(porosity) print(rock_name) print(is_igneous) %%%

Output:

1500 0.23 granite True

Variable Naming Rules

  • Must start with a letter or underscore: depth, _temp ✔, 2nd_layer
  • Can contain letters, numbers, underscores: layer_2
  • Case-sensitive: Depth and depth are different variables
  • Cannot use Python keywords: for, if, class, return, etc.
  • Use descriptive names: porosity is better than p

You can reassign a variable at any time:

%%%python temperature = 25.0 # initial value print(temperature) # prints 25.0 temperature = 30.5 # reassigned! print(temperature) # prints 30.5 %%%

Data Types

Every value in Python has a type. The four basic types you need to know:

TypeNameExamplesGeoscience Context
intInteger42, -7, 0Number of earthquakes, layer count
floatFloating-point3.14, -0.5, 1e6Porosity (0.23), depth (1500.5 m)
strString"granite", 'shale'Rock names, well IDs
boolBooleanTrue, FalseIs the rock igneous? Is the well active?

You can check a value's type using type():

%%%python print(type(42)) # print(type(3.14)) # print(type("granite")) # print(type(True)) # %%%

Arithmetic Operations

Python supports all standard math operations:

OperatorMeaningExampleResult
+Addition1500 + 3001800
-Subtraction2000 - 7501250
*Multiplication3.0 * 500015000.0
/Division (float)500 / 3166.666...
//Floor division500 // 3166
%Modulo (remainder)500 % 32
**Exponentiation2 ** 101024

Geoscience example, computing hydrostatic pressure:

%%%python # Hydrostatic pressure: P = rho * g * h rho = 1025 # seawater density, kg/m^3 g = 9.81 # gravity, m/s^2 h = 3000 # ocean depth, meters pressure = rho * g * h print(pressure) # 30163500.0 Pa, about 30 MPa %%%

Order of Operations

Python follows standard math order (PEMDAS):
Parentheses → Exponents → Multiplication/Division → Addition/Subtraction.
Use parentheses to make your intent clear!

Strings

A string is a sequence of characters enclosed in quotes. You can use single quotes '...' or double quotes "...":

%%%python rock = "basalt" well_id = 'WL-042' # String concatenation (joining) full_name = rock + " from " + well_id print(full_name) # basalt from WL-042 # String length print(len(rock)) # 6 # f-strings (formatted strings) - very useful! depth = 2450.7 print(f"The well reaches {depth} meters") # The well reaches 2450.7 meters # Accessing characters (0-indexed) print(rock[0]) # b print(rock[3]) # a %%%

Important: Indexing Starts at 0

In Python, the first character has index 0, not 1. So "basalt"[0] is "b" and "basalt"[5] is "t".

Lists

A list is an ordered collection of items. Lists are one of the most important data structures in Python:

%%%python # Creating lists depths = [100, 250, 500, 750, 1000] rocks = ["sandstone", "shale", "limestone", "granite"] mixed = [42, "basalt", 3.14, True] # can mix types # Accessing elements (0-indexed) print(depths[0]) # 100 (first element) print(depths[2]) # 500 (third element) print(depths[-1]) # 1000 (last element) # Slicing: list[start:stop] (stop is excluded) print(depths[1:3]) # [250, 500] print(depths[:3]) # [100, 250, 500] (first three) print(depths[2:]) # [500, 750, 1000] (from index 2) # List length print(len(depths)) # 5 # Adding elements depths.append(1500) # add to end print(depths) # [100, 250, 500, 750, 1000, 1500] # Modifying elements depths[0] = 50 print(depths) # [50, 250, 500, 750, 1000, 1500] %%%

For Loops

A for loop repeats a block of code for each item in a sequence. The indented code (4 spaces) is the "body" of the loop:

%%%python # Loop through a list rocks = ["sandstone", "shale", "limestone"] for rock in rocks: print(f"Rock type: {rock}") # Output: # Rock type: sandstone # Rock type: shale # Rock type: limestone %%%

The range() function generates a sequence of numbers:

%%%python # range(5) gives 0, 1, 2, 3, 4 for i in range(5): print(i) # range(start, stop) for i in range(2, 6): print(i) # prints 2, 3, 4, 5 # range(start, stop, step) for i in range(0, 100, 25): print(i) # prints 0, 25, 50, 75 %%%

Geoscience example, computing travel time through multiple layers:

%%%python thicknesses = [200, 350, 500, 150] # meters velocities = [2000, 3500, 4500, 5500] # m/s total_time = 0 for i in range(len(thicknesses)): t = thicknesses[i] / velocities[i] total_time = total_time + t print(f"Layer {i+1}: t = {t:.4f} s") print(f"Total travel time: {total_time:.4f} s") %%%

While Loops

A while loop repeats as long as a condition is True:

%%%python # Count down from 5 count = 5 while count > 0: print(count) count = count - 1 # or count -= 1 print("Blast off!") # Output: 5, 4, 3, 2, 1, Blast off! %%%

Warning: Infinite Loops

If the condition never becomes False, the loop runs forever! Always make sure the loop variable changes in a way that eventually makes the condition False.

Conditionals: if / elif / else

Conditionals let your code make decisions:

%%%python porosity = 0.25 if porosity > 0.30: quality = "excellent" elif porosity > 0.20: quality = "good" elif porosity > 0.10: quality = "fair" else: quality = "poor" print(f"Porosity {porosity} -> {quality}") # Output: Porosity 0.25 -> good %%%

Comparison operators: == (equals), != (not equals), <, >, <=, >=

Logical operators: and, or, not

%%%python temperature = 150 pressure = 50 if temperature > 100 and pressure > 30: print("Conditions suitable for metamorphism") %%%

Functions

A function is a reusable block of code. You define it with def, give it a name, and optionally give it parameters (inputs). The function can return a value:

%%%python # Define a function def hydrostatic_pressure(rho, g, h): """Calculate hydrostatic pressure P = rho * g * h""" pressure = rho * g * h return pressure # Call the function result = hydrostatic_pressure(1025, 9.81, 3000) print(f"Pressure: {result:.0f} Pa") # Output: Pressure: 30163500 Pa %%%

Functions can have default parameter values:

%%%python def travel_time(thickness, velocity=3000): """Compute seismic travel time through a layer""" return thickness / velocity print(travel_time(500)) # uses default v=3000 -> 0.1667 print(travel_time(500, 5000)) # uses v=5000 -> 0.1 %%%

Why Use Functions?

  • Reusability: Write once, use many times.
  • Readability: A well-named function makes code self-documenting.
  • Testing: You can test each function independently.
  • Organization: Break complex problems into smaller pieces.

Importing Modules

Python comes with a huge standard library of pre-built modules. You use import to access them:

%%%python import math # math.sqrt computes square root print(math.sqrt(16)) # 4.0 print(math.pi) # 3.141592653589793 print(math.log(100)) # 4.605... (natural log) print(math.log10(100)) # 2.0 (base-10 log) # You can import specific things from math import sqrt, pi print(sqrt(25)) # 5.0 print(pi) # 3.14159... %%%

Understanding Error Messages

When your code has a mistake, Python shows an error message. Don't panic! Read it carefully, it tells you exactly what went wrong and where:

%%%python # NameError: variable not defined print(depht) # typo! # NameError: name 'depht' is not defined # TypeError: wrong operation for the type result = "depth: " + 500 # TypeError: can only concatenate str to str # Fix: result = "depth: " + str(500) # SyntaxError: code structure is wrong if depth > 100 print("deep") # SyntaxError: expected ':' # Fix: if depth > 100: # IndentationError: wrong spacing for i in range(3): print(i) # IndentationError: expected an indented block # Fix: indent the print line with 4 spaces %%%

Debugging Tips

  1. Read the last line of the error message first, it tells you the type and description of the error.
  2. Look at the line number indicated in the traceback.
  3. Use print() statements to check variable values.
  4. Check for typos, missing colons, and wrong indentation.

References

  • VanderPlas, J. (2016). Python Data Science Handbook, ch. 1 (IPython & Python basics). O’Reilly.
  • McKinney, W. (2017). Python for Data Analysis (2nd ed.), ch. 1–3 (Python language essentials). O’Reilly.
  • Géron, A. (2022). Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow (3rd ed.), appendix A (Python tutorial). O’Reilly.

This page is prerendered for SEO and accessibility. The interactive widgets above hydrate on JavaScript load.