Lesson 24 · Python Fundamentals
13 - Importing Modules in Python: Fundamentals for Beginners
Modules help you organize and reuse code in Python. Learning how to import modules makes your code more powerful and easier to manage. Let us explore…
- CoursePython Fundamentals
- Lesson24 of 22
- Video11 min
- FormatJupyter notebook · 20 code cells
What you'll learn
Data
No separate download needed — the notebook creates or downloads everything it uses.
📓 Full notebook
Download .ipynb
Welcome to Python Modules!#
Modules help you organize and reuse code in Python.
Learning how to import modules makes your code more powerful and easier to manage.
Let us explore together!
# The import statement lets you use other people's code.
import math
# You can now use math functions. Try calculating a square root.
result = math.sqrt(16)
print(result)
Why Are Modules Useful?#
- They let you avoid repeating yourself.
- They make your code easier to read.
- They save a lot of time.
Many Python modules come ready to use!
# You can import more than one module.
import random, datetime
# Using random to pick a number between one and ten.
random_num = random.randint(1, 10)
print(random_num)
# What date and time is it now?
now = datetime.datetime.now()
print(now)
# You can give a module your own shorter name.
import math as m
print(m.pi)
Importing Just What You Need#
Sometimes you do not need to bring in everything from a module.
You can import just one piece instead.
# Import just the sqrt function, not all of math.
from math import sqrt
print(sqrt(25))
# Import multiple tools from math at once.
from math import ceil, floor
print(ceil(4.3))
print(floor(4.7))
# It is possible to give an imported tool a shortcut too.
from random import randint as dice
print(dice(1, 6))
# Sometimes, users make mistakes by typing the wrong module name.
# What happens if you try to import a module that does not exist?
try:
import not_a_real_module
except ModuleNotFoundError:
print('This module cannot be found!')
Get User Input with Modules#
You can use input() along with modules for interactive programs.
# Ask the user for a number, then use math.sqrt
import math
num = float(input('Enter a number to find its square root: '))
print('Square root is:', math.sqrt(num))
# Create your own module in a separate file called my_tools.py
# Put this inside:
# def greet(name):
# print('Hello,', name)
# Use your new module like this (if my_tools.py is in your folder):
# import my_tools
# my_tools.greet('Alice')
# You can reload a module if you change its code, using importlib.
import importlib
# import my_tools
# importlib.reload(my_tools)
# Using help to see what a module can do.
import math
help(math)
Mini-Project: Build a Dice Guessing Game!#
Let us use everything we have learned to make a fun little project. You will guess a dice roll using modules and input.
# Part 1: Roll the dice and get the user's guess.
from random import randint
secret = randint(1, 6)
guess = int(input('Guess a number between 1 and 6: '))
# Part 2: Check if the guess is correct.
if guess == secret:
print('Congratulations! You guessed it!')
else:
print('Sorry, the number was', secret)
# Tip: You do not have to remember every module name.
# Try searching online or in the Python docs for cool built-in modules.
import calendar
print(calendar.month(2024, 1))
Recap: What Did We Learn?#
- How to import modules with import
- Aliases with as
- Picking just what you need with from import
- Handling errors nicely
- Making your own modules
- Using modules for real projects
You are ready to use modules like a Python pro!
Thank You for Learning!#
If you enjoyed this lesson, please:
- Like the video
- Leave any questions in the comments
- Subscribe for more Python projects
- Share it with a friend to help them too!
See you in the next coding adventure!
Found this useful?
All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.



