Total and average
marks = [78, 92, 85, 64, 90]
total = 0
for mark in marks:
total += mark
print(total)
print(total / len(marks))409 81.8
total += mark is short for total = total + mark. Start the total at 0 before the loop and print it after it, not indented, or it prints on every turn.Counting with a condition
To count values that match a condition, use a counter that starts at 0 and goes up by 1 inside an if inside the loop:
record_list = ["absent", "present", "present", "absent", "present"]
count = 0
for day in record_list:
if day == "present":
count = count + 1
print(count)3
If you need the position, use range(len(...)); the program then works however many values there are:
body_temp_list = [36.1, 36.4, 37.2, 36.5, 37.0]
for i in range(len(body_temp_list)):
if body_temp_list[i] >= 37.0:
print("Day " + str(i + 1) + ": Caution!")Day 3: Caution! Day 5: Caution!
Finding the largest value yourself
The max() function does this, but the idea behind it matters: keep the "best value so far" and compare each value with it; when you find a bigger one, replace it:
marks = [78, 92, 85, 64, 90]
biggest = marks[0]
for mark in marks:
if mark > biggest:
biggest = mark
print(biggest)92
Building a new list with append
Start with an empty list [] and add to its end with .append(value). This is how you collect the values that match a condition:
marks = [78, 45, 85, 30, 90]
passed = []
for mark in marks:
if mark >= 50:
passed.append(mark)
print(passed)
print(len(passed))[78, 85, 90] 3
marks[0], not 0; if all values were negative you would get 0, which is not even in the list.Practice
Fill in the two blanks using len(body_temp_list) instead of writing 14, so the average temperature is printed rounded to one decimal place: 36.5.
body_temp_list = [36.1, 36.4, 36.6, 36.5, 36.7, 36.0, 36.2, 36.8, 36.6, 36.5, 36.9, 36.4, 36.7, 37.0]
total = 0
for i in range(___):
total = total + body_temp_list[i]
print(round(___, 1))Press Run to see the result. Go through every value of body_temp_list and print Watch out! for each value that is 37.0 or more.
body_temp_list = [36.1, 36.4, 36.6, 36.5, 36.7, 36.0, 36.2, 36.8, 37.1, 36.5, 36.9, 36.4, 36.7, 37.2]
Press Run to see the result. Count the absent days: add 1 to count whenever the value is "absent", then print count after the loop.
record_list = ["absent", "present", "present", "present", "absent", "present", "absent", "present", "present", "present", "absent", "present", "present", "absent"] count = 0
Press Run to see the result. Print 85 or above once for each mark in mark_list that is 85 or more.
mark_list = [48, 55, 74, 85, 63, 57, 91]
Press Run to see the result. The lowest temperatures of 6 winter nights are in temps. Find the smallest value without using min(), and print Lowest: -2.
temps = [3, -1, 4, 0, -2, 5]
Press Run to see the result. From prices, build a new list cheap holding only the prices under 100, using append, then print it (output [45, 80, 15]).
prices = [45, 120, 80, 300, 15]
Press Run to see the result. Key points
- Total:
total = 0before the loop,total += xinside it; average istotal / len(list). - Counting: a counter that starts at 0 and goes up inside an
ifinside thefor. - Largest value: start with
list[0]and replace it whenever you find a bigger one. new = [], thennew.append(x), to build a new list.