Lab 0: Getting Started
Due by 11:59pm on Wednesday, September 3.
Starter Files
Download lab00.zip.
This lab is required for all students and counts toward your lab score.
Introduction
This lab explains how to setup your computer to complete assignments and introduces some of the basics of Python. If you need any help, please post on Ed or ask for help at your assigned lab section.
Here's an outline of the lab:
Setup: Setting up the essential software for the course. This will require several components, listed below.
- Install a Terminal: Install a terminal so you can interact with files in this course and run OK commands.
- Install Python 3: Install Python version 3.8 or later (ideally Python 3.11 or later) on your computer.
- Install a Text Editor: Install software to edit
.py
files for this course. Almost all students use VS Code.
- Review: Your Computer's File System This is an overview of how your computer's file system works, including the meaning of both absolute and relative file paths.
- Walkthrough: Using the Terminal: This walks you through how to use the terminal and Python interpreter.
- Walkthrough: Organizing your Files: This section walks you through how to use your terminal to organize and navigate files for this course. Everyone should read this section.
- Required: Doing the Assignment: You must complete this section to get credit for the assignment. The main goal of this part is to give you practice using our software.
- Required: Submitting the Assignment: Turn in your work.
- Appendix: Useful Python Command Line Options: These are commands that are useful in debugging your work, but not required to complete the lab. We include them because they are likely to be helpful to you throughout the course.
Setup
To setup your device, select the guide that corresponds to your operating system.
Your First Assignment
When working on assignments, ensure that your terminal's working directory is correct (which is likely where you unzipped the assignment).
1) What Would Python Do? (WWPD)
One component of lab assignments is to predict how the Python interpreter will behave. We call these questions "What Would Python Do?"
In this class, we use a program called ok
to assess your knowledge.
ok
will be included with every assignment. Let's go through
how to do a WWPD question with ok
.
Open your terminal, and make sure you are in the lab00
directory that contains
the unzipped lab files for this assignment. In that directory, type ls
to verify
that there are the following files:
lab00.py
: the starter file for this labok
: our testing programlab00.ok
: a configuration file forok
If you don't see these files, use cd
to navigate to the correct directory.
Attempting to run
ok
in a directory that does not haveok
will produce an error.
Enter the following in your terminal, which will run ok
and begin this section:
python3 ok -q python-basics -u
The command
python3 ok -q python-basics -u
tells the Python interpreter to run the file with the nameok
in the current working directory.-q python-basics -u
are inputs provided to theok
program that identify which question to run.As stated in the setup section, if the
python3
command does not work, please try usingpython
orpy
.
You will be prompted to enter the output of various statements/expressions. You must enter them correctly to move on, but there is no penalty for incorrect answers.
The first time you run Ok, you will be prompted for your bCourses email. Please follow these directions.
>>> x = 20
>>> x + 2
______22
>>> x
______20
>>> y = 5
>>> y = y + 3
>>> y * 2
______16
>>> y + x
______28
2) Implementing Functions
Open the entire lab00
folder in VS Code. You can drag the folder onto the VS
Code application or open VS Code and use Open Folder...
in the File
menu.
Once you open the lab00
folder, you'll see the lab00.py
file in the file
explorer on the left panel of your VS Code window. Click it to start editing
lab00.py
, which is the file you will submit to receive credit for the lab.
Important: Turn on Auto Save
in the File
menu of VS Code. Then, whenever
you change a file, the contents will be saved. If you don't enable Auto Save
,
be sure to frequently save your work.
Recommended: Use the terminal inside VS Code (Terminal > New Terminal
in
the menu). If you opened the assignment folder in VS Code as instructed above,
then the VS Code terminal's working directory will automatically be set to the
assignment folder, which means you won't have to change directories to check
your work.
Now complete the lab. You should see a function called twenty_twenty_five
that
has a blank return
statement. That blank is the only part you should change.
Replace it with an expression that evaluates to 2025. What's the most creative
expression you can come up with?
3) Running Tests
We'll also use ok
to test your code. Switch to the terminal. Make sure you are in the lab00
directory that contains
ok
and lab00.py
.
Pro tip: If you opened the
lab00
folder in VS Code and selectNew Terminal
in theTerminal
menu of VS Code, then the terminal will automatically be in thelab00
directory.
Now, run ok
with this command to test your code:
python3 ok
Remember, if you are using Windows and the
python3
command does not work, please try using justpython
orpy
.
If you wrote your code correctly and you finished unlocking your tests, you should see a successful test:
=====================================================================
Assignment: Lab 0
=====================================================================
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Running tests
---------------------------------------------------------------------
Test summary
2 test cases passed! No cases failed.
If you didn't pass the tests, ok
will instead show you something like this:
---------------------------------------------------------------------
Doctests for twenty_twenty_five
>>> from lab00 import *
>>> twenty_twenty_five()
0
# Error: expected
# 2025
# but got
# 0
---------------------------------------------------------------------
Test summary
0 test cases passed before encountering first failed test case
Fix your code in your text editor until the test passes.
Every time you run
ok
,ok
will try to back up your work. Don't worry if it says that the "Connection timed out" or that you're not enrolled in the course. You can still submit this assignment and get credit.
Submitting the Assignment
Now that you have completed your first assignment, it is time to turn it in. You can follow these next steps to submit your work and get points.
Submit with Pensieve
- Log in with School Credentials using your CalNet ID to Pensieve. You’ll be taken to your Dashboard as soon as you log in.
- On your Dashboard, select this course. (You should have already been added to Pensieve. If this is not the case, please make a private Ed post.) This will take you to the list of assignments in the course that you are able to submit.
- Click on the assignment Lab 0 to open it.
Upload your
lab00.py
file. That is all you need to submit.Next, wait a few minutes for the autograder to score your submission. If you are surprised by your score, make sure you submitted the right file. Assignments can be resubmitted as many times as you would like before the deadline.
Your responses to WWPD questions are not submitted to Pensieve, and they do not need to be. Lab credit is based on code writing questions.
Congratulations, you just submitted your first assignment!
Appendix: Useful Python Command Line Options
Here are the most common ways to run Python on a file.
Using no command-line options will run the code in the file you provide and return you to the command line. If your file just contains function definitions, you'll see no output unless there is a syntax error.
python3 lab00.py
-i
: The-i
option runs the code in the file you provide, then opens an interactive session (with a>>>
prompt). You can then evaluate expressions such as calling functions you defined. To exit, typeexit()
. You can also use the keyboard shortcutCtrl-D
on Linux/Mac machines orCtrl-Z Enter
on Windows.If you edit the Python file while running it interactively, you will need to exit and restart the interpreter in order for those changes to take effect.
Here's how we can run
lab00.py
interactively:python3 -i lab00.py
-m doctest
: Runs the doctests in a file, which are the examples in the docstrings of functions.Each test in the file consists of
>>>
followed by some Python code and the expected output.Here's how we can run the doctests in
lab00.py
:python3 -m doctest lab00.py
When our code passes all of the doctests, no output is displayed. Otherwise, information about the tests that failed will be displayed.