 All right, let's get started. You folks may need to get back to work You know on a rigid time scale, so Let's not wait around anymore. So I'm Jim DeWiler your instructor. I think this is the first time we've held one of these lives. So I Just wanted to go over some of the some points in lesson two that I think are especially important and Could benefit from you know, seeing me walk through something I'm gonna do a little bit of slides talking through some slides, but before I do that. I wanted to do something a little more Perhaps interesting interactive at any point in This session, please feel free to speak up I'm gonna I'm gonna ask you to speak up at times it asked for volunteers with answers to questions But please don't hesitate to speak up if you want to interrupt me Don't understand something that we we talk about so one of the things that is important to understand in working with Python is how to deal with strings How to get subsets of strings? How to use some of the functions that are available when working with strings and so I just want to work here in the interactive window in Python win To you know demonstrate this string manipulation So I'm gonna create a new variable called x and I'm gonna stick string in it sign a string to it and Something really simple if I wanted to get a specific character out of that larger string. I Can inside square brackets after the string? plug-in Single-digit as I just did there or as we'll see I can plug in Followed by a colon followed by another digit to get Substring so, you know, one of the things to Get used to or keep in mind when dealing with strings is that they are indexed starting with zero as the first character And so if I do print x zero in square brackets, I'll get back the first character in this case a capital P And You know as you would expect them if I did this I'll get the second character I do something like this Anybody know what I'll be what I should You can speak up or you can type it into the chat box Thank you. It says a job. I think you would get the From N. Do you that character number six? So looking reading across That span that range You would start at character three, which is the second and actually in pen Now that the kind of tricky part about this is that, you know, lots of programming languages Provide the means to do this sort of thing and they operate in different ways in some languages You might expect to start at character three and then give me the next six next six characters That's not how Python string slicing works and You also might expect or you might also expect to start a character three and go up to character six And that's also not really how Python works how it works is actually It's going to start a character three and go up to but not include character six So in other words, it'll give me characters three four and five So In space and then that's that's something to get used to How about this? I would not just give you Some characters six all the way through the end Or eight Well, it'll it'll start a character The sixth character, which is at And give you the rest of this Leaving off a second digit by leaving off that second digit you're saying that you want everything beyond Characters seven good Now how about this one I guess that I would go from Last so the Third character in from the right and then to the beginning Yeah, I think I think you're right there and that also says in the chat box That'll it'll spit out pen and that's right. It's gonna by leaving off the first digit saying that you want to start at the beginning of And the second digit after the digit after the colon says Give me everything up to but not including character for so character at position X negative two says pen space STA and the other guesses Yeah, so it's gonna start from the end of the string and When you supply just the single digit you're gonna get back just a single character and Well, if you supply a single digit without the colon I should say As we did here, then you're just gonna get back one character. So we're gonna get starting from the end of the string two characters from the end which is E and So does that mean when you're counting from the end it doesn't have the zero index? Yeah, that's a good question Yeah, I guess that's I guess that's how you would say that is You don't start Zero if you're counting from the end and then how about this one that is you're right now that space STI this one says Because the bones involved and there's nothing before the colon you're saying you want to start at the beginning of the string Yeah, the this can get a little tricky to remember the rules on how these work, but Honestly, I don't What I'm writing Python scripts. I don't really remember all the rules and I have to look them up, but that's okay that's that's one one of the things that distinguishes good Programmers is that they're able to look up in the documentation or they're able to look up on Question-and-answer forums online the answers to the questions that they run into So strings have a number of Methods strings are treated as objects in Python and objects have properties and methods methods being Actions that the object is capable of performing knows how to perform and one of the methods that strings have Is the index method? index method Type in a string dot you'll get a list of its properties and methods index Allows you to find the position of a smaller string within a larger string So in this case if I wanted to find the position of the space in the x variable A space just like I did and it'll return to me for That that's something that can come in handy Another one of these methods is split which can be used as the name applies to split a larger string into smaller string and This is something that Used if you had a comma delimited string Or as I'm doing here basing it found in the string and Now this method is going to return to me list Smaller strings that are created by splitting I'm going to want to do something with that list. I'm doing here is storing that list in a variable called pieces and then I could do work with those Items in the list. So here. I'm just going to print the item app position zero in the list And then as you'd expect print One will give me state another method For strings is the ends with this The ends with method Returns a Boolean a true or false value Depending on whether the string that you It's found at the end of the Variable that's your here the X variable holds Penn State and yes that ends in So I would expect to get back true This then as I'd expect Okay, so that's that's another one of those methods that and then the last one I'll go over is a method called replace this takes two arguments the String that I want to replace help box the old string and a new string Optionally, I could also supply a count. I'm gonna leave that off replace all occurrences of Penn with Pennsylvania That leaves the X variable The Y variable now holds so these are just some of the methods that are available Over how to get a substring out of a larger string as well so Hopefully this will help you on the project to assignment expected to do some string manipulation in that assignment, so Please, you know definitely practice and the interactive window is a good place to do that sort of practicing any questions about Manipulating strings, okay So now I want to go through some slides and I've got some review questions talking about Things that you should remember from lesson one. Okay. Sorry. Are you folks able to see my? Slide that says review where you're still seeing we were but now it's down low. Yeah, okay. I had it up for a second Try again. Okay, you should see it now So when you put a print statement in a Python script, where does that output go? Where were we just working in the Python and Interactive window interactive window, right? I'm having all kinds of trouble with our Point here true or false Variables X lowercase and X uppercase used interchangeably in a Python script. No, that's as false. She'll says false. Yeah, right That's correct Python is an example of a case sensitive language. So those are two separate that's not always the case in other languages, but True or false Python was developed as a scripting language for ArcGIS that says false. She'll says false, right? Yeah, if you learn nothing else from this class, hopefully you'll remember this Python was developed completely independently of ArcGIS and Chosen by Esri as a language to use for geoprocessing scripting. Okay. What's the result of this script? Six, but what about this version? Where Y and X are Stored and he says three three and he's right In this case because X and Y are strings when you do X plus Y the plus Operator takes on a different It operates differently Between two strings you're concatenating or merging the strings together Whereas when you put it between two numbers you're saying that you so that's something that's important to Understand about programming in Python All right, how do you add a comment to a Python script? Yeah that pound sign Yeah, so you you stick the pound sign in your your line of code somewhere now an actual line of code and And the idea is that everything to the right of the pound sign is going to be ignored when your code is executed okay, so and You may have scripts where you do both where you have it at the beginning of the statement or at the end of certain lines And both ways is fine What keywords are used to specify what part of the script I'm to execute and which part should be executed if an error I know what I'm talking about there Right, right yep You would use try and then indent that you would like to try and then Later on after you've got all your code you would have an except at the same indentation level as the try and And then indented underneath that you'll have The code that you would like to execute in the event that your your try code Fails generates an error using try and accept is a good good way to Make sure that your script fails gracefully in other words Error message Now when you're developing the code yourself you may want to avoid using try and accept you may want to see the ugly Ugly error message may point you to where the problem is in your code But then when you've got it working and you think it's it's in good shape Then you should and you're ready to expose it to users then having try and accept is a really good idea Make sure a false the code within a try block and accept block must be indented for spaces True Joe says true. Yeah, that's true So let's talk a little bit about lists I think some of what is in the slides that I have here was already talked about in the first Video session the one that I gave you an old recording So I won't I won't spend a whole lot of time on this but So list is a set of related items Unit in your code It could be numbers it could be strings objects or it could be a mixture of and The items are going to be separated by columns and enclose them brackets It would be we have layers variable and we're assigning to that This list of strings counties and I can retrieve an item from the list using its index position Like we saw with strings the characters within strings the indexing is zero based So I do using that there's variable from the previous bullet point if I do like print layers and then to in square brackets that's going to Counties and One of the things we can do with lists that's useful as we could sort them using the list Sort method so using that same layers list. I could do layers that sort And if I wanted it in descending order Now Oftentimes it's Developers we need to modify lists on the fly so here I've got two list variables list one and list two and One of the things that I can do is I can merge them together and here again, we're using the plus operator and again, it's Doing something different Context that it's being used in when the plus operator is used in between two lists Then it merges those two lists together And so I'm taking that merged list and sticking it sticking it in a third variable This one called list three and you can see that it puts together the two lists or if I want to add an item to the end of the list I Can use a method called So here I've got that that same layers list that I had a couple slides ago And I can add a new layer called streams to the end of that list Or if I want to stick an item into the list not at the end, but it adds Use the insert method It's List variable dot insert and then there's two arguments that I have to supply Before I supply the actual By the position that I want to put it in so if I say then it's going to stick it make it the New item at position one in the list so it replaces it takes counties and roads and shifts them and Plugs in streams into position one okay, now one of the things that you can do with a list is you can get its length and You do that using the Len function Built into Python, so here. I've got a list stored in a variable called children so I could say Len parentheses children to Get back the length So let me ask you a question. How would I output the last item? Let's assume that I I don't know the length of the list so Jill says I could say print children and then in square brackets negative one and Yeah, I think that's I think that's true. I Was looking for something that involves the Len function, but I think you're right about that That's another way so what I was looking for Len would be something like this where I could use the Len function. Yes, Henry has a had had a Print children and then Len children inside the square brackets The only trouble with that is Len children in this case is going to reach or which is going to be one One more than the last item So what you actually have to do is take that Len and subtract one Which I show here, but you know as Jill said an easier way is to using the indexing I Could supply a negative number instead of a positive number to start from the end of the list so another another Programming construct. It's really important to work in any language really and loops are used to repeat certain lines of code There's two types in Python the for loop Which is used to iterate through a list or to to cycle through the items in a list and Then the other is a while loop and we use while loops to Loop until some condition. We'll start with for loops as I said a for loop in Used to iterate through That list could be defined before the for statement Here's an example of that I've got this same children list from the previous slide and I could set up a loop See there The key elements of this are that you start with the word for Skip ahead. You've got the word in After in is going to be your list In this case, I'm pulling the list out of a variable In between for and in is going to be a variable that I come up with whose name I come up with right there That I would like to store an individual item from that list on each pass through the loop so as The code inside the loop gets executed on each pass through their Child here in this case is going to take on a different value. So the first iteration through the loop it will take on The first value in the list in this case stand or print it out goes back to the top of the loop Goes and gets the next item in the list Kyle prints that out etc. Etc Once it's reached the end of the list and it goes back to the top again It sees that there is no more items in the list and it would jump down to the next Line after the loop which we're not seeing here in this in this short little example We can also generate a list within the for statement using the range function Here's an example of that This is the kind of loop that you may be familiar with from other programming the way the range function works is it actually returns a list of numbers and So this returns a list starting with one Going up to ten so this is kind of similar to the way we were seeing getting substrings Slicing a string where it gives you up to but not including the second number So as you see here, it would print one two three all the way down to ten a little bit more about the range method Typically, you're gonna supply two arguments like I showed in the slide Reiterating that you start at one and you stop just before getting to in this case But you can also use it with one argument So if I just applied 11 It would assume a starting point of zero So my list would be a little bit different than the previous example. It would start with zero instead of one Or I can supply three arguments. So here's an example that You might use in a situation where you're dealing with census data US census data Where you've got data for every Every 10 years 1980-1990-2000 etc. So this says to start with 1980 and To go up to but not including 2030 and instead of going Instead of incrementing by one on each pass through loop increment by 10 In other words, the step value is 10 So that would produce this list that you see here, right, so that's that's for loops while loops They said the code is executed until some conditions met so here's an example of that I'm gonna set this variable n equal to zero initialize it to zero and Then I say while n is less than 10 Do something in this case just break and then an important thing here is that n is is changing changed This syntax here might look a little odd to you if you haven't Programmed in Python or other languages use the same syntax Basically, it says take whatever n was before and add one to it and Stick that back into the n variable So if n was zero and would become one we'd go back to the loop Condition statement it would see yes n is still less than 10. So I will Do the code inside again and that would continue until n got up to 10 and at that point n would no longer be less than 10 and It would then jump out of the loop and go down to the first line after the loop now one of the things that You can do with loops that's that can be really useful is you can nest them Here's an example of that. So let's say I've got I've got data for three different states and I've got the same layers In each of those states, I've got cities in Pennsylvania cities in New York cities in New Jersey Roads and I want to do something some sort of processing on all those layers Well, I could have a loop that iterates through all the items in my layers list Then I could immediately follow that up with a nested loop inside that iterates through all the states in my states list and You know notice here. I don't think I mentioned it, but one of the naming conventions that's smart to follow is to if you've got a set of items in a list Name that variable something plural and then when you have a loop where you're iterating through those items Assign the Assign a name to the iterator variable as it's sometimes called a Singular version of that same word. Okay, so layer in layers state in states That just makes the code You know, it makes it easier to interpret than if you said X in layers or something like that so Inside the these nested loops what I'm doing is I'm saying Okay, I want to store in this variable Layer whatever layer happens to be I want to concatenate That with an underscore followed by state. So on the first pass through these loops layer will be cities State will start out as PA. So I would end up with X being cities underscore PA and Then I have this print statement that prints out processing layer cities underscore PA It would go back to the top of the states loop State would become NY So then X would be cities underscore NY would print that out then it would be cities underscore NJ It would get go back to the top of the loop. It would see that there's no more states in the states list So that would take it back to the top of the layers list at that point layer would become counties Then it would start up the states Loop again State would be would start out as PA So I'd have counties underscore PA Etc etc etc Okay, so that's that's an efficient way to deal with a situation like that This is the output that I would expect Cities then all the counties and then all the roads. Here's another example Kind of returning to the idea of having census data. Let's say I've got It at different geographies at the county level at the tracks level at the block so I could Have those different geographic levels stored in a list that I could iterate through that list and Then I could use the range function as we saw Generate a list of years 1990 2000 2010 and you know, I could have a nested loop similar to the previous slide and You know one thing that's a little bit different than the previous slide aside from the range used the range function I've got this little bit going on here. Anybody want to Explain what's going on here? with the str Year negative two Why didn't I just have? Why are as the thing that I'm concatenating with a number so you have to specify that it's a string Good. Yeah, exactly. So yeah, the range function Returns a list of numbers and if I want to Merge that number concatenate that number with other text I Need it to be in the form of a string and so I use the string function to cast that number or convert that number into a Into a string and it just does that on the fly. It doesn't change what's held in the year variable But then also the other thing that's going on here is I'm using that substring notation My layer is actually Named using a convention like counties underscore 90 or counties underscore 00 the year as a string Give me everything everything beyond that so it would give me 90 or zero zero or 10 right now ArcPy has a lot of functions that return lists and And we can use a for loop to iterate through those Here's an example of that. Let's say I've got a shapefile called us boundaries or the path to that shapefile in a variable and Then I can use a use a method called list fields access through the ArcPy module and Then specify the feature class that I want to list the fields in That returns to me a list Here I'm storing that list in the variable and Then as we've seen I can have a for loop that goes through that list Again, I'm doing that plural singular naming and Then so so the field each field Returned by the list fields Method is an object that has properties and methods and one of the properties is its name And other properties it's type, you know, whether it stores Geometry or double precision numbers or strings So, you know in this little example here, I'm simply printing the name of the field Concatenated with a dash Concatenated with the type of field I'll just ask at this point. Did anybody have any trouble or questions about these exercises? So Here's an example where we loop and concatenate Your processing context so Start out importing ArcPy I'm defining a variable called in folder that stores the path to some shapefiles folder that I'm going to store some results in Really what this Example does is it clips all of the Shapefiles in my input folder using a Shapefile that stores the boundary of the state of Nebraska. Okay, so it's it's let's say it's a bunch of national level data Different shapefiles and I want to clip them to the state of Nebraska's boundary And stick them in a separate folder so To do this I'm making use of a method called list feature classes and the list feature classes method Very importantly, it's going to list the feature classes in a particular place Instead of telling it a folder supplying a folder as an argument to the method in parentheses What it does instead is it lists the feature classes in whatever the current workspace is and so an important part of using list feature classes is to first set the workspace and so You get to the workspace property through the ENV or environment module So that's what this line is doing here arc pi dot m dot workspace equals in folder Sets the workspace and then I can list the feature classes in that workspace And then as we've seen we can loop through the items in that list using a for loop and Basically, what I do here is I get the name of the feature class And I merge that together with the results folder where I want to put the output Store that concatenation in a variable called output path and Then I make use of the clip tool in the analysis toolbox It wants to know the name of the input feature class what feature to use to do the clipping and What you want to call the output So that's that's a relatively simple example putting together Both string concatenation and looping Two of the topics we've talked about. I've already talked about string manipulation at the very beginning. So I'll just Think we talked about casting variables in the first now tuples You can also be pronounced tuples, I believe But tuples are another data structure in Python very similar to lists Syntax is going to be same as as a list except replaced the square brackets with a set of parentheses Difference between tuples and lists are immutable, which is just a ten dollar word for Meaning that you can't change the items list. So in other words Here's an example of a list which I know I know it's a list because brackets I can change the With a different string as we see there I Try to do the same thing with a couple the tuple because I'm using parentheses instead of If I try that I'll get an error telling me that Which is kind of a really formal way of saying that you can't change items in a couple so If you have a situation where you have a set of items That you would like to do something with in your in your program It might make that set of items in any way I'm not gonna talk about dictionaries. We'll get to dictionaries later later in the class and I think we already talked about if If L if else conditional statements Comparison operators, I think we're already already discussed what I really wanted to talk about was and demonstrate was so Here we've got a little tiny little script Where I'm defining a variable called a year and I'm assigning it a value of 2010 Loop that says while year is greater than 1970 You know do some print statement and then I say There is a problem with the script Does anybody see it the condition associated with the while loop? Will it ever be met? I should say will it ever be false? So In 70 1970 it start out as to starts out as 2010 and on each pass through the loop. It's having 10 added to it so it'll become 20 20 20 30 So that's an example of an infinite loop Maybe some of you have run into one of these before either in Python or some other language The debugging in Python when there is a toolbar called the debugging toolbar and the reason that I'm talking through this Is that I think it's really important that you folks get in the habit You know unless you can write programs that work right start without You're gonna need to debug your code and going through it one line of time You know if you just hit the run button Try to run the whole thing all at once You know you'll get an error message which may or may not point you to the right to the offending line and to the problem line Whereas if you step through it one line at a time you really will Be able to see what line causes a crash in your code and then you can focus on that line and try to fix it The other thing you can do in a debugging environment is you can set break points Let's say you've got you know Thousand line script you know that the first 900 lines are pretty good. Well, you're not going to want to step through 900 lines of codes to get to the place where you know the problem is and that's where setting a break point towards the end of the script You can run up to the break point and then start stepping You can do in debugging environments is you can watch variables in other words you See what the value held in a variable is and watch how it changes as you There is as we'll see in a second when I get into a demo there is a step over button Which I typically use to step through my code and instead of clicking on that button on the GUI it has a Which is the F10 key so Let's let's Do a little demo and this is going to be the last thing that I do in the live session and at that point I'm finished. I'll open up here I've got a little script and First let me turn off the debugging toolbar because it's not on by default This is what you see when you when you run Python win for the first time after installing it So you have to go to the toolbars menu the view menu then toolbars to turn it on and These are the step buttons and I'll just start with the first one and When I click on it, I'm going to turn this off for now When I click on it, you'll notice that couple things the first you'll see a yellow triangle pointing at The first line in the script And that yellow triangle tells me that I'm in debugging mode I'm executing the script, but it's paused waiting for me to do something You'll also see up in the title bar instead of just saying Python win. It now says Python win dash break Which is just telling me again that it's paused waiting But hit step again, and you'll notice that it opens up another window and this is actually a Initialization script associated with ArcPy you can see the copyright Esri and If I would if I were going to continue hitting step I would be stepping through Esri's You know basically their ArcPy code, and I don't really want to do that What I'm interested in is stepping through my code and so I'm going to get out of here Anytime you're in debugging mode and you want to stop executing the code you can click on this close button And I'm gonna close this Esri module that I'm not really interested in So that's that's one of the step buttons It's going to step through all of the code that is going to be executed as part of your script So you're executing not just the code that you write in your script file But if you're calling on other modules as we are here importing your ArcPy module That that means executing other module code too. So I that's why I don't like the step button in most cases There's also a step out Which enables you I think if I remember correctly if you're in a situation like I was just in where I was in inside the Esri module that I didn't care about I could get out of it using the step out button. I think but the one that I use is this step over and so I'll click on it and then again and The first time you import ArcPy as you may have noticed it takes a while And that's because ArcPy is a fairly large module and it takes it takes a few seconds to load it into memory Then if you run the script again, it's very quick because ArcPy is already loaded into memory But eventually as you see there it the yellow triangle jumps down to the next line telling me that that's The next line that it's about to execute if I continue And so you'll notice that it didn't open up a separate module a separate Esri module it just kept me here in my own module, which is why I want and So I'll hit step over again Setting the workspace also takes a few seconds, but eventually it will Finish and move on to the next line and I can continue hitting step and Notice down in the interactive window it printed Zero because of this loop. I'm saying that I wanted to loop Going from zero to 99 using that range method there. I didn't mean to hit that one And I can continue like that again as I said I like to use F10 instead of clicking on the button. So Now I'm just clicking Okay, so this would continue All the way down to 99 and let's say I want to check out what's happening down here as I mentioned I can Had a break point so to do that. I just place the cursor on whatever line I want to This little hand icon as it says there that toggles a break So when a break point is on I'll see a little move that I would just hit the button again Okay, so if I want to go from some place above the break point and then you know execute down to it I would click on the go button here and you'll see that it did the rest of the loop hitting the print statement on each pass through the loop and then at this point I could use F10 which You know takes me through this list feature classes line Generating a list of the feature classes in this workspace And then I've got a loop that that goes through The items in that feature class now one of the things that I wanted to show you as well was Watch variable that on if it's not on for you. You can click on the the eyeglasses icon. I've got some Things in there already that I'm going to take away Because you won't have anything in there if you want to add something to the watch Window You can just double-click on new item type in the variable I In there and it tells me that the value or the value of I right now is 99 that Through the loop and the last I had was 99 I Could also plug in and it tells me that FC is city boundaries That's the first shapefile found in that workspace Notice that it it's actually prefixed with this little lowercase u which Indicates that the that that string is actually in unit code format I don't want to get too deep into the weeds here basically Esri uses unit code to Allow for characters in languages other than English So it's used to support all the world's languages Again, I don't want to go too deeply into that so as I Continue hitting F10 when I hit I went to the top of the loop here if I hit F10 one more time I should notice that the FC Box changes and it does Okay, so using the watch Window can be an effective way to Follow how your your code is working make sure that variables have the values that you expect now. I could also One of the things I'm doing in this little example script is I'm Describing each feature class on each passer loop Saving that describe object in a So if I try plugging DESC in there Is it tells me it's a geoprocessing describe data object Which isn't particularly useful right So one of the things you can do with this As the column header says you're not limited to just plugging in Variables you can also plug in expressions and so I can do DESC Shape type. That's one of the properties that you can get at that Property that expression returns to me continue hitting F10 I get to a new feature class and FC changes in value if I change if I go through the described line I'll see that Shape type fairies is a polyline. Okay, so And that's hopefully a helpful demo through using the the debugging tools close out of get out of debugging mode here and Let me just open up the I've got an infinite loop This is the same code we saw earlier So I just wanted to show you how you can try to get out of a loop like this so I hit go and I Should have made sure the interactive window was showing it would show you that it's you know spinning out years Unendingly and what I can do is I can go down to the the system tree. I guess this is called You should see a Python win icon. I can right-click on that and I could try this break into running code That's meant to allow you to Stop an infinite loop like this But I found that it doesn't really work Sometimes I've seen where there's a little Python icon down here That I can try hitting that break into Running code and it works Can't find anything on that That menu so if that doesn't work for you you're really left with just closing Python win altogether unfortunately, okay, so That's what I wanted to go over with you At this point, I'll just open it up for questions Does anybody have any questions about any material lesson to or the assignment at the end of lesson to? okay, well, yeah, thanks Jill and Hopefully folks found this helpful I'll post the recording on the as an announcement and You can go through it again if you if you'd like to all right, thanks everybody for attending and We'll see you again down the road