Transcript for:
Second Python Tutorial Seminar

welcome back everyone back to the second python tutorial seminar of our series hopefully everyone who is in attendance right now has already attended or watched the recording from our first session which was on opening a dot text file and if you had technical difficulties during that hour-long session uh we hope that you were able to set up a meeting with us so we could work one-on-one to resolve that issue so that you're able to fully follow along and participate today before we begin i need to review our code of conduct by joining the zoom call and participating in this tutorial seminar series you are agreeing to adopt these values and engage in respectful communication only if we see any inappropriate remarks in the chat you will be removed from the call by one of the co-hosts i also want to note that a few people commented last time that they couldn't see the commands at the bottom of my terminal because because their zoom toolbar was blocking that and uh for that i apologize unfortunately i cannot actually move my terminal window around it won't change how it's displayed to you but we have three options um if you hover your mouse over the zoom window it makes the toolbar pop up so if you have uh the ability to try to have your terminal and working space be not in the zoom call the toolbox should disappear and you should be able to see everything fine you also have the option of changing your settings in zoom so that you're viewing this call in side by side view so on one side you would see my shared screen and on the other side you would see uh me talking with my very animated uh hand gestures and whatnot the screen might be a little bit smaller for you but that is an option if your toolbar won't go away and thirdly i will do my best to clear my terminal in between commands i don't want to make things disappear too quickly but if i clear it after we have a long string of output my new command will be at the top of my terminal and you should all be able to see it and alternatively all of the commands are available on our website or tutorial cheat sheet which uh someone from the xdev team will post in the chat the link to the content for this particular tutorial uh so if you have that on one screen you can kind of follow along and see all the commands written out there too and i'm going to point you to two other python learning resources that are just good to have in your back pocket one is the python documentation that has everything that we're covering pretty in depth but maybe a little too in-depth and hard to sort through but it's all up there um and in our tutorial we have links in various sections two specific parts of the python documentation that we recommend you look at and you also have the resource of stack overflow which is a community resource which means that people post questions in there and other users answer and at this level most of the questions that you will have or encounter have already been asked or answered up there but unfortunately you know learning how to ask the right question or what question you need to ask to advance your project is also a bit of a skill that you develop so that's why you know we're not solely giving you those to those resources we're also hosting these tutorials to help you along that path and uh one last reminder before we begin is that this seminar is only one hour long so we cannot stop the lecture to give individualized attention to specific people in the call you might be able to find help from fellow attendees or the ex-dev team in the chat but we hope you can follow along and if you can't and have too many technical issues please relax absorb as much as you can in the hour and just know that the recording is available the website is up and we are willing to set up one-on-one assistance after the lecture for everybody okay we're gonna pick up where we left off two weeks ago to review so give me one second to open up a terminal and uh leave this a quick little powerpoint here okay okay can everyone see my terminal okay good so first thing i'm going to do is move this a little bit i'm going to move in to the directory of our project from the last session so for me that was in my documents and it's called python tutorial we're gonna take a look at what we did and just review what we have here uh with an ls uh that'll list everything we have and you can see that we have our script mysi.pi and the data folder which inside of it just has our text file we also have our corresponding git repository and conda environment that only has python installed in it and i'm going to go ahead and activate that environment now conda activate python underscore tutorial okay i am now in the correct environment and we are going to take a look at our script file so open up whichever text editor you preferred and used last time again for me that was nana so i type nano and then the name of my script my side.pi okay so this is what we have in our file we have three lines of code and one comment with the hash we have um read in our data as this variable data and it's all in string format so throughout the course of today we're going to manipulate this data into a more usable format which is the python dictionary we're going to learn how to write for loops and about python data structures so we're going to change the script and we're going to split this long string up into a python list if you haven't opened your editor yet uh again please do so because we're about to start coding okay the first thing we're going to do in this script is at the very beginning we're going to initialize our data variable so i am again making a comment just to specify what this part is and i'm going to create my variable data equals open close square brackets this is how you indicate a list in python and we're going to populate this list as we move through the file julia there's been a request to see if we can increase your font size uh let me try we we figured out how to do it in the practice i remember i will i am in it now yeah the command plus doesn't work everybody um because command did that that worked good this work i just made my terminal window smaller and then maybe proportionally it's bigger in your call that'll do it okay thank you very much for definitely everyone the command plus uh makes the font bigger but also makes my window bigger too so everything is in the same ratio and it doesn't really translate well for what we're trying to do here so hopefully you can see this yeah that's better thank you of course okay so we have initialized our data variable our file name is still the same path we're not going to touch this and our with open statement um the beginning of this is the same but we're going to go ahead and change what's within the block so go ahead and delete the last line of your script again if you want to have notes and know every version is still here you are welcome to simply comment out the line if you're afraid of deleting it but that could get messy and confusing and we do have everything documented on the website for you already i'm going to go ahead and delete the lines that we're not using anymore as we go through okay and we're still indented by four spaces one two three four and inside of this we are going to write our first for loop so i'm gonna create a comment and i'm gonna say read the first three lines or the header of our file to make it a little easier for you guys i'm going to just add a space here you're probably already familiar with for loops from a previous coding language so we don't have to go too much into detail but we're going to mostly cover the syntax and just in case everyone knows a for loop is used to iterate over a sequence which here is going to be range three since there are three lines in the header so what this looks like is four underscore in range three this is how we're starting our for loop and we'll go down one line and then four spaces and then to be within the four block we have to indent another four one two three four before i go into what we're putting here let's talk a little bit more about the for statement so this underscore is a placeholder variable you could use any character you like maybe you're comfortable with an i or j as i was first taught when i learned my first language in matlab this uh but the underscore specifically is used in python typically when you want to indicate that we're never going to call up this variable we're not going to use the underscore we're only having it be our iterator and we're looping over range 3. so this would be like the same as saying for i in a list with elements 0 1 and 2. this would be about the same but range is slightly cleaner so that's why we're showing you that way and it's important to remember the colon at the end of the statement okay two three sorry one two three four uh inside of this just to kind of make sure everyone understands what's happening we are going to go ahead and print the underscore and what we actually are trying to accomplish in this for loop is to use our read line function that we covered last time remember we covered read and read line read line only reads one line so we're going to read one line three times just to skip through it and we're going to stop here to make sure everything works as expected before we uh continue on so i'm going to give you guys uh one more second before i exit this and and run it okay saving my changes and i run it with python mysi dot pi and you can see that our output is 0 1 2 which are the three different values that our underscore takes within the loop okay i'm going to clear the output i'm going to try to get in the habit of doing that every time we have something and now i'm going to open the script back up we're gonna remove this first print statement here uh i showed you something once we don't necessarily have to show it again and we are going to add a second for loop that begins after we have read in the header so we're going to comment this read and parse the rest of the file what this for loop is going to look like is for each line in our data file for each remaining line so we're starting after the header and continuing until the end and here it's also important to know that line is a string if you remember everything in our data file is a string so each line itself is still a string so the first thing we're going to do to our line is we're going to split it so the way we do this is we're going to create a new variable i'm going to call it datum which is equal to our line dot split which is a new function we're learning today you can use a string dot split method uh which allows you to split up a string among a specified character perhaps you wanted to split a word up every time the letter a came up but most commonly it happens when we're reading in a huge data file and this open closed parenthesis means that we're splitting along white space you also could do something like this if you had a comma separated file and another common one is backslash t if it's tab separated but we're lucky we get to just use the default which is just open close parentheses okay so each datum value is now a list of the string as it's been split up and we're going to go into our data list [Music] data dot append append is another new function that we're about to learn this goes it takes whatever we put inside of this parentheses and tags it on as a new element at the end of our data list so we're going to append datum this means that every time we run through this for loop the data object gets one element longer and in the end we have a list of lists of strings okay we're going to add in a debug section at the end here just to make sure everything's working how we expect it to comment debug for datum and data we're going to go ahead and print datum so this is going to go through our list of lists and print each list okay i'm going to give you guys one more second before i close this and run it okay and whoa that's a lot of data so you know this is definitely a time where i'm going to go ahead and clear it but first you can see that each one of these square brackets you can see me highlighting it is a list so this one here is the last datum it's the last element in data and every comma here separates a different value and these uh apostrophes around it indicate well it did not mean to do that indicate that uh it's a string each element is still a string even the numbers we're just going to go ahead and clear everything now and we are going to open up the script one more time okay so we've already covered a lot you've initialized an empty list with square brackets we've done for loops uh placeholder variables and we've learned range split and the function append so there's already a lot going on here and we are now going to practice a few different methods of list indexing which is a very important skill for probably any coding needs you could have and we're going to clear this uh we're just going to be making changes to our debug section down here it's the only thing we're going to be editing as we uh play around and learn a few different ways and we're going to i'm going to go ahead and delete it in between each one you can also just comment it out so that our output that we print does not get super long okay so first we are going to learn how to pull out the first index from our list data and we're printing it so it's all going to be inside the print statement but if you had to call it up for a different reason you don't always have to print it you could just set it as a different variable but we're going to print data and then open close brackets zero this is how you do indexing in python which is a language that is zero indexed so our first element is zero our second element is one and so forth if you have a list that can be indexed and you put these brackets after it with the number it's gonna return for you what that value now is so this is how you print uh the first element now any guesses on how to print the tenth element i'm gonna just type out the beginning really slowly and kind of lag for a second while i give you guys a second to think about what goes inside of the brackets if anyone said nine good job you got it and one other uh trick in python is that if you wanted to print the last element of your list you do not have to know how long your list is you can do negative indexing so negative one means the last index and negative two means the second to last i'm going to go ahead and exit this and we can print it out by running the script and make sure that it works as we expected okay so hopefully you can see this on your screen as well we now have three different lists that have been printed this one is our first list from data this one's the 10th and the third one is the last okay let's open up the script one more time i'm going to clear it nana my side.pie and open the script back up we're now going to practice what is called slice indexing so this is say if i wanted to get the first 10 rows from the data i could do that with delete these two and instead of data 0 and doing that 10 times i can put this colon here and put the last one so it's exclusive of the last integer and basically it goes start and stop and if we run this we've printed out the first 10 data lists from data and it's a little hard to read so moving forward we are going to we're going to do it in a for loop just to kind of format how we look at it so my for loop is going to look like for datum in data and then here's where i'm going to do the indexing that we're selecting print datum now hopefully this is a little nicer to look at okay uh there's still a lot of data that we've just printed out but you can see that each list starts nicely at the beginning of a line in your terminal so it's a little easier to think about we're gonna also try i'm gonna clear this one more time open the script and we have a few different methods if we deleted this 0 and just did colon 10 what do people think will happen does anyone think this will still work let's find out it still works and it still prints the exact same output as last time if you don't specify your starting index python assumes that you're starting at the beginning and that you're just printing the first 10 elements clear nano my side.pie we can also add a third number here colon two this these three numbers you know when there is a zero it kind of maps to start stop and step so two is our step size meaning that we're only printing every other value from data execute this okay so this is much the same as last time but we've now printed out five lists instead of ten hopefully that makes sense to everybody and now we're going to practice nested indexing so i'm going to open our script we're going to edit the debug section only we're gonna start start fresh here okay if i wanted to print the fifth element from row nine of our script that would look like this print data eight so this first indexing data eight pulls up just that one list that it's pointing to and then we open and close a new set of brackets to index from that new list so four sorry excuse me for coughing because that's part of why we're doing this remotely um so sometimes i've seen people try to do it this way because maybe it's the word nesting or something but they try to do this um this python does not know how to read this we basically just have to set up the list index it and if that new thing can be indexed we can index that so we to show that kind of well we can print two things so first we'll gonna get the same object and then we can index it a third time possibly i'm gonna go ahead and run this okay does anyone understand what happened so the first output is when we printed data index eight and then index four and that points us to this value of 70.3 which still happens to be a string and you can index lists or strings we picked out the 0th index so it just went to the first character of that value and gave us a 7. so you might want to be careful of that making sure that when you're indexing something it you're pointing to what you expected to but i just think that might help people understand how this uh each step that you add to the indexing works clear nano my side up high okay we're also going to add a few different uh sections to the debug is just like a little quiz for yourself can you print out the first five indices from row nine and can you also print out every other one for row nine and i will kind of slowly build both of these out and hopefully you guys have enough time to work on it on your own for a second okay so this is what i have for my nested indexing so you can see the first one we've already printed out and it was something like 70.2 the second one this is going to give us five different values and then the third one this is kind of similar to when we did every other step for 10 but you don't have to put in either the start or stop value as default layer the beginning and end and we can just print every other one with colon colon 2. okay i'm going to go ahead and execute this now okay and hopefully your answer looks the same okay now we're going to show you something in python that does not work but that is a very common mistake or something that people often wish would work and we're just gonna show you and again if you're keeping everything just commented out hopefully you have a code for yourself to indicate when something wasn't supposed to run so you don't confuse your future self so this time we are going to try to get the fifth element from the 5th through 8th rows now this is not going to work and if anyone can take a second to think about why um but maybe first thing is we can take a look at what our error message is when we do run it our index is out of range so what this means is that the new list that we created that is five to eight does not have a fourth element oftentimes people are confused because our list is still a list of lists and every one of those has a fourth element but our list itself is only three elements long to take a closer look at why this is we're going to go in here and instead of printing the fourth element i'm going to go ahead and just print the zeroth element this is the first one and i'm going to print one more thing to show you guys that these are equal we're going to print data five close parentheses okay okay so we've just printed the exact same thing each time so hopefully that helps you see that data five to eight gives us a list that's three long and then our next index after that is indexing that new list it is no longer indexing within that list if that makes sense i want to encourage all of you to practice on your own some more because there's you know a lot of a lot of examples kind of infinite possibilities so please just check out some of the other learning resources on indexing in particular a lot of these are posted in the various see more sections that you'll see as you go through the tutorial website um but because we are short on time and already more than halfway through we're gonna go in we're gonna clean up our script and do our git add and commit so i'm going to do clear nano my side.pie we clean it up by deleting our debug section saving that and then git add the name of our script myslide.pi and then git commit [Music] dash m and then quotes our commit message which i'm going to call parsing file now to see that this has happened again if you remember we can check get log and this will show you that we have three different uh git commits so far the two from yesterday and one from today okay i'm going to clear this out and we're going to begin the next section of today i'm going to open up our script file so we can discuss that some more okay so this is all great but can anyone actually remember which column is associated with which variable was time our first column or our second where's temperature uh it's kind of hard to remember so instead of just storing things as lists we like to store things as data dictionaries they act much like lists uh in that they're changeable and they point to lists but instead of an index that's an arbitrary number like 0 one two you can name those indices uh so instead of indices rather they are keys we call them dictionary keys so instead of the first element we can say date we can say time and we can actually point to that using language that's a lot easier for us to remember so we're going to go in and change our data to be a little bit more conveniently named so here instead of initializing data as a list we're going to initialize it as a dictionary which is curly brackets instead of square ones and then inside our dictionary we can store three different lists that we are going to initialize the first one is called date and that is going to map to an empty list the second one is time mapping to an empty list and then temp out which is the name of the temperature variable in our file so each key is a string and each one the mapping is with a colon and then they're separated by commas if we wanted to pull out say just time that would look like this so it acts very similar to all the indexing methods that we just learned but it's a string so it's a word and hopefully a little bit easier to read and understand immediately what your code is doing okay i'm going to go ahead and delete this that was just to show you how to do it okay we're this is going to look the same our with statement our first for loop looks the same but our second for loop looks a little different so instead i'm just going to go ahead and change what we call the datum to i'm going to call it split line it's a little bit easier for me to think about and we are going to um loop through each line and split it along the white space and then we are going to go ahead and append the appropriate element from that line to the corresponding list within our dictionary so data which is our dictionary and then we use our key to point to the list and we can use our append method just like before and what we're appending is from the line that's been split up our first element or index zero [Music] now it's going to look the same for time and temp out but with index one and two time dot append split line one split line two [Music] so again um we are appending whatever's inside the parenthesis to the end of the list and we specify that list by pulling it out of our dictionary using the appropriate key we're going to add a debug section again just to make sure that everything's working as we expect and i'm going to print data time okay i'm going to go ahead and execute now okay so you can see that we have printed out a bunch of time steps that are all strings let's uh go in again and clean up and add this to our git repository i'll clean up this time just meaning removing the debug section oh sorry i did not mean to run it we aren't outputting anything so that's why nothing happened uh we're gonna get add our script git commit dash m with our message parsing select time series and voila everything is reflected in our git repository and we have four commits so far so it's been easy right now to get time series information from each column we have now worked around the issue that we can't just pull out the fourth index from x y z indices in a single one line you have we have to do it in a for loop for now um and we've now done it so that's really great but um we still have to know the name of each column we're getting and everything is still in a string if we want to actually do math on this data we need it to be in a different data type so we're going to go into our script one more time to fix this and it's really we're just going to change one line here down when we append temperature out what we're appending being our split line index 2 we're actually going to insert another function right into the middle of this which is float which just turns a split line into a float float the float data type refers to floating point real values and it's the data type of any number with values after the decimal point we could also make it an integer which is int which we would do if we if we wanted to we could do int around float and that takes the floating value and rounds it down to the lowest to the whole number below it you could not actually take the integer of a string it has to be in the float so python can understand that first okay let me double check my parentheses right now so we open open close close perfect now we're going to do a debug section we're going to print data temp out to see what that looks like exit save run and we've printed out our long list but this time maybe you'll notice there's not uh apostrophes around our numbers they're no longer in quotes they're no longer strings they are real usable numbers that we can do math on and just to kind of show you that we cannot do i turn any value into any python data type it has to be someone that object object can become if we try to do float of a string of characters like the word error which is what i expect to get and let's go see what we get [Music] okay we cannot convert this string to a float we were able to convert strings of numbers into a float but um python will not let us do that so we say thank you python some languages i'm pretty sure javascript does let you do that and each letter just has a random number it's not random but it could be if you did it by accident so we're gonna we're gonna go in and delete this debug section again we are going to have this represented in our get repository get add git commit this message no space there converting temp out to floats git blog dash dash one line since we're getting a little long here and you can see our most recent commit okay so this seems really great so far um but looking at our script you'll see [Music] that if we wanted to say add a third variable that we wanted to read in from the data file we would have to change when we initialized our data dictionary and again within the for loop we would have to add another line there this means that for any one change in our script we have to make two different changes so it can be a little hard to stay consistent especially as time goes by and you haven't revisited the script in a while so it's best practice to try and lump things together so that you can see everything you have to change in one spot of your script and it not being hidden in the middle we're going to go ahead and we're going to refractor this code which means we're going to move things around in a way to make it cleaner and easier to read for us so we're actually going to start out by adding two new dictionaries to our file okay so i'm going to go here a few lines our comment okay let's see here's my mouse i'm going to first create a column dictionary so column names and column indices to read this dictionary i'm going to call columns and it's a dictionary so curly brackets and we're going to have the same indices as the fourth date but instead of mapping to an empty list it maps to a value this value corresponds to the index of that column in our data file time mapped to one and temp out amount to two we're going to add a second one for data types so data types for each column we're only going to do this if they're not a string so we don't have to go ahead and do it all the time so this one i'm going to call types equals we only have one key here which is temp out it's the only value we're converting to anything right now and what we're converting it to is a float so i know before we used float as a function with the parentheses after it and that can be slightly confusing right now because here i'm just writing float without that the reason this is is because this is a function pointer which means that we're not actually calling or executing the function we're just saying we can just pass around the name of the function and later on use it which is very helpful and nice julia there's uh someone caught a typo up in the definition of the columns dictionary there's an extra apostrophe after the one yes thank you guys okay so now our data dictionary here is going to look a little different we are no longer going to already have it filled but we're going to use a for loop to fill it up based on whatever we put in the columns dictionary so for column in columns four spaces data column equals an empty list so what this does is we loop through each of the keys in our dictionary so the first time we loop through it that key is date and then we go in we do data we initialize a new list pointed to with that same key and we do this for each one of these three keys it's really great how you're able to just add a new key without having to go in and completely redefine your dictionary so we can leave uh the first for loop within our with statement the same again and we're going to scroll down and i'm sorry that this top of my script is now off the screen um it's kind of the trade we're making for the font being big enough and we're now going to change the second part of the script because we no longer have to point to each index and each key specifically we just have to call up the columns and the types dictionary that we created earlier okay let me go in and do this so we're still going to have our split line variable but then much like when we were initializing our data dictionary we're going to loop through each column name so four column in columns one two three four one two three four that's 12 spaces indented we're going to create a variable i being shorthand for index which is equal to columns pull out column so again the first time we loop through this column is equal to date and we go into our dictionary we'll pull out date and that maps to the index zero then we do time and i becomes one and tempo i becomes two now we could do the same thing for types which we're going to go ahead and call t but because we only specified the the type for one out of the three shared keys we can do another dictionary function types which is our dictionary dot get column comma string now i'm just going to kind of show you down in this blank space what this does so when we do a a dictionary so let's just say our dictionary is d dictionary.get and then in parentheses you have a key and a string what this does is it first tries to do to pull out the key from our dictionary and then if that fails because this key does not exist we're going to actually return the value string instead of whatever value that key would have returned so that's kind of what our dictionary.get does it just avoids keynote found errors and allows us to not have to specify a bunch of things we're not using so it's a nice nice little tool okay so we're going to call it value equals t open close parenthesis so this is again where uh float is a function but before it didn't have the parentheses now it does so now we're about to execute it and what we are giving it as input is our split line at the index associated with the column name we are currently working on and then the one last thing in this in this uh code is that we have to append it like we did before so now we can just say column which is going to be the string date the string time or the string temp out and we append our value so this is our new data dictionary just to test this again we're going to add our debug section okay execute give you guys one more second and run okay so we have printed it out and it's floats and it's great and it's beautiful and i'm gonna clear it and we're almost out of time so um which is good we're coming to the end the last thing we have to do here is clean up our code and commit it one last time so that we're all ready and set for our next session open up your script go down to the bottom delete your debugging exit it save it git add git commit this time the message will be re-factoring data parsing code and just to check we now have all of our commits represented in our gear repository so thank you everybody this concludes the second lesson of this virtual tutorial in this section we have saved variables date time and temp out into a data dictionary you should now be familiar with data structures lists their list indexing nested lists and the command list dot append you're also familiar with the data type dictionaries their keys the command dictionary.get and you're familiar with ranges we learned to write for loops about the float data type and using the command string dot split you refactored a code we cleaned it up to make it easy for future manipulation and that's a lot of different content to cover in an hour so hopefully we moved slow enough that everyone could digest it and again if you had technical issues please reach out so that we can get you up to speed and you can join us on november 11th in two weeks time we're going to be using this data dictionary that we just created and we're going to compute wind chill index by writing your first ever python function and doing some basic math operators so we're going to really get into the application of applying this data to a scientific workflow okay thank you everybody and uh we can take questions at this time or if there's any statement from anyone else on xdev uh you're welcome to unmute yourself now we've had a request to see the top of the script again so that someone can see it just a reminder uh you can also see the entire script on the website should i uh if i zoom out a little bit make it the font slightly smaller but you can see a little bit more of the script with that is that what we want right now yeah is this good so so okay it doesn't seem as though there are any further questions uh if anybody has additional questions or uh would like help uh please reach out to us at the email the julia mentioned xdev at ucar.edu we can set up a uh we can set up a session and help you with any particular questions that you might have um and that's it otherwise if you have questions please go ahead and post them in the uh chat ah someone just said this part is still not clear the four underscore in range three um okay so what that is doing is range three is an iterator um it's equivalent to basically saying for underscore in the list zero one two okay so a list that had three items in it uh we're saving we're iterating over that list and putting the value each time for each iteration in that variable underscore which is just a best practice way of saying i need to define this variable but i don't care about it and i'm never going to use it and then every time it loops it calls datafile.readline which means it reads three lines but it doesn't do anything with it it just reads the lines and discards it so what that does is it effectively reads the first three lines the header of the file which was not any data that we cared about just the names of the columns and sets it up so that the next read line which happens in the next for loop is going to be lined up with your first set of data i also saw a question about why you don't have to do an end for statement in a for loop as you do in i know matlab and some other languages well in python knows that you've ended your for loop because you already told it the for loop is only going to last for the length of x in y and once you've moved through all of those it's done and then also because python is sensitive to white space once we're no longer indenting we are no longer within the for loop there's no need for an extra line it's already there in the syntax yeah there's a another question um uh see here if i get them in the right order here uh you mentioned function pointers uh and then could you please explain more about this um so everything people will say this to you all the time about python everything in python is an object and that means variables are objects in in some other languages what you're used to is just uh being able to pass around the by reference so you actually work or pass around by value into functions you can actually pass that variable in or its value into a function so that that function can then use it in python sense quote everything is an object unquote um everything can be passed into a function the name of functions themselves the name of objects the values of variables everything can be passed in so in that particular example that julia has where she's saying that this dictionary with the key temp out points to float float is actually the name of a class in other words the name of a type of object in python and that thing that class can be used like a function and so what she's doing is she's actually storing the name of the function so that she can then actually use the function later by referencing the types type temp out i i don't know if that actually makes sense to you uh if that was a bad uh answer i really apologize i think any other attempts that i might have to answer that will just confuse you more so i'll see if someone else has a better answer if that didn't work jeff dlb asks are there other discardables like underscore technically speaking the underscore is uh is just one example of it you could have say two underscores or three underscores if you had nested loops for example um anything that any variable that starts with an underscore is also usually treated as a discardable uh like you could have underscore i underscore j something like that um but this is just a best practice there's nothing in python that actually says you have to use discardables so if what you actually wanted to do is just call the loops the nested loops i j and k you could do that there's nothing preventing from doing it it's just it's become a common practice in the python world that if you use an underscored variable then it's sort of informing the reader i actually don't really care about this and i'm not going to use it later so uh this is just a filler ah and then you also ask doesn't your use of range three read four lines no um range three actually reads up to the fourth index so it reads zero one two right and then three is the fourth index and there's another question that says do you have to set this to uh empty square brackets in the four columns and columns um yes uh you do because what we're doing later down in the script is we're using the append method when we actually access data and the append method only exists for lists so if you hadn't initialized data column equals an empty list then when you tried to access data column later on or whatever that key happened to be then it wouldn't know it was a list and it wouldn't have the append function and you'd probably get an error there's another question how do types dot how does dot get column string work with respect to columns other than temp out okay so in other words if column was equal to the string temp out then it would just return types temp out but if column was anything else which is to say if it was a key that wasn't in the types dictionary then it would return str which is that function that converts things to a string okay so in this particular case the function str doesn't actually do anything it's basically a null function because it's already a string these are good questions uh there's a lot of content in here and there's a lot of subtlety uh and there's no way we can cover all of it and actually end up getting to really useful stuff later like numpy which we'd like to do uh in a reasonable amount of time especially if it if these sessions are only every two weeks so i really recommend that if you want to know a little bit more depth look up the python documentation on these things that we've just introduced you to and go a little further there's a lot to all of these things there's a lot to lists there's a lot to dictionaries that we aren't even covering yet okay and then joe asks why do you refer to the data dictionary elements using bracket when it's not a list okay that is a little bit confusing when you have an a list called say x to get any item in that list you use x square brackets and then the index square brackets is python's way of getting an item from something in other words it doesn't matter what it is it just also happens to be how to initialize a list so that's a little bit confusing you use square brackets to get the item of anything whether it's a list or a set which we haven't covered or a dictionary you use square brackets to get the item inside that container but when it's on if you if you think of it this way if it's on the right hand side of the equation you're setting something equal to it then square brackets is shorthand notation for initializing a list and then curly brackets is shorthand notation for initializing a dictionary and then evan meeker says so t is acting like string function in the next line exactly exactly has a lot to digest i'm happy to continue to answer questions if you've got them is there a vector or array data type that will only take elements of a single type like int um there is in numpy but there isn't in just raw python uh in python python has generic container types like lists and dictionaries and sets as i mentioned which we haven't covered and the general rule of these generic data types is that they can contain anything uh and they don't have to be uniform types so like you can have a list that contains a string in the first element and a list in the second element and a dictionary in the third element if you'd like there's nothing that prevents you from mixing types in python containers but what you're getting at this ability to basically have an array or a vector of uniform types gives you efficiency and that's something that numpy is going to give us later on so we'll cover numpy later but we're starting at a place to give you hopefully a foundation in just python by itself so that when you then introduce numpy you don't get completely lost by the python okay and there's another question list equals array dot dot dot dictionary equals hash uh yeah in a sense um yeah a dictionary is a hash map um and in fact i think it's implemented that way in c under the hood uh not a simple hash map but essentially a hash map yeah so the keys in the dictionary have to be hashable they could the keys actually could be integers if you wanted they don't have to be strings they just have to be a hashable object and lists are akin to arrays they're actually closer to linked lists which should be if you're familiar with linked lists should be a sounding alarm bells that that's not a hugely efficient data structure to use so yes i i it will not be as efficient as an array but it it works like an array okay and jeff dlb says looks like underscore var is just a convention to show we don't care forever but the value last assigned still persists in outside the loop exactly that is correct yeah it's just a convention uh and evan asks why do you not need something like the append method when adding each column to a data dictionary okay you need an append method with lists because lists are ordered they will always retain and care about the order of the items that you added so you need append so that when you add something you know where you're adding it you're adding it to the end uh with the dictionary the items technically are unordered now as of i can't remember which version of python three uh dictionaries will retain the order that you added added to them uh but that's really just a convenience thing um dictionaries don't actually technically care about the order uh that the keys were added and so you can just add them with the square brackets notation that julia used hope that answered the question um let's see here and then the next question from tammy's ipad is how do you print a single variable like temp out from the data dictionary okay so what julia created was a dictionary with keys that mapped to lists so if you wanted to get for example the tenth item from temp out the temp out variable this is what you'd type and i'll actually type it into the chat here you'd say data temp out yeah yeah that's right evan um if it was the 10th item you would actually need to use uh the 9 index that's correct yeah so i just repeated what evan said but yeah that's exactly right no worries evan uh you beat me you're you're a faster typer than i am i hope i didn't miss anybody's questions if i did please repeat them ah there's another question if your data has headers you would use a different process is that correct um i guess the question would be uh to clarify instead of creating a dictionary um do you mean if your data had like column names at the top is that what you're getting at okay uh well this data actually does have column names um they are spread over three lines so actually getting the names of the columns is a little bit trickier because you'd have to parse three different string lines split them and re-concatenate them in the right ways and so forth so we just didn't include that in here but you could actually read the headers uh as just with a single like data read line uh split on that and use that to initialize your data dictionary use the actual string values to initialize your data dictionary you could do that although in that particular case you wouldn't exactly know what the names of the columns were they would be an internal variable so it would be a little bit trickier you'd have to write a different set of code that's for sure yeah exactly what evan said any other questions hey thank you everybody yeah thank you very much everyone um appreciate the uh the thanks and the comments in there um i'm sorry if i couldn't get to everybody's questions there was a moment in there where the chat was kind of exploding and i couldn't keep up with everything so if i missed your questions i really apologize please send them to xdev at ucar.edu and we'll answer them if we didn't already thank you all see you in two weeks