Due at 11:59pm on Friday, Sep 25 2020.
Starter Files
Adobe Creative Suite 6 Master Collection software is the ultimate design environment for realizing your creative vision. Use industry-leading tools with unlimited creative possibilities, precision, and power to deliver for print, the web, and the silver screen. Option -click (Mac) Alt -click (Win) a second time to zoom to that layer’s contents. 10) Library Support for Gradients – You can now add Gradients to the Libraries panel and access them accessed across multiple computers, and supported apps (including Illustrator and InDesign).
Download lab1.zip.Inside the archive, you will find starter files for the questions in this lab.
Submission
By the end of this lab, you should submit lab1.py to Gradescope. See details on Piazza
Introduction
This lab explains how to use your own computer to complete assignments for CS7, as well as introduce some of the basics of Python.
If you need any help at any time through the lab, please feel free to post on Piazza or the WhatsApp Discussion Group
This lab looks really long, but it's mostly setup and learning how to useessential tools for this class; these may seem a bit difficult now, but quicklybecome second nature as we move further into the course.
Install a terminal
The terminal is a program that allows you to interact with your computerby entering commands. No matter what operating system you use (Windows,macOS, Linux), the terminal will be an essential tool for CS7.
macOS/Linux
If you're on a Mac or are using a form of Linux (such as Ubuntu), you alreadyhave a program called Terminal
or something similar on your computer. Openthat up and you should be good to go.
Windows
For Windows users, we recommend a terminal called Git-Bash. You can try either method below:
Alternate (manual) method:
First, if you already tried the automatic installer above, make sure it's fully cleaned up:- Look for Git as an installed program in 'Add/Remove Programs', and, if it exists, uninstall it.
- Once Git is no longer installed, if a
C:Program FilesGit
folder still exists, delete that too.
Now download and install Git Bash.You can use the default options, with one exception:
Select Use Windows' default console window in theConfiguring the terminal emulator to use with Git Bash step.
This is very important! If you do not select this option, your terminal won't work with Python!
Install Python 3
Python 3 is the primary programming language used in this course.Use the instructions below to install the Python 3 interpreter.
(The instructions may feature older versions of Python 3, but the steps are similar.)
Linux
Run sudo apt install python3
(Ubuntu), sudo pacman -S python3
(Arch),or the command for your distro.
macOS
Download and install Python.Refer to this video for additional help on setting up Python.
You may need to right-click the download icon and select 'Open'. After installingplease close and open your Terminal.
Windows
If you used our automated installer successfully, skip to the next section—you should already have Python.
Otherwise, if you're installing manually, download Python andmake sure to check the 'Add Python 3.x to PATH' box,which will allow you to execute the python
command from your terminal.
After installing please close and open your Terminal.
Other
Download Python from the generic download page. (Scroll down to 'Files'.)
Install a text editor
The Python interpreter that you just installed allows you to run Pythoncode. You will also need a text editor, where you will write Python code.
There are many editors out there, each with its own set of features. We findthat Atom and Sublime Text 3 are popular choices among students,but you are free to use other text editors.
Note: Please, please, please do not use word processors such asMicrosoft Word to edit programs.
For your reference, we've also written some guides on using popular texteditors. After you're done with lab, you can take a look if you're interested:
Using the terminal
Let's check if everything was installed properly! First, open a new terminal window,if you haven't already.
When you first open your terminal, you will start in the home directory. Thehome directory is represented by the ~
symbol.
Don't worry if your terminal window doesn't look exactly the same; theimportant part is that the text on the left-hand side of the $
has a~
(tilde). That text might also have the name of your computer.
If you see your home directory instead of ~
: Try running echo ~
.If it displays the same path, that's also fine.
Python Interpreter
We can use the terminal to check if your Python 3 interpreter was installedcorrectly. Try the following command:
If the installation worked, you should see some text printed out about theinterpreter followed by >>>
on its own line. This is where you can type in Pythoncode. Try typing some expressions you saw in lecture, or just play around to seewhat happens! You can type exit()
or Ctrl-D
to return to your command line.
If you are using Windows and the python3
command doesn't work, try using justpython
or py
. If neither works:
Ask for help!
Organizing your files
In this section, you will learn how to manage files using terminal commands.
Make sure your prompt contains a $
somewhere in it and does not begin with >>>
. If it begins with >>>
you are still in a Python shell, and you need to exit. See above for how.
Directories
The first command you'll use is ls
. Try typing it in the terminal:
The ls
command lists all the files and folders in the currentdirectory. A directory is another name for a folder (such as theDocuments
folder). Since you're in the home directory right now, youshould see the contents of your home directory.
Changing directories
To move into another directory, use the cd
command. Let's try moving into yourDesktop directory. First, make sure you're in your home directory (check for the~
on your command line) and use ls
to see if the Desktop
directory is present.Try typing the following command into your terminal, which should move you intothat directory:
If your desktop directory is not located within your home directory and you can'tfind it, ask Eric for help.
There are a few ways to return to the home directory:
cd ..
(two dots). The..
means 'the parent directory'. Inthis case, the parent directory ofcs7
is your homedirectory, so you can usecd ..
to go up one directory.cd ~
(the tilde). Remember that~
means home directory, sothis command will always change to your home directory.cd
(cd
on its own). Typing justcd
is a shortcut for typingcd ~
.
You do not have to keep your files on your Desktop if you prefer otherwise.Where you keep your files locally will not affect your grade. Do whatever iseasiest and most convenient for you!
Making new directories
Cs7 For Mac
The next command is called mkdir
, which makes newdirectories. Let's make a directory called cs7
on your Desktop to storeall of the assignments for this class:
Photoshop Cs7 For Mac
A folder named cs7
will appear on your Desktop. You can verify this byusing the ls
command again or by simply checking your Desktop.
At this point, let's create some more directories. First, make sure you are inthe ~/Desktop/cs7
directory. Then, create folders called projects
and lab
inside of your cs7
folder:
Now if you list the contents of the directory (using ls
), you'll see twofolders, projects
and lab
.
Downloading the assignment
If you haven't already, download the zip archive, lab1.zip, whichcontains all the files that you'll need for this lab. Once you've done that, let'sfind the downloaded file. On most computers, lab1.zip
is probably located in adirectory called Downloads
in your home directory. Use the ls
command tocheck:
If you don't see lab1.zip
, ask a TA or lab assistant for help.
Extracting starter files
You must expand the zip archive before you can work on the lab files.Different operating systems and different browsers have different ways ofunzipping. If you don't know how, you can search online.
Using a terminal, you can unzip the zip file from the command line.First, cd
into the directory that contains the zip file:
Now, run the unzip
command with the name of the zip file:
You might also be able to unzip files without using the terminal by doubleclicking them in your OS's file explorer.
Cs For Mac
Once you unzip lab1.zip
, you'll have a new folder called lab1
whichcontains the following files (check it out with cd
and ls
):
lab1.py
: The template file you'll be adding your code to. Submit this file to Gradescope after this.
Moving files
Move the lab files to the lab folder you created earlier:
The mv
command will move the ~/Downloads/lab1
folderinto the ~/Desktop/cs7/lab
folder.
Now, go to the lab1
folder that you just moved. Try using cd
to navigateyour own way! If you get stuck, you can use the following command:
Summary
Here is a summary of the commands we just went over for your reference:
ls
: lists all files in the current directorycd <path to directory>
: change into the specified directorymkdir <directory name>
: make a new directory with the given namemv <source path> <destination path>
: move the file at the given source to thegiven destination
Finally, you're ready to start editing the lab files! Don't worry if this seemscomplicated -- it will get much easier over time. Just keep practicing!You can also take a look at this UNIX cheatsheet from Stanford University for a moredetailed explanation of terminal commands.
Python Basics
Expressions and statements
Programs are made up of expressions and statements. In very simple terms, anexpression is a piece of code that evaluates to some value and a statementis one or more lines of code that make something happen in a program.
When you type a Python expression into the Python interpreter, its value willbe printed out. As you read through the following examples, try out somesimilar expressions in your own Python shell, which you can start up by typingthis in your terminal:
Remember, if you are using Windows and the python3
command doesn't work, try using python
or py
. See the install Python 3 section for more info and ask for help if you get stuck!
You'll be learning various types of expressions and statements in this course.For now, let's take a look at the ones you'll need to complete today's lab.
Primitive expressions
Primitive expressions only take one step to evaluate. These include numbers andbooleans, which just evaluate to themselves.
Names are also primitive expressions. Names evaluate to the value that they arebound to in the current environment. One way to bind a name to a value is with anassignment statement.
Arithmetic expressions
Numbers may be combined with mathematical operators to form compound expressions. In addition to +
operator (addition), the -
operator (subtraction), the *
operator (multiplication) and the **
operator (exponentiation), there are three division-like operators to remember:
- Floating point division (
/
): divides the first number number by the second, evaluating to a number with a decimal point even if the numbers divide evenly - Floor division (
//
): divides the first number by the second and then rounds down, evaluating to an integer - Modulo (
%
): evaluates to the positive remainder left over from division
Parentheses may be used to group subexpressions together; the entire expression is evaluated in PEMDAS order.
Assignment statements
An assignment statement consists of a name and an expression. It changes the stateof the program by evaluating the expression and binding its value to the name inthe current frame.
Note that the statement itself doesn't evaluate to anything, because it's astatement and not an expression. Now, if we ask for a
's value, the interpreterwill look it up in the current environment and output its value.
Note that the name a
is bound to the value 75, not the expression(100 + 50) // 2
. Names are bound to values, not expressions.
Doing the assignment
Testyour understanding and make sure you have a good enough grasp on the topic tomake progress on the assignment.
Understanding problems
Labs will also consist of function writing problems. Open up lab1.py
inyour text editor. You can type open .
on MacOS or start .
on Windows toopen the current directory in your Finder/File Explorer. Then double click orright click to open the file in your text editor. You should see something likethis:
The lines in the triple-quotes ''
are called a docstring, which is adescription of what the function is supposed to do. When writing code in CS 7 and beyond,you should always read the docstring!
The lines that begin with >>>
are called doctests. Recall that when using thePython interpreter, you write Python expressions next to >>>
and the output isprinted below that line. Doctests explain what the function does by showing actualPython code: 'if we input this Python code, what should the expected output be?'
In twenty_twenty
,
- The docstring tells you to 'come up with the most creative expression thatevaluates to 2020,' but that you can only use numbers and arithmetic operators
+
(add),*
(multiply), and-
(subtract). - The doctest checks that the function call
twenty_twenty()
shouldreturn the number 2020.
You should not modify the docstring, unless you want to add your own tests!The only part of your assignments that you'll need to edit is the code and filling in your name and last name.
Writing code
Once you understand what the question is asking, you're ready to start writingcode! You should replace the underscores in return ______
with an expression thatevaluates to 2020. What's the most creative expression you can come up with?
Don't forget to save your assignment after you edit it! In most text editors, youcan save by navigating to File > Save or by pressing Command-S on MacOS or Ctrl-S on Windows.
Appendix: Useful Python command line options
When running a Python file, you can use options on the command lineto inspect your code further. Here are a few that will come in handy.If you want to learn more about other Python command-line options, take a lookat the documentation.
Using no command-line options will run the code in the file youprovide and return you to the command line.
-i
: The-i
option runs your Python script, then opens an interactivesession. In an interactive session, you run Python code line by line and getimmediate feedback instead of running an entire file all at once. To exit,typeexit()
into the interpreter prompt. You can also use the keyboardshortcutCtrl-D
on Linux/Mac machines orCtrl-Z Enter
on Windows.If you edit the Python file while running it interactively, you will need toexit and restart the interpreter in order for those changes to take effect.
-m doctest
: Runs doctests in a particular file. Doctests aresurrounded by triple quotes (''
) within functions.Each test in the file consists of
>>>
followed by some Python code andthe expected output (though the>>>
are not seen in the output of thedoctest command).