Tuesday, May 28, 2024

GIS 5103 Module 2 - Python Fundamentals

 


Module 2 of GIS Programming was an opportunity to dive deeper into the fundamentals of Python script coding. We were introduced into the different object / data types, syntax, functions, methods, loops, and conditions. While I am no where near a Python expert, I am one hundred times more comfortable typing small scripts in IDLE and ArcGIS Pro. 

The assignment for this module was broken down into four parts, and then compiling those four parts into one Python script; the result of the script can be seen in the image above. 

The first part of the script was creating a string data type that consisted of our entire name, using the split method to divide our full name into its individual names, and to use indexing to print ONLY our last name to the output. This can be seen in the first line of the output [blue lettering]. This part of the assignment was very easy and did not present any issues.

The second part of the assignment was to troubleshoot a block of code that was provided. Essentially, the code is a dice game that takes a list of players, and each player tries to score higher than the number of letters in their name. For example, if a player's name is made up of four letters, they have to score a five to win, a four to tie, and anything lower than four is a loss. The errors found within this code were simple syntax errors that were easy to fix. The first issue was to import the random module, the second was concatenating an integer with a string data type [the solution is to temporarily convert the integer into a string], and the third was a case-sensitive typographical error. Once these errors were corrected, the game was run effortlessly. The results of the game can be viewed on lines two through seventeen of the output [blue lettering].

The third part of this assignment was to generate a list of twenty random numbers that lie within a range of zero to ten. This was accomplished by creating an empty list, generating random integers, and using the append method to add these random integers to the list. Also, a counter variable was defined that counted up by one each time an integer was added to the list. Once the counter equaled twenty, the loop was stopped by executing the break command. The result of this part of the script can be seen in the eighteenth line of the output above.

Finally, we were tasked with creating our own script that chose an 'unlucky' number and deleted it from the list created in step three. A conditional if statement was used to print a statement to the output; if the unlucky number was present, a message appears that states "Unlucky number [x] will be removed [y] number of times from the list." If the unlucky number was not present, a message appears that states "Unlucky number [x] did not appear in the list." Lastly, the remove method was executed to delete all instances of the unlucky number from the randomly generated list. The result can be viewed in the last two lines of the output. 

I am extremely pleased with the outcome of this script, and I look forward to expanding my knowledge base of this programming language.

No comments:

Post a Comment

GIS 5935 Module 2.2 - Surface Interpolation

  Post in progress - please check back soon...