Day 2 Python Basics Lab
Today, we'll be practicing various basics about how to use Python.
This is a lab activity, not an assignment. Therefore, it's not required that you complete it or hand it in. However, the concepts in this lab will be useful when you go to complete the homework assignments (and will be useful for exams down the road), so I'd encourage you to put in a serious effort on it in class, and to consider finishing any remaining parts outside of class.
Setup
The setup will be the same as last time. Here's a reminder, as well as a link with screenshots that correspond to these instructions.
- Mount the COURSES drive by double clicking the courses Desktop icon (if you didn't use the checkbox when you logged in)
- Create a new folder in your STUWORK folder called
Lab1 - Open VSCode
- Click and drag your
Lab1folder to the VSCode window to open it - Create a new file named
hello.py - In the menu under Terminal, click New Terminal to open a terminal window.
Exercise 1: Gradescope Practice
Homework assignments in this class will be turned in on Gradescope. Homework assignments starting with HW1 (due Friday) will also have an autograder, which runs your code with specific inputs to check that it works as intended.
Try out the following steps:
- Navigate to Moodle. In the top section, there is a link to Gradescope. Follow this link.
- In Gradescope, click on the "Lab 1 (autograder practice)" assignment.
- Submit the (empty)
hello.pyfile. Wait 1-2 minutes for the autograder to run on your submission. Notice what happens when an autograder test fails. - In
hello.pytype the following:print("Hello world!").- Don't copy and paste! You should always type out Python code because it's good practice, and because copying often introduces weird spacing that will make your program fail.
- Save
hello.py(Cmd+s) - Upload
hello.pyto Gradescope again and make sure that you pass both autograder tests. - Add your partner to the submission on Gradescope (on the right sidebar, click "View or edit group" under your name).
Exercise 2: Operators
For each of the following lines of code, predict the output and write this prediction down on your worksheet.
Then, run each line of code and see what is actually printed. (My recommendation:
create a new file experiments.py for this purpose, and type each line from scratch,
rather than copy-and-pasting.)
Operators on numbers
print(4) print(5 + 3) print(5.0 - 3.0) print(5 * 3) print(5 / 3) print(5 // 3) print(5 % 3)
Operators on strings
print("5" + "3") print("5" - "3") print("5" - 3) print("5" * "3") print("5" * 3) print("5" / "3")
Combining strings and expressions
print("4 + 3 =", 4 + 3) print("Three 4s:", 3 * "4") print("a=", 4-3) print("a=", str(4-3))
Exercise 3: Printing
When you call print() in Python, in addition to the expressions you want printed, you can
also provide "keyword parameters" by name to change how print behaves.
The end parameter says how to end a print statement. By default, this is a "newline character"
(specified in Python as \n), meaning that the next thing to print will appear on the following
line.
First, what does this code print?
print("cat") print("dog")
It prints the following:
cat dog
What do you think this code will print?
print("cat", end="!") print("dog")
Write down your guess, and then test it out in Python.
Another keyword parameter of print is called sep, short for separator. What do you
think this parameter does? Predict the result and try it out for the following code:
print("cat", "dog", "rabbit", sep="!") # try changing "!" to something else
Exercise 4: Variables
The following program computes the volume of a sphere given its radius.
1: # Get the radius from the user 2: radius = input("What is the radius? ") 3: radius = float(radius) 4: 5: # Compute the volume (4/3 * pi * r^3) 6: volume = 4 / 3 7: volume = volume * 3.14 8: volume = volume * radius 9: volume = volume * radius 10: volume = volume * radius 11: print("The volume is", volume, "(radius=" + str(radius) + ")")
Fill in the table on the worksheet to keep track of the values of the variables over time.
You can assume that the input radius is 10. Feel free to write fractions instead of real
numbers (e.g., 4/3 instead of 1.333333) and to leave expressions unsimplified (e.g., pi*pi instead of 9.86).
Extra time?
Here's a problem to solve: Suppose you are running a track workout and want to compute how quickly you run one lap, based on the start and end time for that lap.
Write a program that asks the users for these four integer inputs:
- The minutes of the start time
- The seconds of the start time
- The minutes of the end time
- The seconds of the end time
Your code should then calculate the difference between the end time and the start time, and print out the elapsed time in minutes and seconds.
Note that seconds should be a value between 0 and 59, which means that simply subtracting the start seconds from the end seconds won't work. Think about what you need to calculate here, and perhaps write out the algorithm on paper before starting to write the code.
Try to come up with test input that will check for all cases that your code should handle.
Anna's acknowledgements
This activity was borrowed from Anya Vostinar – thanks for sharing!