 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 arc pie site package and Then on the next line I set the overwrite output property to true Which is useful when you're writing strips like these where you'll be doing some testing and you'll often want to overwrite the output files Otherwise ArcGIS is going to return an error message saying that it can't overwrite the files So that's the tips to take from this code In line seven and eight I'm creating a couple of string variables that store the paths of the future classes I'm going to work with The city boundaries feature class and the park and ride feature class both coming from the thought 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'm going to be setting that to true or false And so I make a variable to hold the name of that field in line nine And we'll see that variable used to get later Then in line 10, I'm creating a counter that I'm going to use to count the number of cities that have a park and ride And I'm going to start that counter off at zero Line 12 I Set up a tri block and we've seen these used earlier in the course to produce custom error messages in place of the ones that ArcGIS produces which aren't always the most user friendly So within the tri block we're going to try to make two feature layers and We'll make one that has all of the park and rides and the second one that has all the city boundaries and for both of these in lines 14 and 17 The make feature layer tool is used and it has two Parameters that we have to supply Number one the name of the feature class that's going to act as the source of the features for the layer and number two The name that want to use to refer to the feature layer throughout our the rest of our script Remember that a feature layer exists only temporarily in memory while the script is running. It's not something that gets written permanently to disk So for example in line 14, we're creating a feature layer object That goes by the name of park and ride layer Though we could have named it anything we want really So we've got these two feature layers that we can now use to perform selections and in line 24 We're going to do a location selection to select all the cities that contain features from the park and ride layer If we were to do this in ArcGIS Pro, which sometimes it's helpful to do to make sure you understand the process Here I've got the park and rides features as the black dots and the city boundaries features are the polygons filled with orange So in pro I can do the same thing. I can open the select layer by location tool And I can select Make the input feature layer city boundaries Intersect as the relationship and selecting features would be park and ride. I can run that And there you see the selection the city boundaries features that meet that criterion and you know, I could open up the attribute table and That'll show me that 74 out of 108 city boundaries features were selected and then I could do some Some operations just on those selected features, right? so with Python within Python, it's Same kind of thing. I can select the features and then do something to just the selected features so What we're going to do is use an update cursor and that cursor is just going to be working on that narrowed down layer of cities It's important to note that on line 24 You started with the cities layer that how all the cities and after you run line 24 that feature layer Is only going to contain a selected subset of the cities and that's what we're going to be using in the update cursor So this update cursor is not going to update all the records It's just the the ones that are currently selected and it's only going to be able to update the fields that you specify in the second parameter so Where I've put park and ride field here inside parentheses That's a tuple a Python tuple That has just one item in it in this case Representing the field that I want to make updates to if you'll recall I created that variable Up on line nine to store the the name of the the field that I'm interested in So what I have is a cursor that I can use to iterate row by row And I do that using a for loop which starts on line 28 and Then on line 30 I Take that park and ride field and I set it to true now. Why do I say zero here in square brackets? Well, zero is the index position of the field that I passed in in that tuple up here The first in this case the only field in the tuple is that position zero If I was updating three or four fields, I would use that same row variable But then the square brackets I would use one two three Etc. So after I made the changes to the fields that I want to make I need to call the update row method in order for the changes to stick and that's a mistake that a lot of beginners Make they forget to call on that method Line 32 I'm incrementing the counter variable that I created So I just want to add one to that counter and so the plus equals one Syndax you see there is a shortcut for saying take whatever numbers in this variable add one to it and stick the result back into the variable. Okay So after my loop through the cities On line 35 I create a finally block Which is going to run whether or not the above code failed So it cleans up the feature layers that we created so that they're not stuck in the computer's memory I could put that those two statements In an accept block perhaps But I put them in a finally block because I want them to be I want those lines to be executed whether the the try code Succeeds or fails, right? I want to clean up those References to the feature classes So I don't leave locks on them. I also use a Python Dell statement To clear the row and cursor objects out of memory for the same reason I use the delete tool above Make sure you're making sure I don't have any locks left on the feature classes So 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 So line 41. I'm running the get count tool in our jets And you can run this tool directly on a feature class So here I'm not passing it a feature layer. I'm passing in the path to a an actual feature class notice in 41 That I reference the city boundaries variable That's a variable that I created up at the top of the script to store the path to that feature class When you use the get count tool, it's a bit unintuitive because what it returns isn't actually a number It's a result object What you can do with that object isn't very well documented So you can just follow on this example and the ones ones to follow and Use zero and square brackets to get at the count Which is going to be in string form and then use the int function to convert that string into an integer So what we have at the end in line 42 is a variable called num cities That's the total number of cities in the entire data set And we've been keeping a counter of the cities that have a parking ride So really the last step is to in order to find the percentage. We just divide The counter variable by the total variable And that's what's happening on line 45 So then finally we just print out the result With a print statement