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