-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasics_00.py
More file actions
153 lines (134 loc) · 4.09 KB
/
basics_00.py
File metadata and controls
153 lines (134 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# 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.
# + [markdown]
# ### 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:
# - `numpy` is used for maths (like cosine or exponents)
# - `matplotlib.pyplot` is used for plotting graphs
# - `cartopy.crs` is used to create geospatial map projections
#
# These are imported using the `import` keyword.
# -
# + tags=["empty-cell"]
# Import the required libraries
# import numpy as np
# import matplotlib.pyplot as plt
# import cartopy.crs as ccrs
# -
# + tags=["solution"]
import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
# -
# + [markdown]
# ### 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`.
# -
# + tags=["empty-cell"]
# Define x
# Compute y using a linear expression
# Print the result
# -
# + tags=["solution"]
x = 1
y = 2 * x + 1
print("y =", y)
# -
# + [markdown]
# You can continue calculations using existing variables. Python follows the usual order of operations (e.g., `**` for exponents, `/` for division).
# -
# + tags=["empty-cell"]
# Modify y by adding x squared and dividing by 10
# Print the result
# -
# + tags=["solution"]
y = y + x**2
y = y / 10
print("y =", y)
# -
# + [markdown]
# ### 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.
# -
# + tags=["empty-cell"]
# Format y with 2 decimal places using % formatting
# -
# + tags=["solution"]
print("y = %7.2f" % y)
# -
# + [markdown]
# ### 4. For Loops
#
# A `for` loop repeats a block of code multiple times. The syntax is:
#
# ```python
# 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.
# -
# + tags=["empty-cell"]
# Write a for-loop that:
# - Iterates 5 times
# - Prints i and cos(pi + i * 10)
# - Increments a counter
# After the loop, print the final counter value
# -
# + tags=["solution"]
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)
# -
# + [markdown]
# ### 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.
# -
# + tags=["empty-cell"]
# Create a basic map using Cartopy:
# - Use PlateCarree projection
# - Add coastlines
# - Set a title
# -
# + tags=["solution"]
plt.figure(figsize=(10, 5))
ax = plt.axes(projection=ccrs.PlateCarree())
ax.coastlines()
ax.set_title('Simple World Map with Cartopy')
plt.show()
# -
# + [markdown]
# ### Summary
#
# This notebook introduced:
# - Importing external libraries for maths and plotting
# - Variable assignment and mathematical expressions
# - Output formatting using `%` styles
# - `for` loops 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.