Feel free to ask questions at any point during the lab.
We may not finish all of the lab exercises during the actual lab session. Every Wednesday, we will post solutions to the current week’s lab on the Labs page of the course website.
We don’t grade the lab exercises. However, starting with today’s lab, your attendance at lab will count towards your participation grade. If you attend 85% of the lab sessions over the course of the semester, you will get full credit for lab participation. See the syllabus for more details.
To get full credit for participation in lab, you must work productively throughout the lab session. You will not be penalized if you cannot finish all of the lab exercises.
Feel free to work on the lab exercises with your classmates, and don’t hesitate to ask a staff member for help!
If you haven’t already created a folder named cs111
for your
work in this course, follow these
instructions to do so.
Then create a subfolder called lab2
within your cs111
folder,
and put all of the files for this lab in that folder.
Access the template that we have created by clicking on this link and signing into your Google account as needed.
When asked, click on the Make a copy button, which will save a copy of the template file to your Google Drive.
Select File->Rename, and change the name of the file to
lab2
.
Consider the following program:
s = 'cs111' t = 'is amazing!' u = s[1] + t[-4: ] s = s[ :2] + (t[-1]*2) t = t[1:-2:2] s[::-2] print(s, t, u)
In the Task 1 section of your lab2
file, we have given you a
table that you should fill in to show how the values of the
variables change over time. Try to figure out the values on
your own, without using Spyder.
Based on your tracing, what is the output of this program?
In Spyder, open a new editor window using File->New file, and
save it to your lab2
folder using the name lab2task1.py
.
Run the program using the “play” icon or F5. Does the output match your predictions?
Why doesn’t the line of the program that uses skip-slicing have any effect on the output of the program?
What change could we make so that the skip-slicing changes the
value of s
?
The problems below ask you to create a few lists using only the following:
num = [8, 6, 7, 5, 3, 0, 9]
info = [4, 1, 1]
num[0]
num[1:]
num[1:6:2]
num[:1] + info[:1]
(but don’t use +
for addition)[num[0]]
or [num[0], info[0]]
To get started:
Download the following file: lab2task2.py
Make sure to put the file in your lab2
folder. If your
browser doesn’t allow you to specify where the file should be
saved, try right-clicking on the link above and choosing
Save as... or Save link as..., which should produce a
dialog box that allows you to choose the correct folder for
the file.
In Spyder, choose the File->Open... menu option and locate the file. Double-click on the file to open it in Spyder. This should add a tab for the file to your Editor pane.
Run lab2task2.py
in Spyder. This will make the lists num
and info
available to you in the IPython console, so that you can experiment with
possible answers in the console.
Here are the problems:
Use num
and/or info
to create the list [8, 7, 3, 9]
, and assign
it to the variable list1
. This example has been completed for you
and is already in lab2task1.py
.
Use num
and/or info
to create the list [1, 3, 5, 7]
, and
assign it to a variable named list2
. Then add a print
statement
to print list2
. Follow the same format that we gave you for
list1
.
Use num
and/or info
to create the list [4, 1, 1, 6, 5, 0]
,
and assign it to a variable named list3
. Then add a print
statement to print list3
. Follow the same format that we gave you
for list1
.
Feel free to ask for help if you get stuck!
Once you have completed these problems, run your program in Spyder to ensure that you get the correct output.
In Spyder, open a new editor window using File->New file, and
save it to your lab2
folder using the name lab2task3.py
.
Copy the following (buggy) code into that file:
def mysum(x, y) """ takes two numbers and returns their sum """ total = x + y print(total)
Save the file using Ctrl-S. Then try to run it. What happens?
This function include two syntax errors – errors that violate the rules of the Python language. See if you can find and fix them, continuing until you are able to run the file without any error messages. Feel free to ask for help.
Note: You should not change the print
statement at
this point, since it does not have any syntax errors.
Once you have eliminated the syntax errors, test the function from the IPython console as follows:
>>> mysum(44, 67) >>> print(mysum(10, 7))
What happens? Why?
Fix the function so that both of the above tests work as expected. Hint: The function is supposed to return the sum, not print it!
Don’t forget that you need to re-run the lab2task3.py
file after
making changes to the function.
In lab2task3.py
, write a function named evens_odds
that
accepts a string s
as its only parameter. The function should
create and return a new string that begins with all of the
characters in the even positions of s
(positions 0, 2, ...)
followed by all of the characters in the odd positions of s
(positions 1, 3, ...). For example:
>>> evens_odds('python!') result: 'pto!yhn'
Note that the result begins with the characters in the even positions
of python
('pto!'
) followed by the characters in the odd positions
('yhn'
).
Here is a suggested template:
def evens_odds(s): """ takes a string s and returns a new string in which the characters in the even positions of s are followed by the characters in the odd positions of s """ evens = ____________________ odds = _________________ return _________________
After writing the function, make sure to test it by making function calls from the console like the one shown above.
Here again, don’t forget to re-run the file after making any changes.
Consider the following function:
def mystery(a, b): """ takes two numbers, a and b, and does something with them! """ if a > b: a = a * 2 c = a + 3 elif b > a: b = b * 3 c = b - 2 elif b == 3: c = a + 1 else: c = -1 if a % 2 == 0: c = c // 2 return c
Consider the following call to this function:
mystery(5, 3)
On the second page of the lab2
template, we have given you
a table that you should fill in to show how the
values of the variables change over time. You may not need
all of the rows.
Complete any of the problems listed under the Warmup-1 collection of problems on CodingBat. These problems provide good practice with writing functions with lists and strings.
As mentioned last week, we won’t typically ask you to submit the work that you complete in lab. Rather, there will be an attendance sheet to sign, and staff members will check that you are working steadily on the assigned tasks during the lab.
However, this week we are asking you to submit a PDF formed from
the work that you did in the lab2
file on Google Drive. That’s
because we want you to practice the procedure for submitting PDFs that
contain answers to multiple problems.
We won’t actually grade the work that you submit for this lab.
Here are the steps:
Open your copy of the lab2
file on Google Drive.
Choose File->Download->PDF document, and save the resulting file
(lab2.pdf
) to your machine.
Make sure to put the file in your lab2
folder. If your
browser doesn’t allow you to specify where the file should be
saved, try right-clicking on the link above and choosing
Save as... or Save link as..., which should produce a
dialog box that allows you to choose the correct folder for
the file.
Login to Gradescope by clicking the link in the left-hand navigation bar.
Once you are in logged in, click on the box for CS 111.
Click on the name Lab 2 in the list of assignments. You should see a pop-up window labeled Submit Assignment. (If you don’t see it, click the Submit or Resubmit button at the bottom of the page.)
Choose the Submit PDF option, and then click the Select PDF
button and find the lab2.pdf
that you created in step 2.
Then click the Upload PDF button.
You should see an outline of the tasks along with thumbnails of the pages from your uploaded PDF. For each problem in the outline:
If this were an actual homework submission, you would click on the magnifying glass icon for each page and doublecheck that the pages that you see contain the work that you want us to grade.
Once you have assigned pages to all of the tasks in the question outline, click the Submit button in the lower-right corner of the window.
You should see a box saying that your submission was successful.
Click the (x)
button to close that box.
You can use the Resubmit button at the bottom of the page to resubmit your work as many times as needed before the final deadline.
Important
On the problem sets, it will be your responsibility to ensure that the correct version of each file is on Gradescope before the final deadline. We will not accept any file after the submission window for a given assignment has closed, so please check every submission carefully using steps like the ones outlined above.
Last updated on September 16, 2024.