workshops:blender:python_basics
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
workshops:blender:python_basics [2015/06/20 23:09] – [Variables] Julien Deswaef | workshops:blender:python_basics [2015/06/20 23:29] – [Loops] Julien Deswaef | ||
---|---|---|---|
Line 68: | Line 68: | ||
</ | </ | ||
- | ==== Useful things ==== | + | ===== Functions ===== |
+ | The keyword to define a function in Python is '' | ||
+ | < | ||
+ | def sum(a, b): | ||
+ | return a + b | ||
+ | |||
+ | sum(123, 456) | ||
+ | # 579 | ||
+ | </ | ||
+ | |||
+ | It is possible to define default values for the parameters, with the same previous example | ||
+ | < | ||
+ | def sum(a, b=1): | ||
+ | return a + b | ||
+ | |||
+ | sum(123) | ||
+ | # 124 | ||
+ | </ | ||
+ | So in this case, since we are calling the function without a second parameter, Python will use the default value of that second one to execute the function. In this case, it will just add one. | ||
+ | |||
+ | ===== Loops ===== | ||
+ | Compared to Javascript, Java or C for example, loops are written in a different manner. Python uses iterators a lot for a lot of its datat types. So we don't use or don't have to create an "index variable" | ||
+ | < | ||
+ | a = [ 10, 20, 30, 40 ] | ||
+ | |||
+ | for elements in a: | ||
+ | print( elements ) | ||
+ | |||
+ | # 10 | ||
+ | # 20 | ||
+ | # 30 | ||
+ | # 40 | ||
+ | |||
+ | msg = " | ||
+ | |||
+ | for letter in msg: | ||
+ | print(letter) | ||
+ | |||
+ | # h | ||
+ | # e | ||
+ | # l | ||
+ | # l | ||
+ | # o | ||
+ | </ | ||
+ | |||
+ | If you really need to get an index, it's possible to construct it with the function '' | ||
+ | < | ||
+ | for index, letter in enumerate(msg): | ||
+ | print( str(index) + ": " + letter) | ||
+ | |||
+ | |||
+ | # 0: h | ||
+ | # 1: e | ||
+ | # 2: l | ||
+ | # 3: l | ||
+ | # 4: o | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Useful things | ||
=== Commenting === | === Commenting === | ||
< | < |
workshops/blender/python_basics.txt · Last modified: 2015/06/20 23:29 by Julien Deswaef