doc:user:tutorials:tuto0
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
doc:user:tutorials:tuto0 [2014/08/07 22:48] – [Important remarks] joris | doc:user:tutorials:tuto0 [2020/09/08 12:17] – [Modules] boman | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | | + | ====== Python |
- | ====== | + | |
+ | This section shows how to use [[doc: | ||
- | ===== Introduction ===== | + | Python is a powerful scripting language allowing the creation of input files in a very convenient way for the developer, but it might look a bit tedious for a new user. |
- | This section shows how to use in a basic way [[doc: | ||
- | Python is a powerful language allowing the developer to create a set of data very easily, but it might look a bit tedious for the code user. | + | ===== Introduction |
- | ===== The Interpreter | + | |
- | + | ||
- | + | ||
- | + | ||
- | ==== Introduction | + | |
When Metafor is started, a window appears: | When Metafor is started, a window appears: | ||
Line 22: | Line 16: | ||
Above the interpreter, | Above the interpreter, | ||
- | Once Python is initialized, | + | Once Python is initialized, |
- | A Metafor [[doc: | + | A Metafor [[doc: |
- | Another interesting aspect of Python is its ability to interface compiled [[doc: | + | Another interesting aspect of Python is its ability to interface compiled [[doc: |
- | Python is easily used. For example, it can be used as a calculator : | + | Python is quite simple. For example, it can be used as a calculator |
>>> | >>> | ||
Line 37: | Line 31: | ||
>>> | >>> | ||
- | Careful : this sequence is only correct when using Metafor without its graphical interface. With Metafor GUI, the correct syntax to get the value of //c// is with the command | + | In a script, the correct syntax to get the value of '' |
- | This allows the parametrization of Metafor | + | The definition of variables |
To display memory contents, use '' | To display memory contents, use '' | ||
Line 48: | Line 42: | ||
>>> | >>> | ||
- | The three variables that were just created can be seen, and so can the '' | + | The three variables that were just created can be seen, and so can the '' |
>>> | >>> | ||
Line 56: | Line 50: | ||
' | ' | ||
- | ==== Types ==== | + | ===== Types ===== |
- | The type of a variable is printed using the command '' | + | The type of a variable is obtained with the command '' |
>>> | >>> | ||
>>> | >>> | ||
- | <type ' | + | <class ' |
>>> | >>> | ||
>>> | >>> | ||
- | <type ' | + | <class ' |
>>> | >>> | ||
- | Metafor uses the basic types (float, string, integer, tuple, ...), but also its own types, which are defined | + | Metafor uses the basic python |
Finally, variables can be converted from one type to another. For example, '' | Finally, variables can be converted from one type to another. For example, '' | ||
Line 74: | Line 68: | ||
- | ==== Functions ==== | + | ===== Functions |
- | Simple functions can be created using the interpreter. This is quite useful to avoid any cut and paste, or simply to write algorithms as scripts. For example : | + | Simple functions can be created using the interpreter. This is quite useful to avoid any cut and paste, or simply to write algorithms as scripts. For example: |
>>> | >>> | ||
Line 84: | Line 78: | ||
>>> | >>> | ||
- | Functions are essential in the writing of a data set. This way, complex operations may be defined and called several times, from the same module or even from other ones. For example, if the studied test case contains several similar [[doc: | + | Functions are essential in the proper |
- | Also, default | + | Default |
<code python> | <code python> | ||
Line 97: | Line 91: | ||
y = myfun(3, b=2) | y = myfun(3, b=2) | ||
- | Here, '' | + | Here, '' |
This function may be written in another way, using the '' | This function may be written in another way, using the '' | ||
Line 103: | Line 97: | ||
myfun = lambda x, a=1, b=1, c=0 : a*x*x+b*x+c | myfun = lambda x, a=1, b=1, c=0 : a*x*x+b*x+c | ||
- | + | ===== Modules | |
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | ==== Modules ==== | + | |
It is quite useful to gather several functions into a file, to form a [[doc: | It is quite useful to gather several functions into a file, to form a [[doc: | ||
- | Python | + | By default, |
>> import math | >> import math | ||
>>> | >>> | ||
- | [' | + | [' |
- | | + | |
- | | + | |
+ | ... | ||
+ | | ||
>>> | >>> | ||
0.99500416527802582 | 0.99500416527802582 | ||
Line 127: | Line 117: | ||
>>> | >>> | ||
- | Help on built-in function cos in module math : | + | Help on built-in function cos in module math: |
| | ||
cos(...) | cos(...) | ||
Line 136: | Line 126: | ||
'' | '' | ||
- | It is also possible to import all functions found in '' | + | It is also possible to import all functions found in '' |
>>> | >>> | ||
Line 143: | Line 133: | ||
>>> | >>> | ||
- | With this syntax, the '' | + | With this syntax, the '' |
- | Concerning Metafor, all classes found in the Metafor | + | To use mathematical functions, the module '' |
- | In this case, the module no longer contains functions but objects. | + | |
- | ==== Objects ==== | + | ===== Objects |
- | Python is [[doc: | + | Python is [[doc: |
<code python> | <code python> | ||
- | def vectLength(x, | + | def vectLength(x, |
import math | import math | ||
- | return math.sqrt(x*x+y*y+z*z) | + | return math.sqrt(x*x + y*y + z*z) |
</ | </ | ||
- | At this point, it should be noted that an block of several instructions is defined | + | < |
+ | At this point, it should be noted that a block of several instructions is defined with the indentation, | ||
+ | </ | ||
- | The function called '' | + | The function called '' |
<code python> | <code python> | ||
class vector: | class vector: | ||
def __init__(self, | def __init__(self, | ||
- | self.x=x | + | self.x = x |
- | self.y=y | + | self.y = y |
- | self.z=z | + | self.z = z |
def view(self): | def view(self): | ||
- | print ' | + | print(' |
def length(self): | def length(self): | ||
import math | import math | ||
- | return math.sqrt(self.x*self.x+self.y*self.y+self.z*self.z) | + | return math.sqrt(self.x*self.x + self.y*self.y + self.z*self.z) |
</ | </ | ||
- | Three methods (or member functions) | + | Three methods (or member functions) |
- | Calculating | + | To compute |
- | >>> | + | >>> |
- | This command calls the constructor | + | This command calls the constructor |
>>> | >>> | ||
- | <type 'instance'> | + | <class '__main__.vector'> |
>>> | >>> | ||
x= 1 y= 1 z= 1 | x= 1 y= 1 z= 1 | ||
Line 191: | Line 182: | ||
>>> | >>> | ||
- | With Metafor, the user handles | + | When creating an input file in Metafor, the user manipulates |
- | ==== Loop - tuple - list ==== | + | |
- | In a Metafor [[doc: | + | |
+ | ===== Loop - tuples - lists ===== | ||
+ | |||
+ | In a Metafor [[doc: | ||
<code python> | <code python> | ||
- | for i in (1, | + | for i in (1, 6, 8, 12): |
- | print ' | + | print(' |
</ | </ | ||
where the values in parentheses constitute a " | where the values in parentheses constitute a " | ||
- | Instead of tuples, lists may also be used. To do so, brackets are used instead of parentheses. Unlike tuples, lists may be changed. | + | Instead of tuples, lists may also be used. To do so, brackets are used instead of parentheses. Unlike tuples, lists may be modified. |
- | To create a list, the function '' | + | To create a list, the function '' |
- | >>> | + | >>> |
[1, 3, 5, 7, 9] | [1, 3, 5, 7, 9] | ||
Line 217: | Line 210: | ||
- | ==== Conditional | + | ===== Conditional |
- | It may be useful to parametrize | + | It may be useful to parametrize |
<code python> | <code python> | ||
if x>100: | if x>100: | ||
- | print " | + | print (" |
elif x>10: | elif x>10: | ||
- | print " | + | print (" |
else: | else: | ||
- | print " | + | print (" |
</ | </ | ||
- | ==== Important notes ==== | + | ===== Important notes ===== |
- | * The '' | + | * The '' |
- | + | ||
- | * If two integers are divided, Python will do an INTERGER division, as most compiled languages (C, [[doc: | + | |
* A number starting with 0 corresponds to a number defined using the octal numeral system. For example, 012 corresponds to 10 in decimal. | * A number starting with 0 corresponds to a number defined using the octal numeral system. For example, 012 corresponds to 10 in decimal. | ||
- | * The command '' | + | * The command '' |
- | * The function '' | + | * The function '' |
- | * The null command is called | + | * The null command is named '' |
* Several commands may be written on the same line if separated by a semicolon, as in C. | * Several commands may be written on the same line if separated by a semicolon, as in C. | ||
Line 251: | Line 242: | ||
- | |||
- | |||
===== Conclusions ===== | ===== Conclusions ===== | ||
- | In this page, the basic use of [[doc: | + | In this page, the basic use of [[doc: |
- | After reading this introduction to Python, this language can be used to defined | + | After reading this introduction to Python, this language can be used to define |
=> [[doc: | => [[doc: |
doc/user/tutorials/tuto0.txt · Last modified: 2020/09/08 16:26 by boman