Post Reply 
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
def fib(n):  # write Fibonacci series up to n Print a Fibonacci series up to n 
        
a01
        
while n:
            print(
aendl="\t"  )
            
aba+b
        
return Done
#end 

fib(22) [enter] prints
PHP Code:
0    1    1    2    3    5    8    13    21 

PHP Code:
#cas
def fib(n):  # write Fibonacci series up to n Print a Fibonacci series up to n 
        
a01
        
while n:
            print(
aendl="\n"  )
            
aba+b
        
return Done
#end 

fib(22) [enter] prints
PHP Code:
0
1
1
2
3
5
8
13 

PHP Code:
#cas
def legendre_nevalX="" ):
    
local pxxtype2argpurge(x)
    if 
getType(n) == "NUM":
      
px := 1/(2^n*n!)*diff((x^2-1)^n,x,n)
      
type2arg := getType(evalX)   
      if 
type2arg == "NUM" or type2arg == "EXPR" or type2arg == "VAR":
        
px := subst(px,x=evalX)
      
elif type2arg == "STR" and evalX=="list":
        
px:=e2r(px)   
        return 
px
    
return "Error, se espera un numero entero en el primer argumento"
#end 

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) ] 
[enter] returns
[[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):={
  
local col_row_mt;
  
mt := makemat(rowDim(m),colDim(m));
  for 
col_ from 0 to colDim(m)-do   
      for 
row_ from 0 to rowDim(m)-do   
         
mt[col_,row_] := m[row_,col_];
      
end_for
  end_for
  
return mt;
}:; 

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 = [2357]
for 
prime in primes:
    print( 
primeendl=" "
[enter]

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 out the numbers 0 1 2 3 4
for x in range(5):
    print(
xendl=" ")
print(
"")

# Prints out 3 4 5
for x in range(36):
    print(
xendl=" ")
print(
"")

# Prints out 3 5 7
for x in range(382):
    print(
xendl=" "
[enter]

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 
count_ := 0
while( count_ 5):
    print( 
count_endl=" ")
    
count_ += 1
    
"count value reached=" count_ 
[enter]

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 
count_ := 0
while True:
    print( 
count_endl=" ")
    
count_ += 1
    
if count_ >= 5:
        break 
[enter]

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 
for x in range(10):
    
# Check if x is even
    
if == 0:
         continue
    print( 
xendl=" "

[enter]

returns 1
print on console
1 3 5 7 9
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 numeric language - compsystems - 04-04-2019 01:33 AM



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