Ch2 Python
Ch2 Python
JAAMACADDA SIMAD
Faculty:
Program:
Class:
ID No. :
Phone No. :
Subject : Pearson Starting Out with Python 5rd Tony Gaddis (2021)
A2) A single function that the program must perform in order to satisfy the customer.
A3) A set of well-defined logical steps that must be taken to perform a task.
An informal language that has no syntax rules and is not meant to be compiled or executed. Instead,
programmers use pseudocode to create models, or “mock-ups,” of programs.
A5) A diagram that graphically depicts the steps that take place in a program.
• Oval
• Parallelogram
• Rectangle
A7) print("Mohamed")
Q8) Write a statement that displays the following text: Python's the best!
Q9) Write a statement that displays the following text: The cat said "meow."
99bottles
july2009
theSalesFigureForFiscalYear
October 10, 2021
r&d
grade_repor
A11) 99bottles is illegal because it begins with a number. r&d is illegal because the & character is not
allowed.
Q12) Is the variable name Sales the same as sales? Why or why not?
Q13) Is the following assignment statement valid or invalid? If it is invalid, why? 72 = amount
It is invalid because the variable that is receiving the assignment (in this case
amount ) must appear on the left side of the = operator.
val = 99
value1 = 99
value2 = 45.9
value3 = 7.0
value4 = 7
value5 = 'abc'
After these statements execute, what is the Python data type of the values referenced by each
variable
value3 will reference a float . value4 will reference an int . value5 will reference an str (string).
A16) 0
Q17) You need the user of a program to enter a customer’s last name. Write a statement that prompts
the user to enter this data and assigns the input to a variable.
October 10, 2021
A17) last_name = input("Enter the customer's last name: ")
Q18) You need the user of a program to enter the amount of sales for the week. Write a statement
that prompts the user to enter this data and assigns the input to a variable.
Q19) Complete the following table by writing the value of each expression in the Value column:
Expression Value
6+3*5 ______
12 / 2 – 4 ______
9 + 14 * 2 − 6 ______
(6 + 2) * 3 ______
14 / (11 − 4) ______
9 + 12 * (8 − 3) ______
A19)
Q20) What value will be assigned to result after the following statement executes? result = 9 // 2
A20) 4
Q21) What value will be assigned to result after the following statement executes? result = 9 % 2
A21) 1
A22) String concatenation is the appending of one string to the end of another.
Q23) After the follow statement executes, what value will be assigned to the result variable?
A23) '12'
October 10, 2021
Q24) After the follow statement executes, what value will be assigned to the result variable?
A24) 'hello'
A25) If you do not want the print function to start a new line of output when it finishes displaying its
output, you can pass the special argument end = ' ' to the function.
Q26) How can you change the character that is automatically displayed between multiple items that
are passed to the print function?
A26) You can pass the argument sep= to the print function, specifying the desired character.
Q28) What will the following code display? name = 'Karlie' print('Hello {name}')
Q29) What will the following code display? name = 'Karlie' print(f'Hello {name}')
Q30) What will the following code display? value = 99 print(f'The value is {value + 1}')
Q31) What will the following code display ?value = 65.4321 print(f'The value is {value:.2f}')
Q32) What will the following code display? value = 987654.129 print(f'The value is {value:,.2f}')
Q33) What will the following code display? value = 9876543210 print(f'The value is {value:,d}')
print(f'{name:10}')
A34) It is a field width designator. It indicates that the value should be displayed in a field that is a
minimum of 10 spaces wide.
Q35) In the following statement, what is the purpose of the number 15 in the format specifier
October 10, 2021
print(f'{number:15,d}')
A35) It is a field width designator. It indicates that the value should be displayed in a field that is a
minimum of 15 spaces wide
Q36) In the following statement, what is the purpose of the number 8 in the format specifier?
print(f'{number:8,.2f}')
A36) It is a field width designator. It indicates that the value should be displayed in a field that is a
minimum of 8 spaces wide.
Q37) In the following statement, what is the purpose of the < character in the format specifier?
print(f'{number:<12d}')
A37) It is an alignment specifier. It specifies that the value should be left aligned.
Q38) In the following statement, what is the purpose of the > character in the format specifier?
print(f'{number:>12d}')
A38) It is an alignment specifier. It specifies that the value should be right aligned.
Q39) In the following statement, what is the purpose of the ^ character in the format specifier?
print(f'{number:^12d}')
A39) It is an alignment specifier. It specifies that the value should be center aligned
A40) (1) Named constants make programs more self-explanatory, (2) widespread changes can easily be
made to the program, and (3) they help to prevent the typographical errors that are common when
using magic numbers.
Q41) Write a Python statement that defines a named constant for a 10 percent discount.
A42) 0 degrees
Q45) How would you move the turtle to a new location without drawing a line?
October 10, 2021
A45) You would first use the turtle.penup() command to raise the turtle’s pen.
Q46) What command would you use to display the turtle’s current heading?
A46) turtle.heading()
Q47) What command would you use to draw a circle with a radius of 100 pixels?
A47) turtle.circle(100)
Q48) What command would you use to change the turtle’s pen size to 8 pixels?
A48) turtle.pensize(8)
Q49) What command would you use to change the turtle’s drawing color to blue?
A49) turtle.pencolor('blue')
Q50) What command would you use to change the background color of the turtle’s graphics window
to black?
A50) turtle.bgcolor('black')
Q51) What command would you use to set the size of the turtle’s graphics window to 500 pixels wide
by 200 pixels high?
Q52) What command would you use to move the turtle to the location (100, 50)?
Q53) What command would you use to display the coordinates of the turtle’s current position?
A53) turtle.pos()
Q54) Which of the following commands will make the animation speed faster? turtle. speed(1) or
turtle.speed(10)
A54) turtle.speed(10)
Q55) What command would you use to disable the turtle’s animation?
A55) turtle.speed(0)
A56) To fill a shape with a color, you use the turtle.begin_fill() command before drawing the shape, then
you use the turtle.end_fill() command after the shape is drawn. When the turtle.end_fill() command
executes, the shape will be filled with the current fill color
Q58) Write a turtle graphics statement that displays a dialog box that gets a number from the user.
The text Enter a Value should appear in the dialog box’s title bar. The dialog box should display the
prompt What is the radius of the circle?. The value that the user enters into the dialog box should be
assigned to a variable named radius.
1. A __________ error does not prevent the program from running, but causes it to produce incorrect
results.
3. A(n) __________ is a set of well-defined logical steps that must be taken to perform a task.
4. An informal language that has no syntax rules and is not meant to be compiled or executed is called
__________.
5. A __________ is a diagram that graphically depicts the steps that take place in a program.
8. A __________ is any hypothetical person using a program and providing input for it.
10. Short notes placed in different parts of a program explaining how those parts of the program work
are called __________.
11. A(n) __________ makes a variable reference a value in the computer’s memory.
a. & b. * c. ** d. #
a. x = 17 b. 17 = x c. x = 99999 d. x = '17'
14. In the expression 12 + 7, the values on the right and left of the + symbol are called __________.
a. % b. * c. ** d. /
17. This operator performs division, but instead of returning the quotient it returns the remainder.
a. % b. * c. ** d. /
18. Suppose the following statement is in a program: price = 99.0. After this statement executes, the
price variable will reference a value of which data type?
19. Which built-in function can be used to read input that has been typed on the keyboard?
20. Which built-in function can be used to convert an int value to a float?
22. A __________ is a name that represents a value that does not change during the pro- gram’s
execution.
False 1. Programmers must be careful not to make syntax errors when writing pseudocode programs.
True 2. In a math expression, multiplication and division take place before addition and subtraction.
False 5. If you print a variable that has not been assigned a value, the number 0 will be displayed.
Short Answer
Q1) What does a professional programmer usually do first to gain an understanding of a problem?
A1) Programmers will conduct an initial interview with their customer to understand what exactly the
customer wants out of the program and how it should function. The programmer can also conduct
follow-up interviews if they come up with additional questions.
A2) Pseudocode is an informal language that has no syntax rules, and is not meant to be compiled or
executed.
Q4) If a math expression adds a float to an int, what will the data type of the result be?
A4) If a math expression adds a float to and int, the data type result will be a float.
Q5) What is the difference between floating-point division and integer division
A5) The difference in floating-point division and integer division is that in floating-point division, the /
operator gives the result as a floating-point value, and in integer division, the // operator gives the result
as an integer.
A6) A magic number is an unexplained value that appears in a program's code. Magic numbers can be
problematic, for a number of reasons. First, it can be difficult for someone reading the code to
determine the purpose of the number. Second, if the magic number is used in multiple places in the
program, it can take painstaking effort to change the number in each location, should the need arise.
Third, you take the risk of making a typographical mistake each time you type the magic number in
A7) The named constant makes the program more self-explanatory. In a math statement, it is evident
that PI represents the value of pi. Another advantage to using the named constant is that widespread
changes can easily be made to the program. Let's say the value of pi appears in several different
statements throughout the program. If you need to change the number of decimal places of precision
used with the number, the initialization value in the declaration of the named constant is the only
value that needs to be modified. For example, to use only two decimal places of precision, the
declaration can be changed to: PI = 3.14 The new value of 3.14 will then be used in each statement
that includes the PI constant. Another advantage to using the named constant is that it helps to
prevent the typographical errors that are common when using magic numbers. For example, if you
accidentally type 31.4159 instead of 3.14159 in a math statement, the program will calculate the
wrong value. However, if you misspell PI, the Python interpreter will display a message indicating that