workshops:blender:python_basics
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| workshops:blender:python_basics [2015/06/19 00:30] – [Variables] Julien Deswaef | workshops:blender:python_basics [2015/06/20 23:29] (current) – [Loops] Julien Deswaef | ||
|---|---|---|---|
| Line 36: | Line 36: | ||
| So we just saw here that creating a variable is just writing its name and giving it a value. The type of value does not matter. And the variable will accept any type of value even if it's different from the one it use to have before. | So we just saw here that creating a variable is just writing its name and giving it a value. The type of value does not matter. And the variable will accept any type of value even if it's different from the one it use to have before. | ||
| + | |||
| + | ===== Condititons ===== | ||
| + | if ... then ... else ... | ||
| + | < | ||
| + | if ( something ): | ||
| + | # Will execute code here if something is True | ||
| + | else: | ||
| + | # Will execute code here if something is False | ||
| + | </ | ||
| + | |||
| + | if ... then ... else if ... then ... else ... | ||
| + | < | ||
| + | if ( something ): | ||
| + | # Will execute code here if something is True | ||
| + | elif ( something_else ) : | ||
| + | # Will execute code here if something is False and something_else is True | ||
| + | else: | ||
| + | # Will execute code here if both something and something_else are False | ||
| + | </ | ||
| + | |||
| + | ==== Boolean operations ==== | ||
| + | < | ||
| + | True and False | ||
| + | # False | ||
| + | |||
| + | True or False | ||
| + | # True | ||
| + | |||
| + | not True | ||
| + | # False | ||
| + | </ | ||
| + | |||
| + | ===== 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 === | ||
| + | < | ||
| + | # The most common way of commenting. | ||
| + | print (" | ||
| + | # Works only on a single line | ||
| + | |||
| + | ''' | ||
| + | Multi line commenting is done this way. | ||
| + | It's actually creating a multi-line String. | ||
| + | But not assigning it to any variable. | ||
| + | Works well if you want to comment multi lines of code. | ||
| + | ''' | ||
| + | </ | ||
| + | |||
| + | === What is the type of this? === | ||
| + | If you want to know what is the type of data stored in a variable, use the '' | ||
| + | < | ||
| + | type(" | ||
| + | # <class ' | ||
| + | |||
| + | type(2) | ||
| + | # <class ' | ||
| + | |||
| + | type([1, | ||
| + | # <class ' | ||
| + | |||
| + | type({" | ||
| + | # <class ' | ||
| + | |||
| + | type((1, | ||
| + | # <class ' | ||
| + | </ | ||
| + | |||
| + | === Documentation === | ||
| + | Extensive documentation can of course be found online. | ||
| + | But sometimes, you might be Python scripting in the middle of the forest, on top of a mountain or in a Faraday cage, with no wifi or 3G access of any kind. Don't worry, Guido has you covered. Use the '' | ||
| + | |||
| + | < | ||
| + | help() | ||
| + | ''' | ||
| + | Welcome to Python 3.4's help utility! | ||
| + | |||
| + | If this is your first time using Python, you should definitely check out | ||
| + | the tutorial on the Internet at http:// | ||
| + | |||
| + | Enter the name of any module, keyword, or topic to get help on writing | ||
| + | Python programs and using Python modules. | ||
| + | return to the interpreter, | ||
| + | |||
| + | To get a list of available modules, keywords, symbols, or topics, type | ||
| + | " | ||
| + | with a one-line summary of what it does; to list the modules whose name | ||
| + | or summary contain a given string such as " | ||
| + | |||
| + | You are now leaving help and returning to the Python interpreter. | ||
| + | If you want to ask for help on a particular object directly from the | ||
| + | interpreter, | ||
| + | has the same effect as typing a particular string at the help> prompt. | ||
| + | ''' | ||
| + | |||
| + | help(dir) | ||
| + | ''' | ||
| + | Help on built-in function dir in module builtins: | ||
| + | |||
| + | dir(...) | ||
| + | dir([object]) -> list of strings | ||
| + | | ||
| + | If called without an argument, return the names in the current scope. | ||
| + | Else, return an alphabetized list of names comprising (some of) the attributes | ||
| + | of the given object, and of attributes reachable from it. | ||
| + | If the object supplies a method named __dir__, it will be used; otherwise | ||
| + | the default dir() logic is used and returns: | ||
| + | for a module object: the module' | ||
| + | for a class object: | ||
| + | of its bases. | ||
| + | for any other object: its attributes, its class' | ||
| + | recursively the attributes of its class' | ||
| + | ''' | ||
| + | |||
| + | # In the Blender console | ||
| + | dir(bpy) | ||
| + | # [' | ||
| + | |||
| + | dir(bpy.data) | ||
| + | # | ||
| + | </ | ||
workshops/blender/python_basics.1434666648.txt.gz · Last modified: by Julien Deswaef
