1 00:00:00,009 --> 00:00:01,519 Welcome back in the previous lesson, 2 00:00:01,529 --> 00:00:05,980 we successfully learned how to generate a random letter 3 00:00:06,250 --> 00:00:12,949 by making use of the random module. And also the random dot choice method 4 00:00:13,390 --> 00:00:18,059 a question I have here is, well, what if we wanted to generate multiple letters? 5 00:00:18,069 --> 00:00:19,510 And not just one, 6 00:00:19,850 --> 00:00:24,170 what we have to do is to, first of all, change the method from random dot choice 7 00:00:24,440 --> 00:00:26,889 to random dot choices 8 00:00:27,030 --> 00:00:29,940 because now it's plural, we're dealing with more than one letter. 9 00:00:30,350 --> 00:00:33,970 Another thing about random dot choices as a method of function 10 00:00:33,979 --> 00:00:37,069 is that it's going to accept two parameters instead of one. 11 00:00:37,360 --> 00:00:38,419 The first will be 12 00:00:39,139 --> 00:00:40,479 what we call the population, 13 00:00:40,490 --> 00:00:44,069 which is basically where we're pulling the letters from. 14 00:00:44,330 --> 00:00:45,720 And then the second 15 00:00:45,840 --> 00:00:48,689 is going to be the actual number 16 00:00:48,919 --> 00:00:51,119 of letters we want to generate. 17 00:00:51,130 --> 00:00:54,610 So in this case, right now, I'm going to say K equals three. 18 00:00:54,939 --> 00:00:58,810 All right. And now I can change the print text from 19 00:00:59,060 --> 00:01:02,680 letter to letters and then change it to R. 20 00:01:03,000 --> 00:01:10,419 Now, if I run the program, we have K RJ, I can run it again. We have QMW. Right. 21 00:01:10,430 --> 00:01:13,209 So you can keep that in mind. 22 00:01:13,430 --> 00:01:14,300 However, 23 00:01:14,680 --> 00:01:20,690 we want to move a step further to generate a random password. 24 00:01:21,000 --> 00:01:22,839 So we're no longer just dealing with letters. 25 00:01:22,849 --> 00:01:27,430 We're now going to combine letters with uppercase and numbers and so on. 26 00:01:27,800 --> 00:01:32,449 And I'm pretty sure you've seen random password generators before. 27 00:01:33,199 --> 00:01:35,620 Maybe it's on the websites where you are asked to create an account. 28 00:01:35,870 --> 00:01:40,180 You want to create an account and then they offer to generate a password for you, 29 00:01:40,260 --> 00:01:42,389 which you can then change later. 30 00:01:42,430 --> 00:01:48,069 I'm gonna show you how we can create our random password generator. 31 00:01:48,480 --> 00:01:51,800 And we're just gonna use five lines to do 32 00:01:51,919 --> 00:01:52,779 this. 33 00:01:52,970 --> 00:01:57,459 In fact, four lines, just four lines. OK? You don't believe me, take a look at this. 34 00:01:57,639 --> 00:01:59,209 So first of all, 35 00:01:59,620 --> 00:02:02,080 we're going to keep the import random because obviously 36 00:02:02,089 --> 00:02:04,800 we want to randomize the letters and numbers. 37 00:02:05,099 --> 00:02:06,470 But we're gonna import 38 00:02:07,199 --> 00:02:09,800 a library here called string 39 00:02:10,339 --> 00:02:15,529 because we're gonna be manipulating our strings which will include letters, 40 00:02:15,539 --> 00:02:17,070 numbers and so on. 41 00:02:17,080 --> 00:02:19,039 So we have import string 42 00:02:19,610 --> 00:02:23,619 and now I'm going to create a variable that will represent the password. 43 00:02:23,630 --> 00:02:25,610 I'm gonna call it password 44 00:02:25,869 --> 00:02:27,210 equals. 45 00:02:27,520 --> 00:02:30,940 And I remember from the previous lesson, I'm gonna say random. 46 00:02:31,300 --> 00:02:31,940 OK 47 00:02:32,119 --> 00:02:34,369 dot Choi says, 48 00:02:34,529 --> 00:02:36,979 because now we're going to be dealing with multiple letters 49 00:02:37,550 --> 00:02:42,649 and now in brackets, remember the first parameter for this function or method 50 00:02:42,949 --> 00:02:44,600 is going to be the population. 51 00:02:44,610 --> 00:02:49,929 We have to tell the function where to pull the string from the letters from. 52 00:02:50,330 --> 00:02:52,399 The thing about the string 53 00:02:53,190 --> 00:02:54,380 library 54 00:02:54,500 --> 00:02:55,759 is that let me just show you 55 00:02:56,460 --> 00:02:58,509 over here the string module. But 56 00:02:59,029 --> 00:03:03,990 we have a variety of methods or in this case, we call them constants. 57 00:03:04,660 --> 00:03:06,100 So we can say string 58 00:03:06,679 --> 00:03:09,970 key underscore letters, this will pull in letters 59 00:03:10,229 --> 00:03:13,720 that are both capital, lower case. 60 00:03:13,990 --> 00:03:15,960 And then we can specify, we can say string 61 00:03:16,399 --> 00:03:19,729 key, lower case, this will pull in only lower case letters. 62 00:03:19,979 --> 00:03:22,639 And then we have for upper case, we have for digits, 63 00:03:22,955 --> 00:03:24,324 digits, punctuation 64 00:03:24,485 --> 00:03:29,414 and so on. So we have all these methods, all this constant that we can work with 65 00:03:29,684 --> 00:03:32,455 when importing the string module. So 66 00:03:32,654 --> 00:03:34,125 let me drag this one away. 67 00:03:34,445 --> 00:03:39,455 What we're gonna do right now is for the random choices, we want to pull 68 00:03:39,725 --> 00:03:41,005 string 69 00:03:41,835 --> 00:03:42,145 dot 70 00:03:42,384 --> 00:03:42,845 as 71 00:03:43,044 --> 00:03:43,134 key 72 00:03:43,985 --> 00:03:46,914 and now underscore letters, 73 00:03:47,294 --> 00:03:50,205 ok? We're gonna pull random 74 00:03:50,649 --> 00:03:51,710 letters. 75 00:03:51,889 --> 00:03:54,729 And then in addition, plus 76 00:03:54,910 --> 00:03:58,509 we also want to pull in what digits 77 00:03:58,850 --> 00:04:01,050 we want to have numbers 78 00:04:01,210 --> 00:04:03,910 in our password as well. 79 00:04:04,089 --> 00:04:07,470 So basically we're telling the function we're telling Python that hey, 80 00:04:07,710 --> 00:04:10,630 for the choices of letters 81 00:04:11,220 --> 00:04:16,089 for the password, we're gonna pull in letters that are both uppercase and lowercase 82 00:04:16,209 --> 00:04:19,358 and then also digits as well. 83 00:04:19,488 --> 00:04:20,070 And then of course, 84 00:04:20,079 --> 00:04:24,519 the second parameter we need to specify how many 85 00:04:24,529 --> 00:04:27,989 letters digits are going to be in our password. 86 00:04:28,000 --> 00:04:29,570 So I'm gonna say K 87 00:04:30,239 --> 00:04:32,920 equals and let's go with 10. 88 00:04:33,320 --> 00:04:35,559 Ok? So a pretty strong password 89 00:04:35,910 --> 00:04:39,359 and now all we have to do is what just print, 90 00:04:40,089 --> 00:04:41,109 we're gonna print 91 00:04:41,549 --> 00:04:43,339 and I'm gonna say uh 92 00:04:43,950 --> 00:04:45,760 generated 93 00:04:46,230 --> 00:04:49,149 so generated password. 94 00:04:51,220 --> 00:04:51,959 Ok. 95 00:04:53,459 --> 00:04:54,309 And then 96 00:04:54,529 --> 00:04:57,140 let me add my comma and then password, 97 00:04:58,570 --> 00:04:59,079 that's it. 98 00:04:59,720 --> 00:05:01,299 We're going to generate a password 99 00:05:02,470 --> 00:05:05,760 and now let's try it. Ok. So I'm going to go ahead right now, run the program 100 00:05:06,079 --> 00:05:06,910 and there you go. 101 00:05:07,049 --> 00:05:08,730 We have capital K, 102 00:05:08,890 --> 00:05:13,130 we have six, we have capital F, we have 60, small letter 103 00:05:13,260 --> 00:05:18,160 H, capital J zero, capital W and then capital R. 104 00:05:18,369 --> 00:05:21,369 So you can see right now, it successfully created 105 00:05:22,049 --> 00:05:26,730 it passed with 10 characters. We had upper case letters, lower case letters 106 00:05:26,859 --> 00:05:29,790 and a few numbers as well. 107 00:05:30,209 --> 00:05:31,070 But 108 00:05:31,630 --> 00:05:34,390 what if, what if instead 109 00:05:34,809 --> 00:05:39,480 of having the characters separated by a comma and codes, 110 00:05:39,529 --> 00:05:42,149 we want to join everything together. 111 00:05:42,160 --> 00:05:44,820 So we have just one single string. 112 00:05:45,209 --> 00:05:48,130 How are we going to do this? 113 00:05:48,440 --> 00:05:52,579 We will have to make use of a special function called 114 00:05:52,750 --> 00:05:54,420 the join 115 00:05:54,549 --> 00:05:55,119 function. 116 00:05:55,220 --> 00:05:56,640 It's actually a method. 117 00:05:57,010 --> 00:05:57,519 So 118 00:05:57,750 --> 00:05:59,820 what we want to do right now is 119 00:05:59,929 --> 00:06:02,899 over here where it says random 120 00:06:03,029 --> 00:06:04,109 choices. 121 00:06:04,299 --> 00:06:08,500 We're going to add this very special 122 00:06:08,850 --> 00:06:09,890 function 123 00:06:10,309 --> 00:06:12,239 called join and it goes like this, 124 00:06:12,820 --> 00:06:14,109 we're gonna have 125 00:06:14,609 --> 00:06:16,630 a single codes and then dots 126 00:06:17,690 --> 00:06:21,040 join. And now we'll have to open up a bracket to 127 00:06:21,200 --> 00:06:22,670 cover everything in here 128 00:06:23,920 --> 00:06:24,660 and there it is. 129 00:06:25,359 --> 00:06:29,380 So what this is going to do is that it's going to generate the password, 130 00:06:29,529 --> 00:06:33,089 but then all the characters will be joined together. 131 00:06:33,630 --> 00:06:35,260 Now, if I run the program one more time, 132 00:06:35,489 --> 00:06:36,380 there you go. 133 00:06:36,869 --> 00:06:39,980 Now we have it looking much better. I can run it again 134 00:06:40,250 --> 00:06:41,140 and there you go, 135 00:06:41,390 --> 00:06:42,579 run it one more time 136 00:06:42,829 --> 00:06:45,209 and there you go. 137 00:06:45,529 --> 00:06:49,709 So it's kind of amazing how with just four lines of code, 138 00:06:49,959 --> 00:06:52,950 we've been able to create our very own 139 00:06:53,260 --> 00:06:56,079 random password generator. 140 00:06:56,200 --> 00:06:58,459 We of course had to make use of two 141 00:06:59,109 --> 00:07:00,609 standard modules, the 142 00:07:00,739 --> 00:07:03,429 random module and then the string module and of course 143 00:07:03,579 --> 00:07:05,140 two additional lines of code 144 00:07:05,339 --> 00:07:07,049 and there it is. 145 00:07:07,190 --> 00:07:11,480 So that's it. Thank you for watching the video. I will see you in the next class.