How does python work? - Printable Version +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html) +--- Forum: HP Prime (/forum-5.html) +--- Thread: How does python work? (/thread-17438.html) How does python work? - hamorabi - 09-06-2021 04:37 PM I recently upgraded my Prime to the latest firmware and I'm trying to understand how to write python functions/scripts. I didn't seem to find much a solution while perusing older threads. Let's say my script looks like this: PHP Code: `def func1(a, b):    return a + bdef func2(a, b):    return a - bdef func3(a, b, c):    if c == 0:        return func1(a, b)    else:        return func2(a, b) ` I know this is a contrived example, but I just want to know how to make it work. The question is, how do I call func3 ? RE: How does python work? - Dougggg - 09-06-2021 04:53 PM This will work Code: def func1(a, b): return a + b def func2(a, b): return a - b def func3(a, b, c): if c == 0: return func1(a, b) else: return func2(a, b) d=func3(4,2,0) RE: How does python work? - hamorabi - 09-06-2021 05:01 PM (09-06-2021 04:53 PM)Dougggg Wrote:  This will work Code: def func1(a, b): return a + b def func2(a, b): return a - b def func3(a, b, c): if c == 0: return func1(a, b) else: return func2(a, b) d=func3(4,2,0) Maybe, I didn't explain properly. I am not looking to run the code from the python app. I am looking to run the code from the place where you do regular calculations. For example, if I was using python-syntax under XCas, I would do the following: PHP Code: `#casdef func1(a, b):    return a + bdef func2(a, b):    return a - bdef func3(a, b, c):    if c == 0:        return func1(a, b)    else:        return func2(a, b) #end ` I'm looking to do the same but with python, not XCas. RE: How does python work? - roadrunner - 09-06-2021 08:15 PM Just paste your program exactly as written into a prime program file: [attachment=9790] tap and exit out. Typing func3(1,2,3) on the command line returns -1. -road RE: How does python work? - hamorabi - 09-07-2021 07:45 AM (09-06-2021 08:15 PM)roadrunner Wrote:  Just paste your program exactly as written into a prime program file: tap and exit out. Typing func3(1,2,3) on the command line returns -1. -road Yes, but this doesn't run the program as a python code. It runs it as an XCAS program. It just so happens that XCAS can accept some python syntax. What I am asking how to run python code, not XCAS with python-like syntax. RE: How does python work? - roadrunner - 09-07-2021 12:06 PM Maybe the PYTHON command is what you are looking for? Code: ```#PYTHON test import sys from builtins import float def func1(a, b):     return a + b def func2(a, b):     return a - b def func3(a, b, c):     if c == 0:         return func1(a, b)     else:         return func2(a, b) print(func3(float(sys.argv[0]),float(sys.argv[1]),float(sys.argv[2]))) #end EXPORT func3(a,b,c) BEGIN  PYTHON(test,a,b,c); END;``` RE: How does python work? - hamorabi - 09-07-2021 09:24 PM (09-07-2021 12:06 PM)roadrunner Wrote:  Maybe the PYTHON command is what you are looking for? Code: ```#PYTHON test import sys from builtins import float def func1(a, b):     return a + b def func2(a, b):     return a - b def func3(a, b, c):     if c == 0:         return func1(a, b)     else:         return func2(a, b) print(func3(float(sys.argv[0]),float(sys.argv[1]),float(sys.argv[2]))) #end EXPORT func3(a,b,c) BEGIN  PYTHON(test,a,b,c); END;``` Thanks for this. But how do you display the result in the calculations window like regular functions so that they can be used. At the moment, the result only shows in the terminal. RE: How does python work? - roadrunner - 09-07-2021 10:05 PM I think you would have to save it as a variable, then do what ever you want with the variable like in the following example: PHP Code: `#PYTHON testfrom sys import argvfrom builtins import floatfrom cas import casevaldef func1(a, b): return a + bdef func2(a, b): return a - bdef func3(a, b, c): if c == 0:  return func1(a, b) else:  return func2(a, b)answer=func3( float(argv[0]), float(argv[1]), float(argv[2]))caseval( "pythonanswer1698554:=" + str(answer))#endEXPORT runtest(a,b,c)BEGIN local d; PYTHON(test,a,b,c); d:=CAS("pythonanswer1698554"); CAS("purge(pythonanswer1698554)"); d;END; ` You can also save it as a home variable with hpprime.eval instead of cas.caseval. -road RE: How does python work? - hamorabi - 09-08-2021 10:48 AM (09-07-2021 10:05 PM)roadrunner Wrote:  I think you would have to save it as a variable, then do what ever you want with the variable like in the following example: PHP Code: `#PYTHON testfrom sys import argvfrom builtins import floatfrom cas import casevaldef func1(a, b): return a + bdef func2(a, b): return a - bdef func3(a, b, c): if c == 0:  return func1(a, b) else:  return func2(a, b)answer=func3( float(argv[0]), float(argv[1]), float(argv[2]))caseval( "pythonanswer1698554:=" + str(answer))#endEXPORT runtest(a,b,c)BEGIN local d; PYTHON(test,a,b,c); d:=CAS("pythonanswer1698554"); CAS("purge(pythonanswer1698554)"); d;END; ` You can also save it as a home variable with hpprime.eval instead of cas.caseval. -road Thanks for this. This seems more complicated than I hoped for. Is there some resource I can use to learn things like this? RE: How does python work? - roadrunner - 09-08-2021 11:08 AM I don't know of any resources. There may be a better way to do what you want but that's the best I could come up with. Everything I know about Python on the prime was learned by trial and error (mostly error), and by reading posts in this forum. -road