© Distribution of this video is restricted by its owner
Transcript ×
Auto highlight
Font-size
00:07 greetings. This video is a short to modules in python. Let's get

00:18 . So it is also module five the course and turns out we use

00:25 same word. Again, it's also python modules. What is a

00:33 A module in a in python is python cord that's written by somebody for

00:41 by others. It's that's that's basically it's about. Something you develop and

00:48 it easy for others to use In this course, we've already used

00:54 and we were able to do a of things with turtles without getting into

01:00 nitty gritty ease of how to do graphics and all of that. And

01:05 because we were using the turtle So in general modules are pretty

01:11 They allow beginners to do some, know, really seriously interesting things with

01:19 because you just leverage somebody else's So you may not have the expertise

01:26 do something like the turtles module or programming or whatnot, but you may

01:34 able to use them and then in even build applications on top of

01:38 not necessarily knowing exactly how to do lower details that are left to other

01:44 . So that's the big idea. , in terms of the specific

01:52 python module is just another file that py. It just contains definitions and

02:00 that is used by other programs. , it's nothing, nothing else.

02:03 just another python python program or a containing python code, python has a

02:13 library that comes with a lot of that we can use in our

02:19 Um to see all the modules you go there. They're all over the

02:24 from math to random. That will today to what you can do on

02:30 web, graphics and whatnot. There's lot of stuff there. However,

02:35 all of it. In fact many those modules are not directly available for

02:41 rune stone environment we use for learning . So if you grow beyond the

02:48 we have here, you may need also grow into a more professional programming

03:00 . So let's get the hang of do we use a module? First

03:06 to use a module in your You have to have an import statement

03:13 looks something like this. You have module called Turtle, you say import

03:18 . And suddenly you have access to modules or all the codes in that

03:25 in that module. All the work in that module. That statement has

03:31 effect that once you've imported the module your program you can use total class

03:38 screen class. So these are Total screen are special types with which you

03:47 do special things. Remember the we know our strings into jails

03:52 So those types are defined by what be inside that type and what you

03:59 do with it like for integer, can do multiplication division addition and so

04:05 . So these are a little bit types of classes you that are defined

04:10 the module with which you can do interesting interesting things beyond those basic

04:17 So once you've done in four title you have access to the turtle with

04:22 upper testy class or type and screen or type. What can we

04:29 Now we um make variables that are those types. The screen type and

04:37 turtle type. And this is how do it. This is just a

04:41 that we picked. WN and Mark our variable and Turtle was the module

04:48 . And within that module. That , that the module defined is this

04:54 . So once we have, once made the statements, we have created

05:00 new objects or two new variables. is W N. Of the class

05:05 type screen, other is Mark of type turtle. So, and once

05:14 done with that type, you established imported the module, define your

05:20 Now you can do all kinds of stuff again using the dark notation that

05:25 use there where Now you can have statement like this. Mark is the

05:31 type, you say mark, forward and it moves forward and you can

05:36 it do all kinds of interesting things we saw in the turtles. The

05:41 . Mark shoes before. Okay, is one example of a module.

05:50 much more useful module is the math . The math module in python.

05:58 in any other programming. Anything you need in terms of uh mathematical

06:10 you're probably it's there in the math . So some examples here again to

06:18 the math module, just like the module, you just say important

06:23 And once you say important math, can say things like Math dot factorial

06:28 . It returns the factorial if X an integer math dot log base 10

06:35 . Based on the algorithm of facts math dot sine X. That's the

06:40 . No metric function returns the sine X in radiance. So there's many

06:45 these functions and you just have to them and play with them. Um

06:51 just not here that the usage in case is a little bit different from

06:59 we saw with the turtle module. we're not actually defining new types.

07:05 just makes these mathematical functions available to which perform a task and and return

07:14 value. There is no need to any special object. Should be in

07:18 even more internally that all you do math dot without a function. And

07:24 this is when you say something equals dot, whatever your variable on the

07:29 side gets that value. I think fairly straightforward. You just have access

07:36 all these mathematical functions with the math . We will talk about one more

07:46 in this course and in this which is the random module.

07:52 first, a little bit of introduction random numbers in a lot of applications

08:00 programs, you need random numbers because just a few of those examples if

08:07 want to write some kind of a where somebody throws the dice. So

08:13 the dice, it's basically generating a number between one and six. So

08:18 need a notion of randomness. Same you are shuffling a deck of

08:24 you know, one of those cards to show up, there are no

08:27 cards. So you just use a number generator to generate one of those

08:33 to simulate picking a card from a . And those, there are many

08:41 beyond that for randomness. And if think about it, you can probably

08:46 up with a few. Um one them, one example is modeling road

08:52 , say you are trying to design um an intersection for a highway intersection

09:03 intersection between two roads. And you're to say, trying to decide whether

09:09 should be a stop sign or there be lights and if there are

09:13 how long you know, lights should on or not. And then of

09:20 you need some model of the road and the model of road conditions is

09:27 to be that, you know, during the rush hour you can get

09:33 between, you know, one car second to five cars a minute coming

09:40 . So, but you don't have precise. They don't come uniformly distributed

09:45 second every 10 seconds or every 30 . So you just basically pick a

09:51 inter well saying, hey this car come in, when will the next

09:55 come? It could be anywhere between seconds and a minute. Um so

10:01 are other applications, real world applications random beyond the game. So it's

10:07 fairly important function that we use. python of course provides a random module

10:17 this and this is how it Like all the other modules, you

10:23 access to anything in the random First thing you need to do is

10:27 import it and then uh you have Um two things basically you can do

10:39 it. One is to one of is to say random dot random.

10:46 again, this is more like the module. You just say your variable

10:51 random dot random and that will generate value between zero. A float value

10:59 zero and one. Not including Okay, so this this this interesting

11:06 says that the square parenthesis on the is saying that it's starting from zero

11:12 zero is an acceptable value. The value is one but it has to

11:18 less than one. The highest value up to one but not including

11:23 So it generates a value between zero 2.9999999 whatever. And in case you're

11:32 , you know, why do we it to that? If you think

11:35 it, you can use this function get random values in any range?

11:43 example if you wanted values between you zero and uh one million. You

11:52 basically multiply the value you get by million. And now you suddenly have

11:58 values between zero and a million. that is sort of the floor

12:04 The uh the integral version of the function for random module is rand

12:15 So again we're using the name Random is the name of the module.

12:20 rand range will give us an The interrogator will be between the values

12:29 and high. So generation an indigent this range. Again, not including

12:37 same square here and the regular parenthesis . What that means is that if

12:45 were trying to simulate throwing a dice would use something like this. So

12:54 function will return one will return different but they'll all be either one or

13:07 or three or four or five or . So that's about all we're gonna

13:14 in terms of content will for you The focus this week is gonna be

13:20 on on sort of bringing things into and getting more experience. Uh So

13:29 things you can do yourself as examples to make yourself more comfortable with programming

13:36 general and modules in particular we saw throwing a dice can be simulated by

13:44 equals random door Rand range. Suppose want to write a program that wants

13:52 verify if this is if the dice not loaded, which means is it

13:59 returning numbers from 1 to 6 with you know equal probability. So one

14:06 to do a quick crosscheck is that you call this modules many times 100,000

14:14 more and then average the score it , you can see if they're uniformly

14:22 , what should be the average and uh When you throw the dice many

14:28 and calculate the average, it may be precisely that but if the dice

14:33 fair, it should be about in middle that is between 1-6. If

14:38 of them are equal probability, the should be in the middle. So

14:43 can try that yourself. Another simple to do to see if your table

14:51 these modules and and the math function and random is to verify when you

14:59 something and you take a square do you always get the original

15:05 So one way to think of it that you know x minus square root

15:11 X. Square, What does it you? So it should be

15:15 But you'll find that in like in arithmetic, that's not always exactly

15:21 So again you can think of a where you pick a random number,

15:28 random value for X. But you to make sure you pick something that

15:32 a legitimate angle number and then your and randomly and then take its square

15:46 and then square red and then see you get and compare it to

15:50 To see that if if for a several numbers that is correct, then

15:58 you know, that means that you're correctly and the square root function and

16:04 functions are implemented correctly. So those things for you to try and if

16:11 have questions, you can always contact in short modules that you do seriously

16:19 things even with a basic knowledge of . And that's why we want to

16:23 them early. They're straightforward but very . So you should learn about them

16:29 get used to using them. And not that hard to write your own

16:35 . However, in this class, something you may want to do for

16:39 , but it's not gonna be one the essential exercises and I will stop

5999:59

-
+