generated from giswqs/jupyter-book-demo
-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
825 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"id": "91ffda66", | ||
"metadata": {}, | ||
"source": [ | ||
"# Looping and Control Statements\n", | ||
"\n", | ||
"This notebook introduces looping and control statements in Python, focusing on how these concepts can be applied in geospatial programming. Understanding loops and control flow is essential for processing and analyzing geographic data efficiently." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "9213dfda", | ||
"metadata": {}, | ||
"source": [ | ||
"## For Loops\n", | ||
"\n", | ||
"For loops allow you to iterate over a sequence (such as a list, tuple, or string) and execute a block of code for each item in the sequence. This is particularly useful in geospatial programming when you need to process multiple features or coordinates." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "34de8cdc", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"coordinates = [\n", | ||
" (35.6895, 139.6917),\n", | ||
" (34.0522, -118.2437),\n", | ||
" (51.5074, -0.1278),\n", | ||
"] # List of tuples representing coordinates\n", | ||
"\n", | ||
"for lat, lon in coordinates:\n", | ||
" print(f\"Latitude: {lat}, Longitude: {lon}\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "bc327139", | ||
"metadata": {}, | ||
"source": [ | ||
"## While Loops\n", | ||
"\n", | ||
"While loops continue to execute a block of code as long as a specified condition is true. They are useful when the number of iterations is not known beforehand, such as when processing data until a certain condition is met." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "6c1320d7", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"counter = 0\n", | ||
"while counter < len(coordinates):\n", | ||
" lat, lon = coordinates[counter]\n", | ||
" print(f\"Processing coordinate: ({lat}, {lon})\")\n", | ||
" counter += 1" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "112a434e", | ||
"metadata": {}, | ||
"source": [ | ||
"## Control Statements: if, elif, else\n", | ||
"\n", | ||
"Control statements allow you to execute different blocks of code based on certain conditions. In geospatial programming, this is useful for handling different types of data or conditions." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "3c27e773", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"for lat, lon in coordinates:\n", | ||
" if lat > 40:\n", | ||
" print(f\"{lat} is in the Northern Hemisphere\")\n", | ||
" elif lat < -40:\n", | ||
" print(f\"{lat} is in the Southern Hemisphere\")\n", | ||
" else:\n", | ||
" print(f\"{lat} is near the equator\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "2afbaf30", | ||
"metadata": {}, | ||
"source": [ | ||
"## Combining Loops and Control Statements\n", | ||
"\n", | ||
"You can combine loops and control statements to perform more complex operations, such as filtering data or applying conditions during iteration." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "228a8ab4", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"filtered_coordinates = []\n", | ||
"for lat, lon in coordinates:\n", | ||
" if lon > 0:\n", | ||
" filtered_coordinates.append((lat, lon))\n", | ||
"print(f\"Filtered coordinates (only with positive longitude): {filtered_coordinates}\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "6055fce9", | ||
"metadata": {}, | ||
"source": [ | ||
"## Exercises\n", | ||
"\n", | ||
"1. Create a list of cities with their coordinates. Write a for loop to print out only the cities that are in the Northern Hemisphere.\n", | ||
"2. Write a while loop that continues to print the coordinates in a list until a coordinate with a latitude less than 0 is found.\n", | ||
"3. Create a for loop that iterates through a list of coordinates and prints whether each coordinate is in the Eastern or Western Hemisphere based on the longitude.\n", | ||
"4. Combine a for loop and if statements to count how many coordinates in a list are located in the Southern Hemisphere." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "6f41b987", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Type your code here" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "1f86a36d", | ||
"metadata": {}, | ||
"source": [ | ||
"## Conclusion\n", | ||
"\n", | ||
"Loops and control statements are fundamental tools in geospatial programming. They allow you to process and analyze geographic data efficiently by automating repetitive tasks and applying logic based on data conditions. Practice these concepts by applying them to your geospatial datasets and analyses." | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"language_info": { | ||
"name": "python" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
--- | ||
jupytext: | ||
text_representation: | ||
extension: .md | ||
format_name: myst | ||
format_version: 0.13 | ||
jupytext_version: 1.16.2 | ||
--- | ||
|
||
# Looping and Control Statements | ||
|
||
This notebook introduces looping and control statements in Python, focusing on how these concepts can be applied in geospatial programming. Understanding loops and control flow is essential for processing and analyzing geographic data efficiently. | ||
|
||
+++ | ||
|
||
## For Loops | ||
|
||
For loops allow you to iterate over a sequence (such as a list, tuple, or string) and execute a block of code for each item in the sequence. This is particularly useful in geospatial programming when you need to process multiple features or coordinates. | ||
|
||
```{code-cell} | ||
coordinates = [ | ||
(35.6895, 139.6917), | ||
(34.0522, -118.2437), | ||
(51.5074, -0.1278), | ||
] # List of tuples representing coordinates | ||
for lat, lon in coordinates: | ||
print(f"Latitude: {lat}, Longitude: {lon}") | ||
``` | ||
|
||
## While Loops | ||
|
||
While loops continue to execute a block of code as long as a specified condition is true. They are useful when the number of iterations is not known beforehand, such as when processing data until a certain condition is met. | ||
|
||
```{code-cell} | ||
counter = 0 | ||
while counter < len(coordinates): | ||
lat, lon = coordinates[counter] | ||
print(f"Processing coordinate: ({lat}, {lon})") | ||
counter += 1 | ||
``` | ||
|
||
## Control Statements: if, elif, else | ||
|
||
Control statements allow you to execute different blocks of code based on certain conditions. In geospatial programming, this is useful for handling different types of data or conditions. | ||
|
||
```{code-cell} | ||
for lat, lon in coordinates: | ||
if lat > 40: | ||
print(f"{lat} is in the Northern Hemisphere") | ||
elif lat < -40: | ||
print(f"{lat} is in the Southern Hemisphere") | ||
else: | ||
print(f"{lat} is near the equator") | ||
``` | ||
|
||
## Combining Loops and Control Statements | ||
|
||
You can combine loops and control statements to perform more complex operations, such as filtering data or applying conditions during iteration. | ||
|
||
```{code-cell} | ||
filtered_coordinates = [] | ||
for lat, lon in coordinates: | ||
if lon > 0: | ||
filtered_coordinates.append((lat, lon)) | ||
print(f"Filtered coordinates (only with positive longitude): {filtered_coordinates}") | ||
``` | ||
|
||
## Exercises | ||
|
||
1. Create a list of cities with their coordinates. Write a for loop to print out only the cities that are in the Northern Hemisphere. | ||
2. Write a while loop that continues to print the coordinates in a list until a coordinate with a latitude less than 0 is found. | ||
3. Create a for loop that iterates through a list of coordinates and prints whether each coordinate is in the Eastern or Western Hemisphere based on the longitude. | ||
4. Combine a for loop and if statements to count how many coordinates in a list are located in the Southern Hemisphere. | ||
|
||
```{code-cell} | ||
# Type your code here | ||
``` | ||
|
||
## Conclusion | ||
|
||
Loops and control statements are fundamental tools in geospatial programming. They allow you to process and analyze geographic data efficiently by automating repetitive tasks and applying logic based on data conditions. Practice these concepts by applying them to your geospatial datasets and analyses. |
Oops, something went wrong.