EEEE 460 · Lecture 1

Lists and For Loops

Review and practice

EEEE 460 · Lecture 1 · List review

Lists store an ordered sequence

Create a list with square brackets.

Read or replace an item by its zero-based index.

Negative index -1 means the last item.

temps = [31, 34, 36]
temps[0] = 32
print(temps[1])  # 34
print(temps[-1]) # 36
EEEE 460 · Lecture 1 · List review

Grow, measure, and test a list

append adds one item at the end.

len returns the number of items.

in checks whether a value is present.

A slice selects a range; its end index is excluded.

codes = [200, 404]
codes.append(500)
print(len(codes))   # 3
print(404 in codes) # True
print(codes[0:2])   # [200, 404]
List exercise 1 of 4

Trace indexing and assignment

What is printed?

levels = [4, 7, 9]
levels[1] = levels[0] + 2
print(levels)
Answer
[4, 6, 9]
List exercise 2 of 4

Build the required result

Complete the two blanks so the final list is [18, 21, 24].

values = [18, 21]
values.____(24)
print(values[____])  # must print 21
Answer
values.append(24)
print(values[1])
List exercise 3 of 4

Predict two expressions

For names = ["Ali", "Noor", "Sara", "Omar"], what do these expressions produce?

len(names)
names[1:3]
Answer
4
["Noor", "Sara"]
List exercise 4 of 4

Spot and repair the bug

The program should print the last sensor reading. Why does it fail, and how do you fix it?

readings = [12.4, 12.7, 13.1]
print(readings[len(readings)])
Answer
# len(readings) is 3; the last valid index is 2
print(readings[len(readings) - 1])
# or: print(readings[-1])
EEEE 460 · Lecture 1 · For-loop review

A for loop visits each item

The loop variable receives one list item at a time.

The indented block runs once per item.

Choose a variable name that describes one item.

voltages = [4.8, 5.0, 5.1]
for voltage in voltages:
    print(voltage * 2)
# voltage = one reading
EEEE 460 · Lecture 1 · For-loop review

range generates a counting sequence

range(4) gives 0, 1, 2, 3.

Use the count as an index when the position matters.

Use direct iteration when only the values matter.

for i in range(4):
    print(i, labels[i])
for label in labels:
    print(label)
Loop + list exercise 1 of 4

Accumulate a total

Write a loop that calculates the sum of powers without using sum().

powers = [8, 12, 10, 15]
One solution
total = 0
for power in powers:
    total = total + power
print(total)  # 45
Loop + list exercise 2 of 4

Filter while building a new list

Create a list containing only temperatures greater than 35.

temps = [33, 37, 35, 39, 31]
One solution
hot = []
for temp in temps:
    if temp > 35:
        hot.append(temp)
print(hot)  # [37, 39]
Loop + list exercise 3 of 4

Update every item by index

Increase every value in scores by 5. Modify the original list.

scores = [60, 72, 81]
One solution
for i in range(len(scores)):
    scores[i] = scores[i] + 5
print(scores)  # [65, 77, 86]
Loop + list exercise 4 of 4

Find a position

Print the index of "fault". Assume it appears once.

states = ["ok", "ok", "fault", "ok"]
One solution
for i in range(len(states)):
    if states[i] == "fault":
        print(i)  # 2