Arvutiteaduse instituut
  1. Kursused
  2. 2025/26 sügis
  3. Programmeerimise alused (MTAT.03.236)
EN
Logi sisse

Programmeerimise alused 2025/26 sügis

  • Home
  • Grading
  • Links

Before session 7

Lists

Watch the videos. Next three videos (part 1, part 2 and part 3) show Python's simplest data structure - the list. Lists can store more than one item in a variable.

Slides
Text-book: chapter 8 - lists

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.

The follow-up course Introduction to Programming II (MTAT.03.256) starts off on the 15th 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.

  • Arvutiteaduse instituut
  • Loodus- ja täppisteaduste valdkond
  • Tartu Ülikool
Tehniliste probleemide või küsimuste korral kirjuta:

Kursuse sisu ja korralduslike küsimustega pöörduge kursuse korraldajate poole.
Õppematerjalide varalised autoriõigused kuuluvad Tartu Ülikoolile. Õppematerjalide kasutamine on lubatud autoriõiguse seaduses ettenähtud teose vaba kasutamise eesmärkidel ja tingimustel. Õppematerjalide kasutamisel on kasutaja kohustatud viitama õppematerjalide autorile.
Õppematerjalide kasutamine muudel eesmärkidel on lubatud ainult Tartu Ülikooli eelneval kirjalikul nõusolekul.
Courses’i keskkonna kasutustingimused