 Hello everyone, welcome back. So, we will begin the next session. So, let us start with strings, quick recap. We looked at the basic language, the basic language features that were useful. Why Python is important and why we believe it is a useful language to learn. I did not go into too many details, but you get a rough flavor for what it is. Then we learnt how to start the interpreter, the basic vanilla Python and then quickly we switched over to the IPython interpreter. The IPython interpreter, the reason we are doing that is because it is a lot more powerful and offers a lot of features, many of which you have already seen. You have demonstrated, specifically we have demonstrated tab completion, history, in fact it is nicely color syntax highlighted. You can also look at documentation by doing question mark. These three features are extremely useful. In addition to this, there are a variety of other features that IPython offers. If you are interested in looking at these details, please look up on Google for IPython and there is a plethora of, there is a huge amount of information available on IPython itself. But we will not get into all of those details right here because there is not that much time. And the point, one thing to be noted is one of the goals of this course is not to teach you everything because everything cannot be taught. We are trying to get you started and get you to a point where you are comfortable enough that you can do a great variety of things and fish by yourself. We are trying to teach you how to fish so you can go and do it by yourself eventually. So let us move on to strings. What are strings? So anything within quotes is treated as a string. Unlike in C, there is no single character or thing like that. So basically everything is a string and you do not have to worry about null terminated and all of that. So basically to create a string, there are variety of syntaxes and the reason for all of this variety is to give a lot of programmer convenience. So single quotes will work, double quotes will work or you can do what are called triple coated strings. So you put three single quotes or three double quotes as shown in the example below. So this for example is a simple string. You just use a single quote on each side. Remember that it is the same side of the quote. It is this, the one on top of the double quote. So sorry, the one next to the enter key. Do not put back ticks. That does not have anything to do with a string. Now this is a string to here. This is actually wrong. This is not a string. It should be, this is I am sorry, somehow I overlooked the error we have today night. Similarly, this triple coated string is three single quotes and as is this double coated string. As you can see, there are variety of ways in which you can create strings. Single quote, double quote or triple double quotes. We look at the details in a little while. So why so many? Because it reduces the need for escaping. So supposing I want to say, pythons, so look at the apostrophe S here. So that is again a quote. So you cannot, in normal C you will have to put a, you will have to escape this quotation mark. So whereas when you use a double quote, you can put a single quote inside as many as you want inside this string and it will still be one string. So you do not have to escape this. Similarly, if I want to say, he said I love python and you want to put that in double quotes, you put the original string inside single quotes. The disadvantage with a single coated string like this is that it cannot span multiple lines. It is only meant for single line quotes. To do that, you typically will use what are called triple coated strings and they can be multi line. See for example, I am typing on the shell SS is the quick brown. Notice that when I did this, it said syntax are a end of line EOL when scanning string literal. Which means it was expecting it to end in the same line. But if I put triple quotes, the quick brown and now it prompts me for more, Fox jumped over the lazy talk and that is the legal string. And notice, it stores the new lines that were actually added. So this is extremely convenient and it is typically used for documentation that is supplied along with the code itself. So let us get little more, do more interesting things. Now with strings, again assignment is straightforward. So you can assign a variable to a string. Here as you can see A is hello, B is world. And now you can do simple arithmetic. So it does what you expect. C is equal to A, so that is another string plus B plus exclamation mark. So what this does is it creates a new string that basically has all of the information. Sorry, it is distracted. So C is basically the concatenation of A which is hello, space, world, finally with an exclamation mark. So please try this and see what you get. You should get hello, space, world, exclamation mark. So the plus operator basically does concatenation. Now what will minus do, feel free to experiment on the interpreter and find out for yourself. But before you experiment, think for a second and try to see what you think it should do. Strings can be multiplied with numbers. Basically the idea is it becomes very easy to multiply a string. So supposing I want to put 20, I want to put a string of stars. Sometimes you want to print something like this. So if I say star into 80, it will give me 80 columns of that string. So sometimes it is useful. So if I want to put a line like that, it is easy to do that. So the point is in this case again when I say A star 5, it basically means A plus A plus A plus A 5 times. Now what do you think should happen if I do A star 5.5? So A is hello. So A star 5 will be hello, hello, hello, hello. But what about 5.5? What do you expect? So if you try it out, you will find that you will get a type error. It basically says it does not know how to multiply a sequence by a non-integer. Therefore if it makes sense typically it will happen. If it does not make sense it is not going to happen. And if you do not know what it is going to do, please feel free to try it out on the interpreter. As we said before accessing the elements is as easy as saying A of 0, A of 4. And now notice these two things that I did not cover in the past. So any sequence you can also access it with negative indices. The idea is, so let us look at A. A is hello. So hello A of minus 1, what do you expect? It will give me the last element. So minus 1 will count from the rear. Minus 4 will be the fourth element from the rear which will be O L L E. So it will be E. So indices now can be accessed both ways. Remember what would happen if I said A of 100? It will give me an index error. It will say string index out of range. Similarly if I give it minus 100, same thing. It is going to give me an out of range. So you can access the elements of that string but only the ones that are inside the string. So the indices have to be valid indices but can be either positive or negative. Now we want to change the elements of the string. You might think that I can say given A is this, I want to say A of 0 is F. You cannot do it because strings are considered immutable. Something that is immutable basically means that it cannot be changed. You can manipulate a string to produce a new string. So when you do A plus B or A into 4 or A into 5 or something like that, it is creating a new string. It is not modifying the same string. So Python has two classes of objects, two types, two broad classes of objects called immutable and mutable. Integers, floating point numbers, complex numbers, booleans, tuples, all of these are immutable. Strings are also immutable which means the object cannot be changed. You create a string and that string is basically fixed. You can create a new string using elements of that string or you can manipulate the new string, the string but all of those operations will produce a new string. They will not change the same string. That is a design decision. It is always like that. So questioning me about why it is immutable, what is the reason for immutability is beyond the scope of this class. The point is strings are immutable. All you need to remember is that and it is a design decision that has been made by the benevolent dictator for life and it proves to be a very useful idea. So the upshot is we cannot change the string in place but we can create new strings. So now let us start with a little problem. The first thing you have to note before we solve this problem is that strings have methods that can be used to manipulate them. So what do I mean by methods? So if you pay attention to the terminal, A is a string. So type of A is str. If I do A followed by a dot and hit the tab key, you will see a huge number of completions. To go through these completions, you press space and it will show you all of the completions that are available. For example, there is something called A dot. If I do UP tab, it is called upper. What is upper? To find out you do what you normally do with IPython, you do question mark. So A dot upper, A dot UP tab question mark enter will give you A dot upper. One point to remember when you do A dot tab, you will get a lot of things, lot of completions and it will say more in the bottom. So this is called a pager. If you want to quit this, simply press Q. So when you do tab or you do question mark and you get more than one page and you want to quit, you press Q. Is this clear? Now if you do A dot UP tab, you get upper. You do question mark, hit enter. It will tell you that this method returns a copy of the string S converted to upper case. So let us try it, S dot upper. So to call it, you close, open close parenthesis and it says S dot upper is the quick brown fox jumped over the lazy dog. And please notice that S itself has not changed whereas S dot upper returns a new string. Is this clear? So these are called methods. So every object in Python has a variety of methods. You would have noticed that on S or A dot tab, there are so many underscore fellows. This is a double underscore underscore underscore add underscore underscore class underscore doc underscore eq. You should ignore all of these underscore methods. In general in Python, if any method has an underscore or anything has an underscore, it is meant to be a secret method or it is meant to be a hidden or a private quantity. And as all decent people are, you do not touch other people's private parts. Same way, you do not touch the private parts of any object in Python unless you know what you are doing. So given this information, feel free to experiment on your interpreter with various methods to find out what methods do. Again, the key is to press question mark. Anyway, so let us get to the problem. The problem is we are given a list week. The variable name is week. It contains the names of the days of the week and a string S, which means week is a list containing seven strings. Each string is like Sunday, Monday, Tuesday, something like that. So it will be sun, moon or whatever. Now you are given some other string S and the string is a Saturday, Monday, Tuesday, Wednesday, all of this is maybe there in this string. So the string could be any one of those. We need to check if the string happens to be a day in the week. We should be able to check for any of the forms. We should be able to check for SAT, Saturday. We should not worry about capitalization. So what we have to do now is we have to write a little line of code to perform to solve this particular problem. So I will repeat. We have week is a list, so I will type it out or something like that etc. So I am just going to close it for now. So I do not want to type out all of this and I am given some S which is say Sunday. But week need not be this. Week is something you control. So week is a variable that you have created with a list of seven strings. Given the list of seven strings and given any S, so S could be either Sunday or S could be Sunday or S could be Sunday or S could be whatever it is. I need to know whether that particular string happens to be a day of the week. So how do we solve this problem? So obviously the easiest way to do this is to use only the first three characters of the string. Secondly, we have the issue of dealing with all of these cases. We could have capital, we could have half capital, we could have wrongly typed capitalization. All of that we have to worry about. So what do we need to do? We need to just convert everything to lower case in which case everything becomes the same. We do not have to worry about it. Finally, we need to check for existence in the list week. Correct? So we need to check if this S, if the first three characters of the string that are converted to lower case, finally that string we have to check whether it is there inside the week or not. Correct? Is everybody with me here? Now this makes it obvious as to what week should have. I will repeat this. We need to check for all of these possible forms that I have listed here. All of these should be possible. So which means I need only the first three characters but then I need to convert them to either fully lower or fully upper, whichever one of the two I have to convert because then I completely eliminate the problem of the case. Once I do that, I need to check whether that is inside the week. Therefore obviously our week itself should have everything stored in that same representation. So it should be sun, moon, dew, bed. Is this clear? Why are we doing this? Does this make sense? So that is how week should be stored. Now I give you 5 minutes to figure out how to do the rest. First hint is remember that S has several methods. So if you do S dot L tab, you may get some idea but generally do S dot tab, you will get lots of these methods. Week is this, S could be anything. So given this, think for one minute. I will give you 30 seconds, not even one minute. I will give you exactly 30 seconds. You please think up of an answer before we discuss it because you know everything now about string necessary to solve this problem. For example, take this case Friday. So please do not share your answers immediately that everybody, the point of this exercise is, I find that often when teaching it is helpful to do this to think about the problem and then give your answers. So once again the slide is extremely clear. You have to get the first three characters of the string. So please do not send your answers to everybody and share the answer. The idea is that you think about the problem before you share it. Okay, so one minute is up. If you have not gotten it by this time, we will be discussing it in a short while. Let us move on. We will come back to this problem in the next five minutes. So if we take Q in this example, hello world. What do you think Q 0 colon 3 will be? So stop thinking about the problem now. I have given you enough time to think about it. Just wait for a little while. Q of 0 colon 3 will give me the first three characters. So if you go back to a previous slide, it says check the first three characters of the string, convert it all to lower case, check for existence in the list week. So to get the first three characters, I can do it in three different, in two different ways. I can say 0 colon 3 that gives me hello l. I could also say colon 3. When you do colon 3, it means implicitly if you do not specify the first index, it means in this example, colon 3 suggests that you are not specifying 0. If you do not specify anything, it means 0. So what do you think 3 colon will do? Here is the important part. Q 3 colon will give you low world. So Q colon 3 plus Q 3 colon will give me the whole string. This at some level explains why the last index does not include 3. Because if I, this syntax makes it extremely easy for me to do this. If I did not have it this way, I will have to say supposing Q colon 3 included the third index, I would have to say Q colon 3 then plus Q 3 minus 1 or 3 plus 1 to the end, 3 plus 1 to the end. So I do not want to do all this plus 1 business here. Therefore this convention of not including the last index makes such things very easy. So continuing on with slicing Q 3 colon open bracket, closed bracket will give me the end of the, from the third, from the actually the fourth value, fourth index to the end. Q, so now supposing so clearly two things are obvious. If I do not specify the first index, it means 0. If I do not specify the last index, it means to the end. So now it stands to reason that if I simply say Q colon, what should it do? It should give me from the beginning to the end. So let us try that, gives me the whole string. So if you want a copy of a sequence, you can simply do Q or whatever that sequence open bracket colon closed bracket. It will give you a copy of that sequence. Now what do you think the last one is Q of minus, so let us first try this Q minus 3 colon. What do you think this will give me? So before you do something on the interpreter, it is a good idea to predict what you will get. Q minus 3 colon will give me what you expect R L D because minus 3 counts from the back, minus 1 will be D, minus 2 will be L, minus 3 will be R. So what about Q of minus 1 colon 1? That should give me empty. Because from the last element to the second element, there is no way to go. It does not do any wrapping around. However, Q of 1 colon minus 1 is remove the first element, remove the last element and the remainder. So this should give you some idea of how to do slicing. So I have just given you a bunch of examples. So now let us go back or let us go back, let us solve this problem. So we need to get the first 3 characters of the string. So what is our string? So Friday, this is the week. So I need to get the first 3 elements. How do I do that? I simply say colon 3. But unfortunately it is this stupid capital F capital R little i. How do I make it into a lower? Remember that S colon 3 itself is a string. It is an expression that evaluates to a string. So it has a method. So I can say lower. The only problem is you cannot tab complete in this case because unfortunately I Python does not know what that slice is going to become unless you make that into a separate variable. So I can simply do S dot 3 dot S3 lower. That will give me FRI. Now I do S dot 3 dot lower in week. So we have solved our little problem very simply. Please note that I could also do this. S is, X is S colon 3 X dot lower in week. Note that if I just do X dot lower, it is meaningless. It is simply the method itself. It is not going to call that function. So to call that method, you have to supply this argument. You have to supply the parenthesis. Is this clear? So we have solved a little problem and I guess most of you must have got at least some elements of it. So all of this S of, someone had typed S of S dot colon 3 and all that is wrong. So you have to do it carefully. And every element, every part of the syntax matters. So if you make a mistake, the interpreter will shout at you saying error, syntax error. This is wrong. Something it will say. You have to learn to read that error message, deduce what is wrong and fix it. And this is something that comes with practice. So you have to keep doing it, make mistakes, learn. The important thing is you have to think about the error message and learn to fix it. So moving on, so far we have only looked at slicing. What about sliding? So let us come back to a simple example. Q is hello world. So Q of 0 colon 5 is what? It will give me simply hello. So what do you think Q of 0 colon 5 colon 1 should be? It means start at the 0th hello, that is the first H, capital H. Go till the fifth minus 1 which is fourth index which means H 0, E is 1, L is 2, the second L is 3, O is 4, all of those. So Q 0 colon 5 will give me hello, fine. If I supply an additional colon 1, it says stride by 1 which is exactly the same thing. So I should get the same answer. But what if I did stride by 2? It will give me H, L, O. What if I did stride by 3? So now try the next example, Q of 0 colon 2. Now what do you expect? To answer this, first let us answer what happens of Q of 0 colon. We already have seen this. Q of 0 colon will give me the full string. So if I ignore and do not specify an index, it means to the end. So if I do colon 2, what does it mean? It means give me till the end but stride by 2. So H, L, O, WR, WR, D. What about this 2 colon 2, 2 colon 2, double colon 2? It will start with L, L, O word. So it will start with the third index which is H, E, L, first L, skip by 2 till the end of the word. What about this? Doing this example where the mouse is Q double colon 2. What do you think they should do? Notice that in this case we have neither specified the first index nor the last index. So if you do not specify the first index, it defaults to 0. If you do not specify the last index, it specifies to the end of the list including that last element. Therefore this will give me first to the last. So it will be till H, L, O, WR, D. Same answer. How about 5 colon 0 in steps of minus 1? Will this work? It does. So if you want to reverse the string, you can do it in this fashion because clearly the syntax here is clear. It is unambiguous. It is correct. I am saying go to the fifth index, start there. What is the fifth index? H, E, L, L, O, H, E, L, L, O. But remember this is the fifth index and the index starts at 0. So when I say 5, it means the sixth element. So that is space. So start from the space, go till 0 which is the first element but do not include the 0 and then step in the reverse direction. So what if I do not specify the last index? Now it will give me O, L, H in reverse. Is this clear? So basically with this slice notation as you can see is extremely powerful. I can make a copy. I can give you some sub elements. I can strike by some. I can go positive. I can go negative. So I can reverse. All of these things can be done with strides. So strides are extremely powerful and it is a very good idea to master how to do striding. So moving on, this is something we have already seen. So if you do s dot lower, s dot upper, they will return new strings which will give you lower case and upper case. s dot tab will give you a variety of other methods that you can use. I urge you to try this in your spare time and look at all the methods that you are having available. Please remember though, in all of this strings are immutable which means they do not change. You cannot change them in place. So which means I cannot do Q of 0 equals A for example. I cannot do that. And every time you call a method like this say s dot lower, if it is going to create a string, it is going to return a new string and you have to keep that in mind. So the solution to the problem has two solutions actually. One is this which now you should completely understand. So s dot lower will give you a lower case version of s of which we are finding the first three elements. We are checking if that exists in week. Similarly, this takes only the first three elements of the string supplied, converts only those elements to the lower and then checks in week. So they are both equivalent solutions that are perfectly applicable. Please note that if the final index, if you have a sudden question, what happens if the final index that you specify is very large? Nothing will happen. It will simply slice till the end. Whatever is available, it will slice and provide. So Q of 0 colon 100 will give you all of the elements. So let us move on. There are a variety of other methods that string provides. So there are, if you notice, if you do s dot tab for ignoring all of these underscore methods is capitalized center, count, decode, encode, ends with index, isalnum, isalpha, blah, blah, blah. So you have a variety of these methods. One of whom is what is called join. So if you look up here, there is something called join. So given a list of strings, we may want to join them into a single string. However, this is, so this is something that is very useful which is why we are discussing it here. So supposing I have a list of strings as in given in this example, I have info at fossey dot in, enquiries at fossey dot in, excuse me, and help at fossey dot in. There are three different strings. So let me make a simpler example. So I have this string, cheetahs are fast. Cheetahs are fast as three individual strings. So a of 0 will give me cheetahs, a of 1 will give me r, a of 2 will be fast. I now want to assemble a string that is the combination of this. How could I do that? So I can say a of 0 plus space plus a of 1 plus space plus a of 2. And you can immediately see how this is very stupid. It is a very clunky way of doing it. Supposing I have 5 elements, supposing I have only 2 elements, supposing I have only 1 element in that list, supposing I have 3000 elements in that list. How do I do it? I want a mechanism by which I can do this easily. So the join method allows you to do this very conveniently. All you need to do to this is to say space dot join. Remember space, the single coated string just space is a string so it has the method join a. So now let us, for example, let me create a really long list. I won't do it because if I create that I will have to introduce new syntax that will confuse all of you. But the point is if I have a huge list of lines or huge list of strings and I want to join them, it is easy to imagine that this is a very, very convenient way. But now supposing I don't want to do this as space, I want to put comma space. So it is a cheetah's comma space or space fast. If I don't want the space I can do this or if I want to say okay. So as you can see this makes it extremely convenient. So now supposing I want to say no, I want it to be with comma and I want to put, I want to make it look like a list. So I can say for example okay. So the idea is now I can do very interesting things with this. So it is a very powerful, very simple command that lets you do join elements of the string. So it is called join and it is very useful. So there are a lot of methods and strings which we are not covering. We urge you to look at the either the documentation or just explore on a string. So just create say s, q, look at q, q dot tab and look at each of the methods. So for example if you don't understand swap case, do question mark it will tell you what it is or you want to say q dot split it will tell you what it is. So each thing will give you, each method will give you documentation with which you can figure out what to do with the string. So with this we wind up the strings as I mentioned earlier our idea is not to teach you everything. Our idea is simply to get you started to be able to do useful things. So we only teach you say the most useful parts of string that we think will be useful later on either in this course or also later and get you started so that you are able to find for yourself later on. Let's move on to conditionals. Python has a few mechanisms by which you can do conditional. And it doesn't have too many so it is actually very convenient. You don't have to remember a lot of things. The first is if else that probably the only one we are going to cover. The if else block this is how the syntax goes. So look carefully at this syntax. Over here we have first assigned the variable a to the integer 5. The syntax says if a modulo 2, remember that this is the modulo operator. If a modulo 2 is equal to 0 then you specify the block inside the if along with this indentation. Typically you will give 4 spaces. So let me type this out. a is equal to 5. If a modulo 2 equals to 0, notice that the check for equality for integers you use double equal to. Now when I press enter, I press colon enter it will automatically indent it. So print even. The next line if I want to get out of this if block. So if I print, so I am still inside the same block. Now I move back by 4 spaces. Now in I Python the way to get out is you hit one more return, one more return. If you print 2 in returns it will say you are done with this function definition, with the definition of this block. So I will revise. We have assigned a to 5 if conditional in this case a modulo 2 this is evaluated. If that is equal to 0 it prints even else and remember the colon here. Colon is crucial and remember that it is indented. Now the thing here is it is very clear to anyone who is reading this code what this function block is doing. On the other hand if I add some curly brace and all of this other stuff which in other languages exist it becomes much harder to read. It is one of the reasons why Python syntax is a lot easier to read in practice. So notice that if way percentage to 0 it will execute this if not it does this. Standard straightforward stuff. The extra wrinkle that is there is F if sorry if L if else. So if I want to say else if it is convenient to be able to use L if. So if I say if a is greater than 0 print cos back space L if is less than 0 else. Once again only one block gets executed then it depends on A. Now the else is optional so which means I could do very well have code like this. If user is equal to admin do something user is equal to moderator do something else. L if user is client do something else and you can leave it hanging that does not have to be an else block. So if you do not need one do not put one. Is this clear? I will revise if conditional this conditional should evaluate to true typically true if this is to be executed. If it goes to false it becomes this. You can also have an if L if saying if this else if read L if as else if. So L if a less than 0 else. Note that the else is optional. In addition there is a very convenient ternary operator in python this is extremely useful. So look at the following. First we will read this very carefully. We have a string the variable name for which is score underscore str. So score str can be either a a like what you see here or score str could be a string of one of the number say 99. Or 100. Basically this represents a student score. So if he is absent that data record stores it as capital A capital A. Now what we want to do is we want to convert this string to a number using int. So turn out that this is something I am not shown you. Int of what is the type of this? Type of this will be string. Open quote 9 0 close quote is string. But what is type of int of this? But if I did int of a a what do you think will happen? It should give me an error because it does not know how to convert a a to a number. So what I want to do is we want to convert if this fellow if this score str is a a what is the type? Convert say set the score to 0. If it is not an a a convert it using int. This seems like a reasonable thing to do. Does this make sense? So score str is either a a or a string of one of the numbers in the range 0 to 100. We wish to convert the string to a number using int. So you convert it to 0 when it is a a this is the condition. So if I wanted to write this with if l I would simply say if score str is equal to a a score equal 0 l score and now the score will be 100 because I said score str was this. So this code block is kind of the same I have done the slightly reverse of this code. Both of these do the same thing but you can see that it requires four lines. So the ternary operator you can express this in one line. I have simply made s s is score str to make this line easier to it was becoming too long. So I have just saved it to another variable. So I would read this as score is int score str if s s not equal to a a l 0. This is called a ternary operator. This is called a ternary operator. I will repeat what it is doing. The way to read it is to say if s s is not equal to a a calculate this else give me 0. This is called a ternary operator and it is extremely convenient when you need to do things like this. One thing you will see a lot in code is python needs to use blocks which are indented to denote the block. So for example if I say a is 0 if a is greater than 0 print pass else. Now supposing I do not want to do anything when I say print neg and supposing I do not want to do anything in the else case. I could either leave it or I could say pass in which case it is syntactic evaluate. So when I say else colon I need to give it an indented block. If I leave it hanging for example if I did this it will keep waiting for input. I have to either control c or something which means basically I have entered an else condition and I need to syntactically say do nothing. In that case you say pass. So pass is what is called a syntactic filler and when a certain block has no statements a pass is thrown in this is mostly when you are trying to prototype code. You are saying if something if a is positive put some comment and say pass else pass. So now I have written this block which gives me some structure later on I will decide to say when a is positive I will write some code here I will do some code here. So basically it allows me to write a function structure or the code structure and temporarily make it so that it runs. So that I can actually execute the code and later on you will go and do it. Sometimes it is also used to make your code clearer. You want to say do something in this case else do nothing. When you want to say do nothing you use a pass. So it has its uses in many places you will see this if you are looking at Python code do not worry about it just to inform you of that. Let us move on to loops. So notice that conditionals there is only one there is simply L if else there is no switch case or anything like that. So it is extremely easy and of course switch cases can become a little painful to write because you have to write everything in the form of this L if but it is not too bad because it is only one thing that you need to remember there is only an if you do not have to worry what case do I use here should I use switch or should I use if none of that botheration you just write if. Next thing is loops first one we will consider. So there are a few mechanisms by which you can do looping primarily there is while and for. So the while loop is very straight forward let us look at this example. So this example prints squares of all odd numbers less than 10 using while. So initialize it by saying i is 1 while conditional colon is important and then you give it a block that is indenting while i is less than 10 print i star i i plus equals 2 what did I do wrong. So now this loop is running forever I made a mistake it is gone it is just running 3 3 3 3 3 is printing. So to get out of this I hit ctrl c and it kills that. So what did I do wrong I said i plus 2 I missed the i plus equals 2. So let us do this again basically the while loop executes as long as the condition is true and you saw that I actually created an infinite loop I never increment counter it ran off in which case to exit from that I simply did ctrl c that is an important point to remember with the while loop. The other extremely useful iterator that is the looping construct that is used in python is what is called for. So consider this little code example here for n in 1 2 3 print n you have seen this n in syntax before this checks for existence but in the for case what this does is it iterates over every element of this sequence and remember this is a sequence it can be it can work for both lists tuples strings and other things that behave like sequences I have only illustrated 3 of those. So for n in this print n so this will basically print 1 2 3 let us try it remember to put the colon if you forget the colon it does not going to be a loop it will be a syntax error. Let us try that it says invalid syntax put the colon print so one quick note if you look at the error message over here I will teach you a one small so for n in 1 2 3 colon print n this prints 1 2 3. So remember in the previous case when I typed this I got a syntax error please note if you look carefully at this syntax error I have highlighted it here notice that in this line it is showing this a carrot symbol here. So when you get an error message please look at it very carefully what this thing is saying is at this point in this line there is an error over here there is an error therefore you have to pay attention for n in so it is actually telling you exactly which line which column number in that line your error occurs. So it is good for you to pay attention to your error messages. One additional trick if you notice the coloring for this is actually not very nice unless you have a bright enough monitor it is not going to work I python has a color scheme on the terminal the current color scheme works best if your terminal actually has a black for example if I did this when it is black it looks very nice but when it is white it looks horrible. So you can either go to your edit profiles and change it but unfortunately if I use a black background it may not be very clearly visible. So one way you can do this is you change the profiles edit colors and you change it to say white on black unfortunately I cannot see the end of this oops this is one possibility the other is if you do not want to do that let me go back edit you can do percentage colors question and you can set this to light BG in which case you will get light colored background but I am going to go with the current one which is Linux and I will stick to this color scheme so if you are having problems looking at your error message please remember I am insisting that you learn how to look at your error messages and pay attention to them. So let us make this mistake again now it is very clearly visible for me that on the console I typed something line one may there is an error for N in 1 2 3 at this point there is an error I have missed the call. So please pay attention to this. So let us move on so we want to print the squares of all odd numbers less than 10 using for so the way to do that is to say for N in 1 3 5 7 9 print N star N so let us try that and that works the next line basically says for N in range 1 10 comma 2 print N comma N star N so let us try this and see what we get let us first print N and see what we get 1 3 5 7 9 now let us do this to get our answer so you can clearly see that range is doing something interesting so what does range do how do we find out range question mark will help us so range is it returns so it takes look at the help documentation it says range optionally you specify a start you must always specify a stop and optionally specify a step this returns a list of integers which is what it says return a list containing an arithmetic progression of integers range i comma j returns i i plus 1 i plus 2 dot dot dot to j minus 1 remember it does not really give you the last value just like sliding less like slicing okay when step is given it specifies the increment so let us try this so range of 4 will be 0 1 2 3 range of 1 comma 4 will be 2 1 2 3 range of 1 4 2 will be 1 3 range of 1 2 3 will be 1 range of 4 1 will be 0 because a starting index is gone too far range of 4 1 minus 1 will be 4 3 2 okay so range basically takes you from optional start stop step so range 1 and 2 starts from 1 steps by 2 so you will get 1 3 5 7 9 returns a list and the stop value is not included just like slicing okay consistent with the slicing there are 2 very useful keywords that are used in python that are extremely useful when you are doing looping the first is break the second is continue so break basically breakouts breaks out from the innermost loop so if you let us look at this with an example so we are trying to calculate the squares of odd numbers below 10 using violent break so what we do is we say i is 1 initialize the counter while true what does this mean this will iterate forever will it let us try while true gone it is an infinite loop keeps saying hello hello hello hello to get out I print control C I keyboard interrupted it so while true is going to loop forever doing this it will print i star i increment i by 2 but if i is greater than 2 it will break so let us try this i is 1 while true star i i plus equals 2 okay on the other hand if I did not do this it is going to keep going okay till the end of eternity because remember these are long integers which have no size restriction so till you exhaust the memory of your machine will keep going so hit control C to get out of this similar to break there is also continue what continue does is kind of like the inverse with break you are saying when this condition happens get out of the loop and remember it will only get out of the innermost loop so if I have 2 nested loops okay so for example if I say for i in range 5 for j in range 5 if j is greater than 5 to break print i comma j notice that in this case I have nested loops the code is extremely clear because I know that this if condition is only executed inside the for i and inside the for j so it is extremely clear what it is saying if j is greater than 2 break if not or rather otherwise if you are not broken you print i comma j so it does 0000102 it will never hit 3 because 3 will break out of the j loop but the break will not break out of the i loop it will only break out of the j loop so notice that j goes all the way i goes all the way to 4 but j always remains between 01 and 2 so once again break breaks out of the innermost loop continue on the other hand skips execution of the rest of the loop on current iteration so in this case for n in range 102 if n modulo 3 so when n is divisible by 3 it will continue otherwise it will just print the square so it prints the squares of all odd numbers below 10 that are not multiples of 3 does this make sense so if n percent 3 is 0 it will just continue means it will skip to the end of the loop so it will not print this so when I say continue it basically means skip this entire element so when n is equal to for example 3 or n is equal to 6 actually 6 will not come in this case then let us say n is 3 or n is 9 they are both odd n is n percent 3 is 0 it will continue which means it will not execute this print n into n for those cases it will simply skip that and go back and start here so we have done basically conditional strings and loops so far we can do lists really quickly but before that I think it is a good idea for me to stop and ask for a few questions so if you have any questions please raise them right away if not I will just quickly do lists any questions if everything is clear I have purposefully gone very slowly because today I am hoping to cover basic python but do it slowly and steadily so that you are able to follow all of the aspects so it does not look like anybody so there is one question yes please hello please ask your question hello sir yes I can hear you sir I have tried the same code in the sir I have tried the continue tried the same code in continue for loop with continue sir it is giving indentation error for loop with yeah so what do you think indentation error is the other things are when I am typing the colon and going for the next line yes yeah indentation is not happening defaultly sir I have to give a tab and again are you using ipython I am typing it are you its indentation is happening default are you using ipython or are you using python ipython sir ipython so how have you installed this you are using the live ipython only sir are you using the live dvd or are you using with the dvd sir with the dvd it should happen that way live dvd sir this is also the live dvd it should work because I do not think anybody else is maybe any setting I have missed I do not think it is a setting thing so far so if I do for n in range 4 colon and hit enter it should immediately give me a indented line if it does not for whatever reason if it does not do this yeah you just give 4 spaces and now say print n so for example let me do a demonstrative so let us say you got this you do 1 2 3 4 print n give 4 spaces if it does not work please try it if you have a problem let us know so someone has asked the question how do you decline so does that answer your question maybe you can try it and raise a question on the chat window if you have a further problem yes sir yes sir it is working now okay great okay so someone has asked how do we define scope of the loop scope of the loop is always determined by the indentation so please look at this code for n in range 1 10 2 if n percentage 3 is 0 continue all of this is indented one level so all of this is inside the for loop only the continue is inside this if therefore everything is defined by indentation alone which is by indentation is extremely important and in your if you are writing this in a text file which we look at soon I would suggest you use spaces rather than tabs and there are many editors which will automatically do this way if you type tab it will automatically give you 4 spaces okay I will recap range there is a quick question I will just take a question yes St. Joseph's college from Kerala please have asked your question do loops working the python version I am not using the ipython I am using python well I strongly suggest you please install ipython but yes certainly I mean for loops all of this is basic python it has nothing to do with ipython ipython is only a convenience it is like saying if I edit a c file okay I put for syntax in a c c for syntax it does not matter whether it is you edit it in visual c studio or we edit it in Wim or Emacs or g edit does not matter so long as the syntax is valid you are happy so it should work on python it should work on ipython the only thing that will not work on python is anything that is an extra feature like this question mark business that is not actual python that is ipython doing some magic for you okay so there are certain things that ipython provides that are specific to ipython but those are very often very obvious what I am talking all these slides are only talking pure python so yes the answer is it should work in pure python also unless you are typing something wrong if it says indentation error it means your indentation is neither consistent either not consistent or not correct for example if I say for n in range 4 and just give one line of indent this will work but in the second line say I did print n star n this will say wrong because the first line may have given one line in one character indent second line may have given three character indent that is wrong it has to be consistent and this is one reason why python code tends to be very readable you cannot it is illegal for you to have code that is badly indent does this make sense it is one reason why it is easy to read python code thank you so someone had asked me so if you have another question again I am sorry I am not being rude by shutting off your interaction because if you do not have a further question I am just going to move on because we do not have time so if you have another question please just raise your hand again I am not trying to be rude to you someone had asked a question about how you define range so you want to recap range so range basically says start stop step so if I say range of so look at my terminal session range of 1, 10, 2 it means start with 1 go to 10 minus 1 which is 9 step by 2 so it will give me 1, 3, 5, 7, 9 supposing I did not specify this just like in strides it will give me by default the step is 1 if I did not specify 1 by default the starting point is 0 so is this clear so range simply is kind of like the kind of like slicing I mean think of the syntax very similar to slide one more question good question what is the difference between pass and continue I think it is easy to answer this yourself if you wrote a little bit of code what pass will do is it is simply syntactically all it says is do nothing continue will skip everything in the rest of the loop so when I do let me show you the continue case here if I had put pass it will still print n star n so the easiest way for you to answer your own question is to type in this code run it with continue see what happens run it with pass see what happens this is how you will learn and this is how you teach yourself this is extremely important because obviously I cannot teach you everything or answer every question so to answer your question specifically if n percentage 3 is 0 if you say pass it will basically not do anything for this if block but it will execute n star n but if I do continue it will not execute anything below here inside this for loop so if I had print n star n print hello world print whatever else inside this for loop it will not do it exactly pass is used for representing a null block.