Fall 2011 CSCI 220 Chapter 2

Software Development

 * Writing large programs can be a daunting challenge
 * Need a systematic approach
 * Programming Process is often broken down into stages:
 * 1) Analyze the problem
 * 2) Determine Specifications
 * 3) Create a design
 * 4) Implement a design
 * 5) Test/Debug
 * 6) Maintain the program

Problem
Develop a program that will translate Celsius into Fahrenheit using the temperature announced on the radio so that an appropriate outfit can be selected based on the weather conditions.

Specifications
What are we trying to accomplish?

Do we need speech recognition?

How about a robot to get the close out of the closet?

Design
Write our first pseudocode, which is precise English that describes what a program does. It is meant to communicate the algorithm without implementation details. Input the temperature in degrees Celsius (call it celsius) Calculate fahrenheit as (9/5)celcius + 32 Output fahrenheit

Implement the design (in class exercise)

 * 1) convert.py
 * 2) A program to convert Celsius temps to Fahrenheit

def main: celsius = eval(input("What is the Celsius temperature? ")) fahrenheit = 9/5 * celsius + 32 print("The temperature is", fahrenheit, "degrees Fahrenheit")

main

After completing the program we test our implementation
Try 0 degrees Celsius, which is 32 degrees Fahrenheit. Also, try 100 degrees Celsius, which is 212 degrees Fahrenheit.

Names

 * We give names to modules (e.g., chaos.py)
 * We give names to functions within modules (e.g., main)
 * We give names to variables (e.g., celcius)

Technically, these names are know as identifiers

Python like other languages have rules for their construction:
 * Must begin with a letter or underscore
 * That may be followed by any sequence of letters, digits, or underscores
 * Cannot contain spaces
 * Case sensitive

Some legal names:
 * x
 * celcius
 * SpamAndEggs
 * _celcius
 * Spam_And_Eggs

Good programmers always try to choose descriptive names

NOTE: Some identifiers are part of Python itself. These names are reserved words or keywords and cannot be used as ordinary identifiers. The complete list of Python keywords is Table 2.1.

Expressions
An expression is a fragment of program code that produces or calculates new data values.

Simplest kind of expression is a literal, which is used to indicate a specific values. Our convert program contains 9, 5, and 32. All of them are numeric literals. There are also string literals in our convert.py program. Note that the quotes are not part of the string. They just tell python to create the string.

Process of turning expression into underlying data type is called evaluation.

Some simple expressions: >>> 32 32 >>> "Hello" 'Hello' >>> "32" '32'

Simple identifiers as expressions: >>> x = 5 >>> x 5 >>> print(x) 5 >>> print(spam) Traceback (most recent call last): File "", line 1, in    print(spam) NameError: name 'spam' is not defined

More complex expressions using operators (spaces do not matter): >>> 3.9 * x * (1 - x) >>> 9/5 * celcius + 32

Python provides the normal set of operators:
 * addition is +
 * subtraction is -
 * multiplication is *
 * division is /
 * exponentiation is **

Python's operators obey the same rules of precedence that you learned in math class. You can modify the order of evaluation with parentheses, but only round parentheses.

Python can also operate on strings (i.e., concatenation) >>> "Dr. " "Paul " + "Anderson" 'Dr. Paul Anderson'