Programs, 3rd Edition Kenneth A. Lambert
Notes
1- The file is chapter after chapter.
2- We have shown you 10 pages.
3- The file contains all Appendix and Excel
sheet if it exists.
4- We have all what you need, we make
update at every time. There are many
new editions waiting you.
5- If you think you purchased the wrong file
You can contact us at every time, we can
replace it with true one.
Our email:
,Solution and Answer Guide: Lambert, Fundamentals of Python: First Programs, 3e, CY24, 9780357881019; Chapter 2, Software
Development, Data Types, and Expressions
Solution and Answer Guide
LAMBERT, FUNDAMENTALS OF PYTHON: FIRST PROGRAMS, 3E, CY24, 9780357881019;
CHAPTER 2, SOFTWARE DEVELOPMENT, DATA TYPES, AND EXPRESSIONS
TABLE OF CONTENTS
Exercise Solutions .................................................................................................................1
Exercise 2.1 ....................................................................................................................................1
Exercise 2.2 .................................................................................................................................. 2
Exercise 2.3 .................................................................................................................................. 3
Exercise 2.4 .................................................................................................................................. 4
Review Questions Answers.................................................................................................. 5
Programming Exercises Solutions ...................................................................................... 9
Debugging Exercise Solution ..............................................................................................12
EXERCISE SOLUTIONS
EXERCISE 2.1
1. List four phases of the software development process and explain what they
accomplish.
Solution:
Analysis describes what a system does in terms of its input, outputs, and functions
from a user’s perspective. Design describes how a system accomplishes its tasks.
Coding produces the software for the system. Testing examines whether or not the
software does what it is supposed to do.
2. Jack says that he will not bother with analysis and design but proceed directly to
coding his programs. Why is that not a good idea?
Solution:
Analysis and design provide detailed blueprints for coding a system. Without these
blueprints, it may be difficult to determine whether the system will do what it is
supposed to do, and it may be difficult to minimize errors in the code and structure it
in a way that eases maintenance.
© 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible 1
website, in whole or in part.
,Solution and Answer Guide: Lambert, Fundamentals of Python: First Programs, 3e, CY24, 9780357881019; Chapter 2, Software
Development, Data Types, and Expressions
EXERCISE 2.2
1. Let the variable x be "dog" and the variable y be "cat". Write the values returned by
the following operations:
a. x + y
b. "the " + x + " chases the " + y
c. x * 4.
Solution:
a. "dogcat"
b. "the dog chases the cat"
c. "dogdogdogdog"
2. Write a string that contains your name and address on separate lines using embedded
newline characters. Then write the same string literal without the newline characters.
Solution:
"Ken Lambert\nComputer Science\nWashington and Lee"
"""Ken Lambert
Computer Science
Washington and Lee"""
3. How does one include an apostrophe as a character within a string literal?
Solution:
An apostrophe can be included in a string that is enclosed within double quotes.
4. What happens when the print function prints a string literal with embedded newline
characters?
Solution:
When the Python interpreter encounters a newline character while printing a string,
the cursor moves to the next line of output before the rest of the characters are
displayed.
5. Which of the following are valid variable names?
a. length
b. _width
c. firstBase
d. 2MoreToGo
e. halt!
Solution:
a, b, and c
© 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible 2
website, in whole or in part.
,Solution and Answer Guide: Lambert, Fundamentals of Python: First Programs, 3e, CY24, 9780357881019; Chapter 2, Software
Development, Data Types, and Expressions
EXERCISE 2.3
2. Which data type would most appropriately be used to represent the following data
values?
a. The number of months in a year
b. The area of a circle
c. The current minimum wage
d. The approximate age of the universe (12,000,000,000 years)
e. Your name
Solution:
a. int
b. float
c. float
d. int
e. string
3. Explain the differences between the data types int and float.
Solution:
int is the type of integers or whole numbers, whereas float is the type of numbers
that include a whole part (digits to the left of a decimal point) and a fractional part
(digits to the right of the decimal point).
4. Write the values of the following floating-point numbers in Python’s scientific notation:
a. 355.76
b. 0.007832
c. 4.3212
Solution:
a. 3.5576e2
© 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible 3
website, in whole or in part.
,Solution and Answer Guide: Lambert, Fundamentals of Python: First Programs, 3e, CY24, 9780357881019; Chapter 2, Software
Development, Data Types, and Expressions
b. 7.832e-3
c. 4.3212e0
5. Consult Table 2-5 to write the ASCII values of the characters '$' and '&'.
Solution:
36 and 38.
EXERCISE 2.4
1. Let x = 8 and y = 2. Write the values of the following expressions:
a. x + y * 3
b. (x + y) * 3
c. x ** y
d. x % y
e. x / 12.0
f. x // 6
Solution:
a. 14
b. 30
c. 64
d. 0
e. 0.66666666666666663
f. 1
2. Let x = 4.66. Write the values of the following expressions:
a. round(x)
b. int(x)
Solution:
a. 5.0
b. 4
3. How does a Python programmer round a float value to the nearest int value?
© 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible 4
website, in whole or in part.
,Solution and Answer Guide: Lambert, Fundamentals of Python: First Programs, 3e, CY24, 9780357881019; Chapter 2, Software
Development, Data Types, and Expressions
Solution:
Pass the number to the round function.
4. How does a Python programmer concatenate a numeric value to a string value?
Solution:
Pass the number as an argument the str function and use the result returned with
the string and the + operator.
5. Assume that the variable x has the value 55. Use an assignment statement to
increment the value of x by 1.
Solution:
x = x + 1
REVIEW QUESTIONS ANSWERS
1. What does a programmer do during the analysis phase of software development?
a. Codes the program in a particular programming language
b. Writes the algorithms for solving a problem
c. Decides what the program will do and determines its user interface
d. Tests the program to verify its correctness
Answer: c
Feedback:
a. Incorrect. Coding occurs after design.
b. Inorrect. Algorithms are written during design.
c. Correct. This is a set of instructions that describes a process that halts with a solution
to a problem.
d. Incorrect. Testing occurs after coding.
2. What must a programmer use to test a program?
© 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible 5
website, in whole or in part.
,Solution and Answer Guide: Lambert, Fundamentals of Python: First Programs, 3e, CY24, 9780357881019; Chapter 2, Software
Development, Data Types, and Expressions
a. All possible sets of legitimate inputs
b. All possible sets of inputs
c. A single set of legitimate inputs
d. A reasonable set of legitimate inputs
Answer: d
Feedback:
a. Incorrect. There may be an infinite number of sets of possible legitimate inputs.
b. Incorrect. Some inputs may not be legitimate, and only legitimate ones are candidates
for testing.
c. incorrect. The program might fail on a legitimate input that is not tested.
d. Correct. A reasonable set of legitimate inputs is finite and includes those likely to
produce correct results.
3. What must you use to create a multiline string?
a. A single pair of double quotation marks
b. A single pair of single quotation marks
c. A single pair of three consecutive double quotation marks
d. Embedded newline characters
Answer: c, d
Feedback:
a. Inorrect. This creates a string, but no newlines are specified.
b. Incorrect. This creates a string, but no newlines are specified.
c. Correct. This will create a multiline string if the string’s text is on multiple lines in the
source programs.
d. Correct. An embedded newline character causes a newline to print on the monitor.
4. What is used to begin an end-of-line comment?
a. / symbol
b. # symbol
c. % symbol
d. * symbol
© 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible 6
website, in whole or in part.
,Solution and Answer Guide: Lambert, Fundamentals of Python: First Programs, 3e, CY24, 9780357881019; Chapter 2, Software
Development, Data Types, and Expressions
Answer: b
Feedback:
a. Incorrect. The / symbol serves as an arithmetic division operator or as a line separator
in source code.
b. Correct. The # symbol begins an end-of-line comment.
c. Incorrect. The % operator serves as the arithmetic remainder operator.
d. Incorrect. The * symbol serves as the arithmetic multiplication operator.
5. Which of the following lists of operators is ordered by decreasing precedence?
a. +, *, **
b. *, /, %
c. **, *, +
d. +, -
Answer: c
Feedback:
a. Incorrect. These operators are ordered by increasing precedence.
b. Incorrect. These operators have the same precedence.
c. Correct. These operators are ordered by decreasing precedence.
d. Incorrect. These operators have the same precedence.
6. The expression 2 ** 3 ** 2 evaluates to which of the following values?
a. 64
b. 512
c. 8
d. 12
Answer: b
Feedback:
a. Incorrect. This assumes that ** is left-associative.
© 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible 7
website, in whole or in part.
, Solution and Answer Guide: Lambert, Fundamentals of Python: First Programs, 3e, CY24, 9780357881019; Chapter 2, Software
Development, Data Types, and Expressions
b. Correct. ** is right-associative, which means that 3 ** 2 is evaluated before the
result, 9 is used as the exponent for 2 ** 9.
c. Incorrect. This is the result of 2 * 3 + 2.
d. Incorrect. This is the result of 2 * 3 * 2.
7. The expression round(23.67) evaluates to which of the following values?
a. 23
b. 23.7
c. 24.0
d. 24
Answer: d
Feedback:
a. Incorrect. This would be the result of using the int function, which truncates the
argument.
b. Incorrect. round with a single argument returns an int.
c. Incorrect. round with a single argument returns an int.
d. Correct. round rounds its single argument to the nearest whole number. In this case,
it rounds up because the argument is greater than 23.5.
8. Assume that the variable name has the value 33. What is the value of name after the
assignment name = name * 2 executes?
a. 35
b. 33
c. 66
Answer: c
a. Incorrect. 35 is 33 + 2.
b. Incorrect. A total distractor.
© 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible 8
website, in whole or in part.