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
  • Grading
  • Links

Before session 7

Lists

Work through topics from the reading materials or watch the videos.

Test

Go to Moodle and take the seventh test on the video lectures.

NB! Make sure that the text files (.txt) are in the same folder as your Python code. Otherwise Python will not find the file.

Example 1. Shoe size

The following program reads the foot lengths from the file foot.txt, calculates a suitable shoe size for each foot length and prints the shoe size out. Moreover, the program adds the suitable shoe size into the list. Once the file is read, the program prompts the user for the shoe size. The program then counts how many people in the file have the same shoe size as the entered size and prints the result out.

ffile = open("foot.txt")

shoes = []

for foot in ffile:
    try:
        f = float(foot)
        shoe = round(3/2.0 *(f + 1.5))
        print("Foot length:", f, "Suitable shoe size is", shoe)
        shoes.append(shoe)
    except:
        print("Invalid input")

while True:
    try:
        size = int(input("Please enter the size: "))
        number = shoes.count(size)
        print(number, "people have the same shoe size")
        break
    except:
        print("Please enter a number")

Example 2. Days of the week

The next program repeatedly asks number of day of the week and prints out the name of day of the week until the user enters "done" instead of the number.

def day_of_week(n):
    days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
    return days[n-1]

while True:
    line = input("Enter number of day of the week or word 'done': ")
    if line == 'done':
        break
    try:
        number = int(line)
        if number < 1 or number > 7:
            print("The number of day of the week must be in the range 1-7")
        else:
            print("The name of this day of the week is", day_of_week(number))
    except:
        print("Please enter a number")

Homework

The deadline for homework is Monday. Still, it is recommended that you start with homework before the session.


Optional material: Dictionaries

Besides lists, there is another very cool data structure in Python, where you can store a lot of data. It is called a dictionary. It might be helpful for your project to check out the video by Chuck about the dictionaries. So if you have time and interest, go for it! :) It is not too difficult at all.

Join the follow-up course Introduction to Programming II (MTAT.03.256)

The follow-up course Introduction to Programming II (MTAT.03.256) starts off on the 14th of November. It is an optional course (not compulsory) dedicated to dictionaries, tuples, nested loops and recursion in Python. The format of the course is similar to the present course (Introduction to Programming I, MTAT.03.236). The registration for the course is already available in SIS.

  • 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