Institute of Computer Science
  1. Courses
  2. 2024/25 fall
  3. Introduction to Programming (MTAT.03.236)
ET
Log in

Introduction to Programming 2024/25 fall

  • Home Page
  • Introduction
  • Expressions
  • Conditional Execution
  • Functions
  • Iterations
  • Strings
  • Files
  • Lists
  • Graphics?
< previousTable of Contentsnext >

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.
< previousTable of Contentsnext >

  • Institute of Computer Science
  • Faculty of Science and Technology
  • University of Tartu
In case of technical problems or questions write to:

Contact the course organizers with the organizational and course content questions.
The proprietary copyrights of educational materials belong to the University of Tartu. The use of educational materials is permitted for the purposes and under the conditions provided for in the copyright law for the free use of a work. When using educational materials, the user is obligated to give credit to the author of the educational materials.
The use of educational materials for other purposes is allowed only with the prior written consent of the University of Tartu.
Terms of use for the Courses environment