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

Similarly to the while loop, a for loop is used for repeating a block of code multiple times, thus making it easier to work with data sequences, such as lists, strings, or ranges of numbers.

A for loop in Python iterates over a sequence, such as a list or a string. This means you can execute a code block for each item in the sequence, one after the other. for loops are handy for tasks that require you to act on each item in a sequence, like adding or modifying elements, checking conditions, or performing calculations.

The basic structure of a for loop looks like the following.


for i in sequence:
    # Do something with item

i is a variable that takes the value of the next item in the sequence on each iteration. You can name it anything you want, but usually, it should be descriptive of the data you're working with to make the code more readable. sequence is the sequence you want to iterate over, like a string.

This flowchart demonstrates how a for loop works. As we mentioned, for loops are used to iterate over objects. Taking the example of a string, you can think of it as sequentially working its way through every character in the string and asking itself on each iteration whether it has ran out of characters to work through. Until it hasn't reached the end, the body of the loop will be executed. Once it reaches the end of the string, the loop exits and the program continues execution from the statement immediately after.

Flowchart. For-loop flowchart. geeksforgeeks.org

Here's a simple example that prints each letter in a string.


name = "Peeter"
for letter in name:
    print(letter)
'''
This loop will print out all the characters, each of them on a new line. 
P
e
e
t
e
r
'''

USING THE RANGE FUNCTION

The range() function is often used with for loops to generate a sequence of numbers, which you can then iterate over:


for i in range(5):
    print(i)

This will print numbers 0 through 4. range(5) generates a sequence of numbers from 0 to 4, and the for loop iterates over each number.

for loops should be preferred when the number of iterations is known.

KEY THINGS TO REMEMBER

  • for loops are used to iterate over a sequence.
  • The loop starts with the for keyword, followed by a variable name representing the current item, then the in keyword, and the sequence to iterate over.
  • Each iteration assigns the next item in the sequence to the variable and executes the code block within the loop.
  • The range() function is used to generate a sequence of numbers, which the for loop can then iterate over to act a desired number of times.
  • The variable used in the for loop can be named anything, but it should be descriptive to make the code easily understandable.

SELF-CONTROL EXERCISES

< 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