>>> # control structures influence the order in which commands are executed. >>> # Note: unlike most mainstream programming languages (i.e., C/C++, Java, >>> # Perl) Python is whitespace-sensitive. Specifically with regards to >>> # indentation. Usually this forces the programmer to write legible code >>> # and the interpreter is rather lax in terms of how much to indent as long >>> # as we're consistent. It's best to use 2-4 spaces to indicate that a set >>> # of commands is nested under and command (see below). >>> >>> # Loops >>> i = 0 >>> while i < 10: ... print i ... i += 1 # adds 1 to the current value of i ... >>> # the previous example is equivalent to >>> for i in range(10): ... print i ... >>> # if/elif/else statements >>> isTrue = False >>> if isTrue: ... # do something if a condition is true ... pass # pass is a special command and just used as a ... # place-holder to maintain proper indentation ... >>> if isTrue: ... # do something if a condition is true ... pass ... else: ... # do something else if the original condition is NOT true ... pass ... >>> if isTrue: ... # do something if a condition is true ... pass ... elif isTrue == False: ... # do something 'else if' the first condition is not true but ... # the second IS true ... pass ... else: ... # do something else if none of the above conditions are true ... pass ... >>> # 'break' commands pop you out of the inner most control structure >>> # usually when some condition is true as below >>> for i in range(10): ... print "breaking i1: " + str(i) ... if i > 5: ... break ... print "breaking i2: " + str(i) ... >>> # 'continue' commands skip the rest of the inner block and continue >>> # at the top most control structure (the difference between 'continue' >>> # and 'break' is subtle but important) >>> for i in range(10): ... print "continuing i1: " + str(i) ... if i > 5: ... continue ... print "continuing i2: " + str(i) ... |