WHAT IS SEQUENCE-CONTROLLED ITERATION; FORMATTED STRINGS AND HOW TO USE IN PYTHON PROGRAMMING

SEQUENCE-CONTROLLED ITERATION; FORMATTED STRINGS

This section and the next solve two class­averaging problems. Consider the following requirements statement:
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.
The following script for solving this problem keeps a running total of the grades, calculates the average and displays the result. We placed the 10 grades in a list, but you could input the grades from a user at the keyboard (as we’ll do in the next example) or read them from a file (as you’ll see how to do in the “Files and Exceptions” chapter). We show how to read data from SQL and NoSQL databases in Chapter 16.

1 # class_average.py
"""Class average program with sequence­controlled iteration."""
3
# initialization phase
5 total = 0 # sum of grades
grade_counter = 0
grades = [98, 76, 71, 87, 83, 90, 57, 79, 82, 94# list of 10 grades
8
9 # processing phase
10 for grade in grades:
11 total += grade # add current grade to the running total
12 grade_counter += 1 # indicate that one more grade was process
13
14 # termination phase
15 average = total / grade_counter
16 print(f'Class average is {average}')

Class average is 81.7

Lines 5–6 create the variables total and grade_counter and initialize each to 0. Line 7

grades = [98, 76, 71, 87, 83, 90, 57, 79, 82, 94# list of 10 grades

creates the variable grades and initializes it with a list of 10 integer grades.
The for statement processes each grade in the list grades. Line 11 adds the current grade to the total. Then, line 12 adds 1 to the variable grade_counter to keep track of the number of grades processed so far. Iteration terminates when all 10 grades in the list have been processed. The Style Guide for Python Code recommends placing a blank line above and below each control statement (as in lines 8 and 13). When the for statement terminates, line 15 calculates the average and line 16 displays it. Later in this chapter, we use functional­style programming to calculate the average of a list’s items more concisely.

Introduction to Formatted Strings

Line 16 uses the following simple f-­string (short for formatted string) to format this script’s result by inserting the value of average into a string:

f'Class average is {average}'

The letter 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. Replacement-­text expressions may contain values, variables or other expressions, such as calculations or function calls. In line 16, we could have used total / grade_counter in place of average, eliminating the need for line 15.

FREE HACKING COURSE LAUNCHED

*

إرسال تعليق (0)
أحدث أقدم