Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
10327 views
ubuntu2004
Kernel: Python 3 (system-wide)

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.

  1. Set up a tally, starting at zero, of the number of "f"s.

  2. Read the first letter of the sentence.

  3. If it is an "f" increase the tally by 1, if it is not an "f" do nothing.

  4. 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.

sentence = 'The embryo of Festuca fusca, a native plant species of Old-World countries, is almost half the full length of its grain.' # Loop through each character (letters, spaces, and punctuation) # in the sentence from first to last and print each one. for c in sentence: # start of the loop print( c ) # this print() statement is indented and, therefore, in the loop
T h e e m b r y o o f F e s t u c a f u s c a , a n a t i v e p l a n t s p e c i e s o f O l d - W o r l d c o u n t r i e s , i s a l m o s t h a l f t h e f u l l l e n g t h o f i t s g r a i n .

The for loop starts at the line

for c in sentence:

The colon at the end of this line is important; it tells Python that what follows is within the loop.

The line

print( c )

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.

Read the following code to see if you can understand what it does before running it.
sentence = 'The embryo of Festuca fusca, a native plant species of Old-World countries, is almost half the full length of its grain.' # Initialise a tally to zero. count = 0 # Loop through each character in sentence. for c in sentence: # If the character is an "f" or an "F" then increase tally by 1. if c == "f" or c == "F": count += 1 print( f'There are {count} "f"s in the sentence: "{sentence}"')
There are 7 "f"s in the sentence: "The embryo of Festuca fusca, a native plant species of Old-World countries, is almost half the full length of its grain."

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.

rna_sequence = 'ACCGU' for nucleotide in rna_sequence: print(nucleotide) print() # print a blank line for goat in rna_sequence: print(goat)
A C C G U A C C G U

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.

Exercise Notebook

Next Notebook