CS5: Introduction to Computer Science at Harvey Mudd College
CS5 Web > Homework0Gold > Orientation
Submissions: CS submission site

Lab 0: Terminal, Text-editing, and Python


Welcome to CS 5, Lab 0!

This lab is worth 15 points, and is best if done individually, so that you know Python works on your computer! That said, you're welcome to pair up on this, too.


Welcome! This is the Web page of the first problem (the lab problem) in CS 5.

The goals of this lab are:

If you finish early, you're welcome to head off to other things, or feel free to push ahead to work on more of the homework.



Software installing!


Installing CS5's software

What software are we using?

CS 5 uses some software you might not already have.

The default software we use is


[Task 1]    Download and install VSCode

Visual Studio Code (VSCode) is a free text editor, widely used for editing software source code.


As you wait for downloads and installs, read over—and sign—the honor-code and appropriate-use agreement, here:


CS's appropriate-use agreement   Waiting for a download...? Read this over...!

We <3 the honor code.

The HMC and Claremont Colleges' honor codes, of course, hold for CS work and activities as they do everywhere.

Because CS maintains a full set of computers, software resources, and accounts, our department clarifies how the honor code applies to those computing resources with an appropriate-use policy.


Thus, we ask every student who takes a HMC CS course to complete this form:

    CS appropriate-use policy (a Google Form).

Note: because this Web form is open to all email addresses, you will need to complete one of the "captcha"-type visual-recognition challenges to submit it.     Concerns? Let us know.


Forms filled? VSCode installed?   Onward to file-editing





Using VSCode as an editor

Try out VSCode, a text editor


Next, we'll get things arranged for efficiency—when using a programming language, ensuring that the right windows are simultaneously visible is a huge help!


Arrange things!    This is extra important!


Next, you'll install Python...

[Task 2]    Download and install Python

Python is a popular language for lots of things. Its list-of-uses seems to grow each year.


Let's test it!    We'll run Python within a terminal. Next, you'll open a Terminal Window...


The Terminal Window

Terminal Window?

Most of our interactions with computers are through the windows provided by the OS. The "OS," short for Operating System, is usually either Windows or Mac OS, although there are many others, Linux being the most common. The OS you use provides a windowing system that lets you use a mouse, interact with the computer in an intuitive fashion, and order Starbucks through PostMates. The click-and-drag interface of today's windowing systems is definitely a "good thing"!

However, the graphical interface is also a curtain—one that disconnects users from what's happening with the various files on their device. It's a flexible and useful skill to have a clear picture of how files are being used behind the windowing system's curtain. The Terminal is a program that gets "behind" that curtain. It uses textual commands, the so-called "command line," to handle files and actions on your machine.

Feeling comfortable around the command line is essential for many pathways that create computation. The OS already handles consuming computation brilliantly!


Start your terminal!

So, to pull back your OS "curtain," go to your VSCode text-editor and, among the menus, choose View and then Terminal. You should see a split screen:

The upper half is still the text editor.

The lower half is now your terminal window. The terminal goes by many names:

If this is your first time using the terminal, yay! Our plan is to install a Python library using it. That's next:

Running python3 at the terminal ...

Here's a picture of running the python3 command, at the terminal, on a Mac:



If python3 - or python - is working, stupendous! If not, ask!


Once python is happy, our plan is to install a helpful Python library. That's next:

Installing the ipython library ...

Throughout cs5, we use an interface known as ipython or interactive python

Here's a picture of the ipython install command, in a terminal, on a Mac:


Let's install it!

How to tell if it's worked?

What if it didn't work?!


Ok! It worked! But, what just happened?!

The above is an example of a common practice with Python (and all programming languages):

Thoughts? Concerns? Ask us!


Start ipython...

Here's a picture of running ipython in a terminal, on a Mac.

Whether you're on a Mac or Windows, try it!

If it worked, you're set!    If not, ask!   🦔   ☕


If ipython isn't found, then


Next, you'll get more familiar with the command-line

The command-line

Everything you can do with the windows of your OS, you can do in a terminal at the command line.   (Actually, you can do much more with the command line... .)

For CS5, you'll need to know three terminal commands. We'll dedicate a short section to each. Here's a preview:


pwd

First, the prompt. The prompt is the bit of text on the left that's waiting for you to type something to the command line:

The pwd command is short for print working directory. It prints your current location (folder). Try it:

You'll see the location at which your terminal and command line are currently running. Unless your name is really similar to Zach's, your results will be different!

The output is the name of the folder at which I'm currently located in the terminal. "Folder" and "directory" are the same thing.

The slash characters / show subfolders. On Windows, they're usually backslashes \ or double backslashes: not an important difference.

Thus, I'm currently in the subfolder named zdodds, within the folder named Users on the drive named C: (Windows likes to put colons after the names of disk drives).
This folder, C:\Users\zdodds , is my "home directory."

You'll likely be in your own home directory (and you'll see its full pathname—the name the OS uses to identify it uniquely).


Next, we'll see what's around with the ls command... .


ls      the list command

The ls command stands for list.

Running ls lists everything in your current directory. For Zach, typing ls does this:

The output is a list of all of the files and (sub)folders in the current directory. (Remember, "directory" and "folder" are the same thing—the terms are used interchangeably).

Since this is Windows, you're seeing lots of the default subfolders in every Windows home directory, plus a few extra things. On a Mac, there will be some differences:

Try it to see the list of names of the files and subfolders in your current directory at the terminal.


Next, you'll "move around" from directory to directory with cd... .


cd      the change directory command

The cd command is the most important. It stands for change directory.

The cd command lets you move from your current folder to other folders (directories) around your computer. To use it, you need to have a place to go!

We'd recommend the Desktop... so, type cd Desktop    No Windows Desktop? Try cd OneDrive first, then cd Desktop


Here's an example:

Not much happened...until you type pwd and see you're in a new place:

Also, type ls and there could be a lot more files—well, depending on how cluttered your Desktop is! Try it! Here's a bit of mine:

Your filage may vary!


Next, you'll move "up" within the directory structures...


cd ..      moving "up" one folder

Ok! You cd'ed to the Desktop—how do you get back?!

The special symbol of two periods in a row    ..    means "the next directory up."

So, if you type cd ..    and hit return, you will be back in the folder that contains your Desktop. Try it:

Then, try pwd and ls. You might notice that two of the directories listed are .. (the next directory "up") and . which refers to the current directory.


Practice!


Try these—at least for your setup. You'll find that Mac doesn't show the directory by default. Use pwd in that case!


That's it! You're set for the command line. There are very worthwhile shortcuts that make the command line much more efficient—more efficient than the drag-and-drop windows interface! For instance,


Ok! We're ready for Python. We'll use ipython next.


Python!

Python is a programming language, sometimes called a scripting language. Scripting languages have an interactive command line.

In fact, you've been using the Python command line!

Your ipython command line is a place to experiment with the Python language.

Here it is

If everything is working so far, try some larger computations...

Try computing a googol (ten to the hundredth power). Google is (loosely) named for this number.

All right... but can we go further! (Note: this is actually NOT a good idea)    So, only if you're feeling reckless or angry at your computer, you can distress-or-crash ipython, and maybe your whole machine, by asking it to compute a googolplex, which is ten to the googol power:


Continuing for the less reckless: now, copy-and-paste (or type) this line of Python code:

Extending this idea is the next—and primary—challenge of this week's lab!


First, we'll see how to use a file to hold our Python code, so that it can be submitted...


Running Python from a file

We already have a text editor open!

So, in a new tab, create a new empty file (menus: FileNew File)

Then, into this new empty file paste (or type)


# CS5 Gold: Lab 0, Problem 1
# Filename: hw0pr1.py
# Name: 
# Problem description: The four fours challenge!

from math import *

print("Zero is", 4+4-4-4)

print("sqrt(9) is", sqrt(9))  # This is "cheating" - there are no fours!
print("factorial(4) is", factorial(4))  # This may be helpful (you need four fours...)


Be sure to enter your name on the third line! Then save this file as hw0pr1.py on your Desktop.


Your task: Four fours!



Submitting your hw0pr1.py file


Lab's complete!

We encourage you to get started on Problem 2, hw0pr2rps, the Rock-Papers-Scissors problem and its sibling, The Adventure.

Let us know if you have any questions, concerns, or thoughts!