Chapter 3 part 3 Flashcards
What is an augmented assignment?
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
Fill in the blank
If x is 7, the value of x after evaluating x *= 5 is .____……_____…____
35
Create a variable x with the value 12. Use an exponentiation augmented assignment statement to square x’s value. Show x’s new value.
x = 12
x **= 2
144
What does this operator do? //=
Floor division, divides 2 numbers and rounds down the result to the nearest whole number
Ex:
print(7//2)
Out: 3
What does this operator do? **=
It uses an exponent
Ex:
12 **= 2
144
What does this operator do? %
Divides two numbers and returns the remainder
Ex:
print(10 % 3)
out: 1
Define
Requirements statements
Describes what a program is supposed to do, but not how the program should do it
Provide an example of a simple statement requirement
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
Consider the following pseudocode for the algorithm
- 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
What are the 3 phases a script can be decomposed by?
initialization phase
processing phase
termination phase
Define
Initialization phase
Creates the variables needed to process the grades and set these variables to appropriate initial values
Define
Processing phase
Processes the data, calculating the running total and counting the number of grades processed so far
Define
Termination phase
Calculates and displays the class average
Breakdown step by step how this code works
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
What is an f-string?
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
A(n) ___.___.____ describes what a program is supposed to do, but not how the program should do it
requirements statement
Many of the scripts you’ll write can be decomposed into 3 phases:
a.
b.
c.
initialization
processing
termination
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
What is a sentinel value? aka signal value, a dummy value or a flag value?
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
What is a indefinite iteration?
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
What is the top-down portion of stepwise refinement
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
Give an example of the first refinement from the pseudocode representation of the top ex:
Determine the class average for the quiz
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
What do we need for a second refinement?
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
From the first refinement:
Initialize variables
can be refined as:
Initialize total to zero
Initialize grade counter to zero
- Other variables are created when they’re needed