HP Forums

Full Version: How to use Python syntax ?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I wanted to try out the Python syntax on the Prime emulator but it's giving me syntax error (see attached image). I'm using the code from this post. I am doing the exact thing he's doing, so I don't know why I'm getting this error.
You did not indent your code correctly (good indentation is mandatory in Python because it's the way to mark programming blocs).
(12-12-2019 04:28 PM)parisse Wrote: [ -> ]You did not indent your code correctly (good indentation is mandatory in Python because it's the way to mark programming blocs).

Thanks for your reply. The code now runs. I was a little bit skeptical initially about how much of python structures is supported, but I've been pleasantly surprised that most things work with very little tweaking. Like in some places where I need to use something from numpy or scipy, I just replace with the with the corresponding one from the calculator. Really nice.

I would like to ask you two more questions, if you don't mind:

1. Is there a document where I can know exactly which Python syntax is supported and which isn't?

2. Is it possible for me to see the code that gets generated from the python syntax ? This would enable me to see learn programming in the Prime's native language and also see if I can optimize the converted code?
(12-12-2019 09:33 PM)hamorabi Wrote: [ -> ]I would like to ask you two more questions, if you don't mind:

1. Is there a document where I can know exactly which Python syntax is supported and which isn't?
No. The aim is to support math algorithms in the French curriculum. In short, OOP and advanced strings syntax/commands are not supported.

Quote:2. Is it possible for me to see the code that gets generated from the python syntax ? This would enable me to see learn programming in the Prime's native language and also see if I can optimize the converted code?
Not on the Prime, but you can see the conversion with the xcas(f) command inside Xcas/Xcas for Firefox. I would not recommend to spend too much time trying to optimize the converted code, instead try to optimize the algorithm, or convert it to native code in Xcas. Optimizing interpreted code is often bad because you will tend to write compact code that is difficult to read and maintain and this has sometimes the effect of degrading performances of the corresponding native code.
(12-13-2019 06:12 AM)parisse Wrote: [ -> ]
(12-12-2019 09:33 PM)hamorabi Wrote: [ -> ]I would like to ask you two more questions, if you don't mind:

1. Is there a document where I can know exactly which Python syntax is supported and which isn't?
No. The aim is to support math algorithms in the French curriculum. In short, OOP and advanced strings syntax/commands are not supported.

Quote:2. Is it possible for me to see the code that gets generated from the python syntax ? This would enable me to see learn programming in the Prime's native language and also see if I can optimize the converted code?
Not on the Prime, but you can see the conversion with the xcas(f) command inside Xcas/Xcas for Firefox. I would not recommend to spend too much time trying to optimize the converted code, instead try to optimize the algorithm, or convert it to native code in Xcas. Optimizing interpreted code is often bad because you will tend to write compact code that is difficult to read and maintain and this has sometimes the effect of degrading performances of the corresponding native code.

Thank you very much for your assistance. I have one last question if it's not already too much. How can I make some of my functions private? I am writing something that requires a lot of sub functions and I don't want them to be visible from anywhere except by other functions defined inside the same program/file.
In actual Python the syntax would be
Code:

  Def max
     def max2
       Return
      Return
So that max2 is only visible within max.

But I don't know if that would work here.
It's not supported by the parser, CAS programs are always visible. However, CAS algebraic functions can be local to another program.
Reference URL's