$ update21
Creating /home/adas/cs21/solutions/w02-numstr
Adding /home/adas/cs21/solutions/w02-numstr/average.py
Adding /home/adas/cs21/solutions/w02-numstr/average_word.py
Change into your inclass directory.
[~]$ cd [~]$ cd cs21/inclass/ [inclass]$ ls w01-intro/ w02-numAndString/ w03-decisions/If the w03-decisions directory does not yet exist, create it using mkdir
[inclass]$ mkdir w03-decisionsCopy the sample files for today from my directory using cp
[inclass]$ cp ~adanner/public/cs21/w03-decisions/* w03-decisions/ [inclass]$ ls w01-intro/ w02-numAndString/ w03-decisions/ [inclass]$ cd w03-decisions/ [w03-decisions]$ ls Alpha.py isOdd.py speeding.py [w03-decisions]$
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.
Try a few examples of conditions in the python shell
Make sure you are in the inclass/w03-decisions directory and open cold.py in gvim.
temp=input("Enter the current temperature in Fahrenheit: ") coldTemp = 0 if temp <= coldTemp: print "Brrr!",temp,"is cold." else: print "At least",temp,"is above",coldTempBoolean 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?
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="Feb" >>> day=3 >>> year=2009 >>> print "Today is %d %s %d" % (day, month, year) Today is 3 Feb 2009 >>> print "Tomorrow is %d/%d/%d" %(2,day+1, year) Tomorrow is 2/4/2009 >>> 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