This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
user:afassett:hcp_projects:pythontut [2010/12/16 17:34] – afassett | user:afassett:hcp_projects:pythontut [2011/01/27 17:07] (current) – afassett | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | Python - it is a high level, scripting language, this means it is interpreted rather than compiled, so it saves time during debugging and early development. it is Open Source so you can even modify it's source, at least if you know C. There is a source version and also precompiled binaries for both Linux and Windows. | ||
+ | | ||
+ | =How do I execute a saved script= | ||
+ | This depends on your OS: | ||
+ | Linux/ | ||
+ | Put # | ||
+ | # | ||
+ | < | ||
+ | # comments can span one line only\\ | ||
+ | |||
+ | print 'Hello world!' | ||
+ | print "Hello world!" | ||
+ | You can delimit strings with single or double quotes - it doesn' | ||
+ | foo = " | ||
+ | which spans multiple lines" | ||
+ | print foo | ||
+ | or use HereDoc syntax with """ | ||
+ | foo = """ | ||
+ | comment but it doesn' | ||
+ | matter no backslashes are | ||
+ | needed | ||
+ | """ | ||
+ | You can also access directly single characters or slices from a string | ||
+ | foo = " | ||
+ | print foo[1] # outputs " | ||
+ | print foo[1:] # ello | ||
+ | print foo[-1:] # o | ||
+ | print foo[2:4] # ll | ||
+ | Control structures | ||
+ | if 1 == 2 : | ||
+ | print 'One equals two' | ||
+ | else : | ||
+ | print 'One does not equal two' | ||
+ | As you have noticed no braces are used, just indent, this promotes readable code. | ||
+ | if 1 < 2 : | ||
+ | print 'One is lesser than two' | ||
+ | elif 1 > 2 : | ||
+ | print 'One is greater than two' | ||
+ | Loops | ||
+ | for i in range(0, 10) : | ||
+ | if i % 2 == 0 : | ||
+ | print i, 'is an even number' | ||
+ | else : | ||
+ | print i, 'is an odd number' | ||
+ | This example will loop through the numbers from 0 to 10 and test if the number is odd or even. The modulus operator (%) is used to calculate the remainder from dividing the number by two. | ||
+ | The same example can be rewritten also with a while loop. | ||
+ | i = 0 | ||
+ | |||
+ | while i <= 10 : | ||
+ | if i % 2 == 0 : | ||
+ | print str(i) + ' is an even number' | ||
+ | else : | ||
+ | print str(i) + ' is an odd number' | ||
+ | i += 1 | ||
+ | |||
+ | i = 'Hello world' | ||
+ | print i | ||
+ | This example also shows that even though Python' | ||
+ | To save the space that print adds between different parameters passed to it, you also have to add it to the string (when using string concatenation). | ||
+ | Command-line parameters | ||
+ | import sys | ||
+ | |||
+ | print sys.argv[0] # prints the filename of your script | ||
+ | print sys.argv[1] # and the first parameter | ||
+ | print sys.argv[1: | ||
+ | If you want to pass parameters from the command line you will need the sys module, which is imported on the first line of the example. The module defines the argv list which contains the arguments to the script. | ||
+ | Lists in Python are something similar to arrays in other languages yet Python has arrays also, which are more functional than lists so if you want to use arrays you would have to import the array module. | ||
+ | ==Resource== | ||
+ | [[http:// |