Python is very easy to learn, and with some understanding of its basic syntax, you can get started making your own scripts.
Let's get right to everyone's favorite example, "Hello World." The following script will print out
" to the output console.
Variables are created by simply assigning a value to them. Variables do not need to be declared, because Python has a dynamic type system. That means Python figures out the type of the variable on the fly when the script is executed.
The following script would print out: 15
x=5 y=3 print x*y
Python makes limited distinction between single and double quotes. As long as they are used consistently then there are few times when the type of quotation mark you use matters. Some of the rules are shown here:
print "This is my text" # Using double quotation marks print 'This is my text' # Using single quotation marks print "This is my text' # This will not work because python does not allow mixing the single and double quotation marks print "My name is 'David'" # This will print: My name is 'David' print 'My name is "David"' # This will print: My name is "David" print 'My name is Seamus O\'Malley' # This will print: My name is Seamus O'Malley
Perhaps Python's most unique feature is logical blocks which are defined by an indentation in Python. A colon (:) starts a new block, and the next line must be indented (typically using a tab or 4 spaces). The block ends when the indentation level returns to the previous level. For example, the following will print out
5 4 3 2 1 Blast-off
" with each value on a new line. The final print is not part of the while loop because it isn't indented.
countdown = 5 while countdown > 0: print countdown countdown = countdown - 1 print "Blast-off!"
You can start and end a section of comments with a triple quote (''' or """).
''' This is a lot of text that you want to show as multiple lines of comments. Script written by Professor X. Jan 5, 1990 ''' print 'Hello world'
While Python doesn't explicitly have a way to block comment (comment out multiple lines), multi-line strings are functionally similar.
In Ignition, you can use the Ctrl-/ keyboard shortcut to comment several lines of code at once. Just highlight one or more lines of code and hold the Ctrl key and press "/". This will prepend all of the selected lines of code with the pound/hash (#) sign. Press Ctrl-/ again to remove the pound/hash sign.
Control Flow statements, that is the ifs and loops, make the language do things differently based on the various conditions. Python has all of the basic control flow statements that you'd expect from a programming language.
An if statement allows you to check if a condition is true or not true. Depending on the condition, you can either execute a block of code, or do something else. Many of these can be chained together to determine under what conditions should certain code execute.
if condition == True: print value1
Looping can be done with a for, which executes a block of code a set number of times, or a while, which executes a block of code until a certain condition is met. Both can be incredibly useful.
for item in myList: print item