""" Produced by Andrew Jason Penner Sept 31 2007 """ #! /usr/bin/python """ One needs to assert control over data as the programming becomes more complicated. The easiest to think of is comparison statement, ie do this if this else do something completely different """ # Below is a method of getting input from a user, in practice thsi is not # always the best since many scripts and code should run independent # from the user x=int(raw_input("Please insert an integer: ")) if x < 0: x = 0 print 'Negative changed to zero' elif x == 0: # This means exactly equal to. If you just used '=' you would be # assigning x to be 0 which is not a comparison and is always true print 'Zero' elif x == 1: print 'Single' elif x != 9: # Not equal to print 'Not equal to 9' else: print "More" """ Note that the elif's, if's and else's are lines meant to be followed with further instructions, this is signified by ending a line with a : It is possible to avoid using the :'s but the code will inevitably become unreadable """ """ This is a simple example of the comparison operations allowed. Although not very logical I added the statement x != 9 to show the logic control. Once this statement passes (ie if the integer input was say 5) 5 != 9 is true then that 'if statment' executes, ending all future tests. If the user input 9 that test would have failed and the code continues to execute """ """ The next thing is 'for loops'. These are very similar to the foreach loops found in bash scripts. They require a list and will execute for as many times as the list has elements """ a = [1,2,3,4,'cat','dog','bird',1.3] for x in a: print x b = a[4:7] for x in b[:]: #make a slice copy of the entire list if len(x) < 4: b.insert(0,x) print 'This is the new list b',b """ Further note that python does not have end loop or end if commands. It will execute everything based on the indentation see the same example as above with the last print statement un-indented """ print 'Below is a second instant of the previous example with indentation changed. Note that the list only prints once.' b = a[4:7] for x in b[:]: #make a slice copy of the entire list if len(x) < 4: b.insert(0,x) print 'This is the new list b',b """ The function range() allows a user to create a list with any given number of entries, without having to manually type all that stuff in """ print "This is the range function range(10)", range(10) for i in range(100): pass # this is a command that is used in python that does nothing # but goes in a place where commands are necessary and a blank # line will cause a crash print "This is the last element in the 'for loop'",i """ range() can also have two arguments, the limits on the range """ print 'These are the numbers between 5 and 10',range(6,10) """ Finally range() can have three arguments, the limits on the range and the number to skip by """ print 'This is every thirty numbers from -10 to -100',range(-10,-100,-30) """ note that range also takes negative arguments, but you must be careful to pay attention to the signs of the "skipping number" """ """ We also have break and continue statments which allow us to have more control over the if and for loops. I would argue that this is not always the best way of handling logic, and can lead to a mess. Quite frankly the example below does not work. """ for n in range(2, 10): for q in range(2, n): if n%q==0: print n, 'equals', q, '*', n/q break else: print n, 'is a prime number' """ Now let's move onto functions. """ def fib(n): # Write the Fibonacci series to n a,b = 0,1 while b < n: print b, a, b = b, a+b print 'This is the Fibonacci sequence up to 2000',fib(2000) """ def declares a definition. Pay attention to the indentation, it is required here. Failure to indent terminates the definition prematurely. There is a 'None' at the end of the list since there is no return statement in the function. See below for details on 'return' If a function fails prematurely it will also return a 'None' """ def fib2(n): result = [] a, b = 0, 1 while b < n: result.append(b) a, b = b, a+b return result print 'This is the Fibonacci sequence as a list, up to 2000',fib2(2000) """ Here the return statment returns a particular value from the function.""" """ result.append(b) appends b to the end of the list result """ """ It is the equivalent of result = result + [b] """ """ This ends the flow control example of the code, there is a lot more to functions however on a first pass on the topic this is sufficient """