Post Reply 
Help with HP Prime Python Documentation
10-04-2021, 12:08 PM
Post: #1
Help with HP Prime Python Documentation
Hi All,

I have been playing with microPython of the TI NSpire CAS emulator. When I downloaded the latest HP Prime emulator yesterday I saw the Python App. I was able to easiliy do simple calculations. I am interested in writing Python programs for the HP Prime. In either case, where can I find the best (and most comprehensive) documentation for the Python on the HP Prime?

Namir
Find all posts by this user
Quote this message in a reply
10-04-2021, 03:46 PM
Post: #2
RE: Help with HP Prime Python Documentation
Namir, there isn't much documentation for Python in Prime.

When the Prime was updated (as Beta) Tim released some notes, not really excessive Smile

I did some practice with my Mandelbrot Explorer and scratched a little bit on the surface of Python

Olivier de Smet published a HP41CX Emulator a real marvel I think

These two examples create a new APPS. But a Python program also can be embedded in PPL program as I did here.

Perhaps these examples give you some hints what can be made. Some documentation is available in the Prime itself.
  • Go to the Python App
  • Touch the Cmds tab
  • scroll through the catalog
  • press [Help]
  • for detailed help follow the little right arrows
  • don't expect something reasonable everywhere
  • it's a good starter nonetheless
The Python implementation as of now is not very mature, lots of bugs and resets of the Prime.

But a lot of fun - definitively.
Find all posts by this user
Quote this message in a reply
10-04-2021, 06:57 PM
Post: #3
RE: Help with HP Prime Python Documentation
Thank you Gunter. I have been using Python on the TI NSpire CAS emulator and it works well. I write numerical analysis code and copy-and-run various sorting algorithms from the internet. I appreciate sample code that shows me how to include Python source code and then launch it!

Namir
Find all posts by this user
Quote this message in a reply
10-04-2021, 08:32 PM
Post: #4
RE: Help with HP Prime Python Documentation
(10-04-2021 06:57 PM)Namir Wrote:  Thank you Gunter. I have been using Python on the TI NSpire CAS emulator and it works well. I write numerical analysis code and copy-and-run various sorting algorithms from the internet. I appreciate sample code that shows me how to include Python source code and then launch it!

Namir

Preface: don't rely on what I say, I'm an amateur. But let's give it a try. I've changed my benchmark code a little to avoid the hassle with sys.argv

Code:
#PYTHON name
from math import *
import hpprime
t=hpprime.eval("time")
c=int(input("Enter the exponent! "))
print("Exponent = ",c)
print("Iterations = ", 10**c)
a=0
for i in range(1, 10**c+1):
    a=a+pow((e**(sin(atan(i)))),1/3) 
print("Time (sec) = ",round(hpprime.eval("time")-t,4))
print("Sum = ",a)
#end
Export sums(c)
Begin
  PYTHON(name, c);
End;

with [Shift][1][New] create a new program e.g. sums, delete the template that is in it, copy and paste the code above.

Now from the program catalog run this program. Disregard the input field and touch OK. Now it asks for an exponent, enter e.g. 5 and your done.

Alternatively, create a new APP. Save the Python App under a new name e.G. "Sums"
Open its ("Sums") source code editor by touching [Symb]. Copy the following part of the code
Code:

from math import *
import hpprime
t=hpprime.eval("time")
c=int(input("Enter the exponent! "))
print("Exponent = ",c)
print("Iterations = ", 10**c)
a=0
for i in range(1, 10**c+1):
    a=a+pow((e**(sin(atan(i)))),1/3) 
print("Time (sec) = ",round(hpprime.eval("time")-t,4))
print("Sum = ",a)
into your new App "Sums". Now you can run this app from the Apps screen. And this APP is ready to receive all the bells and whistles you might imagine Smile

Günter
Find all posts by this user
Quote this message in a reply
10-05-2021, 05:59 AM
Post: #5
RE: Help with HP Prime Python Documentation
Impressive Python code! I really like the second code example!

Namir
Find all posts by this user
Quote this message in a reply
10-05-2021, 06:49 AM
Post: #6
RE: Help with HP Prime Python Documentation
(10-04-2021 08:32 PM)Guenter Schink Wrote:  
(10-04-2021 06:57 PM)Namir Wrote:  Thank you Gunter. I have been using Python on the TI NSpire CAS emulator and it works well. I write numerical analysis code and copy-and-run various sorting algorithms from the internet. I appreciate sample code that shows me how to include Python source code and then launch it!

Namir

Preface: don't rely on what I say, I'm an amateur. But let's give it a try. I've changed my benchmark code a little to avoid the hassle with sys.argv

Code:
#PYTHON name
from math import *
import hpprime
t=hpprime.eval("time")
c=int(input("Enter the exponent! "))
print("Exponent = ",c)
print("Iterations = ", 10**c)
a=0
for i in range(1, 10**c+1):
    a=a+pow((e**(sin(atan(i)))),1/3) 
print("Time (sec) = ",round(hpprime.eval("time")-t,4))
print("Sum = ",a)
#end
Export sums(c)
Begin
  PYTHON(name, c);
End;

with [Shift][1][New] create a new program e.g. sums, delete the template that is in it, copy and paste the code above.

Now from the program catalog run this program. Disregard the input field and touch OK. Now it asks for an exponent, enter e.g. 5 and your done.

Alternatively, create a new APP. Save the Python App under a new name e.G. "Sums"
Open its ("Sums") source code editor by touching [Symb]. Copy the following part of the code
Code:

from math import *
import hpprime
t=hpprime.eval("time")
c=int(input("Enter the exponent! "))
print("Exponent = ",c)
print("Iterations = ", 10**c)
a=0
for i in range(1, 10**c+1):
    a=a+pow((e**(sin(atan(i)))),1/3) 
print("Time (sec) = ",round(hpprime.eval("time")-t,4))
print("Sum = ",a)
into your new App "Sums". Now you can run this app from the Apps screen. And this APP is ready to receive all the bells and whistles you might imagine Smile

Günter

Do you really want to set the starting time before the input statement? That won't just time the machine, it'll include the time it takes for you to answer the question as well.

Tom L
Cui bono?
Find all posts by this user
Quote this message in a reply
10-05-2021, 06:27 PM
Post: #7
RE: Help with HP Prime Python Documentation
(10-05-2021 06:49 AM)toml_12953 Wrote:  Do you really want to set the starting time before the input statement? That won't just time the machine, it'll include the time it takes for you to answer the question as well.

No, that glitch happened when I changed this little code snipped in a rush to get rid of the sys.argv command.

But as it wasn't meant to provide an impressive piece of code, rather than an example how to get the basics for creating a basic Python App it should suffice.

Let's leave it to Namir to correct that nonsense Smile

Günter
Find all posts by this user
Quote this message in a reply
10-05-2021, 09:13 PM
Post: #8
RE: Help with HP Prime Python Documentation
Not to disapoint Gunter, here is his code, edited:


Code:
from math import *
import hpprime
c=int(input("Enter the exponent! "))
print("Exponent = ",c)
print("Iterations = ", 10**c)
a=0
t=hpprime.eval("time")
for i in range(1, 10**c+1):
    a=a+pow((e**(sin(atan(i)))),1/3) 
print("Time (sec) = ",round(hpprime.eval("time")-t,4))
print("Sum = ",a)

Namir
Find all posts by this user
Quote this message in a reply
10-05-2021, 10:10 PM
Post: #9
RE: Help with HP Prime Python Documentation
(10-05-2021 09:13 PM)Namir Wrote:  Not to disapoint Gunter, here is his code, edited:

Namir

Thanks for correcting that glitch.

I'd strongly recommend to have a closer look to my Mandelbrot Explorer mentioned in an earlier post. Not so because it's impressive but because it's my Python learning bench. And I've tried to include some Python specifics, just because they are there.

And once again have a look at the HP41cx emulator of Oulan, that really gives you something to chew on and explore.

Let's talk about that in detail during the HHC 2022 Smile

Günter
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)