Skip to content
April 6, 2010 / stevebaer

Getting started: 2 – A little python syntax

The python scripting languages contains a large set of features which you can find documentation on at http://docs.python.org/tutorial/. I’m going to just focus on a few keywords and concepts that may be helpful to get you up and running.

Concept 1: Space is Important
For people who have written code or scripts in other languages, this is one of the most unusual concepts when transitioning to Python. Python uses indentation to define “blocks” of code. The script is considered to be in the same block while the leading indentation for each line of script has the exact same spacing (empty lines are ignored). Here’s an example

#python example

def CompareXY(x,y):
  print "x=", x
  print "y=", y
  if( x == y ):
    print "x equals y"
  else:
    print "x does not equal y"

'RhinoScript example

Sub CompareXY(x,y)
  Call Rhino.Print( "x=" & cstr(x) )
  Call Rhino.Print( "y=" & cstr(y))
  If x=y Then
    Rhino.Print "x equals y"
  Else
    Rhino.Print "x does not equal y"
  End If
End Sub

In the above samples, VBScript uses “End If” and “End Sub” to help define the end of code blocks where python uses indentation. I personally like to use two spaces for indentation, but you can choose to use whatever spacing scheme you want as long as you are consistent. Many people choose to use 4 spaces.

Concept 2: Case is Important
Python scripts are case sensitive. “AFunctionName” is different than “afunctionname”

A Few Keywords
There are plenty of new keywords to learn in python. Let’s focus on a very small set of these words which are common. The links I give provide very clear details about these keywords so I would recommend looking at them as well.
#
The ‘#’ is used to start a single line comment in a script.
# This line doesn't do execute. It is just used for comments

print
Prints a string to the Rhino command line
print "hello rhino"

def
“def” is used to define a function. “def” is always followed by the function name and a list of parameters in parentheses and then a colon

#example of def
def MyFunction():
  print "hello rhino"

You could call your defined function with
MyFunction()

if
Test a condition and executes the enclosing “block” of code if the condition results in true.

None
The None type is similar to Nothing in VB or null in C#

Sample Script
Using the above keywords and concepts, here’s a slightly more complicated script than the “Hello Rhino” script in the previous Getting Started blog.

#A slightly more complicated sample
print "a sample python script"

def Add2Numbers( value1, value2 ):
  if( value1==None ):
    print "you need to pass something for value1"
  if( value2==None ):
    print "you need to pass something for value2"
  #add the two numbers
  sum = value1 + value2
  print value1, "+", value2, " = ", sum

# Notice the decrement in spacing. This means we are out of
# the scope of the Add2Numbers block
x = 12
Add2Numbers( x, 20)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: