hpPrime, with Python Syntax, beyond the Python numeric language
|
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 »
|
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
RE: hpPrime with Python syntax, Beyond the Python language - compsystems - 03-03-2019, 05:31 PM
RE: hpPrime with Python syntax, Beyond the Python language - Giancarlo - 03-05-2019, 12:23 PM
RE: hpPrime, with Python Syntax, beyond the Python numeric language - compsystems - 03-08-2019, 02:35 AM
RE: hpPrime, with Python Syntax, beyond the Python numeric language - compsystems - 04-04-2019 01:33 AM
|
User(s) browsing this thread: 1 Guest(s)