Introduction to Python Programming in GIS


Hello Python Programmers! 

I am very excited to share today my first python script in my Graduate GIS Program. 




Today we are going to be talking about the fundamentals of Python scripting and developing a basic dice game, as well as the ability to sort through random numbers in a list and remove a number from said list. 


Before We Begin...

I want to talk about some things in order to explain the fundamentals of Python coding. 

Look up these concepts below as they are crucial to understanding GIS Python development!

  • Methods
  • Functions
  • Python Syntax
  • Data Types such as arrays, Dictionaries, lists, Tuples, etc...
  • Loops 
  • Boolean Logic
  • Debugging
  • Git and Github
  • Version Control
  • Importing Modules such as Random or Arcpy

Here are some great documentation links below as well for learning syntax, debugging, and structuring of Python and the python API for Arc GIS Pro. 

https://docs.python.org/3/

https://pro.arcgis.com/en/pro-app/latest/arcpy/main/arcgis-pro-arcpy-reference.htm

Now I'll take you through the python script...




Step 1 is a quick run through on the split method and printing string values while indexing certain items in a list. Step's 2 through 4 are more in depth, and are essentially a dice game and a number removal at random sections. These include loops, Booleans, methods, functions, and some cool Python syntax. 

Step 1 - I created a value stringName equal to a string. I then used the split method and set the split to a " " space which separates the three words in my value with single apostrophes and commas in a list. I then printed the index of my name (2) which counts starting at 0 to 2 of the words in my list. So it will only print out the last name in my list, which is Berger. 





My last name is Berger, so index 2 of nameList prints "Berger" because index 2 is the third string in the list. Index counts always start at 0, not 1 which is a common mistake. 




Step 2 - I imported a random module for the code below. I was given an array of players, and a for loop to roll a dice game to see which player would win. The Boolean below in the for loop explains that if a number selected  (X) is greater than the value of a player's number the player looses. If the player has a higher number than the X number, the player wins, If they are equal, it is a tie! This step needed a few debugging tools in order for it to work. 

I imported the random module, in order for the random.randint method to be used on the variable dice for roiling random numbers. 

Also, I had to use the str() function because dice and the X value were not strings, they were integers, and in order to concatenate these values I needed to convert them to string values using the str() and a space with a plus sign. 








Step 3 - I created an empty array called randomNumbers. I then established that my counter for the numbers is equal to 0. With a while loop, I can add random numbers to my empty array ranging from 0 to 10. These numbers will not exceed the value of 20 since I used the len() function to set a maximum integer value. I needed a break clause for this while loop so it would not keep running indefinitely. 








Step 4 - I assigned a variable as my random number, then established a count variable for my random number that was selected. Then, I created a Boolean statement to see if my number is in the list or not in the list. Then I established a while loop, which counts the selected number and removes it from my list of numbers. 








Step 5 - 
I just needed to fill out the metadata at the top of the script...




Here are some questions i wanted to go into detail with as well as some tips!

   What is the index number for the last character in a string or last item in a list?

The index number is the item number in a list. A list uses numbers starting at 0 to identify objects in the list. So if there are 4 strings of names in a list, the index of that list would be 0, 1, 2, 3. So if I wanted to print a index of a list, It would only print that indexed object in the list.

 

names = (“Bob”, “Jack”, “Lily”). Print(names[1]) would give me thee indexed object in the list, which if we count from 0 is “Jack”. 


   What is the difference between a for and while loop structure?

For loops a structured as a “for each” loop which are continuous loops that cycle through a list or range of values, but they are extremely like while loops. For loops are great for iterating through an array or when you know you want the loop to execute a certain number of times. A while loop is great for reading a file into a variable that you have already set in your script, or when you are using an increment value in your loop and the increment is not a standard integer. But both loops execute through a iteration of values. 

Loops can go on forever, or can they?


Use the # key to make comments in your Python code

When typing strings (text) use double quotes!

Functions follow the <function>(<arguments>) structure

Methods follow the <object>.<method>(<arguments>) structure




Don't forget to subscribe to my blog!







Comments

  1. Thanks for sharing the introduction of Python Programming. If you are looking for the Mobile App Development Company In India, then you can go with Lucid Outsourcing Solutions. They have team of expertise and are ready to perform various tasks in your team.

    ReplyDelete

Post a Comment

Popular posts from this blog

TIN's vs. DEM's: 3D Elevation Models In Arc GIS Pro

Bobwhite-Manatee Project: Intro to GIS 11418 GIS5050 Final Project

Isarithmic Mapping: Annual Rainfall in Washington