Education Technology

Do Python Modules Really Have More Fun?

Posted 06/03/2022 by John Hanna

Do Python Modules Really Have More Fun?

 


Since Python programming was first released to the world on the TI-84 Plus CE Python and the TI-Nspire™ CX II graphing calculators about a year ago, I think it’s time to dig a little deeper into the power of modular programming on these remarkable platforms, and discover why using modules can be more fun, or can certainly at least save you time.



Modules give us a powerful way to reuse common code
These custom “support” files are easily shared among my many Python programs and can be shared with others.

When developing a Python programming project, I find it helpful to divide the project into pieces and tackle them in smaller chunks. Python is designed for this approach from the ground up.

Modular programming is also useful if you or your students want to work in teams: Assign each member of the team a clearly defined task, have each develop a module, and then merge the modules into a single project.

The rest of this document explains the process of creating and using modules on the TI-84 Plus CE Python edition. Download the TI-Nspire CX II version for the same information specific to the TI-Nspire™ CX II graphing calculator.




New: Python Modules library on TI Codes

The developers at TI have included some special files in the Python system. When you download and install the very cool Turtle module, you gain the ability to code in Turtle graphics! There are also additional TI_IMAGE and TI_DRAW modules as well as TI_PLOTLIB, TI_SYSTEM, TI_HUB and TI_ROVER, a BBC micro:bit module, and more to come.

For some introduction and practice with some of the TI modules, see the additional [+] Python Modules section of TI Codes for TI-84 Plus CE Python.

Note: There is also a Python Module library for the TI‑Nspire CX II graphing calculator.



Where are my files?
On the TI-84 Plus CE Python edition, all our .py files are stored as AppVars and have a special PY designation next to them when viewing them using [mem] > Memory Mgmt > AppVars. Some of my files seen here are in RAM and others are in Archive (*) to save memory. Only the files in RAM are available to use in the Python App. There may also be AppVars that are not PY files (even some that Python uses).



What’s a module?
I like to make a distinction between a Python program (or script) and a module. Both a program and a module are written using the same Python language but differ slightly in their purpose. A program is executable code that accomplishes a goal — it should do something. A module is also a Python file but is designed to be imported into a program (or another module), like the math and random modules. A module is designed to make common routines portable and can contain any Python code, functions and class definitions. Although it can be executed (“run”), it is not designed to actually do anything.

Some modules are provided in the Python App, and you can find them by entering either the Editor or the Shell and selecting <Fns…> Modul. Additional modules may be found in the <Add-On> sub menu in the Modul screen.



Import this!
The import statement makes the functions (and other code, like classes) that are stored in another file (the module) available to my program. There are three “flavors” of import statements:

  • import math — all functions must be preceded by the module name: y = math.sin(x)
  • import math as hoi — all functions must be preceded by the “alias” name you chose: y = hoi.sin(x)
  • from math import * — all functions can be used without the module prefix: y = sin(x)

And, if you only intend to use one or two of the functions in a module, you can just state which one(s) to conserve memory:

  • from math import sin, cos (no parentheses after the function names)

Writing a module
On the TI-84 Plus CE Python edition, I write a module the same way I write a program. The file is saved as you type. The module file may not do anything if you <Run> it, but running it can detect syntax errors in the code for you (but not runtime errors or programmer errors).




FIREWRKS (the center screen) is the program and COLOR120 is the imported module:

  • FIREWRKS.py uses code from four modules to draw colorful random stars on the screen.
  • COLOR120.py is a module I wrote that lets me use 120 bright colors of the rainbow to make random colors more vibrant. The module just contains three special lists: red[ ] (partially shown above), green[ ] and blue[ ].

Sharing our TI-84 Plus CE Python masterpieces
Python Modules give us a powerful way to reuse common code. These “support” files are easily shared among my many Python programs, and they can be shared with others too. If your TI-84 Plus CE Python program uses your custom modules, be sure to share the modules along with the main program, and be sure to provide ample documentation within the files (as #comments or as “”” comments “””). I’m looking forward to seeing your creations posted online. And a special thanks to the developers at Texas Instruments for creating additional powerful tools for a rich and engaging Python experience.



About the author: John is a retired teacher splitting time between sailing in New Jersey and mountain biking in Florida (did he get that backwards?), still getting kicks out of working with TI to provide feedback on new products and developing meaningful programming content for mathematics and science … and still having fun with all the graphing calculators and the accompanying toys.