Post Reply 
hpPrime, with Python Syntax, beyond the Python numeric language
02-28-2019, 02:32 AM (This post was last modified: 10-25-2019 05:50 AM by compsystems.)
Post: #1
hpPrime, with Python Syntax, beyond the Python numeric language
Hello, Xcas supports Python syntax, although much of it does not work the same as the Python language, since it is numeric and XCAS is numeric / symbolic
I am adapting a Python guide to Xcas with Python syntax, this document may have errors in writing, my native language is Spanish, any contribution is welcome

/!\ The hpprime supports Xcas with Python syntax, within a program code, and history view only from the firmware 2019, or up

Thanks

###########################################################
https://www-fourier.ujf-grenoble.fr/~par...casen.html

###########################################################

3. AN INFORMAL INTRODUCTION TO XCAS WITH PYTHON SYNTAX

In the following examples, input and output are distinguished by lines, the entry value or sentence is located after of a numeral (xcas installer version), this can occupy one or several lines, output located on the next line.

1: 9-2 [enter] returns
7

the above means that on line number 1 the input is located and on the next line the output

switching mode between XCAS syntax and python-XCAS syntax

python_compat(1) [enter] initializes the python syntax in xCAS and ** operator to calculate powers
python_compat(2) [enter] initializes the python syntax in xCAS and ** operator equal to Xor operator
python_compat(0) [enter] ends the python syntax in xCAS

python_compat() [enter] returns the syntax mode in numerical value


The equal sign (=) is used to assign a value to a variable. Afterwards, only the result (right side evaluated) is displayed

Width = 20 [enter] returns 20
Height = 5 * 9 [enter] returns 45
Width * Height [enter] returns 900

Although it is recommended to use better : =, because the equal sign is used for the equations
Width := 20 [enter] returns 20


If a variable is not “defined”, maintains its symbolic value, unlike the python language, it returns a error message

var01 [enter] returns var01

Many of the examples in this manual, include comments. Comments start with the hash character, #, and extend to the end of each line. A comment may appear at the beginning of a line or after a sentence, but not within a string literal. A hash character within a string literal is just a hash character. Since comments are to clarify code and are not interpreted, they may be omitted when typing in examples.

Some examples:

value := 987; # this is a comment [enter] returns 987
txt1 := "# This is not a comment because it's inside quotes."; [enter] returns "# This is not a comment because it's inside quotes."

3.1. USING SYNTAX PYTHON AS A CALCULATOR
Let’s try some simple Xcas commands with syntax Python.

3.1.1. NUMBERS

A sequence of inputs, shows a sequence of outputs, each sentence must end in a semicolon.

2 + 2;
50 - 5*6;
(50 - 5*6) / 4;
8 / 5 # Unlike the python language, a division is not always evaluated to float, because it many times maintains its rational form.
[enter]
4, 20, 5, 8/5

The integer numbers (e.g. 2, 4, 20) have type integer or DOM_INT, the ones with a fractional part (e.g. 5.0, 1.6, 5.) have type 'real' or DOM_FLOAT. We will see more about object types later in the tutorial

Operators with mixed type operands (interger and floating) convert the result to floating point:

4 * 3.75 – 1 [enter] returns
14.0 # and not 14

In a division, an argument with a fractional value or ending with dot returns a float (e.g. 17/3.0 returns 5.66666666667). To do floor division and get an integer result (discarding any fractional result) you can use the // infix operator; to calculate the remainder you can use % infix operator:

17 / 3; # classic division
17 / 3.; # approximate value calculation
17 // 3; # floor division discards the fractional part, the // operator only works in Algebraic mode
17 % 3; # the % operator returns the remainder of the division,
5 * 3 + 2 # result * divisor + remainder
[enter] returns
17/3, 5.66666666667, 5, 2, 17

With python_compat(1), it is possible to use the ** operator to calculate powers

5 ** 2; # 5 squared
2 ** 7; # 2 to the power of 7
[enter] returns
25, 128

3.1.2. STRINGS

The strings require a character container, can be expressed between single quotes pair ('...') or double quotes pair ("..."), but Xcas uses the character ' to perform other operations, for this reason use best double quotes for strings

"Enter x value" [enter] prints only the content of the string, that is without the containers or ("...") quotes pair.
Enter x value # Although internally it is "Enter x value"

"doesn't" [enter] prints doesn't
double clicking on the string, shows the pair of quotes "doesn't"

This is not a string because it's not in quotation marks

Within the content of the strings also can be used to escape character:
\t tabulation
\n new line
\" double quotes

"This is the string in the first line\nand this is the second string in a second line\n\tand now a third tabulated" [enter] prints

This is the string in the first line
and this is the second string in a second line
↦ ↦ and now a third tabulated

To insert a double quote, within a string, the " character must be duplicated.
"Enter a ""String"":" [enter] prints
Enter a "String":

Or with escape quotes
"Enter a \"String\":" [enter] prints
Enter a "String":

If you want to show double quotes at the beginning, you should follow the next sequence "\"…\"" e.g.
"\"Enter a String:\"" [enter] prints
"Enter a String:"

"\"This is the string in the first line\nand this is the second string in a second line\n\tand now a third tabulated\"" [enter] prints

"This is the string in the first line
and this is the second string in a second line
and now a third tabulated"

Strings can be concatenated (glued together) with the + operator, and repeated with * operator:
For example to get: unununium (Element 111)

3 * "un" + "ium"# 3 times "un", followed by "ium" [enter] prints
unununium

Strings can be indexed (subscripted), with the first character having index 0. There is no separate character type; a character is simply a string of size one:

word := "Xcas" [enter] returns " Xcas "
word[0] # character in position 0 [enter] prints
X
word[3] # character in position 2 [enter] prints
s

Indices may also be negative numbers, to start counting from the right:
word[ -1 ] # last character [enter] prints
s
word[ -2 ] # second-last character [enter] prints
a
word[ -3 ] [enter] prints
c
word[ -4] [enter] prints
X

Note that since -0 is the same as 0, negative indices start from -1.
+---+---+---+---+
| X | c | a | s |
+---+---+---+---+
0__1__2__3
-4_-3_-2_-1

/!\ In the hp-prime firmware 2019 calculator in the history view it makes a +-1 adjustment of the index =(


In addition to indexing, slicing is also supported. While indexing is used to obtain individual characters, slicing allows you to obtain substring:

word[1:3] # characters from position 1 (included) to 3 (excluded) [enter] prints
ca
word[0:2] # characters from position 0 (included) to 2 (excluded) [enter] prints
Xc

Slice indices have useful defaults; an omitted first index defaults to zero, an omitted second index defaults to the size of the string being sliced.

word[:2] # same to word[0:2] character from the beginning (position 0) to position 2 (excluded) [enter] prints
Xc
word[1:] # same to word[1:4] characters from position 1 (included) to the end (position 3) [enter] prints
cas
word[-3:] # same to word[-3:-1] characters from the second-last (included) to the end (position -1) [enter] prints
cas

Note string[start:end] how the start is always included, and the end always excluded. This makes sure that string[:end] + string[start:] is always equal to string

word[:1] + word[1:] # same to word[0:1]+word[1 .. -1] [enter] prints
Xcas
word[:2] + word[2:] # same to word[0:2]+word[2 .. -1] [enter] prints
Xcas


Attempting to use an index out of the maximum character size will result in an error:

word[3] [enter] prints
s
word[4] # the word only has 4 characters, between 0..3 [enter] returns
Gen [int] Error: Bad Argument Type

However, out of range slice indexes are handled gracefully when used for slicing:

word[1:5] [enter] prints
cas
word[5:] # same to word[5 .. -1] [enter] returns
""
word[0:1] + word[1:5] [enter] prints
Xcas

Creating string from another:

word[0:] + " with Python Syntax" # same to word[0 .. -1]+" with Python Syntax"[enter] returns
"Xcas with Python Syntax"

The built-in function len() returns the length of a string:
s := "supercalifragilisticexpialidocious" [enter] returns
"supercalifragilisticexpialidocious"
len(s) [enter] returns 34

Unlike the Python language, where the elements or characters of a string are immutable, the elements or characters of a string in Xcas can be changed.
word [enter] returns
Xcas

word[0] := "x"; word [enter] returns
Done, xcas

3.1.3. LIST

Xcas knows a number of compound data types, used to group together other values. The most versatile is the list, which can be written as a list of comma-separated values (items) between square brackets. Lists might contain items of different types, but usually the items all have the same type.

squares := [1, 4, 9, 16, 25] [enter] returns
[1, 4, 9, 16, 25]

squares[0] # indexing in 0 returns the ítem [enter] returns
1
squares[-1] [enter] returns
25
squares[-3:] # slicing (-3 to -1) or squares[-3..-1 ] returns a new list [enter] returns
[9, 16, 25]

All slice operations return a new list containing the requested elements. This means that the following slice returns a new (shallow) copy of the list:

squares[:] [enter] returns
[1, 4, 9, 16, 25]

The lists are a mutable type, i.e. it is possible to change their content:
cubes := [1, 8, 27, 65, 125] # something's wrong here [enter] returns
[1, 8, 27, 65, 125]
4 ** 3 # the cube of 4 is 64, not 65! [enter] returns
64
cubes[3] := 64 # replace the wrong value [enter] returns [1, 8, 27, 64, 125]
cubes [enter] returns
[1, 8, 27, 64, 125]

Lists also support operations like concatenation, you can also add new items at the end of the list, by using the .append() method or . extend() for add other list at the end of the a list. the namevar.append() and namevar .extend() methods Automatically modify the value namevar

APPEND

cubes.append( 216 ) # add the cube of 6 [enter] returns
[ 1, 8, 27, 64, 125, 216 ]
cubes.append( 7 ** 3 ) # and the cube of 7 [enter] returns
[ 1, 8, 27, 64, 125, 216, 343 ]
cubes [enter] returns
[ 1, 8, 27, 64, 125, 216, 343 ]

l1:=[1,2,3] [enter] returns [1,2,3]
l2:=[4,5,6] [enter] returns [4,5,6]
l1.append(l2) [enter] returns
[1,2,3,[4,5,6]]

INSERT

cubes.insert( 0, 0 ) [enter] returns
[ 0, 1, 8, 27, 64, 125, 216, 343 ]


INDEX: Number of elements different from the maximum number of the index.
cubes.index(343) [enter] returns
7

cubes.length [enter] returns
8

You can insert in size + 1 position
cubes.insert( 7+1, 8**3 ) [enter] returns
[ 0, 1, 8, 27, 64, 125, 216, 343, 512 ]

IN (member or belonging to an element) the initial position starts at 1, 0 means it is not in the list

9**3 in cubes [enter] returns
0 (no position)

8**3 in cubes [enter] returns
9

cubes.index(8**3) [enter] returns
8

EXTEND (CONCATENATION)

squares [enter] returns
[ 1, 4, 9, 16, 25 ]
squares.extend( [ 36, 49, 64, 81, 100 ] ) [enter] returns
[ 1, 4, 9, 16, 25, 36, 49, 64, 81, 100 ]

l1:=[1,2,3] [enter] returns [1,2,3]
l2:=[4,5,6] [enter] returns [4,5,6]
l1.extend(l2) [enter] returns
[ 1, 2, 3, 4, 5, 6]

Unlike the Python language, list + list, Xcas does not perform concatenation, but depends on the type of list, it can be sum of vectors (sum of elements from left to right), or sum of polynomials (sum of elements from right to left)

# + as sum of vectors
[ 1, 4, 9, 16, 25 ] + [ 36, 49, 64, 81, 100 ] [enter] returns
[ 37, 53, 73, 97, 125 ]

[1,2,3]+[4] # equal to [1,2,3]+[4,0,0] [enter] returns
[5,2,3]

# sum of polynomials
poly1[1,2,3]+poly1[4] # equal to [1,2,3]+[0,0,4] [enter] returns
poly1[1,2,7]

special case:

a := ["a", "b", "c"];
n := [1, 2, 3] [enter]
a+n # concatenate element by element [enter]
["a1","b2","c3"]



#########
Assignment to slices is also possible, and this can even change the size of the list or clear it entirely:

Letters := [ "a", "b", "c", "d", "e", "f", "g" ] [enter] returns
[ "a","b","c","d","e","f","g" ]

Letters[ 2:5 ] [enter]
["c","d","e"]

# replace some values
Letters[ 2:5 ] := [ "C", "D", "E" ] [enter] returns
["a", "b", "C", "D", "E", "f", "g"]


SUPPRESS

# but now remove them
Letters[2:5] := [] [enter] Python language returns
[ "a", "b", "f", "g"]

Letters[2:5] := [] [enter] Xcas Python Syntax returns
"Letters[2:5]=[] Error: Invalid dimension"

suppress( Letters,2:5) [enter] Xcas Python Syntax returns
[ "a", "b", "f", "g"]

# clear the list by replacing all the elements with an empty list
Letters[ : ] = [] [enter] Python language returns
[ ]
suppress( Letters,:length(Letters)) [enter] Xcas Python Syntax returns
[ ]

REMOVE Remove all occurrences
l3:=[9,9,9,8,8,7,9,6] [enter] returns [9,9,9,8,8,7,9,6]
l3.remove(9) [enter] returns
[8,8,7,6]

Letters.remove("a") [enter] returns
["b","C","D","E","f","g"]

POP
delete last item

Letters.pop [enter] returns "g"
Letters [enter] returns
["a","b","C","D","E","f"]

specifying a position
Letters.pop(3) [enter] returns "D"
Letters [enter] returns
["a","b","C","E","f"]

########
The built-in function len() also applies to lists:
Letters := [ "a", "b", "c", "d", "e", "f", "g" ]; [enter] returns [ "a", "b", "c", "d", "e", "f", "g" ]
len(Letters) [enter] returns
7

########
It is possible to nest lists (create lists containing other lists), for example:
a := ["a", "b", "c"] [enter] returns ["a", "b", "c"]
n := [1, 2, 3] [enter] returns [1, 2, 3]
x := [a, n] [enter] returns
[["a","b","c"],[1,2,3]]

x[0] [enter] returns
["a", "b", "c"]

x[0][1] [enter] returns
"b"

x[0,1] # alternative way, not available in python language [enter] returns
"b"



3.2. First Steps Towards Programming


We will write an initial subsequence of the Fibonacci series as follows:

# Fibonacci series:
# the sum of two elements defines the next

ClrIO
a, b = 0, 1
while a < 10:
↦ ↦print(a)
↦ ↦a, b = b, a+b [enter] returns

0
1
1
2
3
5
8

Writing ...
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
hpPrime, with Python Syntax, beyond the Python numeric language - compsystems - 02-28-2019 02:32 AM
RE: hpPrime with Python syntax - Giancarlo - 02-28-2019, 08:11 PM



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