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.
Homework
The deadline for homework is Sunday. Nevertheless, you can start with homework already before the session.
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")
Exercise 1. Celsius and Fahrenheit
Create the text file temps.txt. In the file, write temperatures in Celsius (each value on a separate line). Create a program which reads temperatures from a file (that contains temperatures in Celcius), converts the temperatures to Fahrenheit and stores the converted temperatures into a list (Hint: you can use the function we wrote in one practice session to convert from Celcius to Fahrenheit). The program should print out the average, maximum and minimum of the Fahrenheit temperatures.
Exercise 2. Number of days in months
Rewrite the number of days in months program so that the program prompts the user for the file name and reads the dates from the file. The dates in the file are written in the format dd.mm.yyyy (see an example). The program looks for the month in each line (hint: use split) and prints out the number of days in that month. Rewrite the function so that it does not contain conditional (if) statements for returning the number of days. The function should do it using a list (look at example code 2). Do not add the check for a leap year (let’s say that there are 28 days in February).
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 12th 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.