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 Thursday. So, you should start with homework before the session.
Exercise 7.1 Failing students
Create the file students.txt containing student names, study book numbers and points (out of 100) that they got in the class. Use ", " as a separator.
students.txt content:
Benjamin Hummel, B82234, 40 Janus Alberink, B82694, 95 Alexandra Peppi, B95274, 50
Create a program that reads data from the file and returns the data about students who failed the course (got less than 51 points).
The output of the program should be:
>>> %Run solution.py
Benjamin Hummel, B82234, 40
Alexandra Peppi, B95274, 50
If you have been struggling with this exercise for some time, maybe you can get help from troubleshooter: https://progtugi.cs.ut.ee#/ts/623af300ef7eaf5634394bf4/
. It tries to explain the most common problems and give hints.
Exercise 7.2 US height and weight 2
Write a program that reads people’s height in cm and weight in kg from stats.txt file, converts them to US units of measurement (feet and lb) and then outputs them. You can use the following ratios for your calculations: 1cm = 0.0328ft and 1kg = 2.20lb. Round the results to 1 decimal.
stats.txt content:
170cm, 60kg 190cm, 95kg 160cm, 65kg 180cm, 75kg
The output of the program should be:
>>> %Run solution.py
5.6ft, 132.0lb
6.2ft, 209.0lb
5.2ft, 143.0lb
5.9ft, 165.0lb
If you have been struggling with this exercise for some time, maybe you can get help from troubleshooter: https://progtugi.cs.ut.ee#/ts/623af5c2ef7eaf5634394c33/
. It tries to explain the most common problems and give hints.
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 11th 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.