By the end of this lesson, you'll be able to:
Definition: A module is a library of code that contains a set of functions that you can import into your program to extend its functionality.
:information_source: Remember! Think of modules like apps on your phone - you only install the ones you need! Python modules work the same way - you only import what you want to use.
Just within the Python standard library there are more than 200 modules available.
Not all the modules are suitable for a typical Python developer. Some are just libraries of code to support compatibility with old codes, which might already have better alternatives out there.
You can imagine these modules as an extension to your Python programs. You only import what you need to use.
:bulb: Pro Tip!
Modules help us write less code! Instead of creating everything from scratch, we can use pre-made functions that experts have already written and tested.
Let's look at a very common built-in module called the Python random module which lets you make random numbers.
In EduBlocks, here are the available random functions:
Step One: Find the imports category - it's the first category in EduBlocks:
Step 2: Drag the import block to your workspace:
Step 3: Type "random" in the import block:
:information_source: Remember! Always place import blocks at the very top of your program - they need to load first before you can use them!
:bulb: Troubleshooting
If you get an error saying "random is not defined", check that:
- Your import block is at the top
- You spelled "random" correctly (all lowercase)
- The import block runs before any random functions
Now let's explore the two most useful random functions!
random.choice()
function picks one random item from a list.The choice method takes in only 1 parameter which is a list.
How to use it:
Step One: Import the random module (at the top of your program)
Step 2: Create a list with items to choose from
Step 3: Drag the random.choice() block and connect your list
Input | Output |
---|---|
![]() |
![]() |
The output will pick a random item from the list each time you run the program!
:information_source: Remember! In real Python code, this looks like:
import random
my_list = ["apple", "banana", "orange"]
result = random.choice(my_list)
print(result)
:bulb: Fun Ideas!
Use random.choice() to:
- Pick a random winner from a list of names
- Choose a random activity for the day
- Select a random color for a drawing program
random.randint()
function generates a random whole number (integer).The randint method takes in 2 parameters separated with a comma:
How to use it:
Step One: Import the random module (at the top)
Step 2: Drag the random.randint() block
Step 3: Enter your minimum number (start)
Step 4: Enter your maximum number (stop)
Input | Output |
---|---|
![]() |
![]() |
:information_source: Remember! In real Python code, this looks like:
import random
number = random.randint(1, 10)
print(number)
The number will be between 1 and 10 (including both 1 and 10)!
:bulb: Common Uses
Use random.randint() for:
- :emoji: Dice rolling games (1 to 6)
- :video_game: Random enemy spawning in games
- :dart: Guessing number games
- :art: Random coordinates for drawings
Create a program that generates a random 4-digit PIN code using random.randint(0, 9) four times!
Make two dice that roll random numbers from 1 to 6, then add them together to get your score!
Create lists of characters, actions, and places. Use random.choice() to create funny random stories!
:bulb: Troubleshooting Common Errors
"list index out of range" - Your list might be empty. Check that you added items to your list!
"random is not defined" - Make sure you imported random at the top of your program.
Getting the same "random" number every time? - Make sure you're calling the function inside your loop or where you want the randomness to happen!
Congratulations! You've learned how to add randomness to your programs. This is a powerful tool that makes programs more fun and unpredictable. Random functions are used in:
Keep experimenting with random functions to create exciting programs!