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 ... |
|||
02-28-2019, 08:11 PM
Post: #2
|
|||
|
|||
RE: hpPrime with Python syntax
Hello Compsystems,
Thank you very much for writing this text. May I ask you to write some complete examples with the full code to digit into the calculator? I could be interested, one day or another to learn python, in particular if some other libraries will be implemented. Thanks Giancarlo |
|||
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 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 |
|||
03-05-2019, 12:23 PM
Post: #4
|
|||
|
|||
RE: hpPrime with Python syntax, Beyond the Python language
Hello compsystems,
Thank you very much. That complete your post in my opinion. I will try it in the next days. Thanks again, Giancarlo |
|||
03-08-2019, 02:35 AM
(This post was last modified: 10-25-2019 08:36 PM by compsystems.)
Post: #5
|
|||
|
|||
RE: hpPrime, with Python Syntax, beyond the Python numeric language
List comprehension.
Is a compact expression to define lists. purge(x,j,k) # Remove content from variables python_compat(1) [enter] initializes the python syntax in xCAS and ** operator to calculate powers # Case 1, define a numerical sequence list1 := range( 5 ) # The "range" function returns a list, starting at 0 and ending with the indicated number minus one, increase in 1 [enter] returns [ 0, 1, 2, 3, 4 ] range( 5 ) is equivalent to [ seq( 0..4 ) ] [ seq( 0..4 ) ] [enter] returns [ 0, 1, 2, 3, 4 ] list1a := range( 1, 5 ) # specifying start and end-1 [enter] returns [ 1, 2, 3, 4 ] range( 1, 5 ) is equivalent to [ seq( 1..4 ) ] [ seq( 1..4 ) ] [enter] returns [ 1, 2, 3, 4 ] list1b := range( 1, 8, 1.9 ) # specifying start and end-1, more step [enter] returns [ 1.0, 2.9, 4.8, 6.7 ] range( 1, 8, 1.9 ) is equivalent to [ seq( 1..7, 1.9 ) ] [ seq( 1..7, 1.9 ) ] [enter] returns [ 1.0, 2.9, 4.8, 6.7 ] range( 11, 1, -1) # negative ranges [enter] returns [ 11, 10, 9, 8, 7, 6, 5, 4, 3, 2 ] range( 11, 1, -1) is equivalent to [ seq( 11..2, -1 ) ] [ seq( 11..2, -1 ) ] [enter] returns [ 11, 10, 9, 8, 7, 6, 5, 4, 3, 2 ] # Case 2, define a list, with an input function list2 := [ k*k for k in range( 5 ) ] # create a list, for each element in the range, multiply it by itself, and add it as a new item in the list [enter] returns [ 0, 1, 4, 9, 16 ] [ k*k for k in range( 5 ) ] is equivalent to seq( j*j, j, 0, 4 ) # variable and domain separately or seq( j*j, j, [ 0, 1, 2, 3, 4 ] ) # explicit domain as a list or [ seq( j*j, j=0..4 ) ] # domain as an expression seq( j*j, j, 0, 4 ) [enter] returns [0,1,4,9,16] seq( j*j, j, [ 0, 1, 2, 3, 4 ] ) [enter] returns [0,1,4,9,16] [ seq( j*j, j=0..4 ) ] [enter] returns [0,1,4,9,16] # Case 3, define list with a symbolic sequence list2 := [ x^j for j in range( 4 ) ] [enter] returns [ 1, x, x^2, x^3 ] [ x^j for j in range( 4 ) ] is equivalent to seq( x^j, j, 0, 3 ) seq( x^j, j, 0, 3 ) [enter] returns [ 1, x, x^2, x^3 ] # Case 3a, define list with a symbolic constant list3a := [ t for c in range( 4 ) ] [enter] returns [ t, t, t, t ] [ t for c in range( 4 ) ] is equivalent to seq( t, 4 ) seq( t, 4 ) [enter] returns [ t, t, t, t ] # Case 3b, define list with a numeric constant list3b := [ -1 for c in range( 4 ) ] [enter] returns [ -1, -1, -1, -1 ] [ 1 for c in range( 4 ) ] is equivalent to seq( -1, 4 ) seq( -1, 4 ) [enter] returns [ -1, -1, -1, -1 ] # Case 4, define a list of list (matrix) list4 := [ [ k, k + 2 ] for k in range ( 5 ) ] [enter] returns [[ 0,2 ],[ 1,3 ],[ 2,4 ],[ 3,5 ],[ 4,6 ]] [ [k, k + 2] for k in range ( 5 ) ] is equivalent to seq( [ k, k + 2 ], k, 0, 4 ), or [ seq( [ k, k + 2 ], k=0..4 ) ] seq( [ k, k + 2 ], k, 0, 4 ) [enter] returns [[ 0,2 ],[ 1,3 ],[ 2,4 ],[ 3,5 ],[ 4,6 ]] PHP Code: #cas testPythonSyntax_listComp()[enter] returns [0,1,2,3,4] [0,1,2,3,4] [1,2,3,4] [1,2,3,4] [1.0,2.9,4.8,6.7] [1,2.9,4.8,6.7] [11,10,9,8,7,6,5,4,3,2] [11,10,9,8,7,6,5,4,3,2] [0,1,4,9,16] [0,1,4,9,16] [0,1,4,9,16] [0,1,4,9,16] [1,x,x**2,x**3] [1,x,x**2,x**3] [t,t,t,t] [t,t,t,t] [-1,-1,-1,-1] [-1,-1,-1,-1] [[0,2],[1,3],[2,4],[3,5],[4,6]] [[0,2],[1,3],[2,4],[3,5],[4,6]] ################################# Functions Functions are defined with the keyword def, followed by the name of the function and its parameters in (). Another way to define functions is with the keyword lambda (only for an expression). The value returned in the functions with def will be the one given with the return statement. using def: PHP Code: def sumxy(x, y = 2): "Done" Above function name is sumxy, it expects two arguments x and/or y and returns their sum. # Call the function sumxy(4) # The variable "y" takes the default value: 2 [enter] returns 6 sumxy(4, 10) # The variable "y" takes the value entered: 10 [enter] returns 14 Let’s see how we can convert the above function into a lambda function: PHP Code: sumxy = lambda x, y=2: x + y sumxy(4) [enter] returns 6 sumxy(4, 10) [enter] returns 14 PHP Code: #cas If we check type of add, it is a function. type(sumxy) [enter] returns func The functions can be passed as parameters to a new function which expects a function name as parameter like map cmd def multiply2(x): return x * 2 map(pow2, [1, 2, 3, 4]) [enter] returns [1,4,9,16] ############# Another example PHP Code: #cas calculator( "addition", 3, 4 ) [enter] returns 7 calculator( "subtraction", 3, 4 ) [enter] returns -1 calculator( "multiplication", 3, 4 ) [enter] returns 12 calculator( "division", 3, 4 ) [enter] returns 3/4~=0.75 to rewrite the code use python(name of the program) python( calculator ) [enter] returns "lambda op_rep,a,b: if ((""addition"")==op_rep) : return(a+b) else : if ((""subtraction"")==op_rep) : return(a-b) else : if ((""multiplication"")==op_rep) : return(a*b) else : if ((""division"")==op_rep) : return(a/b) else : return(None)" ########### ZIP vector1 = [1,2,3,4] vector2 = [7,8,3,2] DispG ClrIO printf("vector1 vector2") for array2d in zip(vector1,vector2): printf("%gen \t %gen", array2d[0], array2d[1]) vector1 := [1,2,3,4]; vector2 := [7,8,3,2] dict1 = table(zip(vector1,vector2)) all(iterable) Return True if in evaluating all elements of the iterable list/string are true (or if the iterable is empty). If not, it returns False. all function equivalent to: #cas def all( iterable ): for element_ in iterable: if not element_: return False return True #end True - If all elements in an iterable are true False - If any element in an iterable is false Truth table for all() When ----------------------------------- Return Value All values are true -------------------- True All values are false ------------------- False One value is true (others are false) --- False One value is false (others are true) --- False Empty Iterable ------------------------- True # Examples for list # all values true lst1 := [ -5.8, -1, 1, 3.5, 4, 10 ]; print( all( lst1 ) ) When executing the program, the output on the I/O line will be: 1 and in the console will be: True # all values false lst1 = [ 0, False, 0=-1 ] ; print( all( lst1 ) ) [enter] returns 1 False # one false value lst1 := [-1, 1, 0, abs(-1)==1 ] ; print( all( lst1 ) ) [enter] returns 1 False # one true value lst1 := [ 0, False, abs(-1)==1 ] ; print( all( lst1 ) ) [enter] returns 1 False # empty iterable lst1 = [] ; print( all( lst1 ) ) [enter] returns 1 True lst1:= [ 1, True, "×÷" ] print( all( lst1 ) ) [enter] Error, can not be determined if the element "×÷" is true or false # Examples on strings (NOT SUPPORTED) str1 := "This is string"; print( all( str1 ) ) [enter] returns Error # Examples on tables In case of tables data, if all the left part are numeric and true or the table is empty, all() returns True. Else, it returns false for all other cases.. tbl1 := table( 0: "", 65: "A", 97: "a", 215: "×", 247: "÷" ); print( all( tbl1 ) ) [enter] returns 1 False tbl1 := table( 65: "A", 97: "a", 215: "×", 247: "÷" ); print( all( tbl1 ) ) [enter] returns 1 True tbl1 := table() print( all( tbl1 ) ) [enter] returns 1 True tbl1 := table("1": one, "2": two) tbl1 := table("65": "A", "97": "a") tbl1 := { -1: False, 0: False }; print( all( tbl1 ) ) [enter] returns 1 Print False tbl1 := { -1: True, 1: True }; print( all( tbl1 ) ) [enter] returns 1 True s = {"000": 'True'} print(all(s)) Error lst1= [ 1, True, "×÷" ] print( all( lst1 ) ) |
|||
04-04-2019, 01:33 AM
(This post was last modified: 04-27-2019 02:09 AM by compsystems.)
Post: #6
|
|||
|
|||
RE: hpPrime, with Python Syntax, beyond the Python numeric language
print() function (Now accept extra parameters)
Definition and Usage The print() function prints the specified message to the console screen Syntax for a only object: print( object ) Example print("Hello world")[enter] prints Hello world Syntax for more than one object, The objects are printed by default, separated by commas print( object(s) ) Example print("Hello", "world") [enter] prints Hello,world optional parameters print( object(s), sep="string sepator", end="string end") separator="string sepator": Specify how to separate the objects, if there is more than one. Default is "," Example print("Hello", "world", sep=" ") [enter] prints Hello world print("Hello", "world", sep="_") [enter] prints Hello_world print("Hello", "world", sep=";") [enter] prints Hello;world print("Hello", "world", sep="\n") [enter] prints Hello world end/endl="string end" Specify what to print at the end. Default is '\n' (line feed) PHP Code: #cas fib(22) [enter] prints PHP Code: 0 1 1 2 3 5 8 13 21 PHP Code: #cas fib(22) [enter] prints PHP Code: 0 PHP Code: #cas Nested List Comprehensions Consider the following example of a 3x3 matrix held as a list containing three lists, one list per row: m := [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] Now, if you wanted to swap rows and columns, you could use a list comprehension: Xcas Syntax Python PHP Code: [ [row_[col_] for row_ in m ] for col_ in seq(k,k,0,colDim(m)-1) ] [[1,4,7],[2,5,8],[3,6,9]] To avoid apprehension when nesting list comprehensions, read from right to left. A more verbose version of this snippet shows the flow explicitly: Xcas Code PHP Code: transpose_(m):={ transpose_([[1,2,3],[4,5,6],[7,8,9]]) [enter] returns [[1,4,7],[2,5,8],[3,6,9]] for i in [0, 1, 2]: for row in mat: print(row[i], end="") print() ##################### LOOPS There are several types of loops in Xcas, FOR and WHILE. more for and while in python syntax The "for" loop For loops iterate over a given input sequence . Here is an script example: ClrIO PHP Code: primes = [2, 3, 5, 7] returns 0,[2, 3, 5, 7], 0, 0 print on console 2 3 5 7 For loops can iterate over a sequence of numbers using the "range" function. The range function returns a new list with numbers of that specified range. Note that the range function is zero based. PHP Code: ClrIO prints 0 1 2 3 4 3 4 5 3 5 7 "While" loops repeat as long as a certain boolean condition is met. For example: PHP Code: # Prints out 0 1 2 3 4 returns 0,"count value reached=5" print on console 0 1 2 3 4 Break is used to exit a for loop or a while loop, whereas continue is used to skip the current block, and return to the "for" or "while" statement. A few examples: PHP Code: # Prints out 0 1 2 3 4 returns 0,"count value reached=5" print on console 0 1 2 3 4 PHP Code: # Prints out only odd numbers 1 3 5 7 9 [enter] returns 1 print on console 1 3 5 7 9 |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 3 Guest(s)