Saturday, April 3, 2021

Python on Calculators: Python Files used as Modules

Python on Calculators:  Python Files used as Modules

(and Programming Tutorials by Mike Dane)


Python Files Can Be Transferred Between Calculators


Python scripts that are calculators have the .py extension, allowing the files to be transferred to and from calculators and python computer applications.   The calculators that I have with Python are:


* Casio fx-9750GIII

* Casio fx-CG 50

* Numworks

* TI-NSpire CX II


To my knowledge, other calculators with a Python module are:


* TI-83 Premium CE Edition Python (primarily sold in France)

* Casio fx-9860GIII (the fx-9750GIII model sold outside of the United States)


Importing Python Files - Extended Functions (extf.py)


For a Python file to imported in a python script, the source python file must be included in the calculator.   


Example: 


I would be working on the file usethesource.py and I want to import the functions defined on the source.py to be used.  All is needed is an import statement:


import source


To use any of the functions from the source.py, use the source-dot prefix, like:


source.function()


Remember to type the name of the module, the decimal point, and the function.  The Casio calculators do not store custom functions in a catalog, while the NSpire CX and Numworks do include the custom functions in the variable list but not the module prefix.


The python scripts extf.py and extfdemo.py serves as an illustration:


extf.py


# create a custom module

# 2021-02-03 EWS


import math


# functions code


def radius(a,b):

  return math.sqrt(a**2+b**2)


def angle(a,b):

  t=math.acos(a/math.sqrt(a**2+b**2))

  t=t*180/math.pi

  if b<0:

    t=-t

  return t


def pchg(a,b):

  return (b-a)/a*100


def taxplus(a,b):

  return a*(1+.01*b)


def pvaf(a,b):  

  return (1-(1+.01*b)**(-a))/(.01*b)

  

def fvaf(a,b):

  return ((1+.01*b)**a-1)/(.01*b)


def rsum(a,b):

  return 1/(1/a+1/b)


# constants

# gravity of Earth

g=9.80665  

# speed of light

c=299792458


extfdemo.py


# 2020-02-03 EWS


# import extf.py file

import extf


print("Import any py file.")

print("The source file must")

print("be in memory.")


# \n new line

print("\n")

print("extf.radius(a,b)=abs(a+bi)")

print("extf.angle(a,b=arg(a+bi) in degrees")

print("a=11.25, b=13.36")

print(extf.radius(11.25,13.36))


# other functions

print("\n")

print("extf.pchg: percent change")

print("old,new")

print("pchg(78.95,96.95")

print("a =22.52,b=29")

print(extf.pchg(22.52,29))


print("\n")

print("present value annuity factor")

print("extf.pvaf(n,i%)")

print("pv = pmt*pvaf")

print("pmt=250, n=24, i%=4.5")

print(extf.pvaf(24,4.5)*250)


# more to discover

print("\n")

print("More to discover!")



You can download the two files here: 

https://drive.google.com/file/d/1-g7oIru7SGFvg-1IhM4CDr6lrmMK7Nrv/view?usp=sharing



Programming Tutorials by Mike Dane


Mike Dane, also known as Giraffe Academy, has many tutorials of current popular programming languages, including Python, C++, Javascript, and HTML. It is his channel where I learned about the ability to use any Python script to be imported as a module and other basic skills for Python.  


Dane has both playlists of short tutorials and a comprehensive tutorial of programming languages.  Please check out his YouTube page at:  https://www.youtube.com/c/GiraffeAcademy/videos


His web site:  https://www.mikedane.com


 

Eddie


All original content copyright, © 2011-2021.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.