Introduction to CS21

Wednesday
In class exercises
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.
Creating accounts
The CS machines in the main lab (Sci240) as well as the machines in 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 terminal 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. To enable this feature, write the first five (5) digits located on the top right of the back of your student ID card on your user agreement form. If you do not have this information now, you can bring your number to Jeff Knerr later. If any students have an old ID that does not have an RFID tag, you can request a new ID free of charge at Ben West. Just tell them you need a new ID for CS lab access.

Course webpage
The course webpage is updated regularly and contains recent course announcements, links to inclass exercises, lab assignments, and practice quizzes (every other Thursday), and other less dynamic course information including office hours, ninja sessions, and python tips. Bookmark the website in firefox 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 assignments.
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 button 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

cumin[~]$
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 commands.
cumin[~]$ python
Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41) 
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>print "welcome to comp. sci."

welcome to comp. sci.
>>> 2+2

4
>>>4*5

20
>>> quit()
cumin[~]$ 
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.
Python basics
commands: print
strings: "hello", 'super bowl', '2008' #a data type
numbers: 1, -4, 3.5 #another data type
variables: x=4 #a named container for data. implicit type
functions: quit() #similar to a command. note parentheses

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.

Anatomy of a full program
Before we dive into more detail of computer science and python, we will cover some basic linux commands for navigating your file system.

Some common linux commands: cd, ls, mkdir, cp

licorice[~]$ cd
licorice[~]$ ls
Desktop/  Documents/
licorice[~]$ mkdir cs21
licorice[~]$ ls
Desktop/  Documents/ cs21/
licorice[~]$ cd cs21
licorice[cs21]$ ls
licorice[~]$ mkdir inclass
licorice[~]$ ls
inclass/
licorice[cs21]$ cd inclass/
licorice[inclass]$ ls
licorice[inclass]$ mkdir w01-intro
licorice[inclass]$ ls
w01-intro/
licorice[inclass]$ cd w01-intro
licorice[w01-intro]$ cp ~adanner/public/cs21/w01-intro/welcome.py .
licorice[w01-intro]$ ls
welcome.py
licorice[w01-intro]$ python welcome.py
welcome to cs21
licorice[w01-intro]$ vim welcome.py
We'll talk more about vim in a minute.

Code features: comment in triple block quotes. main function definition (note parentheses). def keyword. indented body. calling or invoking the function.

vim basics
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. 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:

cumin[~]$ vim

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"
Press Esc 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.

Navigate to your welcome.py file in your cs21/inclass/w01-intro folder. Open in vim. Edit to get/print name.

Lab 00, update21, and handin21
You'll get some practice with vim in Lab 0 which is due Thursday 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 and handin21. 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 and handin21 can submit the same lab multiple times. Only the most recent submission will be kept and graded.

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 to work for the next lab or change permissions on a directory. Trying to submit a lab assignment after the deadline and getting permission denied is one instance in which the error is not my fault. I'll leave it as an exercise for you to determine who is at fault in this case.

More python
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 "hello"+"there"
x=4
x
print "Python says x=",x
n=input("Enter a number: ")
print "You entered",n
name=raw_input("Enter your name: ")
print "Hello",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:

  1. First think about how you would solve the problem. no keyboards, mice, or even writing utensils. Just think.
  2. 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.
  3. Write your solution in python using vim.
  4. 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 brothers. Always do step 4 if you do step 3.
licorice[w01-intro]$ vim grad.py
licorice[w01-intro]$ python grad.py
Enter the current year: 2009
Enter your graduation year: 2012
You have 3 year(s) until graduation
Lists and loops
#List types. A list of what? 
range(5)
range(1,5)
range(1,5,2)
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>:
  <body>
Whitespace is significant. Loop body starts when indentation starts. Loop body ends when indentation ends.
for i in range(1,5,2):
  print i

for i in range(3):
  print "Hello there"
Tracing. Loop semantics. Your ready for lab 01.