© The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2022
K. WilsonThe Absolute Beginner's Guide to Python Programminghttps://doi.org/10.1007/978-1-4842-8716-3_7

7. Using Modules

Kevin Wilson1  
(1)
London, UK
 
When developing more complex Python applications, as the program grows in size, it's a good idea to split it up into several files for easier maintenance and reusability of the code. To do this, we use modules.

A diagram illustrates the modules of the main program code, which are computation functions, user interface functions, and network functions.

Figure 7-1

An example of how a large program can be broken down into modules

Modules are simply files with the .py extension, containing code that can be imported into another program.

In doing this, we can build up a code library that contains a set of functions that you want to include when developing larger applications.

In this section, we'll take a look at how to create modules and include them in our Python programs.

Importing Modules

Python has a whole library of modules you can import into your programs. Here are some common built-in modules you can use:
  • math Mathematical functions

  • turtle Turtle graphics

  • tkinter GUI interface toolkit

  • pygame Toolkit for creating games and other multimedia applications.

To import the modules into your code, use the import keyword. In this example, I’m going to use the import keyword to import the turtle graphics module into a Python program. To do this, we enter the following line at the top of the program:
import turtle
This statement imports all the turtle graphics functions into the program.

A diagram illustrates turtle namespace with forward, left, and right turtles, imported into a program.

Figure 7-2

How to import a module into a program

Turtle graphics operate much like a drawing board, in which you can execute various commands to move a turtle around. We can use functions like forward() and right() . The turtle will travel along the path that you define using these functions, leaving a pen mark behind it.

When we import a module, we are making it available in our program as a separate namespace. In other words, each module has its own private namespace which usually has the same name as the module. This namespace holds all the names of functions and variables declared in that module. This means that we have to refer to the function in a particular module using the dot notation .
moduleName.functionName()
For example:
turtle.forward(100)

turtle is the name of the module we imported earlier, and forward() is a function defined within the module.

Let’s put this into a program. In the following, we have our import statement to import all the turtle graphics modules. Below that, we have a statement that moves the turtle forward and one to finish the program.

A 3-line code of the turtle import in the turtle 1 dot p y window.

Notice that we use the dot notation to access the functions in the turtle module:
moduleName.functionName()
We want to access the forward function , so we specify the module name it’s in (turtle), followed by a dot, and then the name of the function we want (forward). So we get
turtle.forward(100)

This will move the turtle 100 pixels forward.

Here, we can see the output to the program: the turtle has moved 100 pixels to the right.

A window of Python Turtle Graphics has a horizontal arrow line placed near the center.

We can complete the program to draw a square.

A nine-line code in the turtle 1 dot p y window creates a box shape on the Python Turtle Graphics window.

Try out some of the other turtle commands .

A table has ten rows and two columns labeled command and description. In column 1, each command starts with the turtle, followed by attributes.

Creating Your Own Modules

You can declare and store your functions in a separate file and import them into your main program.

All function definitions can be stored in a file, for example, myfunctions.py.

A two-line code of functions definition, in my functions dot p y window.

The main program could be called functionmain.py. At the top of the main program script, you’ll need to import your functions stored in the other file (myfunctions.py). Strip off the file extension (.py).

A code reads import my functions in the functions main dot p y window.

This is called a module . Any functions declared will be included in the main program. You can include these functions in any program you need to. This makes maintenance easier.

My functions dot p y window points to functions main dot p y window with a boxed code that reads my functions dot add num, 4, 4.

Now, to call any functions from that module, you need to specify the module name followed by the function name.

A three-line code of my functions imports in the functions main dot p y window. The code, my functions dot add num is underlined.

Lab Exercises

  1. 1.

    Write a function that accepts a number from the user and uses a function to square the number and then return the result.

     
  2. 2.

    Save this file as a module.

     
  3. 3.

    Import the module you just created into a new program.

     
  4. 4.

    Call the function in the module.

     
  5. 5.

    Create a new program and import the turtle graphics module.

     
  6. 6.

    Experiment with drawing different shapes using some of the turtle graphics methods.

     
  7. 7.

    Use the turtle commands to draw some shapes.

     

Summary

  • Modules are simply files with the .py extension, containing code that can be imported into another program.

  • You can import modules using the import keyword.

  • When we import a module, we are making it available in our program as a separate namespace. This means that we have to refer to the function in a particular module using the dot notation.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.165.247