17 CHAPTER FOUR MORE CONTROL FLOW TOOLS 4.1 if Statements 4.2 for Statements Besides the while statement just introduced, Python uses the usual flow control statements known from other lan- guages, with some twists. Perhaps the most well-known statement type is the if statement. For example: There can be zero or more$_{elif}$parts, and the$_{else}$part is optional. The keyword ‘$_{elif}$’ is short for ‘else if’, and is useful to avoid excessive indentation. An$_{if}$ …$_{elif}$ …$_{elif}$ … sequence is a substitute for the$_{switch}$ or $_{case}$statements found in other languages. The$_{for}$ statement in Python differs a bit from what you may be used to in C or Pascal. Rather than always iterating over an arithmetic progression of numbers (like in Pascal), or giving the user the ability to define both the iteration step and halting condition (as C), Python’s$_{for}$ statement iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence. For example (no pun intended): Code that modifies a collection while iterating over that same collection can be tricky to get right. Instead, it is usually more straight-forward to loop over a copy of the collection or to create a new collection: >>> # Measure some strings: ... words =['cat','window','defenestrate'] >>>for w in words: ... print(w,len(w)) ... cat 3 window 6 defenestrate 12 >>> x =int(input("Please enter an integer:")) Please enter an integer: 42 >>>if x <0: ... x =0 ... print('Negative changed to zero') ...elif x ==0: ... print('Zero ') ...elif x ==1: ... print('Single') ...else : ... print('More ') ... More