Skip to content

Commit

Permalink
Add more chapters
Browse files Browse the repository at this point in the history
  • Loading branch information
giswqs committed Aug 18, 2024
1 parent f69a35d commit ce3924a
Show file tree
Hide file tree
Showing 13 changed files with 825 additions and 6 deletions.
4 changes: 4 additions & 0 deletions _toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ parts:
- file: book/python/02_variables
- file: book/python/03_data_structures
- file: book/python/04_string_operations
- file: book/python/05_looping
- file: book/python/06_functions_classes
- file: book/python/07_files

- caption: GeoPython
numbered: true
maxdepth: 2
Expand Down
2 changes: 1 addition & 1 deletion book/python/02_variables.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@
"id": "29",
"metadata": {},
"source": [
"## Exercise\n",
"## Exercises\n",
"\n",
"1. Create a list of tuples, each representing the coordinates (latitude, longitude) of different cities you have visited.\n",
"2. Calculate the centroid of these coordinates.\n",
Expand Down
2 changes: 1 addition & 1 deletion book/python/02_variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ centroid = [centroid_lat, centroid_lon]
print("Centroid of the points is at:", centroid)
```

## Exercise
## Exercises

1. Create a list of tuples, each representing the coordinates (latitude, longitude) of different cities you have visited.
2. Calculate the centroid of these coordinates.
Expand Down
2 changes: 1 addition & 1 deletion book/python/03_data_structures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@
"id": "23",
"metadata": {},
"source": [
"## Exercise\n",
"## Exercises\n",
"\n",
"Create a dictionary to store attributes of a geographic feature (e.g., a river or mountain). Include keys for the name, length, and location of the feature. Then, add an additional attribute (e.g., the source of the river or the height of the mountain) and print the dictionary."
]
Expand Down
2 changes: 1 addition & 1 deletion book/python/03_data_structures.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ city_attributes["area_km2"] = 2191 # Adding the area of the city in square kilo
print("Updated city attributes:", city_attributes)
```

## Exercise
## Exercises

Create a dictionary to store attributes of a geographic feature (e.g., a river or mountain). Include keys for the name, length, and location of the feature. Then, add an additional attribute (e.g., the source of the river or the height of the mountain) and print the dictionary.

Expand Down
12 changes: 11 additions & 1 deletion book/python/04_string_operations.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,19 @@
]
},
{
"cell_type": "markdown",
"cell_type": "code",
"execution_count": null,
"id": "18",
"metadata": {},
"outputs": [],
"source": [
"# Type your code here"
]
},
{
"cell_type": "markdown",
"id": "19",
"metadata": {},
"source": [
"## Conclusion\n",
"\n",
Expand Down
4 changes: 3 additions & 1 deletion book/python/04_string_operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ print(f"Parsed coordinates: ({latitude}, {longitude})")
3. Create a formatted string that includes the name of a location and its coordinates. Use both the `format()` method and f-strings to achieve this.
4. Replace a substring in the name of a place (e.g., change 'San Francisco' to 'San Diego') and print the result.

+++
```{code-cell}
# Type your code here
```

## Conclusion

Expand Down
155 changes: 155 additions & 0 deletions book/python/05_looping.ipynb
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
}
82 changes: 82 additions & 0 deletions book/python/05_looping.md
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.
Loading

0 comments on commit ce3924a

Please sign in to comment.