For Windows, click on the link labeled Python 2.6.4 Windows installer (the filename is python-2.6.4.msi) and save the file to disk. Once the download has completed, doubleclick on the Python-2.6.4 icon on your Desktop (or wherever the file was saved) to run the installer. You should be able to use all of the default options during the installation.
For Mac OS, click on the link labeled Python 2.6.4 Mac Installer Disk Image (the filename is python-2.6.4_macosx10.3.dmg) and save the file to disk. Once the download has completed, doubleclick on the icon for the downloaded file on your Desktop (or wherever the file was saved) to run the installer. You should be able to use all of the default options during the installation.
Once Python is installed, you are ready to start working on the assignment.
(Note: it is also possible to use Python and IDLE on the UNIX or Windows machines in the undergraduate lab.)
Put your answers to these problems in a text file named
ps4_partI.txt. Don't forget to save the file as a
plain-text document.
You should begin by starting up the Python interpreter shell -- either the command-line version, or the one that you get when you start up IDLE. You should see the following prompt:
>>>
which indicates that it is waiting for you to enter a command.
In the questions below, you will experiment with operators and built-in functions by evaluating a number of expressions. Recall that you can evaluate an expression by just entering it at the command prompt in the interpreter. Make sure that you do not include the expression letter (e.g., a or b) when you enter an expression!
+ operator, part I (3 points)
1 + 2
1.5 + 2.5
'apple' + 'ball'
'apple' + 2
1 + 'wall'
"Python" + ' is ' + "fun"
Based on the results above and any additional experiments that you wish to conduct, briefly describe your conclusions about how the + operator works for different types of data.
+ operator, part II (3 points)
apple = 5
ball = 7
Explain the difference between the following two
expressions:
'apple' + 'ball'
apple + ball
While you are welcome to use the interpreter to see
what these expressions produce, we encourage you to
try to answer the question before you use the interpreter!
* operator (3 points)
20 * 6
'oh' * 2
'oh' * 7
"wow!" * 10
'apple' * 'ball'
5 * 'oh'
Based on the results above and any additional experiments that you wish to conduct, briefly describe your conclusions about how the * operator works for different types of data.
cmp function (3 points)cmp function takes two arguments
and returns a numeric value that indicates how the arguments
compare -- i.e., whether the first argument is less than,
greater than, or equal to the second argument.
What results do you obtain when you evaluate each of
the following expressions in the interpreter?
Include the results in your answer to this problem.
cmp(5, 10)
cmp(10, 5)
cmp(5, 5)
cmp(10, 10)
cmp('apple', 'ball')
cmp('ball', 'apple')
cmp('apple', 'apple')
cmp('ball', 'ball')
Based on the results above and any additional experiments that you wish to conduct, briefly describe your conclusions about the meanings of the values that this function returns.
range() function, part I (4 points)
range() function
is a built-in function that can be used to generate a list
containing a sequence of numbers. It can take one, two, or
three arguments.
What results do you obtain when you evaluate each of the following expressions in the interpreter? Include the results in your answer to this problem.
range(2)
range(8)
range(6, 12)
range(20, 25)
range(0, 24, 3)
range(0, 24, 4)
range(10, 5, -1)
range(30, 15, -3)
Based on the results above and any additional experiments
that you wish to conduct, briefly describe your
conclusions about how the range()
function works when given one, two, and three arguments.
range() function, part II (4 points)
range() function
is often used in conjunction with a for loop.
The for statements below use an explicit
list of numbers (e.g., [0, 1, 2, 3, 4]).
Let's say that we want to replace these explicit lists with
expressions involving the range()
function. What range()-function expression
would be needed in each case?
for i in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]:
print i
for i in [20, 18, 16, 14, 12, 10]:
print i
start = 1000
change = 0
for i in range(2):
amount = input("Enter a deposit or withdrawal amount: ")
change = change + amount
perc_change = 100 * change / start
print "The percent change in the account is", perc_change
We have put this code in a module file called accountCalculator.py.
To download it, click here
and save the file to your computer using the File->Save As
(or equivalent) option in your browser. Once you have done so,
open the file in IDLE by using the File->Open menu option.
Then you will be ready to work on the following problems.
The new account balance is 1200 dollars.
The percent change in the account is 20 percent.
Make sure that all of your changes to the module are saved before you move onto the next problem.
You should make these changes in a separate file.
To do so, save a copy of your
accountCalculator.py file from the previous problem
by using the File->Save As menu option in IDLE.
Give the copy the name accountCalculator2.py. Make sure to
include the .py extension at the end of the file name.
start. Use an appropriate prompt, making sure that there
is a space at the end of the prompt. This input statement should
replace the existing assignment statement for the variable
start.
Enter transaction 1 100
Enter transaction 2 200
...
Note that the prompts should remain on the same line as the
user's inputs (which are shown in bold above). In addition,
make sure that the first transaction has the number 1, the second
transaction has the number 2, and so on. Hint: Instead
of specifying the prompt as part of the input
statement, use a separate print statement that
comes before the input statement, and change
the input statement so that it does not take
an argument, as follows:
amount = input()
ps4_partI.txt file
accountCalculator.py file
accountCalculator2.py file
Here are the steps you should take:
Note: WebSubmit occasionally becomes inaccessible because of problems with the University's password server. If you encounter problems, close your browser and start again, or try again later if you still have time. If you are unable to submit and it is close to the deadline, email your homework before the deadline to cs105-staff @ cs . bu . edu.