Transcript:

Welcome to the next topic. And in this topic we will going to discuss about the loop’s. Loops are very important in any programming language, be it. Python or C C++, Java, or anything, but you will find loop everywhere so very important that you understand what loops are and we will try to keep it simple so that you will understand most of it within the Giulia about how you can write loops like for loop within Giulia, or the while loop and easily perform the data manipulation operations or any advanced machine learning related operations many times. It has been you it. You will be using for for the tasks that you have in the hand, all right, so without much delay, let’s start with first for loop so to write the syntax of for loop, what we need to do Is we need to write for and in the previous video, we created something like sports brand or, for example, If I just go up, that’s what we discussed. Alright, let me just close this. Yeah, sports brands like this, so what we will do is we will try to loop it through by creating a sports brand variable. So what we are doing, We are not using the previous one. We are saying sports brand in in this array, which is added as comma. Nike comma Puma. Now this variable can be anything like, for example. I have mentioned sports brand or I can mention as I or anything. It is dynamically typed. Whatever you will write over there, it will go into basically take a type of that, and then you will be able to process the information, all right, so let’s keep it with the eye itself so that it is not confusing you with the previous declared variable, and then after that you need to write and it is a default indentation that have it has taken and the print print. I and after that, come here at the start for air and should match together and we write and then we press shift enter. So what has given us editors? Nike Puma, and there is no space so to add the space. What we can do is come over here. Write a space like this and press shift. Enter or ctrl enter now. There is a space so you are saying that this operation. You don’t have to write like three times print statement with four statement. You just write one print statement and your information is coming over here, so if you need it in a row with a space you can do like this, or you can write the another print command, which is print. LN and in that case, I think because it will be a new line every every time there is no issue so print a line will print it in a new line all together, so that’s one of the very, very basics of how you can look through the variables, so think like this is like, for example, the array that we declared or the dictionaries we will see in the future or maybe in this video covered how you can iterate through dictionary or tuple and make best use of the looping that is available now. One thing is that you have the values like this. That means three different sports brand. Sometimes we may have to AIA process each character, for example, if we have to loop through the value. Eddie, does that means first? It should print a then B then high, then D then here and then, yes, you can also do that by using for I in Teddy dots. So now I am not specifying anything like array or something, but I am saying this print LM and then I and so what it will do. It will loop through each world of editors and print it in a new line. So as you can see like this, This is how you can achieve it. This is really useful. Whenever let’s say you are processing the textual information right, but the same can be applied to in cases like numbers. If you have numbers if within the array or with if it is a big number, you may want to format it or something you can do like this now. Let’s see how you can iterate through tuple. This was an array and now let’s take a couple example for T. In 1 2 3 4 & 5 Maybe so this is a table because it is. It is not a big bracket like this, and it’s a small bracket, which is a notation for double. And then you can print T and then comma and then put end to it and 1 2 3 4 5 right, or if you need in a new line like we have seen it earlier, print a line and 1 2 3 4 5 so this is how you can loop through tuple, which is sometimes very helpful when you are working with topple related information. Then you have the dictionaries. Most of the time you will see that you are working with the array that we saw it here with the array or with the dictionaries, So how you can do that is for? D in dictionary, be ICT. I’m I’m just using it directly so that you can see it here. But you can also have an object declared as dictionary like we did it earlier and then loop it through. So let me show you both the ways so first is dictionary so car. One is, let’s say thousand dollars and then car two is two thousand dollars, right rent. D and then space. And and here is this information car, one and car too. What you could do is D one dictionary. It may this will be most of the times will be the case over here, not like this, but like this where you have a dictionary object created, then you want to loop it through that object, so for the simplicity. I’m just showing you how you can do this. So we have declared the dictionary for D in D 1 print D space and that’s it. So both will both is performing same operation. The only differences we have directly declared the dictionary over here. But in case of this option, the second line we have declared a d1 which is a dictionary type and given this entire declaration within this object and then looping through this object. This will be the case that you will be encountering most of the time. All right, now, let’s see how you can iterate through set so for S in set 1 2 3 4 5 simple example and then print as this is how you we have declared the set previously, if you remember, and if I if I press shift enter, this is the output of the set that you will get. It has just unordered the output like we have said earlier it doesn’t really take take care of the order of the input that you are giving it. It can be in any order and same is the case with the output. OK, now let’s see how you can loop through a range that we have decrypted that we have seen earlier, so for example, for range in 1 to 5 print range. And so what you get is 1 2 3 4 5 again. You can have space as well, so you will get a space so this is basically a range, so if you have to look through a range either, you can declare the range over here or you declare the range in an object and then use it with the within the for loop, along with the print statement after that, what do we have is for the similar example for range in one to five right, and then let’s say we want to display. The range equals to one range equals to two in in a format like this, so how we can do is by utilizing the inbuilt macros like a direct show range. Okay, and and so what it will give you is Output like this. Range equals to one range equals to two range equals two to three, which is like pretty easy for a normal user. Let’s say if the normal user will not be coding in this, and you need to show it what this variable really is or value related to. Then you can have this variable, which is, which is something which user can understand, okay. This is range of one or two or three or four five, similar like this is. If you have a need, you can use at the rate show. Macro all right now! Let’s see how you can use the if conditions within the within the for Loop, So for example, we have 4x in 1/2 tanh right, and we want to only show those values, which are divisible by percentage to. And if I’m not wrong. We have we call them. Yeah, we call them even number, right. X percentage to equals to equals to zero then print X and and that’s it, so if and for and relatively complex operation that we are doing, but simply, we are trying to identify the even numbers from the from this range. So 2 4 6 8 10 right, that’s? What the number, which is to be sure divisible by 2 and you can basically then easily do it in just in a minute now as an exercise what you can do is you can look for the prime numbers. This is an exercise for you for so what you do. If you don’t know what are prime numbers do a Google Google, and you will figure it out what the prime numbers are what their calculation is and write a loop like this from the hundred one two hundred values figure out the prime numbers, All right, okay, so now let’s see how you can write a well-defined output, so for example, what I have is, Let’s say there is a multiplication of ten, or let’s say 20 so that’s a multiplication between two and ten. If that’s the equation we are writing. We want to write it in a proper format formatted way. So this is a new thing which I’m telling and very, very useful, so pay attention to this for. I in one to ten and J equals two. I multiply with ten, so whatever that number is, multiply it with the ten, so I 1 plus or a 1 multiplied with 10 10 to multiply within 20 then it will loop again 3 multiplied by ten thirty so on and so forth and now here we want to so println we because we want a new line after every loop, so we want to write the we want to display the first variable that means 10 is the multiplication between this value and this value, 20 is the multiplication between this value and this value. So for this, we use this notation dollar and within that variable name dollar. J That means tan is multiplication multiplication between dollar. I bracket I dollar. I and damn, right. So 10 is multiplication between 1 and 10 so 10 is a static value because everything is getting multiplied with that 20 which is output of this expression, 20 is the multiplication between 2 and 10 so on and so forth. All right, so let’s go ahead and see that’s is our well-defined. Output 10 is multiplication between 1 and 10 2 + 23 + 30 so on and so forth, So that’s that’s how you can really put the format in place where within your for loop? Now, let’s learn another special keyword, which is continued. Keyword, so what happens is earlier. If you remember, we have seen the this particular expression where we have identified the odd value now using this expression only if we will identify the even sorry, we identified the even value, but we will identify now the odd value without changing anything but using the continue keyword, so pay attention to this, so first thing is for. I in 1 to 10 right, If I percentage – equals to equals to 0 that means we are identifying even number. Now we are not writing, so we are saying here. Continue what it is saying, Don’t do anything. Just move on to a next block of code and do whatever you want to do. So that means you a skipping this part over here, so continue and and then print Println maybe best way. I and I just want you pause this video for a second, maybe, like 10 or 20 second and think about it. What output you will get here in this case, though I will, I have told you at the start of this expression, but still, I would really like you to evaluate how you can figure it out. The output is giving it will strengthen your coding skills. OK, so I hope you have get out. But if not, let’s execute this and what we are getting is the odd numbers 1 3 5 7 9 so what we are seeing whenever you identify an even number just continue, don’t do anything and then print the other value, which is odd value over here, right, so same example is just with the continue keyword. You can further achieve a different type of calculation or the processing from your business law from the business logic prosperity. All right, now, let’s move on to something which is while loop. And while is condition again, condition based loop and condition is until that condition becomes false. Keep on running that loop. So for example, we have a a equals to 1 right and the condition that you need to write is like this. Sorry what I am doing while a is less than 10 That means keep the loop running when up until a is less than 10 right print L and a sabo and then increment, the value of E E plus equals to 1 So this is a new thing which I am showing you, which is nothing but a equals to a plus 1 so just to shorten that we have written a plus 1 A plus equals to 1 which is equivalent to a equals to e plus 1 All right, lets. Go ahead and see it 1 2 3 4 5 6 7 8 9 so as soon as it has reached 10 it’s become. It came out of the loop, So that’s about how you can write the while loop within the within the within the Giulia and similar to this, I want you to write and an exercise along with the similar to the prime numbers, which I have shown you are which I have explained you earlier how you can do it, not exactly the prime number, but the order and even number, and I told you to do the exercise of the prime number on for loop, and similarly I’m asking you do the same exercise of prime numbers here in the while loop, so that’s pretty much about it for the loops plus of per Se, But I hope you found it helpful and develop some basic understanding about it.