User Tools

Site Tools


programming:python:start

Differences

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

Link to this comparison view

Next revision
Previous revision
programming:python:start [2016/07/08 03:31]
dwheele created
programming:python:start [2026/02/07 19:28] (current)
dwheele [Class]
Line 1: Line 1:
 ====== Python Cheat Sheet ====== ====== Python Cheat Sheet ======
  
 +===== Comments =====
  
 +# - Pound sign
 +
 +<code python>
 +# This is a comment
 +ob = bpy.data.objects["Panel"]
 +</code>
  
 ===== Data types ===== ===== Data types =====
  
 +List
 +
 +<code python>
 +a = [1,4,5,6,9,10]
 +print (a[0])
 +    1
 +print (a[1:2])
 +    4, 5
 +</code>
 +
 +<code python>
 +a = 'hellothere'
 +print (a[4:])
 +   'othere'
 +</code>
 +
 +Dictionary (like a hash map)
 +
 +<code python>
 +a = {}
 +
 +a['name'] = 'bob'
 +a['age'] = 44
 +print (a)
 +   {'age': 44, 'name': 'bob'}
 +</code>
  
  
 ===== Functions ===== ===== Functions =====
  
-def myfunction(aNumber=1, aString="hello):+<code python> 
 +def myfunction(aNumber=1, aString="hello"): 
 +    result = '' 
 +    for i in range(aNumber): 
 +       result += aString 
 +    return result 
 +</code>
  
 ===== Syntax ===== ===== Syntax =====
  
-Indents matter.+Indents matter, usually 4 spaces. 
 + 
 +===== Loops ===== 
 + 
 +<code python> 
 +for i in range(2,10): 
 +    print (i) 
 +</code> 
 + 
 +or use ''xrange(2,20)'' for lazy, as-needed streaming of content. 
 + 
 +===== Conditional ===== 
 + 
 +<code python> 
 +if a ==10: 
 +   print 'yes' 
 +else: 
 +   print 'no' 
 +</code> 
 + 
 +===== Class ===== 
 + 
 +<code python> 
 +class Boy() 
 +    def setName(self, n): 
 +       self.name = n 
 +    def getName(self): 
 +       return self.name 
 +</code> 
 + 
 +self is like *this* in Java. If you don't provide "self," you are referring to the global variable. 
 + 
 +<code python> 
 +b = Boy() 
 +b.setName("bob"
 +</code> 
 + 
 +===== Display ===== 
 + 
 +(Don't forget the parentheses.) 
 + 
 +<code python> 
 +print ("Hello, world!"
 +</code> 
  
programming/python/start.1467948711.txt.gz · Last modified: 2016/07/08 03:31 by dwheele