1 00:00:00,220 --> 00:00:04,290 And welcome back to another class of course about Python. 2 00:00:05,790 --> 00:00:13,490 So in today's class our main topic would be for loops and you will see it's not that hard to understand. 3 00:00:13,890 --> 00:00:16,980 It's when you practice it you'll see it's gonna be very easy. 4 00:00:17,250 --> 00:00:17,810 All right. 5 00:00:17,850 --> 00:00:24,630 So basically what are Froot Loops for loops are technically a loop that is used for iterating over a 6 00:00:24,630 --> 00:00:25,100 sequence. 7 00:00:25,200 --> 00:00:30,090 So the sequence could be a dictionary could be a list could be a string could be numbers could be a 8 00:00:30,090 --> 00:00:35,060 set of numbers could be trouble could be all of those together. 9 00:00:35,100 --> 00:00:42,540 So basically what it does it will execute the set this statement sorry and the ones for each item in 10 00:00:42,540 --> 00:00:44,270 the list topple. 11 00:00:45,780 --> 00:00:49,050 It could be numbers it could be dictionaries or whatever. 12 00:00:49,500 --> 00:00:52,410 So what what does this all mean. 13 00:00:52,410 --> 00:00:57,150 Very simple let's say for example you have a list of I don't know of things. 14 00:00:57,150 --> 00:01:03,360 Well if when you're going to make your for loop when you for example you will use the for loop what's 15 00:01:03,350 --> 00:01:07,120 going to happen is that that each element will be printed. 16 00:01:07,210 --> 00:01:09,160 So let's have an example here. 17 00:01:09,160 --> 00:01:10,690 Let's use for example. 18 00:01:10,690 --> 00:01:12,630 I don't know colors. 19 00:01:12,640 --> 00:01:18,850 So in this case we will have a variable that is will a list that will be named colors. 20 00:01:18,910 --> 00:01:21,800 And in this case we will create our list. 21 00:01:21,820 --> 00:01:26,840 So let's say we'll have blue red 22 00:01:41,400 --> 00:01:41,970 orange 23 00:01:45,150 --> 00:01:46,880 and black. 24 00:01:46,950 --> 00:01:47,540 All right. 25 00:01:47,670 --> 00:01:51,000 So we have a list of far forth colors right now. 26 00:01:51,000 --> 00:01:53,710 And let's say you want to use our for loop. 27 00:01:53,910 --> 00:01:56,380 So how exactly this for loop works. 28 00:01:56,580 --> 00:01:57,890 So very simple. 29 00:01:57,900 --> 00:01:59,450 Write down for. 30 00:01:59,470 --> 00:02:08,500 So this will be our for loop then let's say for example for one color this case since we're talking 31 00:02:08,500 --> 00:02:12,650 about colors we'll say for one color in 32 00:02:15,280 --> 00:02:15,980 colors. 33 00:02:16,120 --> 00:02:20,010 In this case colors is the name of our list 34 00:02:24,820 --> 00:02:25,810 two points. 35 00:02:25,820 --> 00:02:31,380 Print one color. 36 00:02:31,750 --> 00:02:32,050 Great. 37 00:02:32,860 --> 00:02:38,780 So if we run up as you can see your eyes each color will be printed. 38 00:02:39,280 --> 00:02:45,910 This thing right here which is one colors can be changed so it could be just X and if I run the app 39 00:02:46,210 --> 00:02:50,440 as you can see it will still work. 40 00:02:51,420 --> 00:02:53,340 So it's very very simple to use again. 41 00:02:53,350 --> 00:02:56,730 So basically what exactly does loops. 42 00:02:56,730 --> 00:02:58,650 What exactly does. 43 00:02:59,070 --> 00:03:05,920 It simply prints every element let's say off a list of a string of a number of absolutely well anything. 44 00:03:05,940 --> 00:03:10,940 So let's try it just on a string to show you guys what exactly it is. 45 00:03:10,940 --> 00:03:14,550 So let's say for example we have hour another time a color. 46 00:03:14,580 --> 00:03:23,930 So let's say this time will say for letter since we are going to print the letters in. 47 00:03:24,040 --> 00:03:25,880 In this case let's say the word blue 48 00:03:28,860 --> 00:03:29,900 two points. 49 00:03:30,210 --> 00:03:37,920 Print letter because we are printing letters once again could be just X and it will work. 50 00:03:38,490 --> 00:03:42,150 So when we run our up as you can see blue is printed this way. 51 00:03:42,150 --> 00:03:45,950 So we ask Python to print each letter of the string. 52 00:03:45,940 --> 00:03:47,580 So in this case we have a string. 53 00:03:47,910 --> 00:03:52,480 So we ask Python to print every letter of this string. 54 00:03:52,590 --> 00:03:53,220 All right. 55 00:03:53,230 --> 00:04:01,670 And there are a thing in which you can use the for loop while the loop statement will be for example 56 00:04:01,670 --> 00:04:03,170 with the British statement. 57 00:04:03,170 --> 00:04:05,860 So in this case how we can run all this. 58 00:04:05,870 --> 00:04:06,880 Well it's very simple. 59 00:04:06,980 --> 00:04:09,800 Let's say for example we once again have a set of colors. 60 00:04:09,830 --> 00:04:13,400 So let's name it color colors. 61 00:04:13,700 --> 00:04:17,960 In this case we write on equal and we will put our colors here. 62 00:04:17,990 --> 00:04:21,110 So let's say we have black and 63 00:04:26,560 --> 00:04:27,970 yeah. 64 00:04:31,310 --> 00:04:31,700 And 65 00:04:34,880 --> 00:04:35,410 green. 66 00:04:38,050 --> 00:04:40,850 And finally the last one would be red. 67 00:04:41,140 --> 00:04:41,340 All right. 68 00:04:41,350 --> 00:04:50,470 We have our four colors and right now what we can use also let's say we want to stub the eighth iteration 69 00:04:50,800 --> 00:04:51,240 at Green. 70 00:04:51,280 --> 00:04:55,960 So everything will stop at the green so will only generate black yellow and green. 71 00:04:55,960 --> 00:04:57,640 In this case so very simple. 72 00:04:57,760 --> 00:05:00,540 The way it's gonna be written down. 73 00:05:00,550 --> 00:05:02,830 We will use our for loop of course. 74 00:05:02,830 --> 00:05:07,090 So for example in this case it's colors. 75 00:05:07,330 --> 00:05:08,530 So let's name it. 76 00:05:08,650 --> 00:05:12,050 Let's say for one color. 77 00:05:12,850 --> 00:05:19,300 But it could be just X or Y or any letter in colors. 78 00:05:19,300 --> 00:05:21,280 So in this list right here 79 00:05:24,390 --> 00:05:27,930 print one color 80 00:05:33,450 --> 00:05:33,730 all right. 81 00:05:34,090 --> 00:05:34,560 So what. 82 00:05:34,690 --> 00:05:37,290 All those were what all this means. 83 00:05:37,330 --> 00:05:39,790 So very simple for one color. 84 00:05:40,030 --> 00:05:45,990 So only one color in colors in this list print one color. 85 00:05:46,000 --> 00:05:46,800 So what's gonna happen. 86 00:05:46,900 --> 00:05:50,190 It's gonna print each color one after the other. 87 00:05:50,230 --> 00:05:53,960 So if we run this up as so you have black yellow green the red. 88 00:05:54,070 --> 00:06:01,600 But if we want to stop our iteration at Green for example it's very simple we can use an if statement 89 00:06:02,140 --> 00:06:09,600 and simply write down one color if one color is equal to. 90 00:06:09,670 --> 00:06:17,740 In this case green we want the loop to stop. 91 00:06:17,950 --> 00:06:20,280 So a simple two points and write down brick. 92 00:06:20,290 --> 00:06:25,660 So in this case we'll use the break statement and if we run our app as you can see we'll have black 93 00:06:25,720 --> 00:06:26,800 yellow and green. 94 00:06:26,910 --> 00:06:29,300 Everything is going to stop at Green. 95 00:06:29,330 --> 00:06:38,030 So once again this is just by using the well the how you call it the if statement of the bridge statement 96 00:06:38,040 --> 00:06:38,300 sorry. 97 00:06:38,570 --> 00:06:41,300 So if you use the brake statement it's going to all stop again. 98 00:06:41,380 --> 00:06:42,620 Let's again see the loop. 99 00:06:42,680 --> 00:06:48,380 Well the for loop is something that is not hard the very very hard to understand and it's something 100 00:06:48,380 --> 00:06:49,620 that is very simple. 101 00:06:49,730 --> 00:06:57,900 The main goal of the for loop is simply to print everything that can be in a list in a in a string in 102 00:06:57,900 --> 00:06:58,710 numbers. 103 00:06:58,740 --> 00:07:00,510 Well absolutely anywhere. 104 00:07:00,540 --> 00:07:04,730 So let's have another example. 105 00:07:04,740 --> 00:07:07,530 So in this case we'll use another. 106 00:07:07,540 --> 00:07:12,210 Well we'll talk about another statement which is the range statement. 107 00:07:12,210 --> 00:07:16,920 And in this case let's say we have four. 108 00:07:16,920 --> 00:07:21,340 Let's use let's let's call it number four. 109 00:07:21,350 --> 00:07:27,610 Number in range six 110 00:07:30,410 --> 00:07:33,160 print number. 111 00:07:33,150 --> 00:07:40,620 All right if we run this up as you can see it's gonna print all the numbers from 0 to 6 but it's not 112 00:07:40,620 --> 00:07:42,030 going to print the number 6. 113 00:07:42,240 --> 00:07:47,550 And if for example we want to print just numbers that are in let's say from one number to another. 114 00:07:47,550 --> 00:07:51,130 So in this case let's say want to print out the numbers between them. 115 00:07:51,150 --> 00:07:57,570 I don't know it could be 20 and 30 for example you can just write it this way. 116 00:07:58,050 --> 00:08:03,150 And from our app as you can see it's going to put on all the numbers that are between 20 and 30 but 117 00:08:03,150 --> 00:08:06,300 just not the 30 that you can see. 118 00:08:06,300 --> 00:08:08,710 It's not something that is very very hard to understand. 119 00:08:08,820 --> 00:08:11,810 And those are simply the basics of the for loop. 120 00:08:11,880 --> 00:08:16,010 So the way that you can use it can be way more complex. 121 00:08:16,010 --> 00:08:20,760 But in this case this is just this simple the simple part of the fault. 122 00:08:20,980 --> 00:08:26,850 So as you can see guys as I explained to the for loop the main reason why the for loop exists is really 123 00:08:26,850 --> 00:08:31,950 to help you print all the elements of let's say you could be at least it could be a dictionary. 124 00:08:31,950 --> 00:08:34,770 It could be a string could be numbers in range of numbers. 125 00:08:34,770 --> 00:08:37,860 It could be well absolutely anything. 126 00:08:37,860 --> 00:08:39,620 So right now you understand how it works. 127 00:08:39,660 --> 00:08:41,170 So that's it for this class guys. 128 00:08:41,190 --> 00:08:43,050 And see you all in our next class.