 Welcome. So, in the previous session, we looked at control flow constructs in Python and we also saw a few examples. We gave you a few exercises that you are going to solve and we solved them and showed you the solution. In this session, we will have a practice session where you will try to solve a variety of problems just like we did for the basic section we are going to do practice now. So, on the interface again, when you sign up, you will see a practice quiz that you can take, start that and start solving the problems as we had done before. So, before we begin, let us look at some preliminaries. Once again, if you are using Python 2.x on your interpreter, as in when you are testing, if you are installed Python 2.7, remember that you must use raw input instead of input in your code. On the interface, we are running Python 3. So, you can use input just as we had used in the previous session. And for the print statements also, the examples that I am going to be showing here are assuming Python 3, but if you are using Python 2, you need to do from space double underscore future double underscore import print function. Just remember this, if you are trying this code on your own computers. I strongly recommend that you open a terminal with IPython and or an editor. If you are using canopy or spider whatever it is you are using, make sure you have an IPython session available and perhaps an editor. So, that you can try these problems, type them out in the editor or in IPython and then make sure it works and then put it on the interface and try it out on the interface. So, let us begin. We will start with a very simple problem. So, as before we ask the user to enter an integer. Again, do not give any prompt, just ask for the integer and the user will give you an integer. If the number is positive, print positive, remember input will only give you a string. So, the number is positive, print the string positive exactly as you see here with P capital. If the number is negative, say the number is minus 10, let us say the user gave, you must print negative of 10. So, if it is negative of 5, if it is minus 5, if the number is 0, print 0. Finally, after this is done, print on a separate line done, the string done with D capitals. Please try this. I will give you two minutes. It is a very easy problem as an exercise for using if. Once again, remember when you are doing this, the interface will tell you, it expects some output and you have given it some other output. Make sure you type it exactly as requested. So, for example, if it is positive, you must print positive with P in caps and it is negative space of 10 and remember negative of 10, it is not the negative number that you print, actually print the positive part of that number. Also, remember regardless of what code is executed, you must finally print done. Whether the number is positive, negative or 0, you must always print done in the end. Also, remember when you do multiple arguments to print, it will automatically insert a space. So, I hope most of you have tried this and attempted it. Let us look at the solution. It is very easy. You simply say x is int of input. If x is less than 0, so it is important that you convert this to an integer because input will give you a string. If x is less than 0, print negative of, notice that there is no space after the off here. We use the a, b, s built in to give the absolute value. If x is positive, we print positive, else print 0. Notice that finally, we print done because if you look at the if, if, else construction, only one of these will be executed. So, if you just print done here at the end, that will make sure that it always gets printed. So, I will give you 30 seconds to try this and make sure you get it correct. So, you can move on to the next problem. Please make sure you finish this. So, we can move on to the next problem. So, let us move on to the next problem. We now have another problem with if. So, first ask the user to enter two integers, one on each line. For example, you say x and y, which means you need probably two inputs. If x is larger than y, print the string first without the quotes, store the value of x in a variable called highest. If y is larger than x, print second, store the value of y in a variable highest. If they are equal, print same again without the quotes, store either as the highest, either x or y because they are equal. Finally, print the square of the highest. Again, it is very easy practice exercise for using conditional flow. Please try this. Once again, if x is larger than y, print first, y is larger than x, print second. If they are equal, print same. All of these without the quotes, finally print the square of the highest. Give you another 10 seconds. So, let us look at the solution. It is very simple. We first say x is int of input, y is int of input. If x is greater than y, we simply print first, set highest equals x. If y is greater than x, print second, highest is y. Else, print same, highest is equal to x or y, it does not matter. Then print highest into highest. That is all there is to this. Now, some of you when you are doing this, you may run into some peculiar problems. It is very important that you learn how to read error messages and understand them. So, when you get error messages, you have to understand what the error message is saying and interpret it as such. For example, if you do something like this, I made a mistake. So, the mistake is what? It says syntax error, future, feature, print, functon is not defined. The important thing when you are looking at error messages is to read it very carefully. So, you may think you typed it correctly, but what is the mistake I have made here? I have mistyped print function. So, the function I have said functon. So, you have to read this. What does it say? It says syntax error, future, feature, print, functon is not defined. So, when you read this, the first response you will say, you will probably think is, oh, I did the right thing, but in reality you probably typed something wrong. So, read it carefully and then look at what it is saying and you will find your mistake very often. So, now to correct it, I will write it correctly. Now, let us say we did this. If I said print 1. Now, what does it say? It says syntax error, invalid syntax and it tells me print 1, this is wrong. Why? Because print is now a function. So, whenever you get an error message, notice that when you get an error here, it actually shows me saying print 1 and it highlights exactly the character where there is a mistake. So, when you see an error message, often if you are given a program and the program is failing, nobody is going to help you with this. So, you have to be able to read messages and be able to figure out what the problem is. So, it is very important you read the message, understand what is going on which is why when you type this on the interface, you will get the errors as such and you have to interpret those errors and fix the errors. Going back, so this is the solution. I will give you about 15 seconds to finish it and make sure you get it to progress to the next problem. I hope you have all done this. Let us move on to the next problem. So, now, we move on to while loops. So, use a while loop to print the numbers from 1 to 10 and it is important you use a while loop for this. There are many ways to do it obviously. This is just a practice exercise. So, we want you to use while and learn how to use while to print the numbers from 1 to 10. Please try this. Remember, you have to print the numbers from 1 to 10 both inclusive as in 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 and you have to print these numbers 1 in each line. So, 1, 2, 3, 4 in series. It is a very easy exercise. So, I hope you are done. So, let us look at the answer for this. It is very simple. So, first you set x is equal to 1, then you simply say while x less than 11, print x, x plus equal to 1. That is all. Remember here, indentation is very important. This is also true for our previous solution. If you go back to the previous solution, remember that this print highest was done in a separate line at the same level as the else. Even here, if you notice, highest is x is inside. It is indented 1 to the right. So, this indentation is very important. You should be comfortable doing the indentation. Similarly, in this case, this while loop, again it is all indented. So, it is inside here. So, please finish this. I give you 10 seconds to finish this. It is too easy. We will move on to the next problem. So, let us move on to the next problem. Now, do the following. Use a while loop to print the odd numbers from 1 to 10. That is, print all the odd numbers from 1 to 10. Very easy exercise. You should take no more than a minute to finish this. Once again, very easy. The same as the previous. All we are asking you to print the odd numbers from 1 to n. It is very easy to do. I will give you another 10 seconds. You should not have to change too much from the previous solution. Probably one character you have to change. That is all you have to do. Let us look at the solution. Solution is very simple. As I said, there is just one character changed. It is all the same. We just increment x by 2. So, this will give us the desired answer. x is equal to 1, while x less than 11, print x, x plus equal to 2, which means every time x is incremented by 2. Let us move on to the next problem. Now, use a while loop to generate the numbers from 1 to 10. If a number is divisible by 3, print 3 stars. If a number divisible by 5 is found, print 5 stars. If a number divisible by 5 is found, print 5 stars. Otherwise, just print the number itself. I hope you have got this. Let us look at the solution. It is very easy. Start with x is equal to 1, while x less than 11. So, the looping part is the same. All you do is if x percentage 3 is 0, double equal to 0. Remember, 2 equal to 2 is to check equality. Print 3 stars. And if x percentage 5 is 0, print 5 stars. Else print x and then increment x. And please remember, this increment line has to come outside the cells, which means it should be at the same level as the cells. Otherwise, you will end up with an infinite loop sometimes. It may never increment or it might increment only some of the times. It may do all funny things. So, make sure x plus equal to 1 is done at the same level as your if. Do not put it inside the else. It is very important to learn how to indent your code correctly. This is important for multiple reasons. One is firstly, your code will not even execute if you do not indent it properly. You will not get the correct results. Secondly, because it is indented, everybody can read the code. So, anybody you give Python code to will be able to interpret and understand what this code is doing. So, please type this. Make sure you get this. And let us move on to the next problem. I will give you 20 seconds. Let us move on to the next problem. Now, use a while loop to generate the numbers from 10 to 1. For each number, print that many star characters. That is, if the number is 1, print a single star. If the number is 3, print 3 stars, etcetera. Remember, use a while loop to generate the numbers from 10 to 1. And for each number, print that many star characters. Again, this should take you no more than a couple of minutes. Please try it. So, let us look at the solution for this. Very easy. Start with x is equal to 10 while x greater than 0. So, now this changes. So, it is now greater than 0 instead of less than 11. Print star into x. So, this is the only trick in this question, which is if you multiply a string by an integer, it will make that many copies of that string. So, here the simple way of doing it is to simply do print star into x. And then you have to remember to decrement x by 1. So, x minus equal to 1. You could have also written this as x is equal to x minus 1. So, I give you 20 seconds. Please type this. Make sure you get the correct answer. Move on to the next question. All right. Let us move on to the next problem. So, the next problem is ask the user for an integer, say n. And we will assume that the user gives a number that is greater than or equal to 2. Now, use a while loop to print n numbers uniformly spaced between 0 and 1. You must include both 0 and 1. So, for example, if the user entered the number 3, you should print 0.5 and 1.0. Once again, you can ask the user for an integer greater than or equal to 2. You do not have to give any prompt. Use a while loop to print n numbers uniformly spaced from 0 and 1. So, if the n is 2, you will only have 0 and 1. Otherwise, if you have n is 3, it will be 0.51, so on and so forth. So, you will get more numbers in between 0 and 1. Please try this. So, let me give you a hint. Remember, you have to basically increment the values. So, you need to find a suitable increment such that you can add it to get to 1. So, let us look at the solution. It is actually very easy. So, the first thing you do is you get the int n. Then you need to compute a dx, which is 1.0 by n minus 1. Why does this work? Supposing n was 2, what should the increment be? Exactly 1. What is the number of 3? Then the increment should be half. Then you start with x is equal to 0, while x is less than 1.0 minus dx by 2. Why are we doing this? Well, because we want to print 1.0 at the end. And with floating point numbers, you can never be sure that when you add these little numbers dx, you may not get 1.0 at the end. So, we do while x is less than 1.0 minus dx by 2. Print x, x plus equal to dx to go to the next value. This will exit before it hits 1. Why? Because it will not print when x actually reaches the value near 1. So, we finally print the value of 1. Once again, all we are doing is we start with x is 0. We keep incrementing by dx. And this dx is simply calculated as 1.0 dx by 2 divided by n minus 1. And when typing this, remember do not do. If you are using Python 2 especially, 1 divided by n minus 1 will give you 0. So, make sure if you are doing in Python 3, this is alright. But if you make it 1.0 divided by n minus 1, you will get a floating point. So, I give you about 10 seconds to finish typing this and get it correct and move on to the next problem. Let us move on to the next problem. We will now move to for loops. So, in this problem, you ask the user for an integer. Again, use no prompt. Use a for loop to generate the multiplication table for the number up to 10. So, if the user entered 5, print the following. 5 into 1 equals 5. Remember that there is a space. 5 space x space 1 space equal to space 5. 5 times 2 is 10 so on and so forth all the way up to 5 times 10 is equal to 50. Please try this. I give you 2 minutes. Remember again, if you are using range, remember that range starts from 0. So, you need to start from 1 all the way up to 10. Let us look at the solution now. It is very easy. We say n is int of input. You have seen this many times. For i in range 1 comma 11. And why do we use 1 comma 11? Well, we have to start with 1. We have to go all the way up to 10. And remember that the last value is not included in range. So, for i in range 1 comma 11, print n comma x comma i comma within string equal to comma n star i. So, it is very easy. It is just three lines of code. I give you 10 seconds to finish typing this. Get it correct. Move on to the next problem. So, let us look at the next problem. Ask the user for an integer n greater than equal to 1. Now, print the first n numbers of the Fibonacci sequence. Now, you have already done the Fibonacci sequence example, the previous session. This time, we want you to make it so that the user can give a number n. And then it will print the first n numbers. Please try this. If you remember, Fibonacci sequence starts from 0. So, remember to print the full sequence. All right. Let us look at the solution. I am going to go over this quickly because you have already seen this several times. Say n is int input. a comma b is 0 comma 1. We have seen this before. Print a, that is the starting value. Otherwise, you will not get the first value of 0. For i in range n minus 1, y n minus 1, well, range will give us n numbers. We have already printed one of those numbers. So, we want to do n minus 1. Print b, a comma b is b comma a plus b. That is it. So, I give you 30 seconds or 20 seconds to finish typing this to get it correct. And let us move on to the next problem. Let us move on to the next problem. This one is an extremely easy problem. So, we ask the user for an integer n. We use the range function to create numbers from 1 to n. Print each value in this, in a separate line, in this list. So, for example, let us say user enters 3, print 1, 2, 3. This is very easy. So, I give you about 30 seconds to finish this. Let us look at the answer. All we do is n is int input. For x in range 1 comma n plus 1, print x. The idea here is, in Python 2, for example, range is actually a list. Python 3, that is not the case. So, when you are iterating over a list, you can simply do for x in whatever list you give. We have seen this in previous sessions, in the initial sessions that we were looking at numpy lists, etcetera. So, remember that you can iterate using a for loop over all the elements of a list also, with the same syntax that you use with range. Let us move on to the next problem. What we want you to do is, we want you to loop over an array using the for. So, ask the user for an integer n. Use numpy's linspace to create n points from 1 to 2. If you remember linspace, you give it 3 arguments, starting value, ending value and the number of points. Print each value in this, in a separate line, in this array. For example, let us say the user enters 3. We want you to print 1.0, 1.5 and 2.0. But we want you to do this using linspace. So, please try this. I will give you a minute. Again, it is a very easy problem. Remember, you will have to import linspace from numpy. And remember, you have to create points from 1 to 2 and use a for loop. Let us look at the solution. So, first we import from numpy linspace. We say n is int input. Then we create this data is linspace 1, 2, n. This gives us n points including 1 and 2. Then we say for x in data, print x. Notice again the syntax. Even though we are not using range, data is not a list. We can still iterate over every element using the same notion. This is true for any sequence. So, any sequence type you can simply do for x in that sequence type and it will iterate over every element of that sequence. Let us move on to the next problem. I will give you 10 seconds to finish this to make sure you get it correct. All right, let us go to the next problem. So, now ask the user for a list of fruits separated by a comma. Print each fruit in a separate line. So, for example, let us say the user enters apple comma pear. Then you should print apple pear. All right, so let us say the user enters apple comma pear. That is all. You should print apple in one line, pear in one line. Again, very easy. I give you a minute to solve this. Remember that they are separated by a comma. Okay, let us look at the solution. It is again very easy. So, first thing you do is you get the input. You say fruits is input. Then the important part is you need to split this into a list of individual fruits. So, you split it based on the comma. What will this do? This will give us a list of fruits each separated inside the list. So, which means you will get apple pear as two elements of a list. Then you can simply iterate and say for fruit in fruits, print fruit. I hope you understand the process. Fruits itself is a string. Now we are resetting this, we are generating a list based on the input string. That is now a list. So, fruits is now a list. We can now iterate over every element of that list and print each of these elements. So, in the first case, you will get apple and pear. So, I give you 10 seconds to finish this and move on to the next problem. Alright, let us look at the next problem. What we are going to show you now is how you can loop over a string. So, ask the user for a string, print each character of the string using a for loop. Remember, I told you for loop can be used not just for lists, not just for arrays. We saw that we can do it for numpy arrays as well. Turns out you can do it for a string also. So, for example, let us say the user prints gives you box B O X. You should print B O X. Please try this. I give you 30 seconds because it is a trivial easy exercise. I give you 30 seconds because it is a trivial easy exercise. Okay, so let us look at the answer. It is very easy. You simply say S is input for car in S, print car. So, what does this do? So, what you should learn from this is that when you iterate over a string, the iteration loop will give you every character in that string. On the other hand, if you go back to the previous example, when you had a list, it will iterate over every element of that list. Alright, so when you did fruits.split, we converted that long string into individual elements that sit inside a list, which means if you had apple and pear, you will get apple as one separate element, pear as a separate element. On the other hand, in this case, when you just say S is input, you get a single string. And when you do for car in S, it iterates over every character of that string. These are important things for you to learn when you are writing your code in the future. Again, I want you to notice that the syntax is the same for character in some sequence, for object, for x in some sequence, it will iterate over every element of that sequence. That is the important lesson I want you to learn from this. Let us move on to the next problem. This one shows you how you can write nested for loops, which means a for loop within a for loop. So, let us say that the user supplies an integer n. Okay, give it again an empty prompt. Print an n cross n matrix where each entry is the sum of the row plus column. And remember in Python rows and columns start with the number 0. So, for example, let us say the user enters 3. So, you should give me a 3 by 3 matrix. And you should print the following 0, 1, 2, 1, 2, 3, 2, 3, 4. So, for example, this is the 0th row. This is the 0th column. So, if you look at this value 4, this is row number 2, column number 2. So, the sum of these 2 is 4. This is row number 1, column number 1. So, the sum of these 2 is 2. Please try this. I will give you a minute to finish this. I will give you a hint. You have to do a nested for loop. Remember it is an n cross n matrix. So, it is not 3 cross 3. This is just an example. Make sure it is an n cross n matrix. And each entry is the sum of the row plus the column. Now, there is one small trick here that you should remember. When you are printing out a row like this 0, 1, 2, we do not want you to print it in one separate line. We want the entire row to be on the same line. And then you should go to the next row. So, you have to print row number 1, row number 0, then row number 1, then row number 2. Remember print takes extra arguments that you can use. If you remember the slides, there is an argument to print called end. Takes an extra argument saying n equal to something. But then remember after every row, go to the next line. I have given you a lot of hints. Remember print takes an extra argument saying n equal to something. Try that. Remember we want each row to be printed like this. So, let us look at the solution. So, the first line is simple n is int input. Let me say for i in range n. So, remember this is the row for j in range n. Now, notice that this for is one level indented to the right, which means this for will be run for every row. It will run n iterations over j. Then inside this we say print i plus j. And then we say n is equal to space. This is not the empty character. It is space. There is a single space. So, this will give us. So, now how does this work? Well, it starts with i is 0. When i is 0, it will start with j is 0. So, then we will print 0. Then it will go to the next value of j, which will be 1. So, you print 1 followed by a space. Then it will print 2. So, if n is 3, so you will get the first row. Now, j is finished. So, it will come here and print this empty. What is print? Open close bracket do. So, if you do not know what this does, try it on an ipython session. You will see that it just prints an empty line. So, this will print you an empty line. Go to the next line. At which point it will loop back to i is equal to 1. Then start with again j is equal to 0, 1, 2, so on and so forth for all of these values. So, this is how you create nested for loops. You can nest them as much as you want. I hope you understand. I will give you 10 seconds to finish entering this. So, let us move on to the next problem. So, now we want you to find the first number in the Fibonacci sequence divisible by 4, but greater than 8 and less than 500. So, find the first number in the Fibonacci sequence. So, which means you have to generate the Fibonacci sequence. As soon as you find the number greater than 8 that is divisible by 4, you must stop. Find the first number in the Fibonacci sequence divisible by 4, but greater than 8 and less than 500. And obviously, we are expecting you to print that number. Please try this exercise. Since, this is based on the Fibonacci sequence which you have done many times before. I am not going to give you too much time. Let us look at the solution. So, we say a, b is 0, 1 just as before. While b is less than 500, we start the loop. If b modulo 4 is 0 which means if b is divisible by 4 and b is greater than 8 print b and very importantly we break here. Otherwise, it will continue to do the looping unnecessarily. No need to do extra computations. Now, if this is not the case, if this condition is not true, go on to compute the next value which is a, b is b, a plus b and that is it. That is all there is. The only thing here is you are using a break to actually find the value. So, I think the answer you will get is 144. So, I give you about 30 seconds to get this, try it and make sure you get it correct. And notice again that we are using and here. Once again, if you want to make this easier to read, you could put brackets here, b percentage 4 equal to 0 and b greater than 8. But in this case, it is all right. So, this ensures that both b is divisible by 4 and it is larger than 8. Remember that 8 will be one of the values. So, it will immediately exit. We do not want the value 8. We want the next one which is 144. So, that brings us to the end of this programming exercise. I hope you have gotten a feel for programming using if, l if, ls using while loops and for loops. And finally, we looked at how to use the break statement. We have shown you various examples of for loops, how you can iterate over various sequences using for. We iterate over a list, iterate it over an umpire array and we iterate it over a string. In each of them, you have to understand how the for loop is going to behave. So, we hope that this has given you a good exposure to loops. Thank you very much.