ubuntu2004
Loops I - strings
The power of computing lies in its ability to perform the same repetitive task many times very quickly. We do this using the concept of loops.
To help motivate your understanding of loops, we will revisit the question in Notebook 4: How many "f"s are there in this sentence?
The embryo of Festuca fusca,
a native plant species of
Old-World countries, is almost
half the full length of its grain.
Looping through a string
Before writing some code to answer this question, let's think how you would you do it by hand.
Set up a tally, starting at zero, of the number of "f"s.
Read the first letter of the sentence.
If it is an "f" increase the tally by 1, if it is not an "f" do nothing.
Move onto the next letter and repeat Step 3 until you reach the end of the sentence.
The important concept here is the loop between Step 3 and Step 4. You repeat a task (counting "f"s) until you reach the end of the sentence.
In Python this is called looping through a string.
Run the following code to see the basics of looping through a string using a for loop.
The for
loop starts at the line
The colon at the end of this line is important; it tells Python that what follows is within the loop.
The line
is indented. This tells Python that it is within the loop. If we remove the indentation (try it) we'll get an IndentationError
(as for conditional statements) because Python is expecting to find indented code just after we declare a for
loop.
The code works like this. The first character in the variable sentence
is the letter "T". The line of code for c in sentence:
assigns the letter "T" to the string variable called c
. Which means that the value of c
is "T". We then print the value of c
, i.e., we print "T". The loop moves onto the second character in sentence
which is the letter "h". The variable c
is assigned the value of "h" and then printed. The loop moves onto the third character in sentence
which is "e". The variable c
is assigned the value of "e" and then printed. And so on until the end of the sentence is reached and the last character, which is a full stop, is printed. At which point the code exits the loop and it is finished.
The iterating variable
The variable c
in for c in sentence:
has a special name because it forms part of the for
loop. It is called an iterating variable.
Counting "f"s
Now that we've got the basics of a for
loop we can use it to count the number of "f"s.
We start by initialising the variable sentence
and a tally called count
to zero.
The loop is entered. Everything indented below for c in sentence:
is in the loop.
The iterating variable c
is assigned the value "T".
We test the value of c
to see if it equals "f" or "F". As it isn't nothing happens.
After we've tested the value of c
the loop moves onto the second character in sentence
. c
is assigned the value of "h" which is tested, and so on.
When c
equals "F" or "f" the tally count
is incremented by 1. Notice that count += 1
is double indented because it is part of the condition if c == "f" or c == "F":
.
Once the last character in sentence is tested (the full stop) the loop finishes and we drop out of the bottom of the loop. This is the first un-indented line after the loop. In this case it is the print()
statement which outputs the number of "f"s found in sentence
.
Naming the iterating variable
Many newcomers to Python incorrectly believe that the name of the iterating variable influences the function of the code. The name of the iterating variable is usually chosen to indicate what the variable will contain. However, it is useful to realise that the code in the following two loops are equivalent and do exactly the same thing.
However, it is best to name the iterating variable something meaningful. goat
is clearly not meaningful in the above example.
Experiment with the above examples and you will see that any non-reserved word can be used as an iterating variable - as long as you change the variable within the loop to match.