1 00:00:00,570 --> 00:00:02,770 Welcome to another video of our Python lessons. 2 00:00:03,600 --> 00:00:06,150 Today, we're going to talk about loops in Python. 3 00:00:07,560 --> 00:00:11,920 Let's start with a simple prologue that prints a range of numbers using the range function. 4 00:00:13,110 --> 00:00:15,570 The range function can take two or three argument. 5 00:00:16,710 --> 00:00:19,110 Let's start by printing the numbers from zero to nine. 6 00:00:32,040 --> 00:00:34,410 Now let's use the same function. 7 00:00:34,410 --> 00:00:40,560 But this time we need to increment our numbers by two by adding number two as a third argument. 8 00:00:52,080 --> 00:00:52,470 Nice. 9 00:00:53,640 --> 00:00:59,450 Let's look at the nested for loops, assuming we have two lists. 10 00:00:59,880 --> 00:01:05,010 First one is one, two, three, four, and the second one is five, six, seven. 11 00:01:19,730 --> 00:01:24,080 Let's look through both of them using nested loop and see how it works. 12 00:01:38,590 --> 00:01:45,640 As you noticed in the loop, it will start with the first element in this one, which is number one, 13 00:01:46,630 --> 00:01:49,270 then it will loop through old elements list. 14 00:01:50,170 --> 00:01:54,520 And that's the first iteration of the loop, the second iteration. 15 00:01:54,520 --> 00:01:58,530 It will start from number two in this one here. 16 00:02:01,420 --> 00:02:04,740 Then again, all elements in list two and so on. 17 00:02:06,100 --> 00:02:09,760 So it took No one from list one here. 18 00:02:12,740 --> 00:02:20,270 And it went through whole numbers and a list to five, six and seven, and then it took number two from 19 00:02:20,270 --> 00:02:24,800 list one, which is two, and then it went through again, old numbers. 20 00:02:24,800 --> 00:02:30,790 It lists two, five, six and seven and then three from list one and then five, six and seven again. 21 00:02:32,000 --> 00:02:38,390 But what if we have two lists like this one, three, five and list two to four six? 22 00:02:38,960 --> 00:02:39,830 Let's create them. 23 00:02:54,350 --> 00:02:58,070 How can we loop through both of them and print the numbers in order? 24 00:02:59,480 --> 00:03:05,150 So basically you want to start from the first element in this one and then the first element in this, 25 00:03:05,150 --> 00:03:09,380 too, in order for us to do that, we can use that zip function. 26 00:03:10,880 --> 00:03:12,080 Let's see how it works. 27 00:03:25,620 --> 00:03:33,270 As we see here, first number, an ex is one and first number in line is two, then three, then four 28 00:03:33,270 --> 00:03:35,550 and finally five and six. 29 00:03:36,240 --> 00:03:37,930 See, the full loop is very simple. 30 00:03:38,040 --> 00:03:45,720 Now, let's switch to why loop the syntax in while loop starts with the word wild, followed by the 31 00:03:45,720 --> 00:03:50,910 condition you want this while loop to run on for an infinite while loop. 32 00:03:50,940 --> 00:03:52,470 We use the word true. 33 00:03:58,890 --> 00:04:00,120 Or the number one, 34 00:04:03,750 --> 00:04:07,110 let's first use while loop to print the numbers from zero to nine. 35 00:04:09,360 --> 00:04:13,660 We will first create a variable X and assign it a value of zero. 36 00:04:17,550 --> 00:04:21,600 Then we start our while loop and check if x less than 10. 37 00:04:28,210 --> 00:04:37,720 And then print X, so if X is less than 10, we print X and then increment incremented by one. 38 00:04:45,400 --> 00:04:51,940 Now, let's take a look at the infinite while loop, we will take an input from the user and print that 39 00:04:51,940 --> 00:04:55,480 input unless the user provided the word exit. 40 00:05:21,530 --> 00:05:25,430 We will check if the string equalled the word exit by using the statement. 41 00:05:35,500 --> 00:05:38,440 If that's true, then we print something to the screen. 42 00:05:49,830 --> 00:05:51,200 Oh, I always miss a double. 43 00:05:51,610 --> 00:05:51,990 OK. 44 00:06:08,460 --> 00:06:10,470 And then we break out of the wild loop 45 00:06:14,460 --> 00:06:21,210 belts, we print the my string, which we got from the user. 46 00:06:29,690 --> 00:06:31,040 Now, let's type exit. 47 00:06:33,080 --> 00:06:34,640 Thank you for using our program tonight. 48 00:06:35,720 --> 00:06:38,030 Let's take a look now at the continuing statement. 49 00:06:38,030 --> 00:06:38,780 And while loop 50 00:06:41,720 --> 00:06:47,300 by using the word continue, the loop will start over skipping all the other lines. 51 00:07:06,300 --> 00:07:12,080 Let us use the if statement to check if the string we are getting from the user is empty. 52 00:07:19,800 --> 00:07:27,510 If the user provided an empty string, the wire loop will start over asking the user again for another 53 00:07:27,510 --> 00:07:27,960 input. 54 00:07:32,310 --> 00:07:36,660 But if the user hasn't provided an empty string, we basically print it. 55 00:07:51,990 --> 00:07:55,080 Now, let's provide an empty line by just hitting enter. 56 00:07:58,570 --> 00:08:02,390 By hitting enter, we will never get to this line. 57 00:08:03,130 --> 00:08:09,480 It will basically come here and go back again and provide the user and ask him for another input. 58 00:08:10,270 --> 00:08:16,270 We sometimes use this continuous statement if we just want to move to an empty line without providing 59 00:08:16,270 --> 00:08:18,040 an input to the program. 60 00:08:20,110 --> 00:08:22,810 Do you remember where we did we see this before? 61 00:08:25,050 --> 00:08:25,600 Let's see. 62 00:08:30,320 --> 00:08:30,710 S.. 63 00:08:36,560 --> 00:08:40,310 We have reached the end of this lesson, thank you and see you in the next one.