WEBVTT 00:05.410 --> 00:10.100 All right so in this video we're going to look at some more uses for generators to get a feel for how 00:10.100 --> 00:11.630 they can be useful. 00:11.630 --> 00:17.370 Now the usual examples tend to be things like returning Fibonacci numbers or some other series. 00:17.420 --> 00:22.460 So I'm not going to break with tradition but I'm going to start by showing a neat trick that you can 00:22.460 --> 00:23.810 do in Python. 00:23.840 --> 00:28.430 We're going to use in a new pattern for which I want to call scratchpad PI 00:32.170 --> 00:34.890 scratchpad not pi. 00:35.100 --> 00:40.030 They've been saying this before I worked it out but if not here's an efficient way to swap two values 00:40.120 --> 00:40.930 in Pozen. 00:41.200 --> 00:48.940 So I got to start by talking a lot and one I was too slow and B was three going to do a print on line 00:48.950 --> 00:51.480 three parentheses double quite. 00:51.630 --> 01:02.000 I equals what I would be better than a left or right curly braces comma and B is Mr. Right curly braces 01:02.170 --> 01:04.320 top format. 01:04.610 --> 01:05.160 That's kind of me. 01:05.210 --> 01:07.860 I come on SPICE. 01:08.720 --> 01:11.760 And then the line following would do to swaps. 01:11.800 --> 01:17.080 I comma spice B equals B comma spice high. 01:17.150 --> 01:20.350 And if we just copy line three again. 01:20.350 --> 01:22.390 So print said exactly the same. 01:22.600 --> 01:29.700 And if we actually run this you can say that I started having the value of to base that in that having 01:29.700 --> 01:35.540 the value of three and the values are reversed after the code on Lot 5 is executed. 01:35.550 --> 01:40.350 So in many other computer languages you'd have to introduce a temporary variable to store one of the 01:40.350 --> 01:40.840 values. 01:40.860 --> 01:46.530 So in other words you'd have to do something like this to achieve the same goal so you'd normally have 01:46.530 --> 01:50.880 three tempi because I was some tempy they were able. 01:51.360 --> 01:58.350 I was on the next line and that was tip of your run that would get the same results as we got previously 01:58.360 --> 02:00.480 when we read this program. 02:00.480 --> 02:01.820 And if I just comment those laws at the 02:06.530 --> 02:11.720 Essentially this technique relies on the fact that the right hand side of an assignment is evaluated 02:11.720 --> 02:17.320 first then the results assigns to the variable or variables in this case on the left. 02:17.330 --> 02:23.410 So effectively in this case what we're getting is the equivalent of this three commas guys to just put 02:23.410 --> 02:25.510 it up for now. 02:25.640 --> 02:31.970 That's due to the fact that positons evaluation of the right hand side of the assignment first. 02:32.030 --> 02:37.090 Now done that for a reason and that's because we're going to be using this technique in a generator. 02:37.280 --> 02:39.920 And that's because it makes the kind of thing a bit simpler. 02:40.550 --> 02:45.830 All right so a Fibonacci number is the sum of the previous two numbers in the sequence with the first 02:45.830 --> 02:49.320 two values being either 0 1 0 1 and 1. 02:49.510 --> 02:52.470 That is a good definition which I very quickly opened and show you 02:55.590 --> 02:59.020 the link and a Fibonacci number on Wikipedia. 02:59.020 --> 03:03.420 Now I'm not going to read through the whole article but it's a fascinating sequence with all sorts of 03:03.420 --> 03:05.090 useful applications. 03:05.190 --> 03:09.750 Now Fibonacci numbers actually appear in nature to me and as examples of things like to White Point 03:09.750 --> 03:11.750 apples and artichokes cry. 03:12.130 --> 03:14.450 Now a bit further down here is where you have a look. 03:15.820 --> 03:21.100 You say here is this list of things and actually numbers and it's a type of showing the first 21 Fibonacci 03:21.100 --> 03:22.430 numbers in the sequence. 03:22.450 --> 03:27.010 So we're going to use that to check out values to make sure that that program is working correctly. 03:27.010 --> 03:33.220 All right so back to intelligent and we're going to create new path and fall it is Tom will actually 03:33.220 --> 03:36.250 create one called object pi. 03:40.030 --> 03:46.030 And that's to fight a function and actually so spice Fibonacci. 03:46.090 --> 03:51.800 I would say hi and then print my parameters in a colon. 03:51.850 --> 03:58.510 Stop putting count equals zero previous equals 1. 03:58.510 --> 04:03.610 Now we can actually combine them into a single statement by packing a top all which is what that technique 04:03.610 --> 04:10.180 I show daily is actually doing start to initialize that we can do correct and change up to cover previous 04:11.440 --> 04:13.590 equals zero 1. 04:13.920 --> 04:16.620 So that's the equivalent of what I talked previously. 04:16.640 --> 04:17.780 That would certainly work. 04:18.220 --> 04:26.470 And let's go ahead now and we're going to talk while true capital T there were the top covered Komarr 04:26.510 --> 04:38.410 spice previous is equal to current plus previous spice current or yield Cobert. 04:38.920 --> 04:46.180 And you can see now why we talked about you and even this section of this code on line for current gets 04:46.180 --> 04:52.240 the value zero plus 1 and previous gets the old value of current which is the first time it runs is 04:52.240 --> 04:53.070 zero. 04:53.230 --> 04:56.260 Next time around current becomes 1 plus zero. 04:56.470 --> 05:00.240 So once again in other words and previous This Tom becomes one. 05:00.510 --> 05:06.250 And third time around it becomes two previous is one that we get three and two followed by five and 05:06.250 --> 05:07.410 three and so on. 05:07.660 --> 05:10.680 So the big test now will be to see whether this works. 05:10.690 --> 05:14.300 So let's actually try adding some code to check that out. 05:14.380 --> 05:20.840 We're going to top flip because Fibonacci to call our function. 05:20.950 --> 05:30.220 And then we get into print the 21 Fibonacci numbers are print next flip and we want to do the 21 so 05:30.220 --> 05:33.050 we're going to actually copy and paste this another 20 times. 05:33.280 --> 05:34.530 21 lines in total 05:39.650 --> 05:45.230 that should be 21 because that's what you want it to but I'm going to add 21 in this case. 05:45.230 --> 05:53.540 All right so I just wrote this and have a look at some of those numbers there are 1 1 2 3 5 8 13 and 05:53.580 --> 05:59.290 we got it back and have a look at the title 0 1 1 2 3 4 by 13. 05:59.410 --> 06:03.180 And just to be consistent if you want to sequence the start of 0. 06:03.310 --> 06:04.900 You just swap lines 4 and 5 around. 06:04.900 --> 06:08.080 So we return the value of current before calculating the next number. 06:08.170 --> 06:17.890 So you could get back there to intelligence and we can put the yield before the actual and change the 06:17.890 --> 06:19.600 values and actually run that again. 06:22.900 --> 06:33.960 And then we quickly go up to zero studies 0 1 1 2 3 5 8 8 0 1 1 2 3 5 8 13 and 21 34 50 followed by 06:33.970 --> 06:38.180 the dog twenty three point fifty five. 06:38.240 --> 06:47.960 And you can see right through to the last three there are about 4 4 1 8 1 6 7 6 5 2 5 4 4 1 8 1 6 7 06:47.960 --> 06:48.470 6 4. 06:48.470 --> 06:50.690 So I can see it that's working mostly. 06:50.840 --> 06:51.110 All right. 06:51.110 --> 06:57.560 So that's actually a generator that returns successive Fibonacci numbers each time we call next to get 06:57.560 --> 06:58.870 the next value. 06:59.380 --> 07:07.020 So what we've defined here on our mind in our function in a spotlight run by the way is to get that 07:07.020 --> 07:13.010 and fixed that up and change the function call as well calculating Fibonacci numbers. 07:13.040 --> 07:15.920 Sure you actually get the name of the function correct. 07:16.140 --> 07:19.530 Well I notice that this is an infinite generator. 07:19.530 --> 07:23.580 In other words you will keep on generating the next number in the sequence and that's why I've used. 07:23.580 --> 07:30.360 Next he calls between last 10 through 30 rather than trying to loop through the valleys. 07:30.540 --> 07:35.410 We definitely wouldn't for example one of the something like this I just put in temporarily. 07:36.180 --> 07:42.420 So we wouldn't want to do three four if fib call them print. 07:42.770 --> 07:46.770 If you do that just temporarily to have these prints 07:49.520 --> 07:56.370 we actually run this very quickly we get huge numbers that he can say is definitely not the pot that 07:56.370 --> 08:02.580 will now we're actually looking for someone to stop that know many languages were either for the maximum 08:02.580 --> 08:07.800 value that you can store in in it but Python doesn't have that limitation if it's a single valued would 08:07.800 --> 08:12.210 get so large that it takes the full 32 gigabytes of computers to store it. 08:12.360 --> 08:15.000 But that would take a long time if we had enough memory. 08:15.000 --> 08:16.600 This is an infinite series. 08:16.860 --> 08:21.060 All right you could say that I stopped that anyway otherwise I would go on for a very long time so I'm 08:21.060 --> 08:28.100 going to do that and I'll do those changes go back to what we had previously like so that series can 08:28.100 --> 08:33.770 be useful even though you'll never get around to generating an infinite number of values to see how 08:33.770 --> 08:34.640 they can be useful. 08:34.670 --> 08:37.100 Let's look at where calculating the value of pi. 08:37.460 --> 08:43.760 Now Matt spade up a thing but this is a simple example and it includes a neat way that a generator can 08:43.760 --> 08:45.020 make code simpler. 08:45.280 --> 08:50.220 And it also provides a simple challenge just to get you used to creating your own generators. 08:50.240 --> 08:52.840 Now there's loads of ways to calculate pi. 08:52.880 --> 08:58.490 We're going to use an infinite series attributed to Leibnitz who's expanding on the work of Madhava 08:58.520 --> 09:04.820 of saying grab a report restaurant who was an Indian mathematician from about 300 years ago. 09:05.210 --> 09:08.160 Now we can have a look at the is a series. 09:08.210 --> 09:17.280 Let's kind of quickly put up a link to a Wikipedia link up into tabun post that in his formula for working 09:17.290 --> 09:22.660 at I did the series overall was quite simple and it gives Pied-Bot about four. 09:22.680 --> 09:23.940 So he went pi. 09:23.940 --> 09:24.970 So what a multiplier. 09:24.970 --> 09:28.040 Therefore by solids but for for that to happen. 09:28.080 --> 09:34.950 So Paul can be calculated in other words for more than four thirds plus four fifths Marla's four sevenths 09:35.220 --> 09:40.390 plus five dots and so on and so forth is the same as four to water by one. 09:40.410 --> 09:44.670 So we need to start with a sequence of numbers 1 3 5 7 etc.. 09:44.730 --> 09:50.480 Right so let's talk about the challenge and you get the data in the past and fall for this or what. 09:50.480 --> 09:56.530 So the challenge is to create a generator to return an infinite sequence of odd numbers starting at 09:56.540 --> 09:57.460 1. 09:57.510 --> 10:01.170 So the first 100 numbers to check that the generator is working correctly. 10:01.550 --> 10:03.640 And note that this is just for testing. 10:03.710 --> 10:08.040 We're going to need far more than 100 numbers and we don't know in advance how many. 10:08.060 --> 10:12.040 So that's why we're creating our own generator instead of just using a range. 10:12.050 --> 10:14.700 So that's the challenge say hey you guys post the video. 10:14.750 --> 10:17.360 I'll see you when you get back with the solution. 10:19.540 --> 10:19.870 All right. 10:19.870 --> 10:25.240 So there's not actually too many ways to wrought this so you should end up looking similar to this. 10:25.450 --> 10:28.120 So going to create that new followers are talked about. 10:28.200 --> 10:33.840 We're going to call this one pod generation or two. 10:36.010 --> 10:42.060 And we're going to define our numbers functions how deep space odd numbers parentheses. 10:42.120 --> 10:48.800 Caller ID any clue is one that we're going to talk wall true with a capital T. 10:49.490 --> 10:50.950 And you. 10:52.030 --> 10:55.080 And then add spice. 10:55.270 --> 10:59.110 It's like n plus equals 2. 10:59.450 --> 11:08.420 And then in terms of the card odds is equal to our numbers so we're going to call the function that 11:08.440 --> 11:11.030 we're going to do a test here to make sure it's working by using a right. 11:11.030 --> 11:28.520 So for all I in range 100 parentheses colon print next odds and pinchy run that say 1 3 5 9 11 13 15 11:28.520 --> 11:31.670 17 on and 22 and 23 and it seems to be working for. 11:32.030 --> 11:35.950 Right so that's just an odd number generator of course but not actually calculating the value of pi 11:36.000 --> 11:36.920 xt. 11:36.920 --> 11:42.550 So let's go ahead and see how we can use this odd number generator to calculate the value of pi. 11:42.980 --> 11:49.200 So we don't need this test card so I'm going to delete that instead we're going to do is to find another 11:49.200 --> 11:50.040 function. 11:50.220 --> 11:57.840 We're going to default actually to discard asked for the method call only that is defined or def spies. 11:58.000 --> 11:59.340 Def pointless car series 12:02.250 --> 12:07.170 series parentheses colon and we can do odds equals odd numbers. 12:07.170 --> 12:16.140 I'm going to call our numbers function the summation is equal to zero while true. 12:16.910 --> 12:21.480 Now we're going to need to use our number generator but also set the initial value to zero and that's 12:21.480 --> 12:23.570 what I've done there with the approximation. 12:23.730 --> 12:24.970 And we need an infinite loop. 12:24.970 --> 12:28.050 So that's while we're at it the while true on line 11. 12:28.050 --> 12:32.770 Now the body of the loop will get the next number in the sequence and divided into four. 12:33.010 --> 12:39.420 Now we're going to continue doing that and alternately adding and subtracting the result to our approximation. 12:39.570 --> 12:45.450 Now using conventional code you may try to keep track of whether the last value was added or subtracted 12:45.790 --> 12:46.650 did the opposite. 12:46.650 --> 12:47.770 Next time around. 12:48.060 --> 12:54.420 It's not that hard just a bit fiddly but it's also necessary because if you remember that a generator 12:54.420 --> 12:58.090 will continue from where it left off after yielding a value. 12:58.350 --> 13:04.350 So we can at the opposite couriered after the yield and then you can just go ahead and do that. 13:04.350 --> 13:05.840 See if that makes sense. 13:05.850 --> 13:13.850 So it was true that we've got there and we're going to do approximation plus equals parentheses four 13:14.280 --> 13:20.260 divided by next odds two parentheses. 13:20.260 --> 13:21.440 Close it off. 13:21.480 --> 13:25.830 We're going to do a yield approximation. 13:25.830 --> 13:32.340 Then on the next line we're going to do the other one approximation Moine as equals four divided by 13:32.340 --> 13:35.500 next odds. 13:36.060 --> 13:45.450 So the two princes closing it off that we are going to use that against a yield approximation. 13:45.450 --> 13:52.050 So the first time round the loop the code will have a 4 divided by the first number when it Eudes the 13:52.050 --> 13:52.670 result. 13:52.770 --> 13:59.220 The generator remembers it state and exits when we ask for the next approximation will actually continue 13:59.340 --> 14:01.900 from line 14 and here. 14:02.190 --> 14:04.180 So this time the bell is subtracted. 14:04.320 --> 14:08.200 The next time around the loop the is added then subtract it and so on. 14:08.280 --> 14:13.650 They can actually have as many use as she need inside conditions if necessary and the generator picks 14:13.650 --> 14:18.720 up the code after last time it yields a result and that I think is a very useful feature. 14:18.870 --> 14:22.660 So bear that in mind even if you're not interested in calculating the value of pi. 14:23.160 --> 14:25.790 And I should point out that there's no need to calculate it. 14:25.800 --> 14:28.130 People have actually been doing that for centuries. 14:28.260 --> 14:33.960 It's been calculated to billions of digits but that's of any use is 16 significant digits to put a prober 14:33.960 --> 14:38.880 in setten to an accuracy of within a matter of survival distance of more than a billion kilometers. 14:39.240 --> 14:42.500 Of course there are other mathematical uses for infinite series. 14:42.720 --> 14:46.220 So if you were a more mathematically inclined student you should find this useful. 14:46.500 --> 14:51.150 But if you're not into math so come back to why you want to use an infinite series in a moment. 14:51.150 --> 14:55.210 But first I importantly the second to see whether this is going to work or not. 14:55.380 --> 14:59.870 So what I'm going to do is add some cardio to call a PI series. 15:00.510 --> 15:02.700 Let's go ahead alone I take it at approx. 15:02.700 --> 15:08.370 I was called pi is equal to point of score series with the caller function. 15:08.460 --> 15:14.790 There we're going to use a range of Foluke to add a range for x in range. 15:15.540 --> 15:22.130 Start up with one print next approximate as call pi. 15:22.950 --> 15:29.530 So eventually run that then we get the value for point zero which is neither accurate or useful. 15:29.920 --> 15:33.670 So obviously we need to generate a few more terms to get closer to pi. 15:34.030 --> 15:41.880 So I actually change one the range to 10 is rather well that's looking a little bit better. 15:42.090 --> 15:47.820 That's three point forty three point one for the Vinney we're looking for is three point 1 4 1 5 9 2 15:47.820 --> 15:48.640 6. 15:48.990 --> 15:55.040 So let's try it with a hundred and see what that comes out it will just read that again this time about 15:55.040 --> 15:56.590 got three point one three. 15:56.690 --> 16:01.810 And then in the range say three part 1 5 1 6 9 3 3 1 1 3 1 5 9 2 9. 16:01.950 --> 16:05.070 So it's getting better but let's actually try it with a thousand 16:08.140 --> 16:10.340 and this tubber up to three put one four. 16:10.620 --> 16:16.190 So getting pretty close to three for one for one foxhunt still add little bits of 10000. 16:16.200 --> 16:17.360 Let's try that again. 16:18.560 --> 16:23.120 So three point 1 4 1 4 9 is close to three point 1 4 1 4. 16:23.150 --> 16:25.260 So the series is starting to converge. 16:25.370 --> 16:27.230 And one other one if I try say 100000 16:30.070 --> 16:33.440 three point 1 4 1 4 8 is getting much closer. 16:33.500 --> 16:35.850 And let's just try one other one Troy million 16:40.050 --> 16:44.700 and it could say the values at three point 1 4 1 5 9 instead of 2 6 1 6 or so. 16:44.740 --> 16:50.900 Obviously getting really close and you can see also that it took a while to actually to calculate that. 16:50.920 --> 16:55.510 So what you're going to take is 10 million times. 16:55.860 --> 17:00.150 Let's roll that and that's not bad is it. 17:00.430 --> 17:05.390 We're getting this sort of level of accuracy with just a few lines of code which is pretty cool. 17:06.450 --> 17:10.110 So while that's executing we talk about why I want to use an infinite generator. 17:10.200 --> 17:13.970 So just why would you want to use an infinite generator unless you're into mathematics. 17:14.280 --> 17:16.830 Well consider that infinite doesn't have to mean infinite. 17:16.830 --> 17:18.780 It can just mean unknown. 17:19.200 --> 17:23.260 As an example consider Google indexing web pages on the Internet. 17:23.280 --> 17:29.510 They send out robots basically courier that visits all the pages on a site and indexes the content. 17:29.530 --> 17:33.360 Now the card itself in say they'll get back to that in a minute because you can see now that we've actually 17:33.360 --> 17:36.940 finished the three point 1 4 1 5 9 to 5. 17:37.170 --> 17:43.560 So we've got a range of three point 1 4 1 5 9 2 7 3 4 1 4 1 5 9 2 5 and obviously the value we're looking 17:43.560 --> 17:46.390 for is 3 1 4 1 4 96. 17:46.730 --> 17:50.390 So I think that's seriously impressive to get that level of accuracy. 17:50.490 --> 17:51.690 Just a few lines of card. 17:51.690 --> 17:54.000 That's the power of using generators. 17:54.570 --> 17:59.580 But getting back to the Google example and indexing of visiting all pages on a site and indexing the 17:59.580 --> 18:04.530 content Well the Kurds got no idea in advance how many pages there are on each side. 18:04.740 --> 18:07.630 So there's no use passing a number to generate it. 18:07.770 --> 18:09.970 Now of course the general is not really infinite. 18:10.050 --> 18:11.840 There will be a terminating condition. 18:11.970 --> 18:14.710 Once it starts visiting pages that have already been indexed. 18:14.910 --> 18:20.040 So the point here is that the generator will decide when to terminate the calling program hasn't got 18:20.040 --> 18:23.380 any idea what we're going to see that in action in the next video. 18:23.550 --> 18:28.510 When we use a generator to work with the computer's filing system so see you in the next video.