 Hi everyone again welcome to this lecture of the Linux programming class today we are going to actually talk about some examples on Python we went through an extensive study of Python in the last several lectures so culminating in the CGI programming essentially so today we are going to just look at some of the programs how to write it in Python and then I will just go through the examples and give you some hints and then pretty much again all that we have already learned and then if any have any more questions these can answer those questions so the first one is actually a program on the conditional for example the if statement here so the here we are going to actually like I mean all these programs are here in this particular website you can go and look at it these are basically the Python or Pygame programs essentially and then in this particular program basically we have like several variables so the variable a that is defined as 4b that is defined as 5c defined as 6 and once we have these three variables defined now we can do some basic comparisons we can see that actually so the first statement is if a less than b then we just print a less than b if a is greater than b then print a is greater than b so in this case which one do you think will be printed out so obviously like this so we should look for this one is less than b and then if a is less than or equal to b then it prints out a less than or equal to b and we have all these things defined here so let us see how the program actually outputs now let us say scroll a little bit down so if a is greater than or equal to b then it prints a greater than or equal to b so simple program and then you can see that basically like it follows our original convention like this is the expression and then the pollen actually separates and then there is an intent for the print statement and then after that the print actually is there so that all the intended ones are part of this statement now the equal equal double equal to and equal the tendency to mix them and you know that actually like equal to is an assignment and the double equal to is a logical check essentially so that is what is in this comments and as you know like I mean all these things should be some and strict that comments as well so if a equal equal b which is basically like a logical check whether it is an equal to then print this and the not equal to is bang then equal to if it is not equal to then it will be this now if a is less than b and a is less than c then it so then a non-exclusive is a less than b or a less than c then print now there is a Boolean data type which is a equal to true and then this is actually legal so again you can do this assignment a true and then if a then print a is true which is what it is going to be and then if not a then print a is false so now if a is equal to true and b is equal to false and then a and b then a and b both true now we make another false tumor attendance equal to 3b equal to 3 and c equals a equal equal b so now then print c so now let us see what that will output and then as you know like I mean in Python a 1 is actually a true value so 1 print 1 a print a so anything that is 0 exactly what is false and then we also do this if 0 then print 0 and this will not trigger because 0 is false so this expression is false so now now another comparison comparing variables to multiple values the first if statement basically like here in this one it will work but it always trigger as true even if the variable a is not equal to b because b itself is considered true just be alone so a equal to c if a equal equal b or b then print a equal to b maybe so basically like even if a is not equal to b this this will be triggered because or b is basically like all these two normal here the proper way to put it is basically like we need to say b or a equal equal the lower case b then it will never get triggered so now the other one basically the temperature equal to int input what is the temperature in Fahrenheit so now you know that actually like input is query to get a value from the screen and then this is the question and essentially like I mean whatever the number that we type in it is converted into the integer data type and then that is assigned temperature and then if temperature is greater than 19 then print is hot outside and then it prints done so you see basically like again this goes with the print statement goes with the if and then the other print is actually outside so whether this is true or false basically this print done will always be performed and then there is an else class that you can define so and else it is also like all in and then print it is not hot outside and then again print down now the else if statement which is the third category this is also like we saw essentially here we find the eighth statement then the L6 statement and then finally L16 and here note that actually the L6 form after the this is the second conditional which is the temperature less than 30 and then we need to put a column after that so with after each of the expressions we put a column to denote that basically that is the end of the expression and then the remaining ones are the block of code that needs to be that now the ordering of statements basically so temperature basically this is the one and then it's hot outside which is greater than 110 then oh man you could fry eggs in the payment so what is the mistake here if you look at it basically give it is greater than 90 it says it hot outside then only if this condition is false then it will go into the next one so if it is greater than 90 it will print this and then it will exit out so this condition will never get executed so in this one the proper proper ordering should be like I mean this should be the first one then that then the 30 and then it's okay outside and then come out so let's see what what all the things basically so the first one we saw that it's a is less than B so it prints out just a is less than B does not print out the actual values itself even though we have signed a s4 B s5 etc a difference a less than B that's because we didn't put the values as an output value so in the same thing how do we make sure that these things can be output as values for that we need to make sure that these are like the percentage these and things like that and then and comma after the codes we need to specify a and B so that the values themselves get printed so this is all the the various command output I want you to go through this basically along the this particular all the program this program pretty much in many exercises if then sorry if else it else conditionals pretty well in including all the different expressions as well gives you a good understanding of all the logical operations that you can perform so now let's go to the next one which is the program to calculate the kinetic energy so first of all what is the equation for kinetic energy for a moving object so basically we compute it as the mass times velocity squared so and then times half basically so half times mass times that's the equation for updating the kinetic energy so we need to take the input as as the mass and this is a floating point number then we also need to input the objects speed and then we will say basically that's that's the units we define basically these are all in the MPS units of the SI unit essentially you are familiar with those kind of terms so the SI unit basically the mass unit is kilograms and then the the speed of the velocity unit is in meters per second so in this example we start by saying that basically okay what is this program do like just a documentation line which is this program calculates the kinetic energy of the moving object so now we define a variable M string and this is the floating point and then this is where we input the mass of the body basically so enter the objects mass and kilograms and then we leave it and then we call it as an input and then we assign it to the float data type so now this basically this results in the prompt and then somebody has to enter in the screen a particular number and then that is getting a but it's assigned to the string in the form of a floating point number let's not worry about the comments here the comments is essentially like a if you miss this float here you can always have this as a separate one so that's what so much stands for now to get the speed of the this particular object we define another variable called V string and then V string is also a float and then here we get the speed in meters per second and then once we have these two numbers you can simply calculate the kinetic energy as times mass times velocity squared and then now we need to just output this one in the form of a string so see that basically the ease currently supporting point we need to convert it into a string in order to display this so to convert that into a string we basically use the sdre function and then here what we are doing is basically like print first this particular phrase the object has and then we add the string e and then we also then print the remaining ones which is the joules of energy so here one simple example enter the mass in kilograms 30 object speed in meters per second 5 and then outcomes basically 375 joules of energy and this is computed we see we have 10 30 15 times 25 and then that's the pretty straightforward you can also construct this in a different way where we can say basically like the object has and then the percentage B or percentage F kind of formatting string and then comma directly do the e and then close parenthesis and that will directly print the value as a string as a floating point number itself it's a need not convert that into a string and then put the value out and then you can see that actually like it is converting it into a string because I mean the it's actually computing in the floating point because these ones presumably can be an integer but you know the output is actually like 300 upon 0 and then again if you don't specify this as a floating point and instead say integer here you are still going to get a floating point number for the energy can you tell me why because if you remember actually like this is the same thing that happened even in tickles the starting number is a float and then all the remaining ones are automatically at this to the to that particular data type in order to compute that number so even if we take the M string as integer it will still result in the same thing the only thing here is we cannot put this as a string itself we have to take this as an integer or a floating point number so I would I would urge you to try actually like change this float here to INT and then this float to INT and then try this program it should run exactly the same way okay so now we go to the next one which is a program for a simple encryption so the encryption essentially is we change the value of a text to a different one and the kind of input the the text essentially so the there is a simple algorithm here we will probably use this we have a plain text which is this is the the phrase that we are going to encrypt so here you can specify any anything basically for testing purposes but make sure that you use the encryption and then the decryption followed by that in order to verify whether your encryption program is working correctly so the plain text is a string which is this is the test ABC ADC the interpret text we define as an empty point so quote quote which is you know the empty string now we start reading each character at the time from the plain text so for C in plain text so we define X is the ORD of C essentially and then we essentially like we just say basically X is X plus 1 and then we dump out a new character which is the character X basically and then the encrypted text is nothing but basically we just add this C2 to it to the end text basically append it one by one and then once this loop finishes we just print out the interpret text so let's see how this works so again the the particular text is essentially like this is a test ABC ADC so the ORD function as you know basically just gives you in the al in the alphabetical order where this particular character is and then this is plus one is basically the next character is what it's going to give so this becomes UI JT which is U is next to T I is next to H J is next to I and then T is next to S and then wherever there is a space basically the next character is the bank so that is what they spent it out and then where is the full stop is the slash the next one slash so these are all the ASCII characters from the ASCII text so now this works and then we have some encrypted character set now let's do how do we decrypt this one it's fairly simple we capture this one now this is our new string and then we go for the odd of this one and then just reverse this one that is X is X minus one and then we create the character set based on that let's see how the program works so as I mentioned we have the interpret text then we say basically we go and find out the odd order of particular character and then we basically say X is X minus one and then we build the characters act and then we output that so now when we decrypt this particular string we get back our original string which is this is a test ABC I think this one is pretty interesting and pretty straightforward you can actually try with new stuff and in fact what you should do is you should put both these programs into a single program and instead of actually defining this as a as a string you should get this string from the command line and then you can have fun with it as to you can first output the input with string that is print the input with text and then later on you get you yourself can decrypt that and then print the decrypt string and then something else to try is basically how do you instead of the encoding algorithm as the X is equal to X plus one try new ones basically meaning X equal to X plus five or something basically and see how that works so these are all like some of the fun things that you can do with this program so I want you to experiment with it and then so that you feel more comfortable running the Python programs okay so the next one is a program for exception handling this is something that we saw like I mean how to handle the exceptions in fact the same thing like the try and accept where the commands that we saw so here we have to import this particular package the system that the we also like learnt about how to import this in pretty much like last but one lecture I think so first we will find out what the divide by 0 error is so we define try and then X equal to 5 divided by 0 except now we say print error dividing by 0 and then we give also like a exception info basically like 0 so we need to print this particular EXE info 0 1 so we will see like how that gets printed out and then there are some more things basically like a valid number conversion so if you say like int thread and that is assigned to X so here we say basically like error converting thread to a number and then we also like use another system exception and then so number entered equal to false while the number entered equal to false number string is input entering integer so here we are we are actually like converting that number string to an integer and then number entered equal to true and then if it is not then we will say basically like error in valid integer and then opening file text of error is equal to open my file dot text and then basically like if that file cannot be opened then it prints out error opening file and then all these things can be combined basically so if say basically the f o if we go to open my file text as equal to f dot read line so you know about this object and basically it's method the read lines method and then I equal to int f dot strip that's another method and then the X is equal to 101 divided by I now you can print many of these X of scenarios I your value error 0 division error and then and someone expected errors so now to generate exceptions essentially like this is another function that we are defining basically get input and then user input is input something and then if the length is 0 that is like a form user is not entering anything basically then we just say I your user entered nothing so now let's look at what are the outputs so the first one we actually get the divided by 0 so the first one was the 5 divided by 0 for that we get the error dividing by 0 and then also it since it be also like when to the system info system package for the particular exception it gives the exception info has 0 now when we try to convert thread to the number we get the value error as mean so when we try to enter the integer integer number then try to convert that into an or we ask the user to enter so then user enters 5 and then this is essentially like I mean it's basically it goes through basically because this is there is no error so it's a valid integer so it goes through if you put anything something invalid then it will catch the invalid integer error now the opening the my file dot text essentially that's not there so basically it just brings error opening file now these are the things where we had the multiple ones essentially so for the opening file basically like I mean before that actually the so the IO error so in this one essentially like so we had multiple error cases and the only thing there is essentially when it is reading the line it has nothing basically so it is more like an IO error that what gets output but it could be so if there is a value and then the value is 0 then we could have gotten a 0 division error and if something else has happened then basically it can also come out with an unexpected error now the next one is basically like we say look into something which is 7 and then and then this one basically like and the user input length is 0 and then it can give these kind of errors basically then we try to run the get input so that's a brief look in the exception generation now the next one is program on the on lists so let's see so this particular program is taken from the Google's Python class so again you can go into this but the website this is this gives you the Python class and basically like the several examples there so we will talk about few of them so here the main thing is the first one that we are going to look at it is matching the ends so given a list of strings return the count of number of strings where the string length is 2 or more and the first and the last characters of the string are exactly the same so one thing to notice Python does not have this increment operator of that is plus plus it only has an incremental assignment which is plus equal to so keep in mind about that and that is what we will use so this particular function is called match ends and then the arguments are so we define a counter essentially 0 and then for word in words essentially for each of the words we basically see the length of word is there than or equal to 2 and if the first one is not the same as the last one last one minus one actually so we already studied this these notations like word 0 represents actually the first element of the character of the string and then word minus one represents one from the the last one and then the last is just specified as last actually like yeah this denotes the last character essentially the minus one so if this is the same then we increase the count and then we found our word essentially so we just return the the count for how many such things can exist so we will see like output output of these 10 programs later now the second one is actually it is called front underscore X which is the list of strings return a list with the strings in sorted order each except the group all the strings that begin with X first so if I have like mix the XYZ apple X and do our work it results in X Y Z our work Apple and mix so here the hint is basically like we can do it by making two less and sorting each of them before combining them and do you know like I mean so one list is the all the words that begin with X and then the second one is the regular sorted one and then we can actually so so the way we will do it is basically all the things so it is actually all the things that start with X and all the things that does not or do not start with X so those are the two lists that will move so it is called front underscore X and then again we will pass the list of words so we say basically X list and other list those are the two lists that we maintain so we define them as the empty list and then if the W starts with X basically so we know that start with method which can be applied to this W object which is the word inside the list so then we basically happen this word into the X list it is not true then we will append the word to the other list so simple basically so X list dot append W L other list dot append W and then once we have this then we just sort the X list separately and then sort the other list separate so what we return actually is sorted X list plus sorted other list so again very simple as you can see basically was only like one two three four five six seven eight lines and then we could achieve those things now the next example is to extract the last element from a tuple use for custom sorting so the last element is denoted as we know that it may be one so we basically like get the last one and then that's it pretty much when the next program is basically to sort who last essentially so given the list of non-empty tuples return a list sorted in the increasing order by the last element that means basically we have if it is one seven one three three four five two two then started on the last element which is two two one three E three four five and one seven so here the way that we will be using is basically we are using a custom P equal to function to extract the last element from each of the two so let's see how it is again it's just the two lines so sort list tuples is the function and then basically what we do is sort it tuples and then where key equal to last very easily so now let's see the next one which is this function used in main to print basically print what each function returns versus what it is supposed to return so we define this function called test got and expected those are the two things if God equal to expected then we will prefix with okay else we will prefix with not okay which is x so here we say basically like print x percentage s got and percentage s expected and then we define this basically like this prefix and then what is being kept and then report what we get and what we expect it in order to test all these functions that we defined earlier we will give like n number of programs and then basically we will call this program multiple times with various arguments and then test whether this holds good so the first one we will be testing the match ends for that we give like various so various strengths basically like a list and then we test it out and then we will apply various scenarios to test it we will also test the front text here and then we will also test the thought last one so for the match ends pretty much we got perfect so let's look at our test vehicle and see basically what we can do the thing so the first one is actually it identified three such matches so can we identify three such matches one is okay this is one this is the other one and the third one is here so anything it is over I mean two or more is basically our target and then the first one is equal to last one because that's the so we know that actually in this list there are three and then that's what we expected so when we run the program we actually got three so it is a match now in this one actually our thing falls into like one two three these three elements but out of this only two of them are correct so we should get the expected as two and then the last one there is only one that is really good on the other ones are not matching so this one we actually successfully tested our little function and then basically three extra three on the program and then that's what now the next one which is the front underscore X so here as we said basically the something starts with X that needs to be sorted and then badly this needs to be appended before it or appended before the other list which is sorted to so here is a string here is the list and then the sorted one what we expect is a a a there is a last element followed by the second to last and then this and then dbb and then ccc so same thing for the next one this is a xc a dbb ccc and similarly for the third one as well so here also like I mean we get okay and then we got the same thing finally the sort last script essentially which is the number of to boost basically which is 1 3 3 2 2 1 the sorted one will be based on the last one which is 2 1 3 2 1 3 and then the same case for 3 and then all the three are tested so all these functions really work so try this out and then try out also like device how we can test those functions so that's pretty much colors most of this one there will there are few more examples that I want to go through and then I will probably go through it in the next lecture thanks