Lab 2 [GIS5103]: Python Fundamentals

Output from Module 2 Python script

This week's lab focused on practicing with data types and fundamental constructs in the Python scripting language, including for and while loops, as well as if statements. I examined the differences between functions and methods, and I additionally learned how to work with lists, strings, and numbers in Python. 

The output shown above is for a Python script that performed these tasks:
  • Step 1: Print the last element of a list containing a full name.
  • Step 2: Display the dice rolls of a set of players and their game status (win, lose, or tie).
  • Step 3: Create a list of 20 random numbers in the range 0 to 10.
  • Step 4: Remove a preset number from a list.
A flowchart I sketched as part of Step 4 is shown here:

In this case, I have two variables that I initialize prior to entering the while loop. Within the while loop, I continue to remove the preset number from the list based on the number of times it is present in the list using the remove()  method. I then exit the while loop after the last instance is removed.

A final key take-away from this week's lab related the importance of commenting with my Python code. The # symbol is used in Python to indicate to the interpreter that any text remaining on that line following that symbol can be ignored. This symbol should be used extensively at the top of each script for the providing the overall script header information (e.g., name of script, author, date created, general overview). In addition, major chunks of code should be commented at the top of the chunk (for major steps), and in relevant places for subchunks (i.e., the subtasks of the major steps). I was reminded in this lab that code is read more often than it is written, so commenting my code will help with long-term maintenance of my scripts.

Comments

Popular posts from this blog

Lab 2 [GIS5935]: Data Quality Standards

Lab 5 [GIS5935]: Surface Interpolation

Final Project [GIS6005]: Conservation in the State of Florida: Preserving Key Habitats of the Striped Burrfish