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( 2 + 2 ) # 4
print( 50 - 5*6 ) # 20
print( (50 - 5*6) / 4 ) # 5
print( 8 / 5) # 8/5
print( 4 * 3.75 - 1 ) # 14.0 and no 14
print( 17 / 3 ) # 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( 5 * 3 + 2 ) # 17, result * divisor + remainder
print( 5 ** 2 ) # 25, 5 squared
print( 2 ** 7 ) # 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( 3 * "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( len( word ) ) # 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[ 0 ] ) # X, character in firts position
print( word[ 3 ] ) # s, character in position 3, base 0
print( word[ -1 ] ) # s, last character
print( word[ -2 ] ) # a, second-last character
print( word[ -4 ] ) # 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( word[ 1:3 ] ) # ca, characters from position 1 (included) to 3 (excluded)
# To select the first k characters (slice), stringName( [ 0 : k ] ) is used.
print( word[ 0:2 ] ) # Xc, characters from position 0 (included) to 2 (excluded)
# stringName[ k : ], portion from the index k to the end of the string
print( word[ 1:] ) # 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[:3 ] ) # Xca
print( word[:1 ] + word[ 1:] ) # Xcas, stringName[ : k ] + stringName[ k : ] = stringName[:]
print( word[ 3 ] ) # s
# Remove the following comment to verify the error
# print( word[ 4 ] ) # Error: the word only has 4 characters, between 0..3
print( word[ 1:5 ] ) # cas, in an interval values outside the size of the string are ignored
print( word[ 5:] ) # prints ""
print( word[ 0:1 ] + word[ 1:5 ] ) # Xcas
print( word[ 0:] + " with Python Syntax" ) # Xcas with Python Syntax
s := "supercalifragilisticexpialidocious"
print( len( s ) ) # 34
word[ 0 ] := "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 := [ 1, 4, 9, 16, 25 ]
print( squares )
print( squares[ 0 ] ) # indexing in 0 returns the firts ítem
print( squares[ -1 ] )# 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