2.4 Use libraries - Video Tutorials & Practice Problems
Video duration:
9m
Play a video:
<v ->So now that we've looked at types, variables, errors,</v> we're gonna to talk about libraries now, and we've already seen the use of the math library in lesson one point two, and but there are a lot more libraries that exist, and we didn't talk about like how you actually find libraries, and how do you find what you can use from them. Um, so we're going to start off this problem of creating a dice simulator, so in PyCharm open up problem four dice simulator, close the other problem, and we can see here we want to randomly return two numbers between one and six, and we have to of course generate a random number. You could try to come up with a good method of generating random numbers, um, but that seems like a pretty common problem that someone else might have solved, so don't repeat work that other people have done, use the library instead, so I can search, like I already know what it is, but you know, lets see, python random number. So I get some different links here, one of these links is to the python documentation and it actually links us to the library called random. Which generates pseudo random numbers, so that's cool, there is a library already called random that we can use. So I can import it at the top of my file. I say import random and then um. Lets see, number one is equal to random, and then dot means that I'm looking for something that belongs to that random library, and PyCharm gives me a lot of different options here, so we're gonna ignore all the ones that start with under scores, those are kind of like private, um functions, and lets see there's actually this randint one. That I see and I can also look at the documentation, um, so I can look up randint or I can just like search to see if I find whatever I'm looking for, and its says that it takes, um two values a and b, and then returns a random integer n, such that a less than or equal to n and that's less than or equal to b, so I can, looks like I can pass two integers in, and get a random integer out of it, and it includes a and b as the potential integers, so lets see, I can try this, I'll put in one and six, and print number one, so I'll run this and I get two, I can run it again, get one, three, three, four, two. Do I ever get five or six? There we go, six, five isn't, Oh yeah, took five a long time. That happens right? If you playing Catan and you're like four statistically is very high probability and yet it never gets rolled, so here we've got random we're creating one number, we want to create a second number, so I can just copy this, paste it, and then change the variable name, and then want to tell the user what the result was. Um, so maybe we want a different, saying is like you rolled a. Number one and a number 2, um, so remember that we have to put in the spaces if we want to include them in the string, and also that we just looked at um, this type casting error in the last one, so this type error and then the solution is to cast it to a string, so we can do this. And then I rolled a six and a two, cool. Um, so this is not the prettiest way of writing out strings, um, that includes numbers in them and when we're in lesson two point six, we're actually gonna look at other ways of creating strings, so we'll leave it like this for now the main part of this lesson is just to say hey we can use this random library. And so what are libraries? Well if you imagine that each of these books here, contains some like useful functionality, so say like I read one book and I know a whole bunch of math, I read another book and it's like hey now I know how to create random numbers, you know all that information is like coming into me and I can read it like super quickly and just know stuff like Neo from The Matrix. You know? So on the left hand side we have just a stack of books and that would be like the base libraries that come built in when you download Python, so math is a built in library, you can just say import math, and because math book is included in the download and installation, but there are also like whole other books, like huge amounts of other libraries available that other people have written and that are maybe like to specific to include in the general Python download, we don't want your download to be like huge, and so there are lots of other libraries that you can get that do lots of different things, there are other random libraries that use different pseudo random number generators, so Python has something called the standard library. In three point seven two, we can see that this is all included in the standard library, so we saw earlier that there were a bunch of built in functions, and here's the documentation if you're curious about what any of these do. We also have some constants and some built in types, so we already looked at some of these types like int, and float. There are some exceptions, but then there also all these libraries and so the libraries are in this like console font here, so string is a library, re is for regular expressions, there's codecs, date time, that's a useful one for representing dates and times what not. Numbers, math, decimal, fraction, random, there actually quite a few, so Python does come built in with a lot of stuff, it's called batteries included, so you don't have to download many other things in order to get quite a bit of functionality, so if you're curious about the other libraries that are out there that you have to install, we will be looking at how to actually install them, so that you can use them in your Python code in a future lesson in lesson five, where we'll look at using external libraries, so not built in libraries, in lesson six and seven we'll also be using a bunch of external libraries for like one specific to data analysis or creating web apps, so those you'll have to get more comfortable with downloading libraries from the internet on to your computer, so that you can use them in your Python code.