$ update21
Creating /home/adas/cs21/inclass/w03-decisions
Adding /home/adas/cs21/inclass/w03-decisions/speeding.py
Adding /home/adas/cs21/inclass/w03-decisions/isOdd.py
Adding /home/adas/cs21/inclass/w03-decisions/isAlpha.py
myInt = 86 myFloat = 1./3 print "The value", myInt, "is an integer but ", myFloat, "is not"We can get more control over how data values are displayed using string formatting. See Section 4.5.2 on page 102 for more examples.
myInt = 86 myFloat = 1./3 print "The value %d is an integer but %0.2f is not" % (myInt, myFloat)Using this new style requires a few steps. First, set up a string that contains one or more formatting tags. All tags have the form %<width><.><precision><type-char>. The type-char is s for strings, d for integers (don't ask why), and f for floats. Do not put any variable names in the format string, just tags which serve as placeholders for data that will come later. After the format string put a single % symbol after the close quote. Finally, place the data elements that you wish to substitute for the tags separated by commas and enclosed in parentheses. Let's step through a few examples in the python shell.
>>> month="Sep" >>> day=17 >>> year=2012 >>> print "Today is %d %s %d" % (day, month, year) Today is 17 Sep 2012 >>> print "Tomorrow is %d/%d/%d" %(9,day+1, year) Tomorrow is 9/18/2012 >>> for val in range(1,200,20): ... print "%7.2f" % (val*6.582118) ... 6.58 138.22 269.87 401.51 533.15 664.79 796.44 928.08 1059.72 1191.36
>>> 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 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.
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=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)Boolean operators: <, >, <=, >=, ==, !=, and, or, not Why is == a Boolean operator instead of just =? Think syntax and semantics.
$ 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?