prev next 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

Picking a Random Number

Python comes with libraries of functions that do almost anything.

Let's tell Python to pick a random number. Get the "random" library by typing this:


>>> import random
>>>  

Now we can say "random.randint(1,100)" to pick a random number from 1 to 100.


>>> random.randint(1,100)
95
>>>  

Which number did you get?

You can learn about libraries by saying help. Try typing this:


>>> help(random)
Help on module random:

NAME
    random - Random variable generators.
(...and on and on...)

It will show lots of information, and you can press the space bar to see a screenful at a time. Eventually you will see a long list of all the functions in the library.

We don't have time to read it all now, so you can press "q" when you have seen enough.

You can use help() to ask about most things in python.