 This video shows one solution to lesson three, practice exercise A, which determines the percentage of cities in the state of Washington that have park and ride facilities. The script begins by importing the ArcPy site package, and then I put this line five to overwrite output equals true, which is useful when you're writing scripts like these where you'll be doing some testing and you'll often want to overwrite the output files. Otherwise ArcGIS will return an error message saying that it cannot overwrite an output that already exists. So that's a tip to take from this code. In lines seven and eight, I have string variables that I'm creating that have the paths to the feature classes I'm going to work with, the city boundaries feature class and the park and ride feature class, both from the Washington file geodatabase. I'm also going to be updating a field in the city boundaries feature class called has park and ride. I'll be setting that to true or false. And so I make a variable for that in line nine, and you'll see that used later. And then in line 10, I'm starting a counter that I'm going to use in my code to count the number of cities with a park and ride. And I start that off at zero. In line 12, I move into a try accept block of code. In this solution, I show some examples of how you might do error handling and return messages. Usually if you leave these out, your script will return nice error messages to you anyways that you can use to figure out what line has crashed. However, if you distribute your script to an end user, they will want to see nicer messages. So the try and accept blocks can help that you can put your own custom messages in like I did in line 20. But for now, we have a try block and we're going to try to make two feature layers. We'll make a feature layer that has all the parking rides and a feature layer that has all of the city boundaries. And for both of these in line 14 and 17, the make feature layer tool has two parameters that we supply. The name of the feature class is going to act as the feature layer. And then the second parameter is a name that we will use to refer to this feature layer throughout this script and only this script. So this is a name that is in the computer's memory. For example, in line 14 for the park and ride layer, we just call it park and ride layer. We can name that part of it anything we want. So we've got these two feature layers that we can now perform selections on. And in line 24, we're going to do a location selection to select all cities that contain features from the park and ride layer. If we were to do this in ArcMap, which sometimes it's helpful to do to see what's going on. Here I have the park and ride symbolized by black dots in city boundaries that are gray lines with orange fill. And in ArcMap, I can do the same thing where I select by location. And essentially we're selecting features from the city boundaries that completely contain the layer of park and ride. So you can see all those elements there. And then this is the selection of cities that do have a park and ride facility. So line 24 is showing how you would do that with code. And once you've got that selection, then you can then do things with it. So for example, in ArcMap, if I wanted to work with these cities, I could open the attribute table. And I see that some are selected. And then I could just work on those selected rows to do something. And so we're going to do that in Python by opening an update cursor. And the cursor is just going to work on that narrowed down layer of cities. So it's important to know that in line 24, you started with the cities layer that had all the cities. And after you run line 24, that layer is only going to contain a selected subset of those cities. And that's what is going to be applied in your update cursor. So this update cursor will not update everything. It will just update the cities that are currently selected. And it will only be able to update the fields that you specify in the second parameter. So where I've put park and ride field and then a comma. That is a Python tuple with one item in it that represents the field I'm going to edit. And that is the has park and ride field. So cast your minds back up here with line number nine. That's where I created that variable that I'm passing in down in line 27. And so what I have is a cursor that can then go row by row. And I do that using a for loop, which is in line 28. And in line 30, I take that has park and ride field and I set it to true. Now, why do I say zero there in square brackets? Zero is the index position of the field that I passed in in that tuple in line 27. So in line 27, I passed in a tuple with just one field, park and ride field. And because it has one thing in it, that's at index position zero. And that's why I say row zero. If I was updating three or four fields, I would have row and then the field index position in a bracket. It could be one, two, three and so on. After I've made those changes, I need to call update row in order for them to be applied. And this is a mistake that a lot of beginners make. So in line 31, I'm calling update row. And in line 32, I'm incrementing the counter that I'm keeping of the number of cities with the park and ride facility found. So I just add one to the counter. The plus equals one is a shortcut for saying take this number and add one. After looping through all those things, in line 35, I start a finally block which would run whether or not the above code failed. And it cleans up those feature layers so they're not stuck in the computer's memory. I could put those in an accept block perhaps, but I wanted to put them in finally because those need to be cleaned up whether or not the script crashes. So that's what's happening in line 36 and 37. Now that I've done all those things, it's time to do some math to figure out the percentage of cities that have a park and ride facility. So in line 40, I'm running the get count tool in ArcGIS. You could run this tool directly on a feature class. So here I don't pass in a feature layer, I just pass in the string path to a feature class. Notice in 40, I reference that city boundaries variable. That's a variable that I created up in line 7 and it just has the path to the data. When you use the get count tool, you have to also call a get output method to get the actual result and convert it to an integer. Now this is a little complex. Don't worry if you don't understand it all, but just follow the examples that are in this code or also the ArcGIS help code for the get count tool. So what we have in the end in line 41 is a variable called numCities that is the total number of cities in the entire data set. And then we've already been keeping this counter of cities that have a park and ride. So to find the percentage, all we need to do is divide one by the other and that's what's going on in line 44. Now there's a little extra things in the equation here. We take 1.0 and multiply it by the number of cities with a park and ride because we want to do some decimal division. If we didn't do that, it would perform integer division by default and we wouldn't get a nice percentage figure. So we take the cities with park and ride divided by the number of cities and then we multiply by 100 to get a nice interpretable percentage figure. And then we can print out the result at the end of the script in line 46.