< previous | Table of Contents | next > |
5.5 COMMON LOOPING PATTERNS
COUNTING LOOPS
Counting loops are used to count occurrences of certain conditions within a loop. A counter variable is typically initialized outside the loop and then incremented inside the loop whenever the condition is met.
The example below iterates over a string and then counts how many uppercase characters are within the provided string. Here we use a handy built-in function, isupper()
, that helps us check whether a character or a string is uppercase and returns boolean value. However, do not worry too much about memorizing how to use this function. This is just to show you a simple example and also briefly introduce other handy tools within Python.
count = 0 example_string = "HelLo!" for character in example_string: if character.isupper(): count += 1
SUMMING LOOPS
We can use summing loops to, for example, add together all the numerical values in a sequence. Here’s an example of creating a sequence of numbers using the range()
function and then calculating the total sum of these numbers. Here the range function generates and adds numbers from 1 to 10, excluding 11. We previously showed you how to do this with a while loop.
sum_numbers = 0 for i in range(1,11): sum_numbers += i
MAXIMUM AND MINIMUM LOOPS
These loops iterate through a sequence to find the maximum or minimum value. Note that here, we briefly introduce what a list
looks like and how it can be used. However, we will go into more detail about lists in the next few weeks. So feel free to revert to this example while working through the topic of lists.
In the first example, we iterate over a list of numbers. We then aim to find the largest value in that list. To do so, we assume that the first value is initially the largest and then compare the current largest value with each subsequent element. In case we find a larger value, we update the largest
variable accordingly. Once we've worked through all the elements, we will have found the largest value in the list of numbers.
In the first example, we iterate over a list of numbers. We then aim to find the smallest value in that list. To do so, we assume that the first value is initially the smallest and then compare the current largest value with each subsequent element. In case we find a smaller value, we update the smallest
variable accordingly. Once we've worked through all the elements, we will have found the smallest value in the list of numbers.
ACCUMULATING RESULTS INTO A COLLECTION
We can use loops to iterate over a sequence of elements and then collect desired values into a collection, such as a list
.
Expanding on the previous example, we can, for example, use this construct to collect all the even numbers into a new list.
KEY THINGS TO REMEMBER
- Counting loops allow to count occurrences of a certain condition being met.
- Summing loops are used to calculate the total sum of elements in a sequence.
- Maximum and minimum loops iterate through a sequence to find the largest or smallest value.
- Accumulating loops allow items to be collected into a collection, such as a list, based on meeting a certain condition.
< previous | Table of Contents | next > |