This is an old revision of the document!
Python basics
Variables
Python variables are very easy to set up and very flexible.
a = 10 print(a) # 10 a = 2.5 print(a) # 2.5 a += a print(a) # 5.0 a = [ 1, 2, 3, 4, 5 ] print( a[2] ) # 3 len(a) # 5 a = "hello" a += " world" print(a) # hello world len(a) # 11 print(a[10]) # d
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
Commenting
# The most common way of commenting. print ("hello") # Works only for whatever is after the # tag. # 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()
function.
type("hello") # <class 'str'> type(2) # <class 'int'> type([1,2,3,4]) # <class 'list'> type({"name": 'home', "value": 2}) # <class 'dict'> type((1,2,3)) # <class 'tuple'>
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()
and dir()
functions to navigate Python documentation right in the console.
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://docs.python.org/3.4/tutorial/. Enter the name of any module, keyword, or topic to get help on writing Python programs and using Python modules. To quit this help utility and return to the interpreter, just type "quit". To get a list of available modules, keywords, symbols, or topics, type "modules", "keywords", "symbols", or "topics". Each module also comes with a one-line summary of what it does; to list the modules whose name or summary contain a given string such as "spam", type "modules spam". 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, you can type "help(object)". Executing "help('string')" 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's attributes. for a class object: its attributes, and recursively the attributes of its bases. for any other object: its attributes, its class's attributes, and recursively the attributes of its class's base classes. ''' # In the Blender console dir(bpy) # ['__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'app', 'context', 'data', 'ops', 'path', 'props', 'types', 'utils'] dir(bpy.data) #['__doc__', '__module__', '__slots__', 'actions', 'armatures', 'bl_rna', 'brushes', 'cameras', 'curves', 'filepath', 'fonts', 'grease_pencil', 'groups', 'images', 'is_dirty', 'is_saved', 'lamps', 'lattices', 'libraries', 'linestyles', 'masks', 'materials', 'meshes', 'metaballs', 'movieclips', 'node_groups', 'objects', 'particles', 'rna_type', 'scenes', 'screens', 'scripts', 'shape_keys', 'sounds', 'speakers', 'texts', 'textures', 'use_autopack', 'version', 'window_managers', 'worlds']