lundi 27 septembre 2010

the basic syntax (1)

Following the IDL cookbook, I first explore some syntax of Python.

I use the ipython, so the keyboards inputs are transcript with "In [n]" and the corresponding output with "Out [n]". To print something, just type it and press Enter:

In [4]: 'Hello mundo;-)'
Out[4]: 'Hello mundo;-)'
In [5]: a=1
In [6]: a
Out[6]: 1
In [8]: a?
Type:        int
Base Class:    <type 'int'>
String Form:    1
Namespace:    Interactive
Docstring:
    int(x[, base]) -> integer
.... 
In [35]: 3*exp(-12/(27.*log(1.3)))
Out[35]: 0.55135006225210048
In [36]: 3*exp(-12/(27.*log10([12.,14.,15])))
Out[36]: array([ 2.5086749 ,  2.53502116,  2.54592125])
 
Special characters:

;  is use to have 2 comands on the same line (and is NOT starting a comment!) 
a=5 ; b= 3
which actually can be done with:
a,b = 5,3
No matter spaces IF NOT AT THE BEGINNING OF THE LINE:
a , b = 5 , 3  
BTW this can be very powerful:
a, b, c = b, a+b, c+1  
Do you remember this one? Fibonnacci!

# is starting a comment 
a = b # a and b are pointing to different memory place

Parameters are separated by "," on the same line:
can = Canvas(f,width =250, height =250, bg ='ivory')

The blocks are defined by indentation. No begin, no end. In the following, the ....: are printed by ipython. The indentation is automatic in ipython, but not in python: you have to type at least one space to define the IF block. To finish a block, just empty lines (one in python, 2 in ipython...)
In [70]: a,b=5,6

In [71]: if a<b:
   ....:     print a+b
   ....:    
   ....:    
11


In [65]: def f(x):
   ....:     return x*2
   ....: 
In [67]: f(5)
Out[67]: 10



Aucun commentaire:

Enregistrer un commentaire