Lab 1: Your first Python program

The purpose of this lab time is to give you an introduction to the basic tools we will be using this semester. These tools include a terminal, a text editor, and the Python interpreter.

You will also write your very first Python program then turn it in to practice submitting your weekly projects.


Weekly CS151 workflow

Labs. During each week's lab, you will get hands-on practice with concepts from lecture. Labs are also the starting point for the weekly project.

Projects. In the projects, you will get deeper experience working with concepts from lecture by writing Python code and running it to create outputs that you design, such as images.

Project report. You create a Google Doc with images from your project, along with a description of how you created them and what you learned. Labs and projects are tightly coupled.

Submission. Each week, you will see the current project posted on Google Classroom, along with the due date. To turn in your project, click on the post and attach:

The goal of today's lab is to give you to practice completing each of these steps.


Tasks

L1. Introduction to the Terminal

Let's spend a few minutes talking about the Terminal as a class.

L1a. Open up the Terminal

Open up the VS Code text editor.

Open up the Terminal built into VS Code by typing Control+` ( ` is the same key as ~ near the Esc key). Alternatively, you can click the Terminal menu and select New Terminal (Windows: top of the VS Code window. Mac: In the menubar at the top of your screen).

L1b. What is the Terminal?

The Terminal a text-based, keyboard-only interface of using your computer. This is an alternative way to use your computer compared to the graphical interface that you're used to where you click on things like icons, folders, buttons, etc. Voice assistants like Siri and Google Assistant are alternative interfaces to your phone. Anything you can do in the Finder, you can do in Terminal.

Why learn Terminal?

L1c. Directories and paths

Just like the Finder (Mac) or Windows Explorer (Windows), with Terminal you are always "in" a particular folder (directory) of your computer. The current directory is called your working directory. A terminal will usually start in the your home directory. On my Mac, this is:

/Users/hewolfe

If you are on Windows, it will look something like:

C:\Users\hewolfe

As you can see above, directories are separated by a slash. On Macs this is the forward slash /, on Windows this is a backward slash \. They have the same meaning (but you cannot substitute one for the other if you are on a Mac!).

Windows users: The following instructions are written with the forward slash convention, but on Windows forward and backward slashes are interchangeable, so you can still follow along exactly as things are written.

My home directory /Users/hewolfe is an example of a path, which is a complete description of where a directory or file on my computer. Paths usually are case-sensitive, so be sure to use proper capitalization (i.e. /Users/hewolfe has a different meaning than /USERS/hewolfe). You will use paths, and filenames to change directories, list the contents of directories, and run Python programs.

L1d. Useful terminal commands

Here are some important commands that you type into the Terminal to know for this class. You hit the Return key to actually run the command.

"Lifeline" commands

These three commands are super helpful and important for navigating the Terminal:

  1. pwd: Stands for "print working directory". It tells you where you currently are on your computer. For example, when you open the Terminal, usually you are in your home folder. Typing pwd then Enter will tell you this: /Users/hewolfe
  2. ls: Stands for "list" the files/folders in the current directory. See all the folders and files in the current folder that you are "in".
  3. mkdir <new folder name here>: Stands for "make directory". You type the command, followed by a space, followed by the name of the folder you want to make. This will create a folder in your current working directory.
  4. cd <type in a directory here>: Stands for "change directory". You type the command, followed by a space, followed by the directory or path you want to go to. Want to just move up one folder from where you are? cd ... Move up 2 folders? cd ../... etc. Here are some examples:
Command Meaning
cd ~ Set the current directory to your home directory. (The ~ character is shorthand for your home directory.)
cd .. Move up to the parent directory of the current working directory (one folder "up" fro where you are)
cd ../../ Move up to the parent of the parent of the current working directory.
cd blah Move into the sub-directory blah
cd /Users/hewolfe/Desktop Move to the Desktop directory give by the absolute path (starts with /).

L1e. Terminal tips and tricks

Use the "lifeline" commands all the time: As you are getting comfortable using the Terminal, always enter the pwd and ls commands to "see" where you are on your computer.

Hitting return anywhere: You don't have to be at the end of the line to press return. If you go back and edit something in a terminal command, you can press return and execute the whole line no matter where the cursor is located.

Drag folder icon into Terminal (Mac): The best tip. Often you have your project/lab folder in Finder and want to run the code in it. You need Terminal to "look at" the same folder to do this. Since opening usually Terminal starts you in your home folder (e.g. /Users/hewolfe for me), it can be a chore to cd your way to the project folder. There is an easier way. Type cd, space, then drag the blue folder icon at the top of Finder (next to your project folder name; see screenshot below) into the Terminal window. The correct path will magically appear and you can hit Return to move Terminal there!

Drag folder into Terminal (Windows): The best tip. Often you have your project/lab folder in Finder and want to run the code in it. You need Terminal to "look at" the same folder to do this. Since opening usually Terminal starts you in your home folder (e.g. C:\Users\hewolfe for me), it can be a chore to cd your way to the project folder. There is an easier way. Type cd, space, drag the folder into the terminal from Windows Explorer. The correct path will magically appear and you can hit Return to move Terminal there!

Tab completion: Big, big time saver. When you start typing the name of a file or folder, press the Tab key. It will finish the typing for you! After it finishes, you can keep typing a little more, then hit Tab again to have it complete more. Example: Typing cd /Us then Tab results in: cd /Users/. If I then type "o" then Tab, it gives me cd /Users/hewolfe/.

L1f. Create a Lab01 folder inside of a CS151 folder


L2. Introduction to Python turtle

Python is a simple yet powerful language for making computers do stuff. Many people have written modules for Python that do some sophisticated things. One of those modules is called turtle. We will use turtle extensively all semester!

Think of turtle as a virtual turtle that drags along a pen underneath it. As you command the turtle to move, the pen leaves ink on the virtual sheet of paper.

We can make the turtle move forward, backward, turn left, turn right, and pick up or put down the pen using the following commands.

You can find out about more turtle commands on the Python documentation site.

L2a. Write your first Python program!

Text editors are the workhorse programs for writing code. Unlike Google Docs and Microsoft Word, we don't want fancy fonts or formatting. In lecture and lab, we will use the Microsoft Visual Studio Code editor (VS Code for short). Let's use VS code to write our first Python program.

Do not write code in Microsoft Word or Google Docs!

Now let's write your first Python program! Your goal is to use turtle to draw a hexagon:

  1. Open VS Code.
  2. Create a new code file from within VS Code (Cmd+N).
    Windows users: Always substitute Cmd for the Control key.
  3. Import the turtle module. On the first line of your code file, write the command:
    from turtle import *
    This will make all of the turtle commands available to you (i.e. allow you to instruct the turtle to draw for you.)
  4. On the last line, write:
    exitonclick()
    This will keep the turtle window open until you click on the turtle window that pops up.
  5. Save your file (Cmd+S). Name it hexagon.py and place it in your Lab01 directory in your CS151 folder. The .py is important — it tells your computer that this will be a Python code file.
  6. Have the turtle draw a hexagon using the right and forward turtle functions commands. Place only one command per line, in between the import and exitonclick commands.
  7. Instruct Python to execute (run) the program. To run your program that draws a hexagon, in Terminal type:
    python3 hexagon.py
    Windows users: Whenever you see python3 blah.py type instead py blah.py.
  8. If something does not work out, before moving on, change your code, save your file (Cmd+S) and run it again in the Terminal. Update and run your code until your turtle draws a hexagon.

Congratulations, you just ran your first Python program! (Python Humor)

Didn't work? Here are some tips:

from turtle import *

forward(100)
right(90)
forward(50)

exitonclick()

L2b. Why is exitonclick the last command?

In hexagon.py, temporarily remove the last line exitonclick(). What happens?

After you see what it does (by removing it), add it back in.


L3. Google Classroom and written project reports

For each project, you will write a brief report in Google Docs and submit it on Google Classroom.

Each report consists of:

  1. Header At the top of the page should be your name, the date and the class.
  2. A useful Title, like "Name's CS151 Lab 0: My First Python Program" so that the page content is clear from the title.
  3. Abstract: A one paragraph summary of the project.
  4. Results: Images showing what you created, alongside a description of what each image is and how you designed/created it with code.
  5. Art Reflection: A brief statement (1-3 sentences) about which artist or artwork inspired your composition(s) and how it influenced your design decisions.
  6. Conclusion: A brief description (1-3 sentences) of what you learned.
  7. Extensions: A description of any extensions you undertook, including text output and/or images demonstrating those extensions.
  8. AI Usage Statement: A statement on if you used AI on this project. Reflection on how you used AI for the project including software used. If you did not use AI for this project, reflect on how you may have used AI in general this past week. Include all prompts that you used and how you incorporated them into your project or report.
  9. Acknowledgments: A list of websites that you used and people you worked with, including TAs, and professors.

All this will become second nature in a few weeks and is summarized in each project rubric on Google Classroom.

Let's turn in a "mock" lab report with a screenshot of your hexagon to help you get the hang of it. This is only practice, going forward you will not need to turn in labs, only projects.

L4a. Take a screenshot of your turtle creation

Take a screenshot of your hexagon.

Mac users

You can easily take a screenshot of your turtle creations to include in your reports by pressing Shift-Command-4. When you press those keys on your keyboard, a crosshair will pop up. Click and drag to draw a box around your hexagon. When you let go, a screenshot of the region that you selected gets saved to your Desktop.

Windows users

Follow the "Mouse and keyboard" directions to use the Windows Snip Tool to take screenshots. You want to use the Rectangular or Window option. With the Rectangular option, you click and drag a box (while holding down the click) around your hexagon when the screen dims. Let go when you are satisfied. Once you see your screenshot preview in Snip Tool, save it to your Desktop (File -> Save As).

L3b. Create a Lab 1 report

  1. Log onto Google Classroom, then navigate to our CS151 class webpage.
  2. At the top of the Google Classroom webpage, click "Classwork", click on the current assignment (e.g. Lab 1) under Projects, then click "View Assignment".
  3. In each assignment, you will see a rubric. Click to open it. This is today's Lab 1 rubric (real project rubrics will look very similar). This is how we will grade your projects. Take a look through it, but you should not change anything. The ungraded rubric will get turned in along with your report when you are ready to submit your project. When your project is graded, the rubric will be returned to you, show you your score, and have comments about your project.
  4. Go back to the Lab 1 assignment in Google Classroom. Read the instructions and click "Add or create" on the right side and select Google Doc to start writing your report.
  5. This should open a new Google Doc. Give your report a meaningful title like "Otto's CS151 Lab 1: My First Python Program".
  6. Add sections that should be in every CS151 report: Abstract, Results, Art Reflection, Conclusion, and Acknowledgments. The Abstract, Art Reflection, Conclusion, and Acknowledgments sections should be blank — do not write anything in them today.
  7. In all of you project reports, you will need to include pictures of the images you created with your program. In the Results section, Drag in your screenshot of your hexagon from your desktop into the Google Doc. Resize/crop/format the image as necessary in your report.
  8. Always create a caption under your images describing what it is and how it was made. Underneath your hexagon image, write "I created a hexagon shape with Python using the turtle forward and right commands!"
  9. Notice the Turn In button on the top-right corner of the Google Doc. Don't click this button, otherwise you will turn in your project to be graded!

L4. Submitting a project

Let's upload your code in the proper format to Google Classroom and turn in your lab. You should follow the same steps when turning in your projects.

Create a zip file of your project:

macOS: Right-click the lab folder (e.g. Lab01) and click Compress "Lab01".

Windows: Right-click the project folder (e.g. Lab01) and click "Compressed (zipped) folder" in the "Send to" menu.

In the Lab 1 assignment on Google Classroom, click "+ Add or create" > click "File" > click the "UPLOAD" tab > drag in your .ZIP folder. It should upload and bring you back to the Lab 01 assignment page. It may ask you if you turn in your Google Doc lab report, .ZIP file, and rubric. You can click "Turn in". If you don't want to submit yet, click "Cancel", it will bring you back to the assignment page again. You can always click the "Turn in" button on the right whenever you are ready.

Click "Turn in" to officially submit your lab!

Get started with project

Once you are done with each of the lab tasks, you may start on the project.


© 2021 Hannen Wolfe and Oliver Layton