Starting out with Python Chapter 2 checkpoints

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

2.29 What will the following code display? name = 'Karlie' print(f'Hello {name}')

Hello Karlie

2.28 What will the following code display? name = 'Karlie' print('Hello {name}')

Hello {name}

2.12 Is the variable name Sales the same as sales? Why or why not?

No, it is not because variable names are case sensitive

2.23 After the follow statement executes, what value will be assigned to the result variable? result = '1' + '2'

'12'

2.24 After the follow statement executes, what value will be assigned to the result variable? result = 'h' 'e' 'l' 'l' 'o'

'hello'

2.16 What will be displayed by the following program? my_value = 99 my_value = 0 print(my_value)

0

2.42 What is the turtle's default heading when it first appears?

0 degrees

2.21 What value will be assigned to result after the following statement executes? result = 9 % 2

1

2.40 What are three advantages of using named constants?

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.

2.20 What value will be assigned to result after the following statement executes? result = 9 // 2

4

2.11 Which of the following are illegal variable names in Python, and why? x 99bottles july2009 theSalesFigureForFiscalYear r&d grade_report

99bottles is illegal because it begins with a number. r&d is illegal because the "&" character is not allowed.

2.25 How do you suppress the print function's ending newline?

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.

2.10 What is a variable?

A name that represents a value in the computer's memory

2.3 What is an algorithm?

A set of well-defined logical steps that must be taken to perform a task.

2.2 What is a software requirement?

A single function that the program must perform in order to satisfy the customer.

2.4 What is pseudocode?

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.

2.1 Who is a programmer's customer?

Any person, group, or organization that is asking you to write a program.

2.34 In the following statement, what is the purpose of the number 10 in the format specifier? print(f'{name:10}')

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.

2.35 In the following statement, what is the purpose of the number 15 in the format specifier? print(f'{number:15,d}')

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.

2.36 In the following statement, what is the purpose of the number 8 in the format specifier? print(f'{number:8,.2f}')

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.

2.39 In the following statement, what is the purpose of the ^ character in the format specifier? print(f'{number:^12d}')

It is an alignment specifier. It specifies that the value should be center aligned.

2.37 In the following statement, what is the purpose of the < character in the format specifier? print(f'{number:<12d}')

It is an alignment specifier. It specifies that the value should be left aligned.

2.38 In the following statement, what is the purpose of the > character in the format specifier? print(f'{number:>12d}')

It is an alignment specifier. It specifies that the value should be right aligned.

2.13 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.

2.41 Write a Python statement that defines a named constant for a 10 percent discount.

DISCOUNT_PERCENTAGE = 0.1

2.27 What is the '\n' escape character?

It is the newline escape character.

2.6 What do each of the following symbols mean in a flowchart? Oval Parallelogram Rectangle

Ovals are terminal symbols. Parallelograms are either output or input symbols. Rectangles are processing symbols

2.8 Write a statement that displays the following text: Python's the best!

Print("Python's the best")

2.9 Write a statement that displays the following text: The cat said "meow."

Print('the cat said "meow"')

2.22 What is string concatenation?

String concatenation is the appending of one string to the end of another.

2.30 What will the following code display? value = 99 print(f'The value is {value + 1}')

The value is 100

2.31 What will the following code display? value = 65.4321 print(f'The value is {value:.2f}')

The value is 65.43

2.33 What will the following code display? value = 9876543210 print(f'The value is {value:,d}')

The value is 9,876,543,210

2.32 What will the following code display? value = 987654.129 print(f'The value is {value:,.2f}')

The value is 987,654.13

2.14 What will the following code display? val = 99 print('The value is', 'val')

The value is val

2.56 Describe how to draw a shape that is filled with a color.

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.

2.15 Look at the following assignment statements: 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?

Value1 will reference an int. Value2 will reference a float. Value3 will reference a float. Value4 will reference an int. Value5 will reference a str (string).

2.19 Complete the following table by writing the value of each expression in the Value column: Expression 1. 6 + 3 * 5 2. 12 / 2 − 4 3. 9 + 14 * 2 − 6 4. (6 + 2) * 3 5. 14 / (11 − 4) 6. 9 + 12 * (8 − 3)

Value: 21 2 31 24 2 69

2.44 How would you turn the turtle right by 45 degrees?

With the command turtle.right(45)With the command turtle.right(45)

2.43 How do you move the turtle forward?

With the turtle.forward command.

2.57 How do you display text in the turtle's graphics window?

With the turtle.write()command

2.26 How can you change the character that is automatically displayed between multiple items that are passed to the print function?

You can pass the argument sep= to the print function, specifying the desired character.

2.45 How would you move the turtle to a new location without drawing a line?

You would first use the turtle.penup() command to raise the turtle's pen.

2.5 What is a flowchart?

a diagram that graphically depicts the steps that take place in a program

2.17 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.

last_name = input ("Enter the customer's last name: ")

2.7 Write a statement that displays your name.

print('David Hopkins')

2.58 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.

radius = turtle.numinput('Enter a Value', 'What is the radius of the circle?')

2.18 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.

sales = float (input ('Enter the sales for the week: '))

2.50 What command would you use to change the background color of the turtle's graphics window to black?

turtle.bgcolor('black')

2.47 What command would you use to draw a circle with a radius of 100 pixels?

turtle.circle(100)

2.52 What command would you use to move the turtle to the location (100, 50)?

turtle.goto(100, 50)

2.46 What command would you use to display the turtle's current heading?

turtle.heading()

2.49 What command would you use to change the turtle's drawing color to blue?

turtle.pencolor('blue')

2.48 What command would you use to change the turtle's pen size to 8 pixels?

turtle.pensize(8)

2.53 What command would you use to display the coordinates of the turtle's current position?

turtle.pos()

2.51 What command would you use to set the size of the turtle's graphics window to 500 pixels wide by 200 pixels high?

turtle.setup(500, 200)

2.55 What command would you use to disable the turtle's animation?

turtle.speed(0)

2.54 Which of the following commands will make the animation speed faster? turtle.speed(1) or turtle.speed(10)

turtle.speed(10)


Set pelajaran terkait

Business Statistics Ch 1 (Week 1)

View Set

6246 Ch.10 Sample readiness questions

View Set

AP Computer Science Principles Programming (Khan Academy)

View Set

Chapter 7: Interest rates and Bond Valuation

View Set

PrepU Chapter 19: Postop Care (Exam 1)

View Set

MGMT 217 Practice Questions Exam 3

View Set

PrepU Health Assess Ch. 3 Assignment 3

View Set