Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Find Information About Different Approaches

00:00 In this lesson, we’ll pick out two prepositions. I’m going to use this as a chance to, again, use a different approach just to play around a little bit more with the random module and different approaches of solving things.

00:13 Before, I’ve used random.choice() and random.shuffle(). And you may not have known about random.shuffle(), for example, if you haven’t worked with the random module before.

00:22 So in this lesson, I want to show you a little bit how you can find out information about a module and how you could choose to maybe pick another function that might sound interesting.

00:33 I’ll expand here the shell a bit because we’re going to do some interactive looking around. I’m going to import random over here in the shell, and then you can always use the help() function and pass it a module or an object.

00:46 And Python’s going to give you squeezed text. Well, that’s not very helpful, IDLE.

00:54 Okay, so I can double-click it to expand it. So that’s a peculiarity, it seems, of the IDLE shell. So if you run it in a different shell, then it just prints this out for you right away.

01:04 Now let’s see what we’ve got here. This gives you information about the random module and you can see that it gives you information about the different classes and functions that are in there.

01:15 And you can scroll through this and figure out whether there’s anything that maybe sounds interesting. So here’s random.choice() that chooses a random element from a non-empty sequence, right?

01:25 So the next one right after is random.choices(), for example, which maybe that sounds interesting. We want to pick more than one, right? So why not try that out?

01:35 Return a k-sized list of population elements chosen with replacement.

01:40 Sounds a little bit abstract maybe, but the nice thing is you can always just try it out, right? So let me see how IDLE handles this. I have to scroll all the way to the bottom. Yes.

01:50 Okay, let’s try out random.choices() and see whether that might be a good fit for choosing two elements from a list. I’ll make an example list.

02:00 Example list, and that’ll just contain a couple of integers because they’re the quickest to type, one through four. And now I will use random.choices() and pass in the example list and see what the output is.

02:17 This gives me just one element, looks like a random element maybe. Can I just run this again

02:24 and see whether I get the same result? Okay, different selection, but always just one element. I remember there was a mention of another parameter called k where you can pass in a number.

02:36 So I want to get two elements. So let’s say I say k=2, okay, that gives me two elements, but you can already see this is not what we’re looking for because the example list only contains each integer once.

02:48 And this gives me duplicates by chance, actually picked out two times the same element. So if I had gotten this result just four and three, I might have had to try a couple more times to notice that.

03:02 Sometimes it actually returns two times the same element. We’re getting a lot of double threes, but that’s just by chance right now.

03:11 Okay, so this is not the right function that we want to use, right? Because it doesn’t solve our problem of not having duplicates. So let’s give that another go.

03:20 I’m going to say help(random) and we’ll be presented with a bunch of text again,

03:28 and I’ll scroll a bit more. We looked at choice, choices,

03:33 lognormvariate is probably not what I’m looking for right now, looking… sample. That sounds good. Choose k unique random elements from a population sequence or set.

03:43 That sounds exactly what we’re looking for, right? And here we even have an example.

03:47 Okay, no, that’s not exactly what we’re looking for, but let’s give that a try. I think this sounds like a good function to use,

03:57 so I still should have access to my example list. All right, so now if I say random.sample() and pass in the example list again with k=2 in order to get two results, then I get two different ones.

04:13 Now I can try this a couple of times and you’ll see that it’s always two different selected elements from the list and it’s never two times the same element.

04:23 So it picks two unique ones, which is exactly what we want to do, right? We have a list and we want to pick out two unique items from that list. So that’s going to be the approach to use for our prepositions, but I’ll do it in the next lesson.

Become a Member to join the conversation.