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 first time you login, you will likely want to change your randomly
assigned password. Open a terminal window by clicking the terminal button on the
bottom toolbar. 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.
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 inclass 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 in firefox or chrome 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
has the link posted. All sections have the same practice quiz and lab
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.3 (default, Aug 1 2012, 05:14:39)
[GCC 4.6.3] 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 called vim
is one of many choices. If you are feeling adventurous, explore
some others and pick your favorite. Editors are like ice cream; there is no best flavor for everyone.
Start vim by typing vim at the linux shell:
By default, vim starts in command mode. To actually type text, you need to
type i first to enter insert mode. Then you can type python code, a
letter to your mom, your top ten vacation destinations, this web page, etc.
Press the Esc key to exit insert mode and return to command mode. Type
:q! to quit without saving changes. Note we are returned to the linux
prompt. Usually we want to save changes to a particular file. We can start
vim with an a filename as an argument.
cumin[~]$ vim first.py
Enter insert mode (type i
) and type the following:
print("My first saved python program")
to exit insert mode and :wq
to save your program and quit. You can now run your program by typing python first.py
at the linux prompt. If you run vim first.py
again, you can edit your program, make changes, add features, etc.
You can learn more about vim by typing vimtutor at the linux prompt and following along. You can quit the tutor at any time and come back to it later if you forget something or get bored.
For a more graphical vim experience, you can also type gvim which
gives you a menu and some buttons. It's like training wheels for vim.
Vim reference cards are available in the lab and online. By no means do you need to memorize all of these commands. I would start with i, :q, :q!, :w, :wq and move on to dd, yy, x, p and others as needed. It sounds a bit insane at the beginning, but you'll get used to it, and feel free to ask if you have questions.
Lab 00, update21, and handin21
You'll get some practice with vim 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
Some more linux commands: cd, ls, mkdir, cp
Desktop/ Documents/ cs21/
licorice[~]$ cd cs21
examples/ inclass/ labs/
licorice[cs21]$ cd inclass/
licorice[inclass]$ cd w01-intro
licorice[w01-intro]$ vim welcome.py
comment in triple block quotes. main function definition (note parentheses). def
keyword. indented body. calling or invoking the function.
licorice[w01-intro]$ python welcome.py
welcome to cs21
Open in vim. Edit to get/print 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 and 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 vim.
- save, run, and test your solution. Go back and make changes if needed
Innovation in steps 1 and 2. Steps 3 and 4 are sometimes skipped, argued
logically/mathematically, or handed off to new hires, grad students, or little
do step 4 if you do step 3.
licorice[w01-intro]$ vim 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. Lists are enclosed in brackets [ ]. Values in a list are separated by commas. This is the syntax of a list.
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"
Tracing. Loop semantics. Your ready for lab 01.