1 00:00:08,010 --> 00:00:16,540 A very good way to shorten your lines of code in Python is by writing while or for loops. 2 00:00:16,560 --> 00:00:22,650 And this because if you want to print the large amount of numbers that's on your screen, you need to 3 00:00:22,650 --> 00:00:24,270 write so many print statements. 4 00:00:24,270 --> 00:00:28,260 If you want to print the numbers, you need to write the print statements. 5 00:00:28,260 --> 00:00:31,990 Right, so that you can use the while loop. 6 00:00:32,490 --> 00:00:35,440 So let's write I equals zero. 7 00:00:36,220 --> 00:00:39,480 OK, and here's how you write a while. 8 00:00:39,930 --> 00:00:40,170 So. 9 00:00:40,170 --> 00:00:44,340 Right, while I use more than or equal. 10 00:00:45,830 --> 00:00:50,780 Down five, so here we look while I is more or equal to five. 11 00:00:50,810 --> 00:00:55,700 OK, so while this is true, we want to print. 12 00:00:57,220 --> 00:01:02,320 Every night and then in the end, we can simply increment. 13 00:01:03,630 --> 00:01:04,050 I. 14 00:01:05,220 --> 00:01:11,370 I of course, I lost one here in that case, this means that in every law. 15 00:01:11,400 --> 00:01:18,650 So first I will be equal to zero, then it will be printed, it will be incremented by one, OK? 16 00:01:19,140 --> 00:01:25,980 And then the while loop will repeat and this will continue until I becomes equal to five. 17 00:01:26,670 --> 00:01:30,330 Then after it is equal to five, it will become equal to six. 18 00:01:30,750 --> 00:01:33,030 So loss is equal to six. 19 00:01:33,300 --> 00:01:38,120 This condition here will not be met and for that reason will get out of this law. 20 00:01:38,700 --> 00:01:41,940 So let's run our code now and see what happens. 21 00:01:42,990 --> 00:01:48,750 And you can see that we print all the numbers from zero to five, which is exactly what we expected. 22 00:01:50,090 --> 00:01:57,960 You can also go further by displaying not the numbers, but certain amount of character slips, right? 23 00:01:58,670 --> 00:02:04,960 Print I multiplication, same in here, we can write the symbol. 24 00:02:05,780 --> 00:02:16,170 So this means that for every loop we're going to type the symbol I as many times as the value of five 25 00:02:16,170 --> 00:02:16,610 is. 26 00:02:16,760 --> 00:02:19,090 So here's how this system go. 27 00:02:19,220 --> 00:02:27,020 So we can see here that in the final statement we have five, then we have four, three, two, and 28 00:02:27,020 --> 00:02:27,720 then only one. 29 00:02:27,860 --> 00:02:30,680 And this is how that while statement works. 30 00:02:30,950 --> 00:02:36,190 And there, as you can see, some different cases of what you can print or what you can do inside. 31 00:02:36,380 --> 00:02:44,540 Another very useful tool in any programming language in Python as well is the for loop. 32 00:02:44,920 --> 00:02:52,010 OK, so for loop usually loops around sets of numbers. 33 00:02:52,100 --> 00:02:52,500 Let's see. 34 00:02:53,060 --> 00:03:00,050 So for that reason I will create a list here and we're actually going to cover the list a little bit 35 00:03:00,050 --> 00:03:00,790 later as well. 36 00:03:01,130 --> 00:03:10,520 But here I will create a simple list of temperatures so temperatures will be equal to let's write five 37 00:03:10,520 --> 00:03:11,560 different temperatures. 38 00:03:11,960 --> 00:03:16,400 So the first one will be six, seven, then 30. 39 00:03:17,690 --> 00:03:23,330 Then seven two, then four to three, and finally 91. 40 00:03:24,830 --> 00:03:26,640 So this is the least of our temperatures. 41 00:03:27,470 --> 00:03:34,600 Then let's create a law that would prevent every of the temperatures in that list. 42 00:03:35,180 --> 00:03:39,380 So let's write for item in. 43 00:03:41,220 --> 00:03:50,960 Temperatures, so here you can see that item is for each item in the set of the temperatures. 44 00:03:51,240 --> 00:03:56,080 So for each of these values, we're going to do something inside the for loop. 45 00:03:56,730 --> 00:04:04,520 So what I will simply do here is to print the item or the current value that is taken from this set. 46 00:04:04,920 --> 00:04:07,020 So let's see what is going to be the result. 47 00:04:07,060 --> 00:04:12,880 Now, I think you can guess it, but yes, we'll get each of the temperatures on different line. 48 00:04:13,410 --> 00:04:14,690 OK, quite simple. 49 00:04:15,360 --> 00:04:18,180 And imagine if you have to do this with a while loop. 50 00:04:18,450 --> 00:04:24,960 We all need to write while and then for every step you need to increment the different position of the 51 00:04:24,960 --> 00:04:29,640 set and the loop will be way longer, actually double longer. 52 00:04:29,640 --> 00:04:35,670 Comparing to just for loop that we just wrote and for that reason sometimes is quite useful using the 53 00:04:35,670 --> 00:04:37,320 for loop in these cases. 54 00:04:37,600 --> 00:04:45,860 Another quite useful data structure for using in the Falu is the range. 55 00:04:46,110 --> 00:04:52,500 So I will actually change the temperatures here to a range of numbers and you can write. 56 00:04:53,870 --> 00:04:54,710 Numbers. 57 00:04:55,650 --> 00:05:03,120 Equals range then, and this means that here we're creating a range of numbers. 58 00:05:03,790 --> 00:05:13,390 OK, and we can see right here for each number in numbers, print number. 59 00:05:14,010 --> 00:05:22,830 OK, so in that way, we don't need to write every number, but we simply have the set of the numbers 60 00:05:22,830 --> 00:05:24,970 between zero and nine. 61 00:05:25,110 --> 00:05:28,300 OK, and we can print each of them. 62 00:05:28,830 --> 00:05:36,150 So if we simply press run us right or wrong, you can see that we have all the numbers here printed 63 00:05:36,150 --> 00:05:37,930 from zero to nine. 64 00:05:38,220 --> 00:05:41,130 So in the range we can do some actual quite fun stuff. 65 00:05:41,490 --> 00:05:46,690 So the first thing you can do is simply write two numbers here. 66 00:05:46,710 --> 00:05:47,930 So five and ten. 67 00:05:48,780 --> 00:05:53,150 And if you do this, you are simply going to get the numbers between five and 10. 68 00:05:53,550 --> 00:05:57,060 So you can also specify that the lower and the upper level. 69 00:05:57,310 --> 00:06:04,160 Then another thing you can do within the range structure is let's say you want to print some numbers 70 00:06:04,170 --> 00:06:05,910 between then. 71 00:06:07,450 --> 00:06:13,930 And 100, but also you want to print every five numbers, so here I write five. 72 00:06:15,090 --> 00:06:18,070 OK, and this is the increment between each number. 73 00:06:19,090 --> 00:06:25,180 So if I press here, you can see that we are getting excellent numbers between ten and nine to five 74 00:06:25,300 --> 00:06:31,620 or excellent between the 100th and that range, but the difference between them is five. 75 00:06:31,630 --> 00:06:35,050 So we get 10, 15, 20 and so on. 76 00:06:35,230 --> 00:06:41,380 And yeah, you can use the range in your advantage for four loops, which is quite useful then.