Presentation is loading. Please wait.

Presentation is loading. Please wait.

2. WRITING SIMPLE PROGRAMS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)

Similar presentations


Presentation on theme: "2. WRITING SIMPLE PROGRAMS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)"— Presentation transcript:

1 2. WRITING SIMPLE PROGRAMS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)

2 Objectives To be able to understand and write Python statements to output information to the screen, assign values to variables, get numeric information entered from the keyboard, and perform a counted loop

3 The Software Development Process Source: http://en.wikibooks.org/wiki/Introduction_to_Software_Engineering/Process/Life_Cyc le#mediaviewer/File:SDLC_-_Software_Development_Life_Cycle.jpg

4 Example Program: Temperature Converter Analysis – the temperature is given in Celsius, user wants it expressed in degrees Fahrenheit. Specification Input – temperature in Celsius Output – temperature in Fahrenheit Output = 9/5(input) + 32

5 Example Program: Temperature Converter Design Input, Process, Output (IPO) Prompt the user for input (Celsius temperature) Process it to convert it to Fahrenheit using F = 9/5(C) + 32 Output the result by displaying it on the screen

6 Example Program: Temperature Converter Before we start coding, let ’ s write a rough draft of the program in pseudocode. Pseudocode is precise English that describes what a program does, step by step. Using pseudocode, we can concentrate on the algorithm rather than the programming language.

7 Example Program: Temperature Converter Pseudocode: Input the temperature in degrees Celsius (call it celsius) Calculate fahrenheit as (9/5)*celsius+32 Output fahrenheit Now we need to convert this to Python!

8 Example Program: Temperature Converter #convert.py # A program to convert Celsius temps to Fahrenheit # by: Susan Computewell def main(): celsius = eval(input("What is the Celsius temperature? ")) fahrenheit = 9/5 * celsius + 32 print("The temperature is", fahrenheit, "degrees Fahrenheit.") main()

9 Example Program: Temperature Converter Once we write a program, we should test it! >>> What is the Celsius temperature? 0 The temperature is 32.0 degrees Fahrenheit. >>> main() What is the Celsius temperature? 100 The temperature is 212.0 degrees Fahrenheit. >>> main() What is the Celsius temperature? -40 The temperature is -40.0 degrees Fahrenheit. >>>

10 EXERCISE 2.1 Modify convert.py so that it will take the degree in Fahrenheit and change it back to Celsius. A sample output: >>> What is the Celsius temperature? 100 The temperature is 212.0 degrees Fahrenheit. The temperature is 100.0 degrees Celsius. >>>

11 Naming the identifiers Names are given to variables ( celsius, fahrenheit ), modules ( main, convert ), etc. These names are called identifiers. Every identifier must begin with a letter or underscore ( “ _ ” ), followed by any sequence of letters, digits, or underscores. Note that “—”, “.” and many other symbols are not allowed. Identifiers are case sensitive. Identifiers cannot be Python’s keywords.

12 Python’s keywords

13 EXERCISE 2.2 Say, if you suspect that a word might be a Python keyword, how would you find out without looking up the table of the keywords?

14 Expressions The fragments of code that produce or calculate new data values are called expressions. A (numeric/string) literal, which is the simplest kind of expression, is used to represent a specific value, e.g. 10 or “rocky”. A simple identifier can also be an expression. Simpler expressions can be combined using operators +, -, *, /, and **. The normal mathematical precedence applies. Only round parentheses can be used to change the precedence, e.g., ((x1 – x2) / 2*n) + (spam / k**3). Try “i” + “love” + “you”.

15 EXERCISE 2.3 If you assign 5 to x and then type x, the shell will return 5 to you. What if you type y but without assigning any value to it before?

16 Output statements Recall from your previous print exercises: Each print statement will print their content on a different line. The items are separated by a single space Formally, print(*objects, sep=' ', end='\n', file=sys.stdout) To make it simple, print(,, …,, sep=' ', end='\n', file=sys.stdout ) sep : how are the items separated? end : how is the printed content ended? file : where is the content printed?

17 EXERCISE 2.4 o By setting sep appropriately, print out 1*2*3*4. o Use a print statement to print 1 and 2, and a second print statement to print 3 and 4. By setting end appropriately, 1, 2, 3, and 4 are printed on the same line.

18 Assignment statements Simple assignment: = variable is an identifier, expr is an expression The expression on the RHS is evaluated to produce a value which is then associated with the variable named on the LHS.

19 Behind the scene Python does not recycle these memory locations. Assigning a variable is more like putting a “ sticky note ” on a value and saying, “ this is x ”. The memory for the old value will be “released” automatically (i.e., garbage collection).

20 Assigning Input The purpose of an input statement is to get input from the user and store it into a variable. = input( ) E.g., x = eval(input(“Enter a temperature in Celsius: ”)) Print the prompt. Wait for the user to enter a value and press. The expression that was entered is evaluated and assigned to the input variable. Two kinds of input: character string or number

21 EXERCISE 2.5 o Prompt user for a number and then print it out using just input( ). o Prompt user for a number and then print it out using eval(input( )). o What is the difference between the two? o Remove eval() from convert.py and does it still run?

22 EXERCISE 2.6 Ask users to input two numbers and print out the two numbers in a reversed order.

23 Simultaneous Assignment Several values can be calculated at the same time.,, … =,, … Evaluate the expressions in the RHS and assign them to the variables on the LHS. E.g., sum, diff = x+y, x-y E.g., x, y = eval(input("Input the first and second numbers separated by a comma: "))

24 EXERCISE 2.7 Simplify your codes in exercise 2.6 using simultaneous assignment statements.

25 Definite Loops A definite loop executes a definite number of times, i.e., at the time Python starts the loop it knows exactly how many iterations to do. for in : The beginning and end of the body are indicated by indentation. The variable after for is called the loop index. It takes on each successive value in sequence. E.g., for i in range(10): x = 3.9 * x * (1 - x) print(x)

26 EXERCISE 2.8 o Replace range(10) by [0,1,2,3,4,5,6,7,8,9] and rerun chaos.py. Any difference? o Replace [0,1,2,3,4,5,6,7,8,9] with [0,1,3,5,7,9] and rerun chaos.py. Any difference?

27 Flow chart for the definite loop

28 END


Download ppt "2. WRITING SIMPLE PROGRAMS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)"

Similar presentations


Ads by Google