Exercises
To submit your exercises, please create a python folder in your personal folder in the course repository. Place all of the code and files for theses exercises in that folder and be sure to check it in.
Exercise 1: Short Problems
1. Write a bullet list description of what happens when Python evaluates the statement x += x - x when x starts with the value 3. Submit your answer in a file called evaluation.txt.
2. In the United States, a car’s fuel efficiency is measured in miles per gallon. In the metric system, it is usually measured in liters per 100 kilometers.
- Write a function called
convert_mileagethat converts from miles per gallon to liters per 100 kilometers. - Test that your functions returns the right values for 20 and 40 miles per gallon.
- How did you figure out what the right value was? How closely do the
computer’s results match the ones you expected?
Submit your answer in a file called mileage.py (for part 1) and mileage.txt (for part 3).
3. The expression 1+'a' is not legal in Python because 1 is a number and ‘a’ is a string. Do you think 1<'a' is legal or not? If it is legal, what is its value, and why? Submit your answer in a file called comparison.txt.
4. What is the value of each of the following expressions, and why? Submit your answer in a file called boolean.txt.
True and not FalseTrue or True and Falsenot True or not FalseTrue and not 052 < 52.31 + 52 < 52.34 != 4.0
5. Here is a possible definition of how and works:
If both operands are true,
and‘s result is its second value. If either is false,and‘s result isFalse.
Is this the rule that Python actually uses? If not, provide a counter example. Submit your answer in a file called and.txt.
6. You want an automatic wildlife camera to switch on if the light level is less than 0.01 or if the temperature is above freezing, but not if both conditions are true. Your first attempt to write this is as
follows:
if (light < 0.01) or (temperature > 0.0):
if (light < 0.01) and (temperature > 0.0):
pass
else:
camera.on()
A friend says that you could write this more simply as follows:
if (light < 0.01) != (temperature > 0.0): camera.on()
Is your friend right? If so, explain why. If not, give values for light and temperature that will produce different results for the two fragments of code. Either way, which do you find easier to understand, and why? Submit your answer in a file called wildlife.txt.
Exercise 2: Viewing Grades
A post-doc is teaching a biology course for the first time. The university’s learning management system is “temporarily unavailable” while the system administrators try to undo a recent upgrade, so the post-doc has to manage marks herself for the foreseeable future. She needs a program to help her see:
- a list of all students with their marks;
- the highest mark;
- the lowest mark; and
- the average mark.
Grades for each assignment are stored in a separate file. Each file is formatted like this:
aturing: 86 inewton: 67.5 cdarwin: 90 fnightingale: 99 cvraman: 10
Write four functions that work as follows:
grade_show('ex3.dat', 'inewton'): print inewton’s grade inex3.datgrade_highest('ex3.dat'): print the highest grade inex3.datgrade_lowest('ex3.dat'): print the lowest grade inex3.datgrade_average('ex3.dat'): print the average grade inex3.dat
You may assume that all students’ IDs are unique. Submit your solution in a file called grade1.py. Please also submit a plain text file called grade1.txt that explains ambiguities or omissions there are in this specification, i.e., what do you have to implement that someone else might reasonably implement differently?
Exercise 3: Managing Grades
The post-doc also needs to be able to:
- add a new student with a grade;
- change a student’s mark; and
- remove a student
Create and submit another file called grade2.py containing functions that work as follows:
grade_create('ex3.dat'): create an empty grades filegrade_add('ex3.dat', 'inewton', 58): set inewton’s grade inex3.datto 58grade_change('ex3.dat', 'inewton', 88): change inewton’s grade inex3.datto 88grade_remove('ex3.dat', 'inewton'): remove inewton’s grade fromex3.dat
Exercise 4: Steganography
Steganography is the art or science of hiding secret messages in plain sight. For example, the first letter of every fifth line of the original edition of The Tempest might spell out “Francis Bacon writ Shakesprs plays”, or we could introduce deliberate misspellings into an email message such that every wrong letter, taken together, spelled out “Higgs Boson found”.
Write a program that takes a short message and a positive integer N as arguments, reads in a text file, and writes out the text with every N’th letter changed to encode the message. For example, suppose the file
example.txt contains:
Since 1997, Software Carpentry has taught scientists and engineers the concepts, skills, and tools they need to use and build software more productively. All of the content is freely available under a Creative Commons license, and we are constantly adding and updating lectures, videos, and exercises.
If the program is run as:
python steg.py "Hacker Within" 5 example.txt
then the output should be:
Hincea1997c SofkwareeCarprntry has Waughiscitntishs ani engnneers the concepts, skills, and tools they need to use and build software more productively. All of the content is freely available under a Creative Commons license, and we are constantly adding and updating lectures, videos, and exercises.
(Changes are underlined.)
Submit your solution in a file called steg.py. Please also create a plain text file called steg.txt containing answers to the following questions:
- What ambiguities or omissions are there in this specification? I.e., what do you have to implement that someone else might reasonably implement differently?
- This program’s output is clearly garbled. One way to make that less obvious is to increase the spacing between changes. What are some others?
- How did you test your program? How confident are you that it works correctly? How confident do you think someone else could be that it works correctly?

In example 4, the output should contain
“Waughi scitntishs”
rather than
“Waughiscitntishs”
@Kevin Can you explain why?
A space is missing between two words here?
taught scientists
Waughi scitntishs
I also have a small question not really related to the previos one:
The assignment says “every N’th letter changed ” but, judging from the given example, it means rather “character” (including a space if one occurs) not “letter”. Without the example I would count letters only and ignore the spaces. I am not sure though if letter/character makes any difference in Python.
@Tanya Good catch: that’s why the first follow-up question in the exercises asks, “What ambiguities or omissions are there in this specification? I.e., what do you have to implement that someone else might reasonably implement differently?” The exact meaning of “letter” is one; what are others?
Thanks, Now I got it
)
Greg, I understand you don’t mind discussing such questions here? or forum would be a better place?
@Greg Wilson Tanya nailed it…the given output isn’t consistent with itself.
BTW, if you DO ever need to handle whitespace characters differently, the string module has `whitespace` you can test against.
`>>> import string
>>> string.whitespace
‘\t\n\x0b\x0c\r ‘
>>> ” ” in string.whitespace
True
>>> `
It looks like that covers horizontal tabs, newlines, vertical tabs, form feeds, carriage returns, and spaces.
Greg and Tanya: Regarding Tanya’s post from Jan 26. I would rather see ALL discussions take place on the forum as opposed to here. The forum is organized into sections based on the Lesson and you can target your questions to a specific query. Here – is just a mishmash of stuff. I actually propose that this comment section be shut off in favour of the forum. Since I would not be following my own suggestion – I will repost this in the main forum.
Have the solutions been published?
Not online, but I’d be happy to send them to you if you’d like.