$ update21
inclass/w03-decisions/broken.py
inclass/w03-decisions/cold.py
inclass/w03-decisions/formatting.py
inclass/w03-decisions/isAlpha.py
inclass/w03-decisions/isOdd.py
inclass/w03-decisions/speeding.py
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) print(item)
>>> let='a' >>> let 'a' >>> ord(let) 97 >>> chr(97) 'a' >>> chr(98) 'b' >>> chr(ord('d')+18) 'v' >>> 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.
x=True y=FalseNote, 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 x=7 (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"
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 <condition>: <body> if <condition>: <body> else: <body> if <condition>: <body> elif <condition>: <body> else: <body>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) else: print "%d is at least above 0" % (temp)
$ python isOdd.py Please enter a number: 0 0 is even $ python isOdd.py Please enter a number: 1243678 1243678 is even $ python isOdd.py Please enter a number: 2371 2371 is odd
$ python isAlpha.py Enter a single keyboard character: 3 '3' is a decimal digit $ python isAlpha.py Enter a single keyboard character: a 'a' is a lowercase letter $ python isAlpha.py Enter a single keyboard character: Q 'Q' is an uppercase letterWhat if the user enters more than one character? Can you inform them politely on how to run the program correctly?