September 13, 2021
This lab is designed to help all of us get started in the course. First, you will help me get to know you by filling out a survey. Second, you will get oriented to the CS lab environment and/or set up Python on your own computer. Finally, you will practice the Python we’ve seen in class by implementing some mathematical formulas.
Post questions to the Moodle Forum!
Complete the introductory survey on Moodle. This is a really important part of helping me make the course as useful for you as possible.
Take some time to get oriented to the setup in the computer labs (Olin 310, 308, and 304). Go to one of them to complete the following two activities. There is nothing to turn in for this section, but please post any questions or problems you encounter to the Moodle forum or bring them to class. Even if you plan to work on your own computer, it’s a good idea to make sure your computer lab login works.
Log in using your Carleton user name and password. If you can’t log in, contact Mike Tie, the all-star CS technical guru, in Olin 335.
You can use any text editor to write Python code, but I recommend Visual Studio Code (VS Code for short). If you are working in the computer lab, you can skip to Start VS Code in a project (workspace) folder. If you’re on your own computer, you’ll need to install Python and VS Code first.
If you are working on your own computer you will need to install a Python interpreter.
Install Python from python.org. You can typically use the Download Python button that appears first on the page to download the latest version.
For additional information about Python on Windows, see Using Python on Windows at Python.org
The system install of Python on macOS is not supported. Instead, an installation through Homebrew is recommended. To install Python using Homebrew on macOS use brew install python3
at the Terminal prompt.
The built-in Python 3 installation on Linux works well, but to install other Python packages you must install pip
with get-pip.py
.
You should also install VS Code if you’re working on your own computer.
Install the Python extension for VS Code from the Visual Studio Marketplace. The Python extension is named Python and published by Microsoft.
START HERE IF YOU’RE WORKING IN THE LAB
Create an empty folder called hello
wherever you’re going to be working on CS 111 projects. As the lab computers are reset whenever you log off, any files you save to the computer will disappear. Fortunately, there is a network drive that isn’t reset and that can be accessed from any lab computer. On the desktop of every lab computer, you will find a shortcut to connect to this drive:
Double click on it, and you should see a login prompt after a few seconds
Enter your Carleton username and Carleton password then press Connect. Your login name does not contain the text @carleton.edu
. This will then open a folder where you can store things. This step can take a minute or two, it really seems to depend on how many courses you see listed and the load on the file server. If you’ve taken a class that used the network drive, a folder with that class name may be in the folder. For this class, you can store files in cs111-02-f21/StuWork/YOUR_USERNAME. Create a new folder called hello
there now.
Then, run VS Code and use File > Open Folder to open the hello
folder. Opening a folder makes that folder your workspace. If something pops up like the prompt below, click Cancel.
See these instructions for how to connect to this drive from your own computer.
From the Explorer toolbar, select the New File button on the hello
folder:
Name the file hello.py
, and it automatically opens in the editor:
Note: The File Explorer toolbar also allows you to create folders within your workspace to better organize your code. You can use the New folder button to quickly create a folder.
By using the .py
file extension, you tell VS Code to interpret this file as a Python program, so that it evaluates the contents with the Python extension and the selected interpreter.
In the lower left corner of the VS Code window, you should see something like “Python 3.8.2 64-bit”. If so, then you’re good to go and can move to the next step. If you see something like “Python 2…” or “Select Python Environment”, you’ll need to adjust this before moving on.
If you don’t see anything like this in the lower left, you may have missed Install the VS Code Python Extension.
To fix this, click on the text in the lower left. A list of different Python interpreters VS Code has detected should pop up. Select one that begins with Python 3. The lower left should then look something like this:
Now that you have a code file in your Workspace, enter the following source code in hello.py
:
= "Hello World"
msg print(msg)
When you start typing print
, notice how IntelliSense presents auto-completion options.
IntelliSense and auto-completions work for standard Python modules as well as other packages you’ve installed into the environment of the selected Python interpreter. It also provides completions for methods available on object types. For example, because the msg
variable contains a string, IntelliSense provides string methods when you type msg.
:
You don’t need to know what any of this means right now, but this will come in handy as we move through the course.
It’s simple to run hello.py
with Python. Just click the Run Python File in Terminal play button in the top-right side of the editor.
The button opens a terminal panel in which your Python interpreter is automatically activated, then runs python3 hello.py
(macOS/Linux) or python hello.py
(Windows):
Python can be an incredible tool for mathematical or scientific work. This lab asks you to translate several mathematical formulas into code and will help you practice using variables, arithmetic operations, print
, and Python’s math
module. You should create a file called lab0.py
and write your solutions to the problems below. print
statements in your solutions should include text that clearly indicates the meaning and units of any numbers being printed.
Write a program that computes and prints out the molecular weight of a carbohydrate based on the number of hydrogen, carbon, and oxygen atoms. The molecular weight is just the sum of the weights of all the atoms in the molecule. You should use the following weights:
Atom | Weight (grams / mole) |
---|---|
H | 1.0079 |
C | 12.011 |
O | 15.9994 |
Assign variables for the number of each type of atom and use them in your calculation as follows: 22 hydrogen atoms, 12 carbon atoms, and 11 oxygen atoms. The result should be rounded to three decimal places (use the built-in round
function–it takes two arguments, the number to round and the number of decimal places the return value should have).
Write a program that computes and prints out the distance between the two 2D points (10, 5) and (8, 4). Assign four variables for the \(x\) and \(y\) values of each point and use them in your calculation. The distance formula is:
\[distance = \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2}\]
Python’s math
module has a sqrt
function you can use to perform a square root.
Write a program that computes and prints the area of a triangle given the length of its three sides \(a = 17\), \(b = 15\), and \(c = 13\) using the formulas below. Assign variables for each side and use them in your calculation. Python’s math
module has a sqrt
function you can use to perform a square root.
\[s = \frac{a + b + c}{2}\]
\[area = \sqrt{s(s - a)(s - b)(s - c)}\]
Snow is already falling and soon snow drifts will inundate campus (if it ever stays cold enough for the snow to build up). To clear snow out of hard-to-reach places, facilities will need ladders. Write a program to compute and print the length (in meters) of a ladder required to reach a height of 8.5 meters when leaned against a building at an angle of 75 degrees. Assign variables for the required height and angle. To compute length of the ladder use
\[length = \frac{height}{\sin (angle)}\]
Note: Python’s math
module contains a sin
function. This function requires the angle to be in radians. Use this formula to convert from degrees to radians:
\[radians = \frac{\pi}{180}degrees\]
It’s always a good idea to test any code you write. Generally, testing code involves running it and verifying that it produces some expected output. Every lab will have a section like this with advice on how you might approach checking your work.
In this lab, you’re writing code to crunch some numbers. All of the formulas are simple enough that you can do the calculation by hand or with a calculator. Thus, a great way to make sure your program is doing the right thing is to do each problem manually and compare your result to what your program prints out. If they’re the same, chances are your program is correct. If you got something different than your program, it’s time to carefully check your code and your manual computation–one of them has an error!
lab0.py
with your solutions to all four problems via the Lab 0 Moodle page. When this script is run, the answers to all four problems should be printed out and clearly labeled.This assignment is graded out of 15 points as follows:
=
and math operators (+
, *
, etc.) and not putting too much math on a single line – 0.5 points