Python / Python Functions and Modules

Using Built-in and Custom Modules

This tutorial will guide you through using built-in and custom modules in Python. We will explore how to import and use modules, and how to create your own.

Tutorial 4 of 5 5 resources in this section

Section overview

5 resources

Covers defining and using functions, lambda functions, and importing modules.

Introduction

Goal

This tutorial aims to guide you through using both built-in and custom Python modules. We will take a deep dive into how to import and use these modules, as well as how to create your own.

Learning Outcomes

By the end of this tutorial, you will be able to:
1. Understand what a module is and its purpose
2. Import and use built-in Python modules
3. Create, import and use your own custom Python modules

Prerequisites

To follow this tutorial, you should have a basic understanding of Python syntax and programming concepts. Knowledge of functions would also be beneficial.

Step-by-Step Guide

What is a Module?

In Python, a module is a file containing Python definitions and statements. It helps in organizing related code into a single file, which can be easily managed. Python has several built-in modules that you can use as is, and also allows you to create your own modules.

Using Built-in Modules

To use a built-in module, you need to import it using the import statement. Here is an example of importing and using the built-in math module:

import math

# Now we can use the math module's functions
print(math.sqrt(16))  # prints: 4.0

Creating and Using Custom Modules

Creating your own module is as simple as creating a Python file. Let's create a module named mymodule.py:

# mymodule.py
def greet(name):
    print(f"Hello, {name}!")

To use the greet function from mymodule.py, you can import it into another Python file:

# main.py
import mymodule

mymodule.greet("Alice")  # prints: Hello, Alice!

Code Examples

Example 1: Using the random module

import random

# Generate a random number between 1 and 10
random_number = random.randint(1, 10)
print(random_number)

# This will print a random number between 1 and 10

Example 2: Creating and using a custom module

# mymodule.py
def multiply(x, y):
    return x * y
# main.py
import mymodule

result = mymodule.multiply(7, 8)
print(result)  # prints: 56

Summary

In this tutorial, we learned about Python modules, how to import and use built-in modules, and how to create and use custom modules. The next step is to practice these concepts until you're comfortable with them. You could also explore more built-in Python modules and their functionalities.

Practice Exercises

  1. Use the datetime module to print the current date and time.
  2. Create a custom module that contains a function to calculate the area of a rectangle. Use this module in a different Python file.
  3. Create a custom module with several different functions, and then import only one of those functions into another Python file.

Solutions

import datetime

print(datetime.datetime.now())
# rectangle.py
def area(length, width):
    return length * width
# main.py
import rectangle

print(rectangle.area(5, 10))  # prints: 50
# mymodule.py
def func1():
    pass

def func2():
    pass

def func3():
    pass
# main.py
from mymodule import func2

func2()

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Countdown Timer Generator

Create customizable countdown timers for websites.

Use tool

Keyword Density Checker

Analyze keyword density for SEO optimization.

Use tool

Lorem Ipsum Generator

Generate placeholder text for web design and mockups.

Use tool

Open Graph Preview Tool

Preview and test Open Graph meta tags for social media.

Use tool

URL Encoder/Decoder

Encode or decode URLs easily for web applications.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help