xuv's notebook

Side notes, howtos and random bits of information related to Julien Deswaef's projects

User Tools

Site Tools


workshops:blender:python_basics

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
workshops:blender:python_basics [2015/06/20 19:54] – [Useful things] Julien Deswaefworkshops:blender:python_basics [2015/06/20 23:29] (current) – [Loops] Julien Deswaef
Line 37: Line 37:
 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.
  
-==== Useful things ====+===== Condititons ===== 
 +if ... then ... else ... 
 +<code=python> 
 +if ( something ): 
 +    # Will execute code here if something is True 
 +else: 
 +    # Will execute code here if something is False 
 +</code> 
 + 
 +if ... then ... else if ... then ... else ... 
 +<code=python> 
 +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 
 +</code> 
 + 
 +==== Boolean operations ==== 
 +<code=python> 
 +True and False 
 +# False 
 + 
 +True or False 
 +# True 
 + 
 +not True 
 +# False 
 +</code> 
 + 
 +===== Functions ===== 
 +The keyword to define a function in Python is ''def''
 +<code=python> 
 +def sum(a, b): 
 +    return a + b 
 + 
 +sum(123, 456) 
 +# 579 
 +</code> 
 + 
 +It is possible to define default values for the parameters, with the same previous example 
 +<code=python> 
 +def sum(a, b=1): 
 +    return a + b 
 + 
 +sum(123) 
 +# 124 
 +</code> 
 +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" to loop over some content. We just need to use the iterator. 
 +<code=python> 
 +a = [ 10, 20, 30, 40 ] 
 + 
 +for elements in a: 
 +    print( elements ) 
 + 
 +# 10 
 +# 20 
 +# 30 
 +# 40 
 + 
 +msg = "hello" 
 + 
 +for letter in msg: 
 +    print(letter) 
 + 
 +# h 
 +# e 
 +# l 
 +# l 
 +# o 
 +</code> 
 + 
 +If you really need to get an index, it's possible to construct it with the function ''enumerate()'' 
 +<code=python> 
 +for index, letter in enumerate(msg): 
 +    print( str(index) + ": " + letter) 
 + 
 +# 0: h 
 +# 1: e 
 +# 2: l 
 +# 3: l 
 +# 4: o 
 +</code> 
 + 
 + 
 +===== Useful things =====
 === Commenting === === Commenting ===
 <code=python> <code=python>
workshops/blender/python_basics.1434822870.txt.gz · Last modified: 2015/06/20 19:54 by Julien Deswaef