Exercises: Introduction to Python for Scientific Analysis¶
This exercise introduces some core Python programming concepts:
- Importing useful libraries
- Performing basic mathematical operations
- Writing for-loops
- Creating a simple map using Cartopy
By the end of this activity, you should be comfortable with basic syntax and plotting capabilities for spatial data.
1. Importing Python Libraries¶
Python uses libraries (also called packages) to extend its capabilities. Rather than writing every function from scratch, we import code written by others.
In this example:
numpyis used for maths (like cosine or exponents)matplotlib.pyplotis used for plotting graphscartopy.crsis used to create geospatial map projections
These are imported using the import keyword.
import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
Matplotlib is building the font cache; this may take a moment.
2. Basic Mathematical Operations¶
Python can perform mathematical operations like addition, multiplication, and exponents. You can assign values to variables using =, then reuse them in expressions.
Let's define a variable x, and compute a simple linear equation y = 2x + 1.
x = 1
y = 2 * x + 1
print("y =", y)
y = 3
You can continue calculations using existing variables. Python follows the usual order of operations (e.g., ** for exponents, / for division).
y = y + x**2
y = y / 10
print("y =", y)
y = 0.4
3. Formatted Output¶
Python allows you to format numbers when printing them. For example, %7.2f formats a number as:
- 7 characters wide
- 2 digits after the decimal point
This is useful when printing aligned tables or controlling decimal precision in scientific output.
print("y = %7.2f" % y)
y = 0.40
4. For Loops¶
A for loop repeats a block of code multiple times. The syntax is:
for i in range(n):
# do something
range(5) generates numbers from 0 to 4. In each loop, you can perform calculations, print results, or update counters.
counter = 0
for i in range(5):
print("i =", i, "cos(pi + i*10) =", np.cos(np.pi + i * 10.))
counter += 1
print("End of for loop. Counter =", counter)
i = 0 cos(pi + i*10) = -1.0 i = 1 cos(pi + i*10) = 0.8390715290764525 i = 2 cos(pi + i*10) = -0.40808206181339207 i = 3 cos(pi + i*10) = -0.15425144988758743 i = 4 cos(pi + i*10) = 0.6669380616522644 End of for loop. Counter = 5
5. Map Plotting with Cartopy¶
Cartopy is a mapping library that allows you to create geographic visualisations. It supports many map projections — ways to represent the Earth's curved surface on a 2D plot.
A simple starting point is PlateCarree, which uses latitude and longitude directly. Below, we create a map and plot coastlines.
plt.figure(figsize=(10, 5))
ax = plt.axes(projection=ccrs.PlateCarree())
ax.coastlines()
ax.set_title('Simple World Map with Cartopy')
plt.show()
/opt/hostedtoolcache/Python/3.13.7/x64/lib/python3.13/site-packages/cartopy/io/__init__.py:242: DownloadWarning: Downloading: https://naturalearth.s3.amazonaws.com/110m_physical/ne_110m_coastline.zip
warnings.warn(f'Downloading: {url}', DownloadWarning)
Summary¶
This notebook introduced:
- Importing external libraries for maths and plotting
- Variable assignment and mathematical expressions
- Output formatting using
%styles forloops for iteration- Creating a simple map using
cartopy
These concepts are foundational for data analysis and spatial visualisation in Python. In future tutorials, we'll build on them to handle data files, automate workflows, and create more advanced maps.