Skip to content
Snippets Groups Projects
Commit f08bb10e authored by Ashwin Maran's avatar Ashwin Maran
Browse files

add lab-p1 and p1

parent eddc0b26
No related branches found
No related tags found
No related merge requests found
Showing
with 446 additions and 90 deletions
# cs220-f23-projects
## Getting started
To make it easy for you to get started with GitLab, here's a list of recommended next steps.
Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)!
## Add your files
- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files
- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command:
```
cd existing_repo
git remote add origin https://git.doit.wisc.edu/cdis/cs/courses/cs220/cs220-f23-projects.git
git branch -M main
git push -uf origin main
```
## Integrate with your tools
- [ ] [Set up project integrations](https://git.doit.wisc.edu/cdis/cs/courses/cs220/cs220-f23-projects/-/settings/integrations)
## Collaborate with your team
- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/)
- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html)
- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically)
- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/)
- [ ] [Automatically merge when pipeline succeeds](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html)
## Test and Deploy
Use the built-in continuous integration in GitLab.
- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html)
- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing(SAST)](https://docs.gitlab.com/ee/user/application_security/sast/)
- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html)
- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/)
- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html)
***
# Editing this README
When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thank you to [makeareadme.com](https://www.makeareadme.com/) for this template.
## Suggestions for a good README
Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information.
## Name
Choose a self-explaining name for your project.
## Description
Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors.
## Badges
On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge.
## Visuals
Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method.
## Installation
Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection.
## Usage
Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README.
## Support
Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc.
## Roadmap
If you have ideas for releases in the future, it is a good idea to list them in the README.
## Contributing
State if you are open to contributions and what your requirements are for accepting them.
For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self.
You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser.
## Authors and acknowledgment
Show your appreciation to those who have contributed to the project.
## License
For open source projects, say how it is licensed.
## Project status
If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers.
UW-Madison course CS220/CS319 project repository for Fall'23 semester.
# Lab-P1: Running Programs
Welcome to your first lab! This semester, you're going to learn how to write your own Python code. But for this lab, you're just going to practice running some Python programs we give you.
**Important:** Before going to the lab, install Anaconda with Python 3.11 on your laptop. We've created videos showing how to do this on [Windows](https://youtu.be/jUGTRDXJBtI) and [Mac](https://youtu.be/tL8reqOXwjE). Please make sure that **all** checkboxes are checked during the Windows installation.
**If you have already installed Python or Anaconda before this semester, please uninstall and reinstall, following the instructions in these videos. Please do not upgrade your Python version anytime during the semester. We expect you to retain Python 3.11 throughout this semester.**
As a reminder, labs are a part of your grade and are meant to be guided by a TA/peer mentor. Thus, in-person attendance is **mandatory** (unless you are a CS319 student or an AmFam student).
The lab is divided into **Segments**. Each segment will introduce a new concept to you, and will build on concepts introduced in earlier segments. If you ever get stuck on any segment, please feel free to reach out to your TA/peer mentor.
This lab also contains **Tasks**. Pay attention to these, as you'll be expected to be able to do similar tasks on your own moving forward.
## Learning Objectives
After completing this lab, you will be able to...
* download files from GitLab,
* use basic terminal commands,
* run a Python script,
* understand and use absolute and relative paths,
* create and run a Jupyter Notebook,
* download and run a Jupyter Notebook.
---
## Commonly used Terminal/PowerShell commands
Here are a list of Terminal/PowerShell commands that you shall be learning more about, in this lab. You can refer to this section if you need to be reminded of what the different commands do.
|Command|Function|Example|
|---|---|---|
|`pwd`|displays path of current directory|`pwd`|
|`ls`|lists files in current directory|`ls`|
|`cd`|changes the current directory|`cd "C:\Users\myname\Documents\cs220\lab-p1"`|
|`mkdir`|makes a new subdirectory inside the current directory|`mkdir lab-p2`|
|`cat`|displays the contents of a file|`cat hello.py`|
|`python`/`python3`|executes Python script|`python hello.py`|
|`jupyter notebook`|open Jupyter notebook from current directory|`jupyter notebook`|
---
## Segment 1: Download Your First Program
The first thing you're going to need to decide is where to keep your work this semester.
### Task 1.1: Create the folders for this lab
Create a folder named `cs220` under `Documents`. Then, inside the new `cs220` folder, create a sub-folder called `lab-p1` and use it for all your files related to this lab.
To find `Documents`...
#### Windows Users
Open `File Explorer` and select `Documents`:
<img src="images/windows-documents.png" width="400">
#### Mac Users
Open `Finder` and select `Documents`:
<img src="images/mac-documents.png" width="200">
---
Before we begin the next task, let's learn about the term **file extension**. When you have a file named `somefile.some_extension`, the part after the `.` is called the **file extension**. A `.py` extension refers to a Python program. Other examples of a file extensions include: `.txt` (text file), `.docx` (document file), and `.xlsx` (spreadsheet file).
To show file extensions, you will need to...
#### Mac Users
1. Open `Finder` window and click on `Finder`(menu) > `Preferences`:
<img src="images/finder_preferences.png" width="500">
2. Navigate to `Advanced` and enable `Show all filename extensions`:
<img src="images/finder_preferences_settings.png" width="500">
#### Windows Users
1. In your `File Explorer`, Click `View` > `Show` > `File name extensions`:
<img src="images/windows_file_ext.png" width="500">
### Task 1.2: Download the file `hello.py` to your `lab-p1` folder
**We strongly recommend using Google Chrome browser, throughout this semester. Other browsers might add unpredictable extensions to your files and they might not work as intended.**
At the top of this page, you'll see a list of files, something like this:
<img src="images/github.png" width="800">
Downloading files from GitLab (the site hosting this document) is a little tricky for those new to it. Try this:
1. Right-click on `hello.py`.
2. Choose `Save Link As...` (or similar).
3. Navigate to your `lab-p1` folder in the pop-up.
4. Ensure that you download file with the **proper extension**:
* Windows users: ensure that `Save as type` is **not** `Text Document`, but is either `Python File` or `All Files`.
<p align="center"><img src="images/save_windows.png" width="500"></p>
* MAC users: ensure that you have followed the above Finder settings steps before this step. Replace the `.txt` extension with `.py`.
<p align="center"><img src="images/save.png" width="500"></p>
<p align="center"><img src="images/save_mac.png" width="500"></p>
* You should be following this process for **all** downloads from GitLab. Here is another example: if you are downloading a `.ipynb` file, you should make sure the extension is exactly `.ipynb` and not `.txt.ipynb` or `.txt` or `.ipynb.txt`.
5. Hit Enter.
Alright, we've downloaded our first Python file! *Or have we?*
### Task 1.3: Open a terminal emulator aka a Terminal.
**Windows**:
1. Hit the Windows logo key on your keyboard.
2. Type `powershell`.
3. Open `Windows PowerShell` (be careful, do **not** choose the ones that say `ISE` or `x86`).
4. Pin this application to Start bar. If you don't know how to do this, please do a Google Search.
**Mac**:
1. Open Finder.
2. Click `Applications`.
3. Open `Utilities`.
4. Double-click `Terminal.app`.
5. Pin this application to Dock. If you don't know how to do this, please do a Google Search.
We're going to talk more about terminals later - don't worry too much about the details yet.
### Task 1.4: Copy the pathname of your `lab-p1` folder.
1. Open your `cs220` folder in either File Explorer or Finder.
2. Copy the pathname of `lab-p1` using either these [Windows directions](https://www.pcworld.com/article/468873/windows-tips-copy-a-file-path-show-or-hide-extensions.html) or [Mac directions](https://osxdaily.com/2015/11/05/copy-file-path-name-text-mac-os-x-finder/).
3. Paste the pathname of `lab-p1` in your notes somewhere.
### Task 1.5: Navigate to the `lab-p1` directory.
**Directory** is just a fancy word for **folder**. You can use them interchangeably. The command to change directories is `cd`, which stands for **C**hange **D**irectory. The directions are the same for Mac and Windows. Type the following in the terminal (replace `LAB-P1-PATH` with the pathname of `lab-p1`, which you copied above; keep the quotes around the pathname, though) and hit Enter:
```
cd "LAB-P1-PATH"
```
So if my `lab-p1` pathname is "C:\Users\myname\Documents\cs220\lab-p1", I'd type:
```
cd "C:\Users\myname\Documents\cs220\lab-p1"
```
If you're using a Mac, your slashes will point the other way. This isn't a big deal... for now.
```
cd "/Users/myname/Documents/cs220/lab-p1"
```
**WARNING:** Remember to paste the pathname **inside quotes** (i.e., `cd "LAB-P1-PATH"` instead of `cd LAB-P1-PATH`).
Otherwise, you might get an error that says you have too many arguments.
### Task 1.6: Use `cat` to inspect hello.py.
Just type `cat hello.py` in your terminal and press Enter. As one might guess, `cat` command enables you to look into the contents of a file.
Wooooooooah! That's a lot of stuff. Wait a minute... we've been bamboozled! This is an HTML file! **Delete this impostor `hello.py` using your file explorer!**
So, this is the incorrect way of downloading files from GitLab, when you encounter any error while running your program which lists `\<!DOCTYPE html\>`, then you followed these wrong steps to download the program file.
### Task 1.7: Correctly Download a Python file
After noticing our mistake, let's download `hello.py` the **correct way**:
1. At the top of this page, Left-click on `hello.py`.
2. Right-click on the `Open raw` (<img src="images/raw_gitlab_button.png" width="30">) button. In Chrome/Brave, right-clicking the `Open raw` button looks like this:
<img src="images/raw_gitlab.png" width="300">
3. Choose `Save Link As...` (or similar).
4. Navigate to your `lab-p1` folder in the pop-up.
5. Ensure that you download file with proper extension:
* Windows users: ensure that `Save as type` is **not** `Text Document`, but is either `Python File` or `All Files`.
* MAC users: replace the `.txt` extension with `.py`.
6. Press Enter.
7. For image reference, please go back to Task 1.2.
8. Type `cat hello.py` in your terminal and press Enter. If you downloaded the file correctly, you should now see:
```python
print("Hello, World!")
```
**Warning**: Verify that your file is saved as `hello.py` and not `hello.txt` or `hello.py.txt`. Reminder: we recommend you use the Chrome browser to avoid issues (other browsers might automatically change extensions of files to `.txt` when downloaded).
------------------------------
## Segment 2: Run Your First Program
A **path** is how a computer knows where a file is, and a **pathname** describes a path. Put simply, a pathname is a more complete name for a file or folder.
`hello.py` is just a file name. `C:\Users\myname\Documents\cs220\lab-p1\hello.py` and `/Users/myname/Documents/cs220/lab-p1/hello.py` are pathnames. The former is a pathname on Windows and the latter on a MAC or Linux. Windows and MAC or Linux use a different path separator, that is `\` (Windows) and `/` on MAC or Linux. Eventually, you will learn how to handle the `\` (Windows) versus `/` difference in a generic manner.
### Task 2.1: Use a Shell command to list the contents of the `lab-p1` folder
The Shell command we will use is `ls` (LS, but lowercase) to **L**i**S**t and inspect the contents of the `lab-p1` folder.
Type `ls` in the terminal and press Enter. If you've done everything correctly so far, you should see the `hello.py` file that you downloaded in Task 2 listed.
### Task 2.2: Run `hello.py`.
Type `python hello.py` and press Enter. Note that you may need to instead type `python3 hello.py`, depending on your setup (on most Mac setups, you'll be typing python3). If everything is working correctly, you should see the following message printed:
```
Hello, World!
```
Congrats, you just executed your first Python program!
------------------------------
## Segment 3: Use Terminal to run Shell Commands
You should have a terminal open that's working in your `lab-p1` directory. You have already tried examples for three shell commands, which are: `cat <filename>`, `cd <pathname>`, `ls`. Shell is a program that is run by the terminal application. Shell commands are commands that you type within your terminal window.
Why learn about terminal?
- Typing on keyboard is way faster than using a mouse (also good for your wrist)
- Mastering Terminal will enable you to work on a remote or cloud-based computer
Let's learn more about shell commands.
### Task 3.1: Use `pwd` (Print Working Directory) to see your working directory.
In your Terminal, simply type `pwd` and press Enter.
You should see that the output looks something like `C:\Users\myname\Documents\cs220\lab-p1`.
The important part is the end: `cs220\lab-p1`.
Let's learn about two different types of paths:
1. Absolute paths:
* full path name to your file or folder.
* there can be only one absolute path to a single file or a single folder.
* example: `C:\Users\myname\Documents\cs220\lab-p1\hello.py` and `/Users/myname/Documents/cs220/lab-p1/hello.py`.
2. Relative paths:
* specifying the part of the path relative to current working directory (CWD).
* example: assuming you are inside the `Documents` directory (that is, you have executed `cd "C:\Users\myname\Documents"` or `cd /Users/myname/Documents`), then `cs220\lab-p1` or `cs220/lab-p1` are examples of relative paths to the `lab-p1` directory.
* unlike absolute paths, there could be multiple ways of specifying relative paths. Let's say you are inside `Documents\cs220` or `Documents/cs220`. Then the relative path to `lab-p1` will just be `lab-p1`. For the same `lab-p1` directory, we were able to use two different relative paths from two different locations.
### Task 3.2: Navigate to the parent directory of `lab-p1`, which is `cs220`.
Remember how we used `cd` to go directly to a specific directory using its absolute pathname? We can also use `cd` to move around the file system using relative paths. To go to the parent directory, we use:
```
cd ..
```
`..` is a special reference. It always refers to the parent directory of the current working directory.
### Task 3.3: Use `pwd` to verify that you are now in the `cs220` directory.
### Task 3.4: Use Shell commands to make a directory for next week's lab.
Use mkdir, that is **M**a**K**e**DIR**ectory, to *make* a `lab-p2` directory.
Here's how:
```
mkdir lab-p2
```
### Task 3.5: Use `ls` to verify that a `cs220` now contains `lab-p1` and `lab-p2`.
### Task 3.6: Navigate back to the `lab-p1` directory.
Since `lab-p1` is so close to our current(working) directory, we don't have to use the absolute pathname of `lab-p1`. We can use a relative path.
You can probably guess that we have to use `cd`. So let's type that first (without pressing Enter!), **followed by a space**:
```
cd
```
Now press Tab, that key right above your Caps Lock. What happens? Press it multiple times. You should see suggestions. This can be useful when navigating. This useful shortcut saves you a lot of typing time!
Either one of the following will work:
```
cd lab-p1
```
OR
```
cd .\lab-p1\
```
The above options are identical. `.\\` for Windows and `./` for Mac mean **relative to the current directory**. The `\\` or `/` *after* `lab-p1` indicates that `lab-p1` is a directory.
As an additional exercise, consider the following...
Windows:
```
ls ..\
```
Mac:
```
ls ../
```
What do you think this does?
------------------------------
## Segment 4: Use a Jupyter Notebook to run Python code
You'll be doing the majority of your coding this semester in Jupyter Notebook files. Let's make one.
### Task 4.1 Start up a Juptyer Notebook
You should have a terminal open that's working in your `lab-p1` directory. We will be creating a new Jupyter Notebook called `lab-p1.ipynb`.
In your terminal, type `jupyter notebook` and press Enter. This should open up Jupyter as a web page in your browser. If it doesn't, and you've already carefully followed our video install instructions (links at the top of this page), please **immediately ask for help** -- there are probably some tricky configuration issues remaining.
**WARNING:** Your Terminal window should now look something like this:
<img src="images/jupyter_shell.PNG" width="700">
Even though we'll be working in the web browser now, **NEVER** close this terminal window where you typed `jupyter notebook` until you're done -- if you do, Jupyter will crash and you will lose any unsaved work.
If you need to use other Terminal/PowerShell commands, **open a new Terminal/PowerShell window**.
If everything works properly, you'll see something like this (notice you can see your downloaded files in Jupyter):
<img src="images/jupyter.jpg" width="700">
Click `New`, then `Python 3`. A new tab like this should open:
<img src="images/new_tab.jpg" width="700">
Notice how it says `Untitled` at the top? Click that word, type `lab-p1`, then click `Rename` or press Enter.
### Task 4.2: Write and run code in your Jupyter Notebook
Jupyter notebooks have **cells** where you write code. Cells are these rectangular boxes where you can type code... We're going to write one line in a cell (copy-paste the below code):
```python
print("I'm done!")
```
To run this cell, click the `Run` button in the toolbar, which is highlighted green in the image below. You should see the output below the cell, as pictured.
<img src="images/jupyter_lab.jpg" width="700">
Alternatively, you can click `Kernel & Restart & Run All`.
### Task 4.3: Save your notebook.
Jupyter's autosave is quite unreliable, so make sure to frequently hit the save button. It is the **save icon**. The keyboard shortcut for this would be `command + s` or `ctrl + s`.
### Task 4.4: Download a different Notebook file.
Download `main.ipynb` using the same steps as Task 1.7. Make sure that the extension is `.ipynb`. Now refresh the browser tab containing the `jupyter notebook` homepage. You should be able to see `main.ipynb` in the list of files. Open `main.ipynb` and run the cell containing the code.
Most lectures will have live-coding examples, so you will be doing this step at the start of each lecture.
------------------------------
## Segment 5: Reading Code
Before we move onto the next program, let's have a look at `hello.py` again.
Remember that you are currently running Jupyter on your previous Terminal window.
So, you cannot execute any more Shell commands from that window. Instead, you must
**open a new Terminal/PowerShell window**, and navigate back to the `lab-p1` directory
on the new Terminal. Do **not** close the old Terminal/PowerShell window unless you want
to close your Jupyter notebook.
### Task 5.1: Use `cat` to see the contents of `hello.py`.
You should see the following:
```python
print("Hello, World!")
```
What you're looking at is the code for the `hello.py` program. Feel free to use `cat` in the following steps to view the code of other programs.
### Task 5.2: Download `double.py` to your `lab-p1` folder.
At this point, you should still have a terminal that is working in your `lab-p1` folder (aka directory).
### Task 5.3: List the contents of your `lab-p1` directory.
It should contain `hello.py`, `lab.ipynb`, `main.ipynb`, and `double.py`.
### Task 5.4: Run `double.py`.
Type the following and press Enter (Reminder: most of the MAC users will have to type `python3` instead of `python`):
```
python double.py
```
The program will say `please enter a number:`. This is known as a **prompt** (a fancy way to say a program is asking you a question).
Type `5` and press Enter. Make sure that the program tells you the answer is `10.0`.
### Task 5.5: Use Up Arrow to view previous commands
While using the terminal, press the up arrow key to scroll through the previous commands.
If you press the up arrow key once, the prompt should show `python double.py` again. You can use the up and down arrow keys to go through your command history. Press Enter to run `double.py`, and this time try entering a negative number.
Let's run `double.py` one last time, but now when you're prompted for a number, **enter the word `five`**.
Do you see something like this?
```
Traceback (most recent call last):
File "double.py", line 2, in <module>
print("2 times your number is " + str(2*float(x)))
ValueError: could not convert string to float: 'five'
```
When you see the word `Traceback`, **it means the program crashed**. The `double.py` program can only accept digits so it crashed when you typed something else. Eventually, we'll learn how to understand what gets printed when a program crashes to identify the root of the problem, but for now we won't worry about it any further.
----
Congrats on finishing your first CS220 lab! You do not need to submit anything for the labs. The labs are designed to prepare you for the project and help you engage in the lab sessions run by TAs and peer mentors. Your TA will come around to check that you finished and enter your lab attendance points on Canvas.
It is recommended that you try to finish P1 right away during lab hours, so you can fix any installation issues with the help of your TA and peer mentor. Good luck with [P1](https://git.doit.wisc.edu/cdis/cs/courses/cs220/cs220-s23-projects/-/tree/main/p1)!
x = input("please enter a number: ")
print("2 times your number is " + str(2*float(x)))
print("Hello, World!")
# Images
Images from lab-p1 are stored here.
lab-p1/images/finder_preferences.png

346 KiB

lab-p1/images/finder_preferences_settings.png

381 KiB

lab-p1/images/github.png

86.7 KiB

lab-p1/images/html_error.jpg

14 KiB

lab-p1/images/jupyter.jpg

47.3 KiB

lab-p1/images/jupyter_lab.jpg

18.4 KiB

lab-p1/images/jupyter_shell.PNG

62.4 KiB

lab-p1/images/mac-documents.png

76.1 KiB

lab-p1/images/new_tab.jpg

32.5 KiB

lab-p1/images/raw.png

66.3 KiB

lab-p1/images/raw_gitlab.png

112 KiB

lab-p1/images/raw_gitlab_button.png

4.52 KiB

lab-p1/images/save.png

77.9 KiB

lab-p1/images/save_mac.png

137 KiB

lab-p1/images/save_windows.png

23.7 KiB

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment