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

Introduction to Programming 2025/26 fall

  • Home Page
  • Introduction
  • Expressions
  • Conditional Execution
  • Functions
  • Iterations
  • Strings
  • Files
  • Lists
  • Graphics?
< previousTable of Contents järgmine >

8.7 COMMON ERRORS

INDEX ERRORS

Trying to access an index that is out of range for the list will raise an IndexError. This often happens when looping over items. For example, when the range() function input creates an iterable that extends beyond the number of elements in a list that you're iterating over.

Here's an example of how this would look like in Thonny.

REFERENCES AND COPIES

When you assign one list to another variable, both variables refer to the same list in memory. Changing the list using one variable will affect the other.

# Example of reference issue
original = [1, 2, 3]
copy = original
copy.append(4)
print(original)  # Original is also changed to [1, 2, 3, 4]

# To avoid this, create a copy
original = [1, 2, 3]
copy = original.copy()
copy.append(4)
print(original)  # Original remains unchanged [1, 2, 3]

ISSUES DURING ITERATION

Modifying a list while iterating over it can lead to unexpected behavior and errors. In the example below the loop would keep on running indefinitely as the length of the elements list is increasing on every iteration and outpacing how quickly i is incremented.


# Example of a possible infinite loop
elements = [1, 2, 3]
i = 0
while i < len(elements):
    elements.append(i)
    i += 1

< previousTable of Contents järgmine >

  • 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