1.10.Exercises 21 c)it has many solutions d)it is not practical to solve 4.Which of the following is not an example of secondary memory? a)RAM b)hard drive c)floppy d)CD-Rom 5.Computer languages designed to be used and understood by humans are a)natural languages b)high-level computer languages c)machine languages d)fetch-execute languages 6.A statement is a)a translation of machine language b)a complete computer command c)a precise description of a problem d)a section of an algorithm 7.One difference between a compiler and an interpreter is a)a compiler is a program b)a compiler is used to translate high-level language into machine lan- guage c)a compiler is no longer needed after a program is translated d)a compiler processes source code 8.By convention,the statements of a program are often placed in a function called a)import b)IDLE c)program d)main 9.Which of the following is not true of comments? a)They make a program more efficient b)They are intended for human readers c)They are ignored by Python d)In Python,they begin with a pound sign(#) 10.The items listed in the parentheses of a function definition are called a)parentheticals b)scripts c)comments d)parameters Discussion 1.Compare and contrast the following pairs of concepts from the chapter:
1.10. Exercises 21 c) it has many solutions d) it is not practical to solve 4. Which of the following is not an example of secondary memory? a) RAM b) hard drive c) floppy d) CD-Rom 5. Computer languages designed to be used and understood by humans are a) natural languages b) high-level computer languages c) machine languages d) fetch-execute languages 6. A statement is a) a translation of machine language b) a complete computer command c) a precise description of a problem d) a section of an algorithm 7. One difference between a compiler and an interpreter is a) a compiler is a program b) a compiler is used to translate high-level language into machine language c) a compiler is no longer needed after a program is translated d) a compiler processes source code 8. By convention, the statements of a program are often placed in a function called a) import b) IDLE c) program d) main 9. Which of the following is not true of comments? a) They make a program more efficient b) They are intended for human readers c) They are ignored by Python d) In Python, they begin with a pound sign (#) 10. The items listed in the parentheses of a function definition are called a) parentheticals b) scripts c) comments d) parameters Discussion 1. Compare and contrast the following pairs of concepts from the chapter:
22 Chapter 1.Computers and Programs (a)Hardware vs.Software (b)Algorithm vs.Program (c)Programming Language vs.Natural Language (d)High-Level Language vs.Machine Language (e)Interpreter vs.Compiler (f)Syntax vs.Semantics 2.List and explain in your own words the role of each of the five basic func- tional units of a computer depicted in Figure 1.1. 3.Write a detailed algorithm for making a peanut butter and jelly sandwich (or some other everyday activity).You should assume that you are talking to someone who is conceptually able to do the task,but has never actually done it before.For example,you might be telling a young child. 4.As you will learn in a later chapter,many of the numbers stored in a com- puter are not exact values,but rather close approximations.For example, the value 0.1 might be stored as 0.10000000000000000555.Usually,such small differences are not a problem;however,given what you have learned about chaotic behavior in Chapter 1,you should realize the need for cau- tion in certain situations.Can you think of examples where this might be a problem?Explain. 5.Trace through the Chaos program from Section 1.6 by hand using 0.15 as the input value.Show the sequence of output that results. Programming Exercises 1.Start up an interactive Python session and try typing in each of the follow- ing commands.Write down the results you see. (a)print "Hello,world!" (b)print "Hello","world!" (c)print 3 (d)print 3.0 (e)print 2+3 (0 print2.0+3.0
22 Chapter 1. Computers and Programs (a) Hardware vs. Software (b) Algorithm vs. Program (c) Programming Language vs. Natural Language (d) High-Level Language vs. Machine Language (e) Interpreter vs. Compiler (f) Syntax vs. Semantics 2. List and explain in your own words the role of each of the five basic functional units of a computer depicted in Figure 1.1. 3. Write a detailed algorithm for making a peanut butter and jelly sandwich (or some other everyday activity). You should assume that you are talking to someone who is conceptually able to do the task, but has never actually done it before. For example, you might be telling a young child. 4. As you will learn in a later chapter, many of the numbers stored in a computer are not exact values, but rather close approximations. For example, the value 0.1 might be stored as 0.10000000000000000555. Usually such small differences are not a problem; however, given what you have learned about chaotic behavior in Chapter 1, you should realize the need for caution in certain situations. Can you think of examples where this might be a problem? Explain. 5. Trace through the Chaos program from Section 1.6 by hand using 0.15 as the input value. Show the sequence of output that results. Programming Exercises 1. Start up an interactive Python session and try typing in each of the following commands. Write down the results you see. (a) print "Hello, world!" (b) print "Hello", "world!" (c) print 3 (d) print 3.0 (e) print 2 + 3 (0 print 2.0 + 3.0
1.10.Exercises 23 (g)print"2"+"3" (h)print"2+3=",2+3 (i)print 2 3 (j)print 2 *3 (k)print 2/3 )print2.0/3.0 2.Enter and run the Chaos program from Section 1.6.Try it out with various values of input to see that it functions as described in the chapter. 3.Modify the Chaos program using 2.0 in place of 3.9 as the multiplier in the logistic function.Your modified line of code should look like this: x=2.0*x*(1-x) Run the program for various input values and compare the results to those obtained from the original program.Write a short paragraph describing any differences that you notice in the behavior of the two versions. 4.Modify the Chaos program so that it prints out 20 values instead of 10. 5.Modify the Chaos program so that the number of values to print is deter- mined by the user.You will have to add a line near the top of the program to get another value from the user: n input("How many numbers should I print?" Then you will need to change the loop to use n instead of a specific number. 6.(Advanced)Modify the Chaos program so that it accepts two inputs and then prints a table with two columns similar to the one shown in Sec- tion 1.8.(Note:You will probably not be able to get the columns to line up as nicely as those in the example.Chapter 4 discusses how to print numbers with a fixed number of decimal places.)
1.10. Exercises 23 (g) print "2" + "3" (h) print "2 + 3 =", 2+3 (i) print 2*3 (j) print 2 ** 3 (k) print 2/3 (1) print 2.0 / 3.0 2. Enter and run the Chaos program from Section 1.6. Try it out with various values of input to see that it functions as described in the chapter. 3. Modify the Chaos program using 2.0 in place of 3.9 as the multiplier in the logistic function. Your modified line of code should look like this: X = 2.0 * X * (1 - x) Run the program for various input values and compare the results to those obtained from the original program. Write a short paragraph describing any differences that you notice in the behavior of the two versions. 4. Modify the Chaos program so that it prints out 20 values instead of 10. 5. Modify the Chaos program so that the number of values to print is determined by the user. You will have to add a line near the top of the program to get another value from the user: n = input ("How mciny numbers should I print? ") Then you will need to change the loop to use n instead of a specific number. 6. (Advanced) Modify the Chaos program so that it accepts two inputs and then prints a table with two columns similar to the one shown in Section 1.8. (Note: You will probably not be able to get the columns to line up as nicely as those in the example. Chapter 4 discusses how to print numbers with a fixed number of decimal places.)
Chapter 2 Writing Simple Programs Objectives To know the steps in an orderly software development process. To understand programs following the Input,Process,Output (IPO)pat- tern and be able to modify them in simple ways. To understand the rules for forming valid Python identifiers and expres- sions. To be able to understand and write Python statements to output infor- mation to the screen,assign values to variables,get numeric information entered from the keyboard,and perform a counted loop 2.1 The Software Development Process As you saw in the previous chapter,it is easy to run programs that have already been written.The hard part is actually coming up with the program in the first place.Computers are very literal,and they must be told what to do right down to the last detail.Writing large programs is a daunting challenge.It would be almost impossible without a systematic approach. The process of creating a program is often broken down into stages according to the information that is produced in each phase.In a nutshell,here's what you should do: 25
Chapter 2 Writing Simple Programs Objectives • To know the steps in an orderly software development process. • To understand programs following the Input, Process, Output (IPO) pattern and be able to modify them in simple ways. • To understand the rules for forming valid Python identifiers and expressions. • 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 2.1| The Software Development Process As you saw in the previous chapter, it is easy to run programs that have already been written. The hard part is actually coming up with the program in the first place. Computers are very literal, and they must be told what to do right down to the last detail. Writing large programs is a daunting challenge. It would be almost impossible without a systematic approach. The process of creating a program is often broken down into stages according to the information that is produced in each phase. In a nutshell, here's what you should do: 25
26 Chapter 2.Writing Simple Programs Analyze the Problem Figure out exactly what the problem to be solved is.Try to understand as much as possible about it.Until you really know what the problem is,you cannot begin to solve it. Determine Specifications Describe exactly what your program will do.At this point,you should not worry about how your program will work,but rather about deciding exactly what it will accomplish.For simple programs this involves carefully describing what the inputs and outputs of the program will be and how they relate to each other. Create a Design Formulate the overall structure of the program.This is where the how of the program gets worked out.The main task is to design the algorithm(s)that will meet the specifications. Implement the Design Translate the design into a computer language and put it into the computer.In this book,we will be implementing our algorithms as Python programs. Test/Debug the Program Try out your program and see if it works as expected. If there are any errors (often called bugs),then you should go back and fix them.The process of locating and fixing errors is called debugging a program.During the debugging phase,your goal is to find errors,so you should try everything you can think of that might"break"the program.It's good to keep in mind the old maxim:"Nothing is foolproof because fools are too ingenious.” Maintain the Program Continue developing the program in response to the needs of your users.Most programs are never really finished;they keep evolving over years of use. 2.2 Example Program:Temperature Converter Let's go through the steps of the software development process with a simple real-world example involving a fictional computer science student,Susan Com- putewell. Susan is spending a year studying in Germany.She has no problems with language,as she is fluent in many languages (including Python).Her problem is that she has a hard time figuring out the temperature in the morning so that she knows how to dress for the day.Susan listens to the weather report each
26 Chapter 2. Writing Simple Programs Analyze the Problem Figure out exactly what the problem to be solved is. Try to understand as much as possible about it. Until you really know what the problem is, you cannot begin to solve it. Determine Specifications Describe exactly what your program will do. At this point, you should not worry about how your program will work, but rather about deciding exactly what it will accomplish. For simple programs this involves carefully describing what the inputs and outputs of the program will be and how they relate to each other Create a Design Formulate the overall structure of the program. This is where the how of the program gets worked out. The main task is to design the algorithm (s) that will meet the specifications. Implement the Design Translate the design into a computer language and put it into the computer In this book, we will be implementing our algorithms as Python programs. Test/Debug the Program Try out your program and see if it works as expected. If there are any errors (often called bugs), then you should go back and fix them. The process of locating and fixing errors is called debugging a program. During the debugging phase, your goal is to find errors, so you should try everything you can think of that might "break" the program. It's good to keep in mind the old maxim: "Nothing is foolproof because fools are too ingenious." Maintain the Program Continue developing the program in response to the needs of your users. Most programs are never really finished; they keep evolving over years of use. 2.2 Example Program: Temperature Converter Let's go through the steps of the software development process with a simple real-world example involving a fictional computer science student, Susan Computewell. Susan is spending a year studying in Germany. She has no problems with language, as she is fluent in many languages (including Python). Her problem is that she has a hard time figuring out the temperature in the morning so that she knows how to dress for the day Susan listens to the weather report each