 Hi everyone again welcome to the next lecture of the LPS class the LPS is essentially the the programming languages that we are going to as you know we started with the units the covered pearl then we moved on to tickle and tk explaining the some of the advanced concepts in tickle and tk now we are actually going to Python we already finished a couple of lectures I hope you remember I just wanted to recap what we did in the last lecture. So we went through the very basic stuff which is essentially how do we initially write a Python program basically for just printing something and then we talked about so in this one we have covered several topics like variables how the variables are what are the different types of variables how do you actually print them how do you actually input every input some value into the variable and what essentially are the variables so we covered several key things and then we also went through like some operators what are the keywords that are reserved in the language which we cannot use for variables even like variable name rules that we covered in the last lecture then we also talked about how to be formed and then we went to some conditional essentially make sure conditionals if a let and or else I hope you remember those things basically when we talk about that then we also talked about functions the functions are essentially the starts with the DEF as the keyword and then followed with function name and then we go into the functions one thing we also talked about in that context is the scope of a variable how the scope cannot be cannot go into the function itself and how Python actually even if you put the same name Python actually adds a new variable and then assigns values to that variable and then again the scope of the variable basically like finishes when it's only the extent of the function so when it exits out of the function all the variables are gone it's all like thrown away and see we saw some examples as to how this can this affects your programming we also saw like how to get around this by using this keyword called global inside the function with variable name so that it you can actually pass the variables that are outside of the function into the function and then manipulate them so I think that was a good introduction essentially like what we covered now I think we will go into today's lectures basically I will be talking about iterations so let's go into that so this is the new one that we are starting today so without much I do want to start that chapter on iterations so let's begin so the iteration consists of two major things basically while loops and for loops and then we also have a range function which we will talk about and then we need to also like we can do some controls in the inside the flow so the flow control within the books such as break continue pass and then the loop else so we will we will talk about those things today so first of all the while loop essentially it is repeats a statement or a group of statements while a given condition is true it tests the condition before executing the loop body so once it tests it basically starts running the loop body and then every time the body finishes it again checks for the condition the condition is still valid if the condition is true it goes ahead and then does that move until the condition becomes false the for loop on the other hand that is actually it is execute the sequence of statements multiple times and abbreviates the code that manages the loop variable so the follow-up is simply like I mean say like okay I want it to be n number of times or m number of time and then basically like I mean it the loop variable can be abbreviated to be a some fixed kind of quantity one thing with both these conditions are basically like there are nested loops basically you can use while followed by a for followed by while so these kind of things can be like nested so once finishes then this will finish then this whole loop will finish now the definition for the these flow control essentially the break statement this terminates the loop statement and transfers the execution to the statement immediately following the loop so whenever this break condition basically that is satisfied the loop completely is broken and then it the flow control just goes to the next statement pass to the loop the continue is more grateful in the sense that it causes the loop to skip the remainder of its body and immediately retest the condition prior to the refrading so say like I mean you have one and then you have XYZ then if if a equal to be and then you have break and you have again CDF and if this is your while loop then this a equal to be happens and then immediately like the loop is broken and then the condition is retested and goes back to here in the break actually it falls through and then goes to a next statement it goes to that whereas in a continue statement it breaks and then goes back to the top and then it tests the thing and then it goes around the block now the past statement is this is used when a statement is required syntactically but you do not want any command or code to execute so if you ask put an L then you can say like as which is basically if a is not be then it just goes to see yeah so this is basically like it just for the syntax purpose is the path okay so now let us look at some of these conditions so here example of a while loop is define is one while I less than four all in this a print I and then I plus equals to one you know that this is the increment operator that we studied earlier so the output will be like since I is one it becomes one and then here I get symptom 2 and it goes back and gain I less than four that is still true so it executes so it prints to and then it adds one more so I becomes three and then it goes back and now still this condition is true so it goes down and it prints three and then now I equal to four I becomes four then it goes to this loop and then it is basically I smaller than four it falls so it just comes out so it only prints one two and three okay in example for far essentially so we specify for I in range three this is basically it is the same as 0 to 3 and then we say print I and then at comma essentially like these are the two things that you want to make sure so basically like in this case I is printed as 0 because it is a 0 it starts with 0 1 and 2 and then when it is 3 it is out of the range so that it stops printing basically like I mean it increments and then when it becomes 3 it is the same for it goes outside so one of the key thing is this range function so here is the definition range n returns the list of integers from 0 to n minus 1 so here it returns actually like it is 0 to 2 if you specify one additional number for example here it is 0 10 2 this returns in an increment of this inside this so the incremental in incremental is already built in into the range so this will return 0 2 4 6 and 8 because it starts from 0 to 10 in it with an increment of 2 that is how we should read it so if you do not specify then it assume that it is 0 through n with an incremental of 1 incremental 1 so range this can be made into a shortcut as range n for anything else you need to specify all these numbers so here is much more complicated the loop so while some statement you can also define like for some item in object now there are some statements and then we do a test one and then cause it to break so when test one is true it basically goes and then goes outside now the test 2 basically it is a continue so now when test 2 fails it basically goes back or test to passes it goes back to the top of the loop and then we also use a passage and this is a test 3 so when test 3 is true it does nothing and basically like control so here it is actually an else condition so now let us look at the loop else the L statement after the loop is useful for taking care of the case where the item is in component list for example here search items dot py so for I in range 3 this 0 3 1 if I equal to 4 then the print I found for and then break otherwise print do not care about I now obviously like I mean in this case I will never before so after the loop after going through the three iterations it is going to exit the loop without anything because here basically it is like it says I do not care about and I so do not care about 0 1 2 so those things kept printed out by the statement but now we have to also like print out so so basically like I mean for this particular loop if there is an else basically then you can actually like anyone's never hit the break then we can actually let do something so here we put that break so if it does not hit the break in the else it's printed and that says basically I searched but never found for so this is the example for using the loop else which is an interesting command for in Python in in more details so one thing is basically like I mean you can use the for loop with collection data types the collection data types are explained in the next chapter and this can be iterated through what are known as each of the iterables so for example here the simple one is for name in this particular collection so and if the first letter which is denoted as the name 0 is M then we say print starts with M including the name otherwise the print name does not start with M so this prints or what is M and Mika it prints first one and then for Ryan it prints the next one so we will we learn about some of these lists and strings on the later part of the this lecture so now let us see what kind of what is parallel traverses for this we use another command called zip for doing the parallel traversal so what is the parallel traversal use things together we say like I mean A B for AB in zip these two lists A and B we can say like print a then the star B is a times B so now what Python does is it groups these two together these two together so that is where we can traverse two lists in parallel this is all very useful because when we have like coordinate systems where you need to come up quickly with an addition of two coordinates you can actually like do it in just one command rather than actually spending multiple so here the output is going to be like 4 10 and 18 because it is multiplying 1 times 4 and 2 times 5 and then 3 times 6 so that is pretty much that covers on the iterators and how we can perform iterations now we will go into the strings as variable type so we will talk about the string basics the escape sequences slices is another key concept and then we will use we will do some block codes some formatting and then the string methods strings are delimited by single or double codes there is both of this we saw in tickle one thing is basically that Python is actually uses Unicode so strings are not limited to just ASCII characters so Unicode has much more characters than just ASCII characters ASCII characters amounts to probably about 65 of them so here we can use much more on that the empty string is denoted by having nothing in between the delimiter so here single code delimiter nothing between that that is an empty string we can access the elements in the string with the square brackets the indexing starting from 0 so if you say like I mean say string and then the third element actually it is 1 0 1 2 3 so that is k a gets printed out and then the other thing is even the strings are immutable we thought this for the variables variables are immutable in the sense that we cannot change the value of the variables here is the same thing the strings are immutable so that you cannot assign a snake 3 to P and except that that work because make three is always okay to access the last element of a string we can have two ways basically you can for example in this if you want to access this one that is a 0 1 2 3 4 5 you can say this makes 5 or you can also say negative 1 so the minus 1 gets the last element box and then the negative indexes work backward so this is minus 1 minus 2 minus 4 minus 5 so they work this way then positive so the strings actually also have the concept of being raw string and usually the raw string starts with the character are before the delimiter so if you say like R then the delimiter some string that is raw string and they are interpreted literally so there is you cannot change anything there now you can convert any numbers into the string and vice versa for example we can say like in of string 42 that is the actual value 42 and then string of 20.4 now appears within the codes you can also compare strings with is equal operator that is the W equal to this is just like in C and C++ so we do not have to write specific we command for the strings in Perl we saw that actually like so to write that basically we just say B and then that depends true now a good thing in in Python is that Python provides the way to increment the order add two strings or append the string to another one by just using this addition symbol so if you do a Chattanooga plus Tennessee now and we want to print the location which is things like with the space now let us look at some of the escape characters and what their meanings are so the escape sequences so the backslash is a very common one that is basically for escape and then the meaning is just backslash so backslash backslash should be this backslash so that is so as the thing now quotation is also like the same so this escape sequence is basically the backslash and if you say like I mean backslash n is for the new line and then backslash t is for tab now backslash n and ID is the Unicode database ID for that particular thing now we can also the represent the 15 bit x by just going like escape under under escape and then lowercase you followed by the extra decimal number if you specify an uppercase followed by extra decimal number it treats that as a Unicode 32 bit and then if you specify like backslash x and then H H and that is the hex digits notation or values for H H and then backslash 0 if you null bit so what does in industry and then if you just specify like a n and n this is actually octon notation that is within n has to be from 0 to 7 in octal that is how it is then some other sequences are also supported basically for example backslash C X is a shorthand for control X and then there is also like a vertical tab that you can instantiate which is backslash instead of backslash t only like the plenty of other characters because it is a Unicode it has a rich set of various characters now let us talk about the block codes we can use triple codes to represent multiple line streams so here we have a triple line triple codes then basically like all the way up to here this entire thing is treated as just one string only so thank you Lincoln but it works because of triple codes yeah now we talked about the concatenation operator before I go into the string there is also a repeater operation which is essentially the star similar to tickle so if you say like my name and still then it is basically my name so one thing that I also want to want you to understand is we start talk about this things you can actually like specific then my slices so you can have like a small slice of the 0 3 which basically is here it is a SNAK so those kind of sliding is also possible now let us go talk about the string formatting and this is a percentage reading so replaces this basically hello here so it becomes hello welcome to Python the formatting actually creates new string because as you know the strings are immutable so you cannot change it so the format actually creates a new string so a printf style formatting also works we can format multiple variables into one string by collecting them into a triple so basically a comma separated list and after the percentage character so here we say basically the grade for percentage s is percentage 4 1 s so here we can actually see how we will get translated so here basically it specifies Tom and then this number gets converted into 4 plus 1 so that is 76.1 so basically like I mean it takes and then based on the format string it form of it so here it is 4 digits prior to 0 and then 1 after 0 so it formats it as 76.1 it rounds it the string formats and also refer to dictionary keys so again here percentage name so got a grade something and the name Bob grade a 2.5 and then again it is a integer so it basically converts it into 2 so some more formatting basically percentage C is for character percentage s for string conversion we have str prior to the formatting so we write as str and some number and then basically like that is formatted percentage I is for signed decimal integer percentage D is for unsigned decimal integer and then percentage uppercase x is for the hexadecimal and then the lowercase x is for the lowercase lower integer or lowercase letters which are used inside the hexadecimal so if you want to you if you want to format this into a or say like 15 the first one which is the uppercase x will format this as uppercase F and then whereas lowercase will follow so now let us look at how we can step through the string we already saw this some of those stuff so we have to treat the string as a collection of characters so it has some property in common with the other collections like lists and tipples so we can step through this for example as you can define like C in the string print C that prints one that one character at a time because that is what the C takes the value off and we can also do some checking basically like is a in snakes then the self-consess true so now let us look at the slices in some more details so here we can actually give snakes one column three that returns basically the first two actually like this second and the third that is in and here actually like both are optional basically if you specify just a and then without anything it just provides a copy of the string okay now let us talk about the string method the strings again it is a class with many built-in methods these methods create new strings that mean that create the new strings that need to be assigned basically so you need to assign a string variable to these methods basically or using the method so it goes into that particular variable and then the strings are immutable so you cannot change basically so you cannot change in place that is the reason why you need to assign it to a new variable so let us look at some of these methods basically and these are all built-in methods to manipulate strings so the first one is capitalized this capitalized basically it capitalizes the first letter of the string so it capitalizes first now what about center the center actually has an argument with you can also provide another argument called fill character and what it does is it returns a space added string centered to the total width columns this is total of the width columns so whatever that width that we specify and we can actually specify this fill characters if you do not specify it is a space otherwise you can it can fill with whatever that is so it treats the string it expands the string and puts those fill characters in the middle and it expands it to fill the width essentially so this is another nifty command now we can also say a count we give like a substring a star in a start index and the end index basically here it counts how many times this substring occurs in the string or in a substring of the string if starting index beginning and in the ending index and so these are two optional ones so if you do not specify and basically count substring it just sees how many times the substring occurs in the string if not it actually now gives basically like I mean it it also meaning like if you are giving both the start index and the end index it starts from that start index and the ends at the end index and look for the substring so it takes a substring and then it massages that for to find the substring specification inside the count then we also have fine and the find it also again a substring of start and then end these two are optionals the find again if it determines if the this particular substring occurs in a string or in a substring of the string starting from start to the end and it returns the index if found otherwise it returns minus 1 so it returns index then this thing the substring is occurring or minus 1 then there are other other ones also you can also like decode and encode strings we can along with this we can also see another one which is n-suit which is n-suit which has a suffix beginning and ending this actually determines if the string or the substring of string ends with the suffix returns true if ends with the suffix or it returns false otherwise is digit again it is opposite of that basically where it returns the true if the string contains only digits and false otherwise is lower is basically if at least at least one case character and all case characters are in the lower case and then we also have ease numeric is another one so that is another one and then the join is basically to take multiple sequences and join them so and there are others also so we also have another method on the replace it really does not replace again go back to your first principle which is strings are immutable but it makes new strings with the replacement performed so here is an example we define as a variable string variable a with a b c b e f g and then another variable B which is now we are going to call it as a dot replace C with uppercase C so now if you want to print B it is basically a b uppercase C B e f whereas the a is simply in CDF okay so and then there are more examples of the methods so I will go through this one so here we can actually like say that basically so the we define a variable a this whole sentence and then now we say like I mean find essentially so we define the location to be a find boring so right here so whatever the number of characters to this that stored in the location now what we need to do is you should add that whatever the location we add fun to it and then now we print a actually like here it should be the replace command essentially we can we have to replace this with nothing and then add on to it and then we define another one B with this and then we say like join our structs motorcycles so the and they supply to join these the different strings so it becomes cars and trucks and motorcycles then if you say like I mean so now C is defined as a split of B or B dot split and we print C it prints each one as said in the element in the string list or string collection and then if you specify again like I mean the split the string B but with this particular string so then it takes out that string and only reports cars trucks and motorcycles so here we are using like several things find there is also like replace then and then join and then split so all these are different string methods now let us talk about the regular expression the regular expressions are a way to do pattern matching the basic concept is pretty much it is the same as Java or Perl that we already are we are used to this the common regular expression in a syntax the dot matches any character but new line then the carrot matches the beginning of the string and then the dollar matches the end of the string so these are the things that we already know from Perl and we carried over to tickle also for the same sentence and then a star is any number of whatever comes before this so extra will be like X X X whatever and then plus is essentially like only one item so it is a n plus or this will be a n that is only those now there is also like the R function basically which means this it is either the first string or the second string is not either one of them and then when we escape W that is basically like any other new characters and then escape D becomes any digit and escape S becomes the non-white space and then uppercase is our just exactly opposite of these so that you just have to just remember that so uppercase W matches non-alpha B non-digits and then uppercase S is a non-white space and then if you specify it in the square bracket basically E I O U it matches any of A E I R I O R U so any letters it can match but if you do not specify the square bracket it exactly matches both string which is jump so some reg X examples essentially like I mean so here this is basically like we are seeing import RE stands for the importing the regular expression model so once we export the regular expression model so we specify basically like in file we are opening this text test or text in a read mode and then we for the lines essentially like I mean we just start reading the line with this method read lines so just watch out I will be talking about some of these things basically like the method but this will be like coming in the future but this is again another method you can see read lines is a method that is applicable for file structure and then that is why we are calling the in file and then and then once we read all the lines we just say before now we can actually replace all PCI 3 with PSI force so the way that we can do is like we can say match string the regular expression compile so this is the method for the regular expression and then basically like it is a raw string that is specified as PSI 3 and then we also specify another method the ignore case in RE so it ignores the case and it composes and then for the line in the lines the lines that we read here we use the match string and then we say like sub R PSI 4 and then the line and then we print that those lines so basically like I mean this means that we read all the lines and we replaced any anywhere where we can see PSI 3 with PSI 4 a couple of things about the that example we needed to compile the I mean first of all the regular expression module needs to be imported that is the using the import command then we also need to compile the regular expression with the RE compile and then the R character is means the raw string that we already saw that basically so we do not have to escape every special character and then ignore case is another it is an optional argument another would be like dot R basically which is like it matches all the strings essentially we also have another one called the multi line which is REM which makes the carrot and dollar match after and before each line in the string so now let us talk some more about the regular expressions the regular expression compile is an optional step it is more efficient if we are doing lot of regular expressions we can also do this regular expression search and then very extend the subject basically or even regular expression match that is also we can do that is a alternate syntax for without doing the compile and then the match only looks for match at the beginning of the line it does not need to match the whole string just the beginning the search attempts to match throughout the string until it finds a match the find all reg X the subject return the array of all known overlapping matches so alternately we can do basically like find filter with reg X and object and the subject and then the other thing that to note is also that match search find iteration find all they do not support an optional third argument for reg X matching plans it start reg X with the question mark one customer guess more time of the phone so now we come to the split command so the reg X split basically returns an array of strings so we saw that basically like how we can split the strings are all the parts of the subject besides the parts that much if two matters are adjacent in subject then the split will include an empty string so here is one example like I mean lines set as a upgrade PSI 3 to PSI 4 PSI 4 3 is and was an excellent program so we here we say like match string PSI 3 we get a match and then for each of these match strings we just print I so so the result will be this much and then it gets this much and then it also splits here so this is also gone and then here so now you can see how the answer or how this I is being printed basically prints upgrade there as one then program to PSI 4 is one and then was an excellent program is the third one so let us actually continue the remainder of the strings in the next lecture so there are few more things that we need to cover before we can go into the next chapter okay thank you very much