Lecture 11
Review
- If your source code file is at:
Projects/FilesReview/src/main/open_data.py
and the data file you want to access is at:
Projects/FilesReview/data/2015-06-17/collected.dat
write a relative path that opens the data file from the open_data.py file.
- What happens if you try to open a non-existent file for reading?
- What happens if you try to open a non-existent file for writing?
Files
File Formats
In the writing examples earlier, we were just writing sentences to a file with no regard for how to make extracting the information as easy as possible. The reading examples earlier were just spitting out to the user the contents of the file. Often, you will want the information stored in a file to be easily extracted by your program. To accomplish this, you must think about the best way to store data in the file.
Usually, each line in a file represents a single record (i.e. everything related to a single data point, single person, or single element of interest) since you can use the readline
and readines
methods or the for-loop iteration example to read in one line at a time and process it.
Within a line, it is a very good idea to put values in the same order on each line. That makes processing the data much easier. If you're storing names, birthdates, and favorite colors, don't sometimes store them as name, birthdate, color
and other times as birthdate, color, name
Decide how the fields in a record are separated. Commas are traditional, but if you're saving text from the user that could contain spaces, then you might want a different separator. Tabs are also traditional and less likely to cause problems than commas. But, you can use any separator you want.
Which of the following would be easier for a program to read?
Alice 10/24/86 red
Bob 3/14/90 green
Carol 7/14/80 blue
or
Alice,10/24/86,Red
Bob 1990-03-14 green
Carol blue 7/14/80
Once you've decided on a format for the file, you will likely be using readline
, readlines
, and writelines
to read and write files.
Write a program that asks the user for their name, birthdate, and favorite color. Save this information to a file (have the user specify the file). Continue asking until the user wants to stop. Write another program that reads this information and prints it to the screen.
Midterm Review
Based on the midterm review survey, the order for the review topics are:
- Lists and Tuples
- Files
- Loops
- Python Basics
- Boolean Expressions
- if statements
Lists and Tuples
The specific questions were:
- List and Tuples, Because this concept involves a lot of function, for example, lower(), append(), etc. There are too many operations associated with lists and it is hard to get familiar with all of those and be not confused.
- I would also benefit from a brief review of some of the common functions to add to or remove items from a list. As well as a review of when an unchangeable tuple should be used over a list.
- Slicing. It requires you to remember various ways of performing the similar actions.
- List Comprehension. The last lecture cleared it up a little by explaining the structure of going from a for loop to a list comprehension versions. However, a few more different examples may help.
- I still really don't understand the purpose of tuples all together actually.
- I can't remember the limits of Lists or the extents it can do.
Files
The specific questions were:
- Material is just entirely new to me. Going over how to write paths for macOS may be helpful.
- I'm just not used to it yet. The rest of what we've done seems pretty similar to what I've learned before, but files are something fairly new to me. Any kind of review examples in class would be helpful.
- At this point very little has been talked about Files.
- Further explanation of opening and writing to files. Maybe elevation to a super user in unix/linux os su? sudo? commands.
Loops
The specific questions were:
- Loops I think are just an overall difficult concept to grasp.
- Personally, I would benefit from a review of both 'for' and 'while' loops and when to use which one.
- What loop would you use to:
- Ask the user for numbers until they enter 'stop'.
- Count all of the words starting with a vowel (given a list of strings/words)
- Ask the user for a list of pet names, adding each into a list
- Go through a list of pet names and convert each to Title Case
- It isn't incredibly difficult, just more so than the rest. More examples of potential uses and applications of loops would be helpful.
Python basics
The specific questions were:
- I think I found it the hardest because I often forget everything that I can do. I often forget what all the tools I have to use are and it prevents me from doing more complex things.
- evaluating expressions and python formatting that differs from java and concepts
Boolean Expressions
The specific questions were:
- Because for Boolean Expressions, for me it's kind of hard to remember the order of the operations, like which one has the higher priority than the other one. Sometimes, it is easy to forget.
- Indicate the order of operations for the following expressions:
found or count < MAX
15000 < TAX_RATE * sum(totals) and x != 15.3
pet_type.lower() == 'cat' or age > 7 # and last_visit > 12
if statements
The specific questions were:
- Again, not very difficult, but next in the order of difficulty for me. Same as above, any examples and applications are helpful.
Midterm Review Page
The midterm review page has additional practice/review that you may find helpful: midterm.html.