User Tools

Site Tools


user:afassett:hcp_projects:pythontut

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. Simple run the Python interpreter by typing python. The interpreter isn't very useful for large pieces of code so you will want to use your favorite text editor to create the scripts and then execute them. =How do I execute a saved script= This depends on your OS: Linux/Unix Put #!/usr/bin/env python at the top of the script and do chmod +x foobar.py. If you want to run the script as a CGI (from a web server) you should better use #!/usr/local/bin/python or #!/usr/bin/python, depending on the system configuration. Python identifies statements by indentation only. #!/usr/bin/env python

# This is a comment\\
# comments can span one line only\\

print 'Hello world!'\\
print "Hello world!"\\

You can delimit strings with single or double quotes - it doesn't make any difference to Python. If you want to write a string on more than one line you can escape the end of the line with a backslash: foo = “I'm a very long string

      which spans multiple lines"

print foo or use HereDoc syntax with ”“” or ''': foo = “”“I am a really long

      comment but it doesn't
      matter no backslashes are
      needed

”“” You can also access directly single characters or slices from a string foo = “Hello” print foo[1] # outputs “e” since indexing starts at 0 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's variables are loosely typed (you can assign any type of data to a single variable), you have to convert them to a single type if you want to operate on different kind of variables. 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:] # or all parameters 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
user/afassett/hcp_projects/pythontut.txt · Last modified: 2011/01/27 12:07 by afassett