Use the random Module
Beginner · Write the code · from Python Imports: How import Really Works (Modules, Packages, Paths)
From the lesson: Python Imports: How import Really Works (Modules, Packages, Paths)
Think of a library. Instead of buying every book in the world and keeping them in your house, you go to the library and check out only the books you need. When you're done, you return them.
Read the lessonImport the random module and use random.seed(42) to make results predictable. Then:
1. Use random.randint(1, 100) to generate a random number and print it
2. Create a list colors = ['red', 'blue', 'green', 'yellow']
3. Use random.choice(colors) to pick a random color and print it
The seed ensures the same "random" numbers every time.
# Import random and set seed to 42
More Imports exercises
- Predict the Output: import vs from...importBeginner
- Fix the Bug: Broken ImportsBeginner
- Use Import AliasesBeginner
- Write a Module with __name__ GuardIntermediate
All Python exercises · 50 practice problems · Modules & Packaging quiz