Learn Python in 30 Days — Day 19: Using Modules
Learn Python in 30 Days — Day 19: Using Modules
Welcome to Day 19 of the Learn Python in 30 Days series!
Today we’re stepping into one of Python’s biggest strengths, modules.
A module is simply a file or built-in library that gives you extra functions so you don’t have to reinvent the wheel. Python comes with a huge “standard library”, and learning how to import and use it will instantly make your programs more powerful.
All example files for this series are available on my GitHub: Learn-Python-in-30-Days
By the end of this lesson, you’ll know how to:
-
Import modules (3 main techniques),
- Use math for accurate calculations
- Use random for games and generators
- Use datetime for working with dates and times
-
Build a mini-project: Random Dice Roller + Timestamp Logger
Let’s get into it.
1 — What Is a Module?
A module is a collection of ready-made functions you can use in your program.
To use one, you simply import it:
Think of modules like toolkits. You choose the one you need for the job.
2 — Three Ways to Import Modules
1. Import the whole module
2. Import specific functions
3. Import with a nickname
Useful when the module name is long:
3 — Using common standard modules
Using the math Module
The math module gives you accurate mathematical functions that go beyond normal arithmetic.
Here are some useful ones:
When to use math?
-
Scientific / engineering calculations
-
Geometry
-
Precision work (no floating-point shortcuts)
Using the random Module
Great for games, simulations, random choices, procedural generation, etc.
You can use this with existing variables: -
Using the datetime Module
Time, dates, timestamps — Python handles them all.
Formatting dates
Creating a specific date
Adding time (timedelta)
Mini-Project
Let’s put all three modules to work by making a random dice roller with a timestamp log of when the role was made.
This will work by undertaking the following actions: -
-
Rolls a 6-sided die using
random -
Prints a fun message
-
Adds a timestamp using
datetime -
Uses
mathto show a bonus calculation (square the result)
Code:
Try improving it by:
-
Letting the user choose the number of sides (D20, D12, etc.)
-
Logging results to a file (ties into Day 18)
-
Showing average roll after several rounds
Summary
Today you learned how to use Python’s standard modules to supercharge your programs.
You can now do the following: -
- Import modules
- Use
mathfor accurate calculations - Use
randomfor games & simulations - Use
datetimeto handle dates & times - Build real-world features with these tools
Next Up — Day 20 – Creating Your Own Module
Tomorrow, you’ll take this further and create your own module, your first step into structuring multi-file programs.
All example files for this series are available on my GitHub: Learn-Python-in-30-Days
You can see the full series here Learn Python in 30 Days series!
Hope you have enjoyed this post, thanks Matty








Comments
Post a Comment