Wednesday, May 28, 2025

M2 - GIS Programming - Python Fundamentals

Screenshot of a script I created and ran using IDLE.

Week two of GIS Programming was a crash course introduction to the fundamentals of Python programming. It was a tremendous amount of information to absorb, and I see that I'll be referring back to this chapter of our textbook frequently for some time.

Module 2's lab student outcomes were:

Work with string variables in Python

Use and import modules

Save Python code as a script

Include comments in scripts

Create loops and conditional statements

Iterate variables within loops to control script workflow


We were tasked with working on a prewritten script as well as writing our own script using functions, methods, lists, loops, and more. I enjoyed this module, though I found it somewhat challenging. As mentioned, I'll be utilizing Chapter 4 in our textbook until I master the fundamentals.

The image above is a screenshot of a script I created and ran using IDLE. I found working in IDLE much easier than working directly in ArcGIS Pro Notebooks. However, I appreciate that in Notebooks, you can easily test individual lines of code.

Our script had to successfully perform two main functions. First, we needed to create a loop that adds 20 random numbers between zero and ten to a list, which we assigned to the variable luckyList. This involved initializing an empty list, setting up a counter variable, and using a while loop to append randomly generated numbers until the list reached twenty integers. An if statement was used to check when the count reached twenty, and a break statement was included to exit the loop. Finally, we printed the list after all numbers had been added.

The next part of the assignment required us to create a loop that removes a chosen integer from the previously generated list. We assigned this integer to a variable and used an if-else statement to check if it was present in the list. If the integer was not found, we printed a message indicating this; if it was present, we used the count() method to determine how many times it appeared and printed that information. We then employed a while loop to remove the integer from the list and printed the updated list afterward.

In my initial attempt at writing this code, I completed the expected tasks. However, upon review, I realized I hadn't followed the instructions precisely and had used a different method to achieve the same goal. This was actually exciting, as I was able to rely on my intuition to complete the task, even though it wasn't the instructed method. It perfectly illustrates the principle from “The Zen of Python” that there is often “one obvious way” or a “better” way to achieve a goal. However, I'm still uncertain which of the two approaches (my initial working version versus the assignment's requested method) would truly be considered the better way in terms of general programming practices.


Below is the flow chart I created for this script. I'm anxiously looking forward to what's to come!






No comments:

Post a Comment

M5 - GIS Programming - Explore and Manipulate Data using Python

This week allowed us to dive deeper into ArcPy and explore data manipulation within ArcGIS Pro. We focused on using Python scripts to intera...