Post Reply 
hpPrime, with Python Syntax, beyond the Python numeric language
03-03-2019, 05:31 PM (This post was last modified: 05-18-2019 11:12 PM by compsystems.)
Post: #3
RE: hpPrime with Python syntax, Beyond the Python language
Testing the previous examples in Xcas and hpprime, please copy the following code

PHP Code:
#cas
def testPythonSyntax():
    
local Done "Done"
    
ClrIO
    python_compat
(1)

    
# this is a comment
    
print( # 4
    
print( 50 5*# 20
    
print( (50 5*6) / # 5
    
print( 5# 8/5
    
print( 3.75 # 14.0 and no 14

    
print( 17 # 17/3, rational division
    
print( 17 3. # 5.666..., approximate value calculation
    
print( 17 // 3  ) # 5, floor division discards the fractional part
    
print( 17 3  # 2, the % operator returns the remainder of the division
    
print( # 17, result * divisor + remainder

    
print( ** # 25, 5 squared
    
print( ** # 128, 2 to the power of 7

    # Strings
    
print( "Enter x value" # prints only the content of the string
    
print( "It's not a python language, it's CAS + python syntax" )
    print( 
"This is the string in the first line\nand this is the second string in a second line\n\tand now a third tabulated" )
    print( 
"Enter a ""String"":" # to insert a double quote, within a string, the " character must be duplicated
    
print( "Enter a \"String\":" # or with escape quotes
    
print( "\"Enter a String:\"" # if you want to show double quotes at the beginning and end, you should follow the next sequence "\" … \"" e.g.

    # Arithmetic operations in strings, sum or concatenation and multiplication by a scalar (constant)
    
print( "un" "ium" # 3 * "un" + "ium" # 3 times "un", followed by "ium"

    
word := "Xcas"

    
# The built-in function len() returns the length of a string:
    
print( lenword ) ) # 4 chars

    
print( "1__2__3__4" )
    print( 
"0__1__2__3" # pos 0...3
    
print( "+---+---+---+---+" )
    print( 
"| X | c | a | s |" )
    print( 
"+---+---+---+---+" )
    print( 
"-4_-3_-2_-1" # pos -1...-4

    # Strings can be indexed. The first character on the left occupies the first positive position and starts at 0 and continues upward for the positions on the right. The first character on the right occupies the first negative position and starts at -1 and continues downwards for the positions on the left

    
print( word[:] ) # Xcas, print the entire string
    
print( word] ) # X, character in firts position
    
print( word] ) # s, character in position 3, base 0
    
print( word[ -] ) # s, last character
    
print( word[ -] ) # a, second-last character
    
print( word[ -] ) # X

    # Strings can be indexed by slicing to get a new list
    # Note string[  start  :  end  ] the start is always included, and the end always excluded.
    
print( word1:] ) # ca, characters from position 1 (included) to 3 (excluded)


    # To select the first k characters (slice), stringName( [ 0 : k ] ) is used.
    
print( word0:] ) # Xc, characters from position 0 (included) to 2 (excluded)

    # stringName[ k : ], portion from the index k to the end of the string
    
print( word1:] ) # cas, characters from position 1 (included) to the end (size of the string, position 3) 
    
print( word[ -3:] ) # cas, characters from the third-last (included) to position -1

    # stringName[ : k ], portion from the begin of the string to the index k-1  
    
print( word[:] ) # Xca


    
print( word[:] + word1:] ) # Xcas, stringName[ : k ] + stringName[ k : ] = stringName[:]

    
print( word] ) # s
    # Remove the following comment to verify the error
    # print( word[ 4 ] ) # Error: the word only has 4 characters, between 0..3
    
print( word1:] ) # cas, in an interval values outside the size of the string are ignored
    
print( word5:] ) # prints ""
    
print( word0:] + word1:] ) # Xcas
    
print( word0:] + " with Python Syntax" # Xcas with Python Syntax

    
:= "supercalifragilisticexpialidocious"
    
print( len) ) # 34

    
word] := "x" # 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.
    
print( word[:] )


    
# List
    
squares := [ 1491625 ]
    print( 
squares )


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


    # ...
    
return Done
#end 

testPythonSyntax() [enter] returns

4
20
5
8/5
14.0
17/3
5.66666666667
5
2
17
25
128
Enter x value
It's not a python language, it's CAS + python syntax
This is the string in the first line
and this is the second string in a second line
and now a third tabulated
Enter a "String":
Enter a "String":
"Enter a String:"
unununium
4
1__2__3__4
0__1__2__3
+---+---+---+---+
| X | c | a | s |
+---+---+---+---+
-4_-3_-2_-1
Xcas
X
s
s
a
X
ca
Xc
cas
cas
Xca
Xcas
s
cas
""
Xcas
Xcas with Python Syntax
34
xcas
[1,4,9,16,25]
1
25
[9,16,25]


Sequences

t1:=("one", "two", "three")

"one" in t1 [enter] returns 1
"two" in t1 [enter] returns 2

t2:=("a","a","a","e","e","i","i","i","u") [enter]
t2.count(t2,"a") [enter] returns 1? > 3

t2.length [enter] returns 9

t3:=("XCAS",123,x+y)
str1, num1, symb1 := t3 [enter]
str1 [enter] "XCAS"
num1 [enter] 123
symb1 [enter] x+y
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: hpPrime with Python syntax - Giancarlo - 02-28-2019, 08:11 PM
RE: hpPrime with Python syntax, Beyond the Python language - compsystems - 03-03-2019 05:31 PM



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