Chapter 9

Performing Repetitive Tasks

IN THIS CHAPTER

check Performing a task a specific number of times

check Performing a task until completion

check Placing one task loop within another

All the examples in the book so far have performed a series of steps just one time and then stopped. However, the real world doesn’t work this way. Many of the tasks that humans perform are repetitious. For example, the doctor might state that you need to exercise more and tell you to do 100 push-ups each day. If you just do one push-up, you won’t get much benefit from the exercise and you definitely won’t be following the doctor’s orders. Of course, because you know precisely how many push-ups to do, you can perform the task a specific number of times. Python allows the same sort of repetition by using the for statement.

Unfortunately, you don’t always know how many times to perform a task. For example, consider needing to check a stack of coins for one of extreme rarity. Taking just the first coin from the top, examining it, and deciding that it either is or isn’t the rare coin doesn’t complete the task. Instead, you must examine each coin in turn, looking for the rare coin. Your stack may contain more than one. ...

Get Beginning Programming with Python For Dummies, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.