Loops, Numbers, and Strings

In class exercises
The goal for this week is to become comfortable with four basic data types in python: integers, floats, strings, and lists. By now you should be familiar with some basic linux (cd, ls); the vim editor; editing, saving, and running python files in your cs21 directory; recognizing the linux shell ($) and the python shell (>>>), and running basic python commands in the python shell. On the python side of things we talked about print, input() vs raw_input(), saving data in variables, and the basic structure of a program (descriptive comment, definition of function, calling the function). If you have any questions about these topics, please let me know. We will be building on them this week.
Lists
We start today by looking at a new data type: the list. Run the following range commands in the python shell. Remember to start python by typing python from the linux prompt.
cumin[~]$ python
>>> range(5)
[0, 1, 2, 3, 4]
>>> 
#List types. A list of what? 
range(5)
range(1,5)
range(1,5,2)
Think about the following questions and discuss them with a neighbor:
[2, 4, 6, 8]
[-1, 0, 1, 2, 3]
[0, 3, 6, 9, 12]
[1, 1.5, 2, 2.5]
[4, 3, 2, 1, 0]
[1, 2, 4, 8, 16]
Loops
Just generating lists can be pretty boring, but we can loop over a list to have python execute code multiple times. This construct is called a for loop and it has the following syntax:
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"
  print "i=", i
Tracing. Loop semantics. You can now finish lab 01.
Numbers
Two types: integers or whole number and floating point or decimal numbers.

Built in operations: + (addition), - (subtraction), * (multiplication), / (division), ** (exponentiation), % (remainder or mod), abs() (absolute value).

One tricky thing about python is that if a mathematical expression involves only integers, the result must be an integer. If an expression has at least one floating point number, the result is a float. Floating point numbers are approximations to real numbers. Usually this approximation is good enough (unless you are flying a spacecraft to Mars or trying to forecast the path of a Hurricane), but the answers may surprise you.

You may want to convert an int to a float. This can be done via casting using e.g., float(3). Alternatively, if we remember that an operation involving a float and an int returns an int, we can convert a possible integer variable val to a float using val=1.0*val, or val=1.*val.

You can import additional math functions from the math library.

>>> from math import *
>>> pi
3.1415926535897931
>>> sqrt(2)
1.4142135623730951
>>> sin(pi/4)
0.70710678118654746
>>> sin(pi/2)
1.0
>>> import math
>>> help(math) #displays all functions available to you. 

If you need to import additional feature use the from <library> import * at the top of your program. You only need to import a library once. If you want to get help on a library, start a python shell, run import <library> followed by help(<library>).

Strings
The string data type represents text. A string can be thought of as a list of characters, though there is a slight difference between a string and a list of characters. More on this later.

Python supports a few standard operations on strings including + (concatenation), * (duplication), and % (string formatting, more later).

List indexing and slicing
We can loop over any list and since strings are almost like a list of characters, we can can loop over strings:
>>> s="Swarthmore"
>>> for ch in s:
...   print ch
... 
S
w
a
r
t
h
m
o
r
e
Try the above loop in vim by following the instructions below.
lime[~]$ cd
lime[~]$ cd cs21
lime[cs21]$ ls
inclass/  labs/  solutions/
lime[cs21]$ cd inclass/
lime[inclass]$ ls
w01-intro/
lime[inclass]$ mkdir w02-numstr
lime[inclass]$ cd w02-numstr/
lime[w02-numstr]$ ls
lime[w02-numstr]$ gvim strings.py 
lime[w02-numstr]$ python strings.py
For any list, we can also use the function len() to determine the number of items in the list.
>>> len(s)
10
>>> values=range(3)
>>> len(values)
3
We can also select parts of a list using the indexing operator. Try the following statements in the python shell. What are the semantics of ls[0], ls[-1], ls[2:4], ls[:-1], and ls[2:]? Try some more examples to experiment.
ls=range(1,11)
s="Swarthmore"
ls[0]
ls[-1]
ls[2:4]
ls[:-1]
ls[2:]
s[0]
s[-1]
s[-4:]
s[:3]+s[4]

The primary difference between lists and strings is that lists are mutable while strings are not. Thus, we can change elements in a list, but not in a string.

print ls
ls[0]=2009
s[3]='a'
Number and letter representation
By now we know that python can save and store data in variable for future use. Additionally, vim and linux save our python programs in files for future use. Where does all this data go and how does the computer keep track of it all? For a full explanation, you would need to take one or more additional CS courses (Computer Organization or Operating Systems), but the short answer is that data is stored in the computers memory, which to an extremely rough approximation is like a sheet of graph paper where each cell can hold a single symbol. While we commonly work with the decimal digits 0-9, the letters a-z, and a few other common symbols, the computer really only understands two symbols: 0 and 1. These symbols can be represented as an electrical signal or bit that is either on (a 1) or off (a 0). Switches are easy to make, we can make them really small, and we can cram about half a billion of them into a single chip. A billion bits can store a lot of data, but it is still a finite resource. To represent data types such as integers, floating point numbers, and characters, we must encode these data types as a sequence of 1's and 0's. Integers are stored in binary format. Characters are stored using ASCII character codes. We can convert between characters and their ASCII equivalent using the functions ord and chr. A quick example is shown below. We will see how this is useful later.
>>> let='a'
>>> let
'a'
>>> ord(let)
97
>>> chr(97)
'a'
>>> chr(98)
'b'
>>> chr(ord('d')+18)
'v'
>>> chr(ord('z')+2)
'|'
>>> print "A"+1
Traceback (most recent call last):
  File <stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects
>>>quit()
A word of caution: Do NOT use ASCII character codes directly. There is no need to memorize them in python. You may want to memorize them for party tricks, but it is unlikely that you will gain many friends this way. If you need to know the ASCII character of 'c', use ord('c'), not 99. If I see a spurious 65, 90, 97, or 122 in your code, I will take off points. I assign no significance to these numbers aside from the fact that 65 is the number of teams in the Men's NCAA basketball tournament, which is not very useful in python. We'll see some interesting applications of ord() and chr() next week when we combine them with branching and Boolean operations.
The accumulator pattern
Time and weather permitting. We'll probably talk about this next week.

A design pattern is a generic method for solving a class of problems. The standard algorithm might be described as follows:

get input
process input and do computation
display output

Almost any computational problem can be set up in this very general way, but step two is a very vague. Let's look at another common pattern, the accumulator.

initialize accumulator variable(s)
loop until done:
  update accumulator variable(s)
display output
Many useful computational problems fit this pattern. Examples include computing a sum, average, or standard deviation of a list of numbers, reversing a string, or counting the number of times a particular value occurs in a list. Let's try to compute the average of a list of numbers entered by the user. Prompt the user to first enter the number of values he/she wishes to average and then prompt for each number. Finally display the average. Start with pseudocode, a written idea that organizes your thought process. Then write your solution in python and test.