FAQ: Modules: Python - Modules Python Random

This community-built FAQ covers the “Modules Python Random” exercise from the lesson “Modules: Python”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science

FAQs on the exercise Modules Python Random

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

2 posts were split to a new topic: What is the point of using range combined with randint?

2 posts were split to a new topic: What does random.randint() do?

3 posts were split to a new topic: When do we need from and import vs just import?

2 posts were split to a new topic: How can I set a random value during my code?

Isn’t this list comprehension from solution redundant?

random_list = [random.randint(1,101) for i in range(101)]

Firt part is already saying that we want a random number in range from 1 to 101. Why do we need the last part saying the same thing again? When I tried to change it to any number, even 1, I still got random numbers from 1 to 101.

What do you argue it should be instead?
What is the purpose of it that it’s supposed to implement?
Would you still be satisfying that purpose with your proposed alternative, after removing whatever you are arguing is redundant?

I have a feeling that we put it there only because list comprehension is like [X for Y in Z] so it just has to have some Z. If I remove last part I get syntax error. But when I put ANY >0 number in there it works just fine, doesn’t matter if it’s 1 or 100000.

Note that the range is only used to count the number of iterations. The result is 101 numbers between 1 and 101 inclusive.

What do you mean by “just fine”?
You have to consider what the purpose is.
What does that part of a list comprehension do, what effect would changing it therefore do?
Why is list comprehension used at all?

Anyway I have no clue what it’s supposed to do, so I couldn’t possibly tell whether it’s doing something redundant, but it does do things and changing inputs to it will have effects in the result and whether that matters comes down to the purpose of it all… (and again, you should be considering the intentions before you consider the code) But what the effect of changing it is, well, ask yourself what that part of list comprehension does, you need to know that to reason about list comprehension anyway, you can’t begin to say it’s redundant before you know what it does

0 is still there.


Anyway, after looking at the exercise, what’s redundant is in the idea that is implemented.
…so consider the intention before considering the code
the redundancy is in the intention

If you create a list of random numbers
And then choose a random value from that list.
Then your result is equivalent to generating a single random value, the list is therefore redundant.
What does that have to do with list comprehension? It doesn’t, it’s the whole plan/concept/idea/intention that’s bad, and that’s the thing to reason about, not list comprehension.

D’oh! Was thinking zero and wrote 1.

We are supposed to make a list that provides us with 100 different numbers between 1 and 100.
Using the code provided from the solution:

random_list = [random.randint(1,101) for i in range(101)]

if I print the random_list I do see the number 101 showing up amongst the 100 numbers.

So my question is:
Does the argument in random.randint() not work the same as in range()?
By that, I mean how the first argument is inclusive while the second argument is exclusive.

If I change the second argument in random.randint() from 101 to 100 I only observe numbers between 1 and 100.

… is not an argument, but a value to be inserted into the resultant list.

documentation for those functions will tell you how they behave