Accessing Items in a List
Contents
Accessing Items in a List#
Consider a Circuit with Resistors in Series#
Letβs use a circuit with 9 volts of energy and three different resistors in series.
Letβs use nested lists to store information about the circuit.
# create each list individually before nesting
r1 = [3, "series", 1, 2]
r2 = [10, "series", 2, 3]
r3 = [5, "series", 3, 4]
# make a nested list of the resistors in the circuit
circuitSeries = [r1, r2, r3]
Ohmβs Law shows that the voltage (E) is equal to current (I) multiplied by resistance (R).
\(E = IR\)
Letβs apply Ohmβs Law to find the current going through each resistor and append it to the nested list.
# voltage through the circuit
V = 9
# current through first resistor
circuitSeries[0].append(V / circuitSeries[0][0])
print(circuitSeries[0])
[3, 'series', 1, 2, 3.0]
# total resistance and current
totalResistance = 0
for resistor in circuitSeries:
totalResistance += resistor[0]
print(totalResistance)
18