4 – The famous for


🎯 Learning Objectives

After this lesson you should be able to:

  • Use iteration (for statements) to iterate over list items
  • Perform common operations on lists or strings
💬 Key Vocabulary
  • Iteration
  • index
  • string operations
  • Boolean/logical expression (condition)
  • list item
  • list
  • list operations

Introduction

In this lesson, you will:

  • Use iteration (for statements) to iterate over list items
  • Practise using common operations on lists and strings

📖 for loops: Syntax and use

for loops are convenient for iterating over the items of a list.

Note: for loops can be used for iterating over any sequence of elements, e.g. strings.

📖 Lipogram

The 1939 novel Gadsby, by Ernest Vincent Wright is a lipogram (missing a letter).

A note at the end of the book claims: “Not a word containing the letter ‘E’ has appeared in this story of over 50,000 words.”

We will make a program to check this claim.

This code retrieves the entire text of Gadsby and stores it as a list of words (wordlist).

Note: The textfile module and the words function are not standard Python components.

We will make a program that checks the claim that no words containing ‘E’ are to be found in the book. 

📝 Activity 3 – English words

Use a for loop to iterate over a list containing thousands of English words, to complete the tasks from your worksheet.

Download the worksheet and drag it to one side of your screen, then move this window with the Trinket window below the the other side of your screen.

Task 1

Task 2

Task 3

📝 Activity 4 – Heartbeats

So far, you have used for to iterate over lists of textual data. In this activity, you will iterate over lists of numbers.

Use the worksheet below to process real ECG (electrocardiogram) data from a medical database, to detect heartbeats.

💬 Summary

In this lesson, you…
  • Used iteration (for statements) to iterate over list items
  • Practised using common operations on lists and strings

In the next lesson, you will…
  • Use variables to keep track of counts and sums
  • Use iteration (for statements) to iterate over strings of characters
  • Develop guided mini–projects that will help you apply what you have learnt

🏅 Badge it

🥈Silver Badge
  • Upload your completed Activity 3 – English Words worksheet to the Silver badge task on Bourne to Learn.
🥇Gold Badge
  • Upload your completed Activity 4 – Heartbeats worksheet to the Gold badge task on Bourne to Learn.
🥉Platinum Badge
  • Upload your completed Activity 3 – English Words worksheet with the completed Explorer task to the Platinum badge task on Bourne to Learn as well as the Explorer task.