Chapter 3 part 3 Flashcards

1
Q

What is an augmented assignment?

A

An abbreviated assignment expressions in which the same varianble name appears on the left and right of the assignment’s =, as total does in:
In [1]: for number in [1, 2, 3, 4, 5]:
…….. …….total += number # add number of total and store in number
In [2]: total
Out[2]: 15

Note that this is not in range and is added like normal
0 + 1 + 2 + 3 + 4 + 5 = 15
The += snipper first adds numbers value to the current total, then stores the new value in total

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Fill in the blank

If x is 7, the value of x after evaluating x *= 5 is .____……_____…____

A

35

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Create a variable x with the value 12. Use an exponentiation augmented assignment statement to square x’s value. Show x’s new value.

A

x = 12
x **= 2
144

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does this operator do? //=

A

Floor division, divides 2 numbers and rounds down the result to the nearest whole number
Ex:
print(7//2)
Out: 3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does this operator do? **=

A

It uses an exponent
Ex:
12 **= 2
144

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does this operator do? %

A

Divides two numbers and returns the remainder
Ex:
print(10 % 3)
out: 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Define

Requirements statements

A

Describes what a program is supposed to do, but not how the program should do it

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Provide an example of a simple statement requirement

A

A class of ten students took a quiz. Their grades (integers in the range 0 - 100) are 98, 76, 71, 87, 83, 90, 57, 79, 82, 94. Determine the class average on the quiz
- Once you know the problem’s requirements, then you begin creating an algorithm to solve it. Then you can implement that solution as a program and it must:
- 1. keep a running total of the grades
- 2. calc the average-the total of the grades divided by the number of grades
- 3. display the result

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Consider the following pseudocode for the algorithm

A
  • Note the mentions of total and grade counter
  • We’ll use these in the script to calculate the average
  • Variables for totaling and counting normally are initiated to zero
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the 3 phases a script can be decomposed by?

A

initialization phase
processing phase
termination phase

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Define

Initialization phase

A

Creates the variables needed to process the grades and set these variables to appropriate initial values

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Define

Processing phase

A

Processes the data, calculating the running total and counting the number of grades processed so far

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Define

Termination phase

A

Calculates and displays the class average

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Breakdown step by step how this code works

A

Initial state:
- total = 0
- grade_counter = 0
First iteration:
- grade = 98
- total = 0 + 98 = 98
- grade_counter = 0 + 1 = 1
Second iteration:
- grade = 76
- total = 98 + 76 = 174
- grade_counter = 1 + 1 = 2
Final state:
- total = 817 (sum of all grades)
- grade_counter = 10 (number of grades)
Avegare calculation:
- average = total / grade_counter
- = 817 / 10 = 81.7

the part that says “for grade in grades” starts a loop that will go through each element in the grades list. Then the total is added with the += grade starting from 0 to each individual grade until all of them have gone through the loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is an f-string?

A

Short for formatted string, allows inserting values into a string. In the previous example, it does this by inserting the value of the average into a string.
- The leter f before the string’s opening quote indicates it’s an f-string
- You specify where to insert values by using placeholders delimited by curly braces ({ and }). The placeholder {average} converts the variable average’s value to a string representation, then replaces {average} with that replacement text

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

A(n) ___.___.____ describes what a program is supposed to do, but not how the program should do it

A

requirements statement

17
Q

Many of the scripts you’ll write can be decomposed into 3 phases:
a.
b.
c.

A

initialization
processing
termination

18
Q

Display an f-string in which you insert the values of the variables number1 (7) and number 2 (5) and their product. The displayed string should be
7 times 5 is 35

A
19
Q

What is a sentinel value? aka signal value, a dummy value or a flag value?

A

These indicate the “end of data entry” It determines when to stop processing a program so it can move onto to the next program segment

20
Q

What is a indefinite iteration?

A

It is a sentinel-controlled iteration and the number of iterations is not known before the loop begins executing. Ex:
Develop a class-averaging program that processes an arbitrary number of grades each time the program executes.
– arbitrary

A sentinel value must not be confused with any acceptable input value and should not be considered as another input value

21
Q

What is the top-down portion of stepwise refinement

A

It is a single statement that conveys the program’s overall function
- it rarely conveys enough detail from which to write a program
- specifies what should be done but not how to implement it
- Begin the refinement process by decomposing the top into a sequence of smaller tasks -divide and conquer

Pseudocode representation of the top ex:
Determine the class average for the quiz

22
Q

Give an example of the first refinement from the pseudocode representation of the top ex:
Determine the class average for the quiz

A

Initialize variables
Input, sum and count the quiz grades
Calculate and display the class average

  • Each refinement represents the complete algorithm
  • These pseudocode statements correspond to the three execution phases described in the preceding section. The algorithm does not yet provide enough detail for us to write the Python program, so onto the next refinement
23
Q

What do we need for a second refinement?

A

Commit to specific variables:
- a grade variable in which each succcessive user input will be stored
- a running total of the grades
- a count of how many grades have been processed
- a variable that contains the calculated average

24
Q

From the first refinement:
Initialize variables
can be refined as:

A

Initialize total to zero
Initialize grade counter to zero

- Other variables are created when they’re needed

25
Q

From the first refinement:
Input, sum and count the quiz grades
can be refined as follows:

A

*Input the first grade (possibly the sentinel)
While the user has not enetered the sentinel

…..Add this grade into the running total

….Add one to the grade counter

….Input the next grade (possibly the sentinel)*

26
Q

From the first refinement
Calculate and display the class average
can be refined as follows:

A

*If the counter is not equal to zero
……. Set the average to the total divided by the grade counter
…….. Display the average
Else
…….. Display “No grades were entered”

27
Q

Refinement completion reminders

A
  • Sometimes more than two refimenets are necessary
  • You stop refining when there is enough detail to convert the pseudocode to Python
  • You can include blank lines for readability
28
Q

Observe the following sentinel-controlled iteration and examine it

A

Under # processing phase, the program reads the first value before reaching the while statement. If we had initialized it then that value would have been replaced immediately by this assignment

29
Q

Observe the following sentinel-controlled iteration and discuss how the value input affects the while suite

A
  • The value inpute determines whether the program’s flow of control should enter the while’s suite
  • If the condition is false the user entered -1, so the suite does not execute
  • If true, the suite executes, addint the grade value to the total and incrementing the grade_counter
  • grade is always input immediately before the program tests the whilecondition
30
Q

Observe the following sentinel_controlled iteration and discuss what occurs when a sentinel value is input

A
  • When the sentinel value is input, the loop terminates, and the program does not add -1 to the total
  • After the loop terminates, the if…else statements execute
31
Q

How do you format the class average with 2 decimal places?

A

Using a format specifier that describes how to format the replacement text
In the included image, we used the format specifier .2 and tells Python to format the floating-point number (in this case, the average) to 2 decimal places. f stands for floating-point number
Use for readability and precision

32
Q

Sentinel-controlled repetition is called ___blank___ because the number of repetitions is not known before the loop begins executing

A

indefinite repetition

33
Q

T/F

Sentinel-controlled repetition uses a counter variable to control the number of times a set of instructions executes

A

False. Sentinel-control reptitioin terminates reptition when the sentinel value is encountered