Decision structures

In class exercises
$ update21
Print formatting
print("*%s*%s*" % ("hello", "there"))
print("*%10s*%10s*" % ("hello", "there"))
print("*%3s*%10s*" % ("hello", "there"))
print("*%-10s*%-10s*" % ("hello", "there"))
print("*%10f*%10s*" % (2.34656, "miles"))
print("*%10.2f*%10s*" % (2.34656, "miles"))
print("*%.2f*%10s*" % (2.34656, "miles"))

item="%-10s $%.2f" % ("coffee", 1.4)
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 (See CS31: Introduction to Computer Systems), but the short answer is that data is stored in the computer's 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
>>> ord(let)
>>> chr(97)
>>> chr(98)
>>> chr(ord('d')+18)
>>> chr(ord('z')+2)
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.
Boolean type
In addition to the basic types int, float, string, and list, python has support for a Boolean type. A Boolean can have one of two possible values: True or False.
Note, there are no quotes around the values True and False.

Relational Boolean operators: <, >, <=, >=, ==, !=. Relations Boolean operators compare elements of a similar type and return a Boolean value. Try to guess the answer to the expressions below. Then test your hypothesis in python

3 < 4
3 <= 4
3 > 4
3 >= 4
3 == 4
3 == 3
3 != 3
3 != 4
"apple" < "zebra"
"apple" < "Zebra"
"zebra" == "Zebra"
"y" < "and"

3.2 < 4
4 < 3.2
3 < 3.00000000001
3 < 3.000000000000000000001

"4" == 4
"4" < 4
4 < "4"
4 < "3"

Logical Boolean operators: and, or, not. Logical Boolean operators compare and and or compare two Boolean expressions and return a Boolean value. The operator not negates a single Boolean expression.

True and True
False and False
True and False
True or False
True or True
not True
not False

(3 < x) and (x < 5)
(3 < x) or (x < 5)

Why is == a Boolean operator instead of just =? Think syntax and semantics.

Contains Boolean operator: in

3 in [1, 2, 3]
3 in range(5)
3 in "123"
"3" in "123"
"23" in "123"
"awesome" in "awesomesauce"

Decision structures
We'll be looking at some in class examples for decision structures today that will require you to copy some files from my directory.

Change into your inclass directory.

[~]$ cd
[~]$ cd cs21/inclass/
[inclass]$ ls
w01-intro/  w03-decisions/
If the w03-decisions directory does not yet exist, create it using update21
if, else, elif
Sometimes we need to make decisions based on various input parameters or conditions in our programs. Python supports automated decision making using if, else, elif syntax.
if <condition>:

if <condition>:
if <condition>:
elif <condition>:
The condition must be an expression that evaluations to True or False. The term Boolean is used to describe a type of data that can be either true or false.
temp=float(raw_input("Enter the current temperature in Fahrenheit: "))
if temp <= 0: 
  print "%d is cold. Brrr!" % (temp)
  print "%d is at least above 0" % (temp)
Never odd or even?
The first program we will look at is Modify the program so that it prints out if a number entered by a user is odd or even. Think about how the mod or remainder operator % can be used with an if statement to solve this problem. Some sample runs are shown below.
$ python 
Please enter a number: 0
0 is even

$ python 
Please enter a number: 1243678
1243678 is even

$ python 
Please enter a number: 2371
2371 is odd
What did you type?
Next open and modify the program so that it determines if a single character (a length 1 string) entered by the user is an uppercase letter, a lowercase letter, a decimal digit, or none of the above (usually punctuation). This program relies on python's ability to compare two strings using the operators < >, ==, <=, >=, and !=. Try it! Comparisons are done using the ASCII character codes. If you want to quickly see what happens, try a few example in a separate python shell. Some example runs of a sample program are shown below.
$ python 
Enter a single keyboard character: 3
'3' is a decimal digit

$ python 
Enter a single keyboard character: a
'a' is a lowercase letter

$ python 
Enter a single keyboard character: Q
'Q' is an uppercase letter
What if the user enters more than one character? Can you inform them politely on how to run the program correctly?