Welcome to CS21. This course is designed to have a mix of lecture and hands on in class lab exercises. This page will review some of the tools we will use during the labs and help you get set up with the system in the CS department.
The CS machines in the birds' nest (Sci256), main lab (Sci240), the overflow lab (Sci238) and the robot lab (Sci252) are on a separate network than the machines managed by ITS. You need a separate account with a separate password to access these machines. If you do not have an account, please see Jeff Knerr to get a user agreement form.
The terminal button looks like this and can be found on the bottom toolbar.
The first time you login, you will likely want to change your
randomly assigned password. Open a terminal window by clicking the
terminal button. At the $ prompt, type passwd, press
enter and follow the instructions. Note that when you are typing your
old or new password, nothing will be displayed to the screen
(no *'s and certainly not your password). The program will
not allow you to pick a password that it thinks is too short or too
easy to guess.
If you ever forget your password, you can reset it by visiting http://password.cs.swarthmore.edu.
Your student ID has an RFID tag that can allow you access to the building
and the labs after hours. This year, you should have automatically have access to the lab space by enrolling in the course. If you are having trouble with the card readers, please let me know.
Adding the course, switching sections
If you are looking to add this course or switch to a different section, please talk to Lauri Courtenay before you leave class today. Lauri manages the waitlist for all sections. We will try to provide you an answer to your registration related questions as soon as possible.
The course webpage
is updated regularly and contains
recent course announcements, links to in-class exercises, lab
assignments, and quiz topics for quizzes every other Thursday, and
other less dynamic course information including office hours, ninja
sessions, and python tips. Bookmark the website and refer to it
regularly. On the occasional occurrence that a lab or practice quiz is
not immediately available, it is sometimes useful to check if
the other sections
have the link posted. All sections
have the same practice quiz and lab assignments.
This semester, we are using Piazza
to manage course discussions and announcements. If you are registered for the course, you should get an email soon about registering for Piazza. You are encouraged to ask questions on the Piazza forums instead of sending me email directly. We'll talk a bit more about what to post and what not to post on Piazza later this week.
This course has a mandatory lab section. Attendance is required unless you have completed and submitted the lab for the week prior to the start of lab. Labs start Tuesday.
What is computer science?
Computer science focuses on two primary questions; what can be computed, and how efficiently can computers solve problems? The answers are more nuanced than "everything", and "really fast". At the core of the discipline is algorithms. Algorithms are concise descriptions of how to solve a problem computationally. Algorithms can be implemented in a programming language and interpreted by computer hardware to automate computation. Programming is NOT
the core of computer science. Programming is a way to automate and test the creative thought process that resulted in an algorithm. Programming is one tool at the disposal of computer scientists, but it is not the only tool. This course will teach you how to discover, develop, and write clear computational solutions to, often times non-computationally themed, problems. To check your thinking, you will also learn programming, debugging, and testing skills.
What is python?
If you have heard some examples of programming languages, you may not have heard of python, but it is gaining in popular for a number of reasons. It is relatively easy to learn, even for people not in computer science or related fields. It is free to download and it runs on many platforms including linux, Mac OSX, and Windows. It's just plain fun. Plus, we can get started right away.
Python shell vs linux shell
In this course (and many other CS courses) we will be using the terminal
window frequently. You can open a terminal window by clicking the terminal
on the toolbar on the bottom of the screen. The terminal window is sometimes called the console window, an xterm, a shell, a command shell, or my personal favorite, the black screen (if you adjust the preferences, your black screen may be a different color). These are all names for pretty much the same thing. Typing certain commands in the terminal tells the computer to perform certain actions. We will examine a few commands over the next few weeks, but there are literally thousands of possible commands you can run from the terminal. We will not cover all of them.
By default, the terminal starts in a linux shell with a prompt that looks (by default), something like
The name cumin
is the hostname or name of the computer, and is likely different for each student. This linux
prompt indicates that the terminal is ready to accept linux commands. Lab 0
asks you to explore some of these linux commands. Additional help is available during the using linux sessions this week. Plus, we'll be practicing the commands in class.
One command python tells linux to start the python shell.
This program changes the terminal prompt to
and now the
terminal is ready to accept python
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print("welcome to comp. sci.")
welcome to comp. sci.
In this mode, python commands are interpreted one line at a time and the result is printed to the screen. Typing quit()
in the python shell quits the python interpreter and returns the user to the linux shell. Learn to recognize what program is running by inspecting the prompt.
strings: "hello", 'grumpy bear', '2015'
#a data type
numbers: 1, -4, 3.5
#another data type
#a named container for data. implicit type
functions: print() quit()
Syntax describes the structure of a program and arrangement of symbols that a programming language understands. Semantics describes the meaning of the syntax. What happens when x=4 is typed?
When you quit the python shell, commands you entered are lost.
Eventually we will write programs longer than one line. We may even
want to keep some of them around and run them multiple times. We need
a way to edit and save programs for later. We'll use an editor
is one of many choices. If you
are feeling adventurous,
some others and pick your favorite. Editors are like ice cream; there
is no best flavor for everyone.
Start emacs by typing emacs at the linux shell:
When you start emacs, it will look a lot like a basic text
editing program you might find on a Mac or Windows computer. Many of
the features that you'd like to use are intuitive or can be deduced by
looking at your choices in the menu bar. However, as you become more
comfortable using emacs, you will find that it is quite a bit
more powerful than a basic text editor.
If you start emacs with a filename after the
word emacs, that file will open when emacs starts.
cumin[~]$ emacs first.py
loads, type the following into the window:
print("My first saved python program!")
You can now run your program by typing python first.py
linux prompt. If you run emacs first.py
again, you can edit
your program, make changes, add features, etc.
You can learn more about emacs by clicking on
the Help menu within emacs and selecting the Emacs
Tutorial. You can quit the tutorial and come back to it
later if you forget something or get bored.
Depending on how you are connected to the CS machines, you might
find that emacs starts without a fancy user interface. In
this case, you'll need to become proficient with the keyboard
shortcuts. You can find a reference to some of the most important
shortcuts on the
cheat sheet here.
Lab 00, update21, and handin21
You'll get some practice with emacs
in Lab 0
which is due Saturday at midnight. You'll be asked to write a short bio of yourself in a file called bio.txt
in a directory named cs21/labs/00
. You will also be using two commands regularly through the course to get and submit files for the course: update21
. Try to run them now. You should run update21
before starting each lab assignment and handin21
to turn in lab assignments. You can run them as often as you like. update21
will not clobber files you modify after running update21
can submit the same lab multiple times. Each handin submission is stored separately, but only most recent copy submitted prior to the deadline will be graded. You may continue to submit after the deadline, but these submissions will be ignored.
If update21 or handin21 does not work for you or it says
you are not allowed to run these programs, email me. It is usually my fault,
not yours. I may need to add you to the class roster, change handin or change permissions on a directory.
Let's examine the basics of python some more so we can get to the core of CS and start solving problems.
print("hello" + " there")
print("Python says x=%d" % (x))
nText=raw_input("Enter a number: ")
n = int(nText)
print("You entered %d" % (n))
critter=raw_input("Enter your favorite animal: ")
print("I would like to see a %s" % (critter))
Anatomy of a full program
Let's take a look at a full program. You should have already run update21
from the linux prompt. If not, do that now.
We will use the linux commands cd (change directory) and ls (list files) below.
Desktop/ Documents/ cs21/
licorice[~]$ cd cs21
examples/ inclass/ labs/
licorice[cs21]$ cd inclass/
licorice[inclass]$ cd w01-intro
licorice[w01-intro]$ emacs welcome.py
Some things to notice:
- a comment in triple block quotes at the top of the program
- a main function definition (note parentheses) defined
using the def keyword.
- indented body of the main function
- a call to invoke the main function
licorice[w01-intro]$ python welcome.py
!! Welcome to CS21 !!
Open in emacs. Edit the file so that it gets and prints a name.
Writing a full program
Suppose we are given the following problem: Have the user enter the
current year as a number and the year he or she plans to
graduate. With this information, have your program print how many more
college years he/she has left. We want to design an algorithm that can
solve this problem (this one is a bit easy) and then implement and
test our solution in python.
Steps to solving a problem computationally:
- First think about how you would solve the problem.
No keyboards, mice, or even writing utensils. Just think.
- Write or sketch a solution on paper. Discuss you solution
with a friend or neighbor. This solution does not have to be in python.
pseudocode is fine.
- Write your solution in python using emacs.
- Save, run, and test your solution. Go back and make changes if needed.
The real innovation in steps 1 and 2. Steps 3 and 4 are sometimes
skipped, argued logically or mathematically, or handed off to new hires,
grad students or younger siblings. Always
do step 4 if you
do step 3.
licorice[w01-intro]$ emacs grad.py
licorice[w01-intro]$ python grad.py
Enter the current year: 2015
Enter your graduation year: 2019
You have 4 year(s) until graduation
Lists and loops
#List types. A list of what?
Range returns a list. The syntax of a list is as follows: Lists are
enclosed in brackets [ ] and values in a list are separated by commas.
Here is the syntax of a for loop:
for <var> in <sequence>:
Whitespace is significant. Loop body starts when indentation starts. Loop body ends when indentation ends.
for i in range(1,5,2):
for i in range(3):
print "Hello there"