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:

import math

Now you have access to everything inside the math module. 
You can you use by calling functions such as: -

math.pi math.sqrt(25)

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

The most simple way to access functions of different modules is to just import the whole module as shown below: -
import math print(math.sqrt(16))

This allow access to all the functions the imported module contains.
Try it yourself, hopefully you'll see something like that shown below: -
You can also download this example from my GitHub here and run it yourself.

2. Import specific functions

If we don't want to import all the functions and we know the specific ones we wish to use, we can just import the exact functions from the module. This can be done using the from command as shown below: -
from math import sqrt, pi print(sqrt(16)) print(pi)

Try it yourself, hopefully you'll see something like that shown below: -
You can also download this example from my GitHub here and run it yourself.

3. Import with a nickname

Useful when the module name is long:

import datetime as dt print(dt.datetime.now())

Try it yourself, hopefully you'll see something like that shown below: -
You can also download this example from my GitHub here and run it yourself.

3 — Using common standard modules

Python has many built in standard modules, we will look into how to use three common ones, math, random and datetime

Using the math Module

The math module gives you accurate mathematical functions that go beyond normal arithmetic.

Here are some useful ones:

import math print(math.pi) # 3.14159... print(math.sqrt(49)) # 7.0 print(math.floor(5.9)) # 5 print(math.ceil(5.1)) # 6 print(math.pow(2, 3)) # 8.0

When to use math?

  • Scientific / engineering calculations

  • Geometry

  • Precision work (no floating-point shortcuts)

Try it yourself, hopefully you'll see something like that shown below: -
You can also download this example from my GitHub here and run it yourself.

Using the random Module

Great for games, simulations, random choices, procedural generation, etc.

import random print(random.random()) # 0.0 to 1.0 print(random.randint(1, 6)) # random number between 1–6 print(random.choice(["orc", "troll", "slime"])) # random pick

You can use this with existing variables: -

weapons = ["Sword", "Axe", "Bow", "Dagger"] print("You found a:", random.choice(weapons))

Try it yourself, hopefully you'll see something like that shown below: -
You can also download this example from my GitHub here and run it yourself.

Using the datetime Module

Time, dates, timestamps — Python handles them all.

import datetime now = datetime.datetime.now() print(now)

Formatting dates

print(now.strftime("%Y-%m-%d %H:%M:%S"))

Creating a specific date

birthday = datetime.date(1990, 7, 14) print(birthday)

Try it yourself, hopefully you'll see something like that shown below: -
You can also download this example from my GitHub here and run it yourself.

Adding time (timedelta)

from datetime import datetime, timedelta future = datetime.now() + timedelta(days=7) print("One week from now:", future)

Try it yourself, hopefully you'll see something like that shown below: -
You can also download this example from my GitHub here and run it yourself.

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 math to show a bonus calculation (square the result)

Code:

import random import datetime import math def roll_dice(): roll = random.randint(1, 6) timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") power = math.pow(roll, 2) # just for fun! return f""" 🎲 Dice Rolled! Result: {roll} Result squared (math.pow): {power} Time: {timestamp} """ print(roll_dice())

Try it yourself, hopefully you'll see something like that shown below: -
You can also download this example from my GitHub here and run it yourself.

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 math for accurate calculations
  • Use random for games & simulations
  • Use datetime to 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

Popular posts from this blog

Math Behind Logic Gates

6502 - Part 2 Reset and Clock Circuit

Building a 6502 NOP Test