avatarJim McAulay🍁 I'm nobody. Are you a nobody too?
# Summary

This article provides a beginner's guide to importing modules and using functions in Python.

# Abstract

The guide explains the concept of modules in Python, detailing how to create, import, and use them. It illustrates the process of importing a custom module named `hello_world` and using its functions, such as `pr_hworld()`. The article emphasizes the necessity of specifying the module name when calling its functions from external modules. It also presents different import strategies, including using aliases with `as` and selective function import with `from ... import ...`. The author assures readers that more complex and interesting applications will be explored beyond the simple "Hello World" example.

# Opinions

- The author acknowledges the simplicity of the "Hello World" example and promises to move on to more sophisticated uses of modules and functions.
- The article implies that understanding how to import and use modules is a fundamental step in Python programming, essential for leveraging the language's extensive standard library.
- The author, through a quote, seems to appreciate humor in learning, as indicated by Jim McAulay's light-hearted comment about chameleons.

Importing Modules And Using Functions In Python

A beginner’s Guide

Photo by Ante Hamersmit on Unsplash

In this lesson I introduce the import command.

Here’s what we have accomplished so far.

We learned that a module is just a file with python code. We created a module called hello_world that contained a simple command. We turned that simple command into a function and we learned how to call that function from inside our module.

When you downloaded Python it came with many modules containing useful functions. We will be importing some of theses modules and using functions from them. The process of using these useful functions is identical to what we are doing with the useless function that we are going to play with today.

We are now going to create another module that will call our pr_hworld function from our hello_world module.

Before we call the function we have to import it. The first line in our new module will be

import hello_world

To use the function in our first module we might try just typing the name of the function.

import hello_world
pr_hworld ()

It does not work. The computer says that it does not recognise pr_hworld (). We have to tell it where pr_hworld() came from. When we called the function from the inside the module that created it we did not have to import it or identify where it came from but this is a new module.

Try this.

import hello_world
hworld.pr_world ()

Once again we see Hello World! printed on the screen. I know this is getting a bit silly. I promise that we will do more interesting stuff soon.

In addition to the simple import command there are a few other options.

import hello_world as hw
from hello_world import pr_hworld, another_function

Import hello_world as hw

Allows you to substitute an abbreviation or shorter form

import hello_world as hw
hw.pr_world)()

from hello_world import pr_hworld

This allows you to list one or more functions that you intend to use when you import the module and you won’t have to type in the source.

from hello_world import pr_world
pr_world()

Jim McAulay🍁 says, “ When a chameleon can’t change its colours it is called a reptile dysfunction”

45__46

13__11

Technology
Python
Programming For Beginners
Jim Mcaulay
Illumination
Recommended from ReadMedium