Last week we talked about the fact that a computer is, among other things, a tool to perform high speed arithmetic and logic operations. In order to learn how to write more advanced programs in Python, we need to explore how python wants us to configure instructions requesting these basic operations.
During this lab you will complete a set of activities based on the material in chapter 2 in your textbook. You were supposed to have read chapter 2 already, but you probably didn't have the ability to actually DO what the book asked. In PART of this lab you will actually DO activities like those listed in chapter 2.
If you have not already done so, start JES.
[Q1] Enter each of the following mathematical statements at the interactions pane prompt and press enter. Record what "action" the computer takes in response to your command
The results here shouldn't surprise you once you recognize that the asterisk (*) and the forward slash (/) are the operators that python uses for multiplication and division. For the most part, mathematics in python works the way that it worked in your high school algebra class.
[Q2] Given this knowledge, predict what will happen when you invoke the following statements. After making your prediction, enter the statement at the interactions prompt and check if you were correct. If you were incorrect, determine why.[Q3] Predict what will happen when you invoke the following statements. After making your prediction, enter the statement at the interactions prompt and check if you were correct.
[Q4] Given what you observed in the previous step, write a short explanation of how the division operator works in Python.
Unlike "high school mathematics" - where there are four binary operators (addition, subtraction, multiplication, and division) - python contains at least six binary operators (at least, we will use with some regularity, six).
These additional operators include the ** symbol and the % symbol.
[Q5] Create a series of statements using the ** symbol (such as 2 ** 3) and determine what this operator does. Write down the four or five statements that you tried and their results.
[Q6] When you are done, write a short explanation of how the ** operator works in python.
[Q7] Enter each of the following mathematical statements at the interactions pane prompt and press enter. Record what "action" the computer takes in response to your command
- print 9 % 2
- print 9 % 3
- print 9 % 4
- print 10 % 4
- print 11 % 4
- print 12 % 4
[Q8] Given what you observed in the previous step, write a short explanation of how the % operator works in Python.
[Q9] Predict what will happen when you invoke the following statements. After making your prediction, enter the statement at the interactions prompt and check if you were correct. Warning, one of these statements is invalid and will produce an error.
Most of these should make sense once you realize what is meant by '1' and what is meant by '0'
[Q10] Predict what will happen when you invoke the following statements. After making your prediction, enter the statement at the interactions prompt and check if you were correct.
These might not be as obvious as the earlier ones, but they might make a little more sense if you type in the following
[Q11] What do you observe?
Strings
A String is a sequence of characters (letters, numbers, etc). Print statements are intended to display these Strings.
[Q12] Enter the following statements into the interactions pane and observe what happens (note, one of these will cause an error).
[Q13] What happens when you join two strings together using the plus sign? What happens when you join two strings together using the comma?
Section 2.4.3 in your textbook contains a good discussion on why we would use variables. We will not try to reproduce that here (read your book!). However, let's try it for yourself.
Let's begin by reproducing what you did in Lab1 and what is listed in section 2.4.3 in your textbook.
Type
filename = pickAFile() picture = makePicture(filename) show(picture)
When asked for a file in the first step, select the logo you saved to your directory on the p drive during lab 1.
Now, type
a = pickAFile() b = makePicture(a) show(b)
[Q14] What happens when you invoke these two sets of commands? How are they the same? How are they different? Which is "easier" to write? Which is easier to read? Why?
It is pretty common that we have a sequence of commands that are frequently performed together as part of a larger action. For example, it is REALLY kind of a pain to have to type the three lines from Activity D above just so that we can open and display a single picture. It would be much easier if python just had one command called something like
selectAndDisplay()
or
pickAndShow()
which would allow us to say only ONE thing and we were able to select it and open it for viewing all at once. Well, python doesn't, but python DOES let us write our own commands so that we can add this functionality.
Much like we decide to stop worrying about flour, sugar, butter, milk, and yeast and just start thinking about the single entity "bread" we can write a program (recipe) that combines multiple commands to be used by just one name.
In the program area of JES (the white area at the top of the environment) type the following commands:
def pickAndShow(): filename = pickAFile() picture = makePicture(filename) show(picture)
A couple of things to notice about this:
When you are done with this, select "File -> Save Program" and save this with the name "lab2.py" in your root folder on the p: drive.
Once you have saved your program/recipe, you can use it just like you would any other command. First however, you have to load it into JES. I know, this seems strange. It's right there at the top of the screen, but you can't use it until you load it.
After you have loaded it, you should be able to invoke this command in the interactions pane by typing
pickAndShow()
If you typed everything correctly you SHOULD be asked to pick a picture file (select your logo again) and then the file should display automatically. If you made a typo you may receive an error while runnign your program. If this is the case, try to fix the problem, save the file again, reload the file, and test is again.
Keep fixing the problems until you get this simple program to work.
[SIG1] Finally, modify the pickAndShow() program so that is does everything that it used to do, plus displays the basic information about the picture you selected. Notice this is only one line of code added to your program. When you have this saved and working, demo this for the instructor and get a signature indicating it was checked in.
Sometimes we want to write a program that takes in information from the user. For example, when we use the show() program we don't say:
show()
we say
show(picture)
and we pass in a variable containing a picture object. Writing a program (recipe) which takes in information is actually very easy to do:
Underneath the definition for your pickAndShow() recipe, add the following definition for a new program called greet()
def greet(person): print "Hello" , person print "How are you?"
Save the file (notice that you can save a file with multiple definitions), load the file, and then invoke the greet program by typing something like the following at the interactions prompt.
greet("Ben")
greet("Amy")
[Q15] What happens?
Modify your greet program from Activity F so that it takes two names at once and greets both people at the same time.
For example, I might ask it to greet "Ben" and "Amy" and the message that gets printed says something like:
Hello Ben and Amy How are you two today?
Just play around with this and see if you can figure out how to pass in two parameters to the greet method and then display both of those methods.
It turns out this is easy once you see it done, but it may take you a while to get it. Just TRY it
[SIG2] If you get this to work, demonstrate this program for the instructor.
Congratulations!
Whew!! You accomplished a lot this week.
You are now free to stick around and use the machines to try some other examples provided in your textbook, check the class web site, check your email, browse the web etc. On the other hand, you are also free to leave early.
When you are ready to leave, make sure that your activity log is completed before submitting it to Dr. Schafer.
Do not forget to “log off” of the machine you are working on.