Table of Contents

Python Cheat Sheet

Comments

# - Pound sign

# This is a comment
ob = bpy.data.objects["Panel"]

Data types

List

a = [1,4,5,6,9,10]
print (a[0])
    1
print (a[1:2])
    4, 5
a = 'hellothere'
print (a[4:])
   'othere'

Dictionary (like a hash map)

a = {}
 
a['name'] = 'bob'
a['age'] = 44
print (a)
   {'age': 44, 'name': 'bob'}

Functions

def myfunction(aNumber=1, aString="hello"):
    result = ''
    for i in range(aNumber):
       result += aString
    return result

Syntax

Indents matter, usually 4 spaces.

Loops

for i in range(2,10):
    print (i)

or use xrange(2,20) for lazy, as-needed streaming of content.

Conditional

if a ==10:
   print 'yes'
else:
   print 'no'

Class

class Boy()
    def setName(self, n):
       self.name = n
    def getName(self):
       return self.name

self is like *this* in Java. If you don't provide “self,” you are referring to the global variable.

b = Boy()
b.setName("bob")

Display

(Don't forget the parentheses.)

print ("Hello, world!")