 Hi everyone again welcome to the LPS class we are continuing our discussion on Python in the last lecture we saw like the regular expression pretty much lately covered how to do the expression we saw this particular thing basically where we kind of match the string using the R which is the raw string basically like I mean then once we have that the one the match string then we substitute it PSI 3 PSI 4 and then we explain this concept of this basically so event will do this section this section we also talked about some of the additional stuff so we can use basically can do the search regex is the subject or match regex is the subject as alternative syntax and the match basically it only looks for the match at the beginning of the line it does not match the whole string just the search attempt to match throughout the string until it finds a complete match so that is the difference between the match and the search we also have this find all that is another method inside the class re and that actually returns then array of all the non-overlapping matches we can also do like using a far loop here basically which is for M in the find-deter method basically and then you can find each one and then on the M is basically and then find all of them so this same as the find all and then the the matching itself is basically the level where the we can start the regex with question mark I question mark S question mark M so this is something that we saw and then we also saw the split basically which is another method in the regular expression class and here when we do the split essentially splits on that particular string so it basically it replaces the PSI 3 with PSI 4 so in this one basically it just splits and then this is removed so your output is going to be just the program PSI 4 was an excellent problem that's all so today we will continue some more the discussions on the regular expression finish off the regular expression and then move on to the next chapter so so basically the match object function essentially like so search and match return a match object the object has some useful functions basically and do group that is to return the match stream and then start basically that is the starting position of the match and then end is the ending position of match and then we can also like do a span which is basically it's simple containing the start in positions of the match so just to understand it more thoroughly like I mean the Python offers two different primitive operations based on the match checks for the match only the name that we saw and then the search basically searches for the whole thing and both of them returns a match object now here are some match object examples so we declare or we define one of the variables line also the string and then we say match string search for PSI 3 in line and then so basically the match starts a character we can say match string start master this particular variable start dot start and that gives basically like I mean where it starts the match which is the PSI 3 and then there it ends and then before the match before match and after match basically that is with you can specify the index and then start and then all the way up to the end now start at end so so this will give you like on this section so the first one will say the three and then essentially like I mean from three next to all the way up to end and then here it will start from here and so now let's talk about the capture groups so this is useful to actually apply certain after after obtaining a match take that match and then use it in then as a next variable to do some processing so we did this for both pearl and tickle so the parenthesis inside the regular expression denote a capture group which can be accessed by the number or by name to get the matching essentially of the capture text so we can name the capture groups with this kind of syntax where we can say like in question mark P and then the name and then we can also take advantage of the triple quoted strings which are multiple lines if you remember we talk about that to define the regular expression and that can include comments as well and if you use the this particular method which is verbose method that option and you can use that so let us look at some examples here so again we define a in file where we open this test or text with a read only and then we define all the lines basically like when read the lines and this is the method for the reading the lines for the in file and then we close that particular file now we use the triple quoted string essentially like I mean so we want to be spring so we start with the triple quoted string basically like some optional white space in the beginning and then we say basically so okay now question mark P key and then followed by any other thing so any word that begins with P we match that here and then we again have like you know equals time with some optional white spaces so whatever there is some sign there and then finally like I mean the non space after the equals to is the value this is so we match the key and we match the value and they are stored in the key and then the value so here as we saw here basically the P name so so assume that you have like X Y equal to 3 B equal to 4 C equal to 5 something like that in this particular test of text file so now we have the thing before the equal to is a P and then thing after the equal to is the value so this becomes an an array that you can store as a key value that then we do the restring ignore case essentially like I mean then then pipe it to the verbose basically and then that is what we are compelling and then we do like the for line in the lines so print key equal to the match group key and then value is match group value so this again prints basically X Y and then 3 and then it continues on the 4 C 5 so that is what we want to get basically so in the in other words actually like now this is for as an array basically you can think of this as restring X Y E equal to 3 this is what we have store essentially so now let us move on to the next collection data types so here we will talk about tuples the lists and dictionaries essentially so tuples are collections of collection of data items they may be of different types and usually tuples are immutable just like strings that we saw basically like I mean the the lists are like tuples but they are mutable so that is the difference between a list and the tuple so Python uses this parenthesis to denote a tuple you could also use the parenthesis if you have just one you have only one item but if you have one sorry if you have only one item then we need to use a comma to indicate the tuple so here we have like three names so we just put comma and then put it and put it into whereas if you have just one name we just put the name then follow the comma and then put it inside the parenthesis and then the empty tuple is denoted by just empty parenthesis we need to enclose a tuple in a parenthesis if you want to pass it all together as one argument to a function so this is you can think of in the typical we had this basically like notation there we pass the entire thing as this one object so some of the other things that you want to know about tuples are tuples are also like iterable meaning like you can iterate one by one and the anything like I mean that that is pretty much it basically on the tuple now the list essentially lists are like tuples the only difference is that they are mutable that means that you can actually change that data structure and they are denoted by square brackets instead of the parenthesis so here we have like 1 3 5 7 11 and that is the that is denoted as a list and then you can also have different values this 0 1 boom is also list and empty list is just square brackets stuck together and so the mutable property is described here and add elements to the list or basically you can append an item here we define a particular list 1 2 3 and if you want to append done to it we use the method except and done and then that prints out with that both the lists and tuples contain object references so the list and tuples are also they can be nested so you can actually describe the tuple within a list and the list within a tuple and tuple within a tuple and list within a list so here we can say basically so a equal to 0 1 2 b is a 34 and when we print a it basically a nested list is getting printed if you print b 0 1 it prints 1 so if you print b 1 0 basically that results in an error so the 0 1 is the subject so b 0 if you just print b 0 that is 0 1 2 and b 1 is 3 so b 0 1 is actually this object which is 1 there has be 0 0 is just 0 so some more examples of lists so this is like a list example of I we define one list which is just the odd number except odd numbers except 9 and we print x now when we print x 2 and anyone tell me like what is this one this is actually like now it is 5 x 2 is 5 now we do this operation which is here to in assignment x 2 equal to 0 now what do you think will this is a replacement happen it happens in list because this are mutable so we can you can actually change them so we will replace 5 with 0 with when we assign this one and then if you print out x if it prints like 1 3 0 7 11 and then we can apply x append the method append to it with number 13 which just adds 13 and then everything in square brackets okay and then if you remove one this removes one and it is not the item at position 1 it just removes one item so the remove one basically let me remove the first item the position 1 is actually number 3 so this is the first item just gets removed and then we after that we insert at the first position the insert number 42 so this now the insert actually takes this as the positioning thing so for that it starts from 0 1 2 so the one position is going to insert 42 so overall list is going to be like 3 42 then 0 7 so the 0 is pushed and then it is inserted there so I want you to remember this thing the remove one doesn't mean that the first position gets moved but the insert one means that the first insertion the insertion happens at the first position okay now let us look at the some of the indexing so the indexing we know that actually it starts from 0 1 2 3 and 4 so in this case the x2 is actually 5 that is because it is a counting from just to write it is 0 1 and 2 and then when we do an assignment with x2 equal to 0 since it is a mutable it basically now changes that and then so that the 5 is replaced with 0 so the difference between again the list and the triple that we saw is while the index indexing of a triple also works exactly the same way starts from 0 1 2 3 4 you cannot do this operation because the the triple is an immutable data structure so you want to do that and then slices is another one slices work as for strings basically like so if you remember in a string like I mean S T R ING we can specify a slice of R IN which is like starting from 2 3 4 2 column 4 that is the string those are still apply with the lists so here we can specify like I mean it's a 2 column 4 and then that is like 0 1 2 2 4 so 2 and 3 will be printed out so here it's basically R IN is 2 equal 5 and now we cannot this additional operator which is basically the incremental operator that we saw basically here we have a list 1 3 5 so we can use the method a dot up n this is one way to do it and then we say 7 and then that added at the end we can also do just simply a equal to plus and then the square brackets 7 and then this will add that element into that list if you try just a plus equal to this regular 7 this is a failing fail case because you need to specify the 7 inside the list file for it to be added to the existing list so when we do this the result is 1 3 5 and 7 and then we can also add anything basically so we say like the end add the end into the list so again we use the same thing and then now we feel printed a it's 1 3 5 7 and then the string the hyphen end so this is another shortcut operator that you can use it instead of the append function now let us look at the dictionaries the dictionaries are nothing but items are it is an unordered collection where items are accessed by a key and not by the position in the list so a dictionary is just like a hash in the collections of arbitrary objects and we can use object references like this and then dictionaries are also nestable you can grow and shrink in place just like this and it allows concatenation slicing and other operations that depend on order of the elements so they depend on the order of the elements do not work on this so all these concatenation slicing and operations which depend on the order of elements they do not work in dictionaries so let us look at some examples of a dictionary so here in this example made like jobs basically David professor Saharan postdoc Sean grad student so these are the three elements that we define inside the dictionary and you notice that actually each of them is basically it is a keyword followed by a colon and then another keyword and then a comma so anything before calling is the easier key and then the other side is the value so if you just say jobs Sahan if you post it off and then this is also a mutable database so essentially you can just say like jobs Sean equal to postdoc and then you can change the job of Sean also to a postal and then if you want to list of keys and values we use this basically like the this method is so for jobs dot is basically it prints the three keys and if you notice basically like the order actually it changed originally our order was David Sahan and Sean now the new order is Sahan Sean and then values generate again the corresponding values in the thing so this is another method that you want to remember and then once you specify the values you get postdoc postdoc and professor which has professors are in profession of the jobs of these people to query like every element which is like a key and the value together use the items function so the jobs dot items function returns Sahan postdoc Sean postdoc David professor so that is how you can access those values in the dictionary so the key thing that you want to remember is it starts with the curly braces ends with the curly brace that is the dictionary and then the items the each key is separated from its value by this column and then the each items are separated by commas essentially so that is what here and then the whole thing is enclosed within the databases so the empty dictionary you can create it by just putting empty databases so here only thing is even though we said that basically like this is a mutable type of database the keys are of immutable database in data type so these are immutable they they should be one of either string number or a chip these are the only data types allowed as the key the value can be anything else so because the value itself is not immutable it is actually immutable so now let us look at some of the common operations that we can do in a dictionary so we can delete an entry by just using DAL followed by D and then the key name then if you want to add an entry then the new key is the new value if you want to find out if particular key is in the dictionary and we can use the has key function as key key name or a name in D basically this is another another thing the get is a method that is useful to return value but no it does not fail basically so it returns none the keys does not exist so you can use the get to get the one but if it it won't fail basically the key doesn't exist so that is only target so the way to use it is be get and then the key value then the default update merges one dictionary with another and then this overwrites the values with the same key so the usage is like the update D2 and then we give the dictionary version so now if you want to iterate dictionary through the keys we can use the far command so here actually like I mean we can specify like one with the win and Margaret Mitchell in a the Virgil and Odyssey by Homer and then we do the par book in book authors so it knows that the key is actually like going to be the book and then we basically book authors basically so it gets each one and then it adds the buy and then basically prints the whole thing so wherever like I mean when you specify variable that always iterates on the key so as you can see like actually key is the most important item in dictionary so the properties for the dictionary keys essentially the values themselves have no restriction they can be any arbitrary Python of it either standard objects or use of different objects but there are two important things that you want to remember one is more than one entry for key is allowed so if you have so when a duplicate keys encountered during an assignment the last assignment always win so the simple example will be define a dictionary it equals again the three things that you want to define in between your intro within the value basis and then put the key say name and then put the colon essentially and then Zara and then comma so this forms one element and then age I mean pollen and then cell and then another comma and then finally again we call it name pollen num this also includes we need to close the process so now in this particular dictionary if you want to print the square brackets name and this will always be many the Zara name is gone only we can smell because that is what its last updated values so if it cannot be hashed it just just overwrites it overwrites the previous value or with the new value now constructing dictionaries from lists so for this we can use the zip function so if there are two separate lists for keys and values we can combine them into a dictionary using the zip function and the dict constructor so that we have a dict constructor and a zip function so the way to use is basically we define the keys we define the values and then the dictionary variable equal to dict zip keys with values so some general keys instruction basically like so keys don't have to be strings it can be any value that we already saw it is only it is immutable data that including tuples this particular property is good for representing sparse matrices so here there is a matrix which is just a blank a dictionary and then we populate essentially one value 101 0.5 so the key is triple you can see that basically it's imposed in the analysis and then 1 1 4 is another one which is 0.8 so to represent this basically so it's it's a good that you don't have the string or any other this attack so collection length essentially is computed using the length variable that returns the length of a triple list or a dictionary that is like how many number of characters of a string so LEN Tony with these parenthesis because this is a triple and we are looking for that the length of the triple it returns 1 but if you do a length of just Tony without the triple now it returns 4 and length of a list essentially 0 1 boom is 3 so let me talk about some of the built-in operators dictionary functions and methods within the thing so LEN LEN is one of them the other one is CMP CMP basically the usage is dict one and it too so this method actually uses CMP basically compares these two dictionaries and then sees whether they are the same or different we can also have a STR dict which is a conversion from the dictionary to string and then there is also a type variable this returns the type of the past variable if the past variable is a dictionary it will it will define or it will return a dictionary type now Python also supports a lot of methods one is dict clear this clears the all the elements in the dictionary essentially dict and then copy is another one which makes a shallow copy of the dictionary itself and then the dictionary from keys is another object that creates a new dictionary with keys from the old dictionary and then it sets the values so to 0 no sorry the set the values to the value itself so the usage is this dict not from keys and then the palm keys as the parenthesis as well and then there is also a dict key with the default none this returns the key and then it returns a value or a default if the key is not in the dictionary and then dict has key actually returns true if the key in the dictionary is in the dictionary dict is form and dict items essentially it returns the collection of dict key value to put that big standard faulty is another one which is similar to the get but it so the dict has has key returns true is the key is in the dictionary dict or returns false and then we have items update dict values there are several methods that are available now let us talk about the ease operator the Python variables are really like object references is something that we saw the ease operator checks to see if the references referred to by the same by the object the same object are they like actually like referenced by the same object or not here we can have two identical objects that are not the same object actually try to access the other other motion and then the references to the individual constants should be identical so the reference to strings may or may not show up in the top as referring to the same object to identical mutable objects are not necessarily the same object so let us look at an example some examples of ease operator so here we define two of them and then print x is y then it is true here but it is not necessary 1, 2 this one is false because it is a list and then in a triple again if you ask print x is y it is also false even though it is identical and then it is equal to the stack bracket basically there x is none true now we talk about an in operator in operator is for the traction data again the in operator determines whether something is a member of a function or not so this is what we used for for like some value that is part of the program or not so we use that so here an example basically like team we define as three people and then we say like other how well is in the team and then it is false and then stored not in team so keywords are here then it becomes true to travel the collection data type we are to use the you can use the IN essentially for something in something so the something in something is different here numbers are different at 1, 2, 3 and then we can fail it for I in numbers so basically takes those things and then print so here we get 1, 2, 3 so now how do we copy collections essentially we can actually if you if you make an if you use an assignment it it just makes a new can new reference to so here basically like we just say it's a list B equal to a C equal to a column a star brackets and then just call and then you say B equal to 1 and C equal to 1 and then we say like then as to print the A B and C so then now essentially like it prints 0 5 3 0 5 0 7 and we can copy the dictionary with the copy function so essentially a column copy now there are a lot of other methods essentially like for the this variable essentially so some of them are one is clear which removes all elements of the dictionary or dictionary whichever dictionary that is specified the copy that we saw basically it returns a shallow copy of the dictionary from keys we saw already like that is set and then get E default none it gets all the keys and then has key returns the value basically of the key returns true if the key is in the dictionary else it falls and then the item that we saw keys we saw and then we can also do a set default there we can which is very similar to the get or DB copy sorry DB get of the get parenthesis function and update is essentially we saw that that is updates the key value there and then values actually gives the dictionary of the dictionary values so let's stop at this point today and then we will go into the advanced functions in the next class