Module 2- Python Fundamentals
This week began to dive into the syntax of common python commands. Since python shell cannot edit multiple lines of script, scripts were edited and run on Python IDLE and ArcGIS Notebook to complete the 4 assigned steps below:
- Print your last name from a created list
- Fix errors within a prewritten code set
- Populate an empty list with 20 random integers
- Remove an 'unlucky' number from the previously created list
A full output results screenshot for each of the final scripts, run using ArcGIS Pro Notebook, is included below:
I found this module to be challenging, as learning scripting commands is like learning a new language. Some of the key methods used in this module were the append, remove, split methods which usually are formatted in the <object>.<method>(<arguments>) format, or the object is listed first, followed by a “.” and the method name and the arguments in parentheses. Also, functions like len() and range() were used, which are in <function>(<object>) format, or the function is first and then followed by the object in parentheses. Other key ideas were conditional statements, importing modules, working with lists, working with strings, adding comments using the # symbol, defining variables, and loop structures, such as the "while" and "for" loops. The difference between a "while" loop and a "for loop is that a "while" loop is meant to used to run a script multiple times while a condition is true and then stop when the condition is no longer true, and a “For” loop is designed for running over a collection or sequence of items. An if/else/elif statement is also used for conditional statements with multiple outcomes. An example from our course textbook, Python Scripting for ArcGIS, written by Paul Zandbergen, is below:
import random
x = random.randint (1,6)
print (x)
if x == 6:
print ("You win!")
The first line brings in, or imports, the random module. Then, the variable x is defined as a random integer between the values of 1 and 6 using the randint method. The random integer is produced using the print (x) command. Additionally, a condition is used to say that, if the random integer is 6, a text string is added to say "You win!
I also learned that there is usually more than one way to complete the same output. Therefore, there is hope that if one way to complete a task is difficult or does not work, there is likely a better or simpler way, as per the "Zen of Python" rules described in module 1.
Comments
Post a Comment