Loops and Lists¶
da PowerShell -- jupyter nbconvert --to html notebook.ipynb¶
Loops for Automating Repetitive Tasks¶
$$\large A=P\left( 1+\left(\frac{r}{100}\right) \right)^n $$
In [16]:
P = 100
r = 5.0
n = 0
while n <= 7: # loop heading with condition
A = P * (1+r/100)**n # 1st statement inside loop
#print(n, A) # 2nd statement inside loop
n = n + 1 # last statement inside loop
for n in range(1,8,1):
A = P * (1+r/100)**n # 1st statement inside loop
print(n, A) # 2nd statement inside loop
n = n + 1 # last statement inside loop
# helpi(range)
1 105.0 2 110.25 3 115.76250000000002 4 121.55062500000003 5 127.62815625000003 6 134.00956406250003 7 140.71004226562505
Boolean Expressions¶
In [23]:
t = 40
print(t == 40)
print(t != 40)
print(t >= 40)
print(t > 40)
print(t < 40)
True False True False False
In [25]:
x = 0; y = 1.2
print(x >= 0 and y < 1)
print(x >= 0 or y < 1)
print(x > 0 or y > 1)
print(x > 0 or not y > 1)
print(-1 < x <= 0) # same as -1 < x and x <= 0
print(not (x > 0 or y > 0))
False True True False True False
Using Lists to Store Sequences of Data¶
In [30]:
n = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
L1 = [-91, 'a string', 7.2, 0]
mylist = [4, 6, -3.5]
print(mylist[0])
print(mylist[1])
print(mylist[2])
len(mylist) # length of list
4 6 -3.5
Out[30]:
3
In [32]:
n = [0, 1, 2, 3, 4, 5, 6, 7, 8]
n.append(9) # add new element 9 at the end
print(n)
n = n + [10, 11] # extend n at the end
print(n)
print(9 in n) #is the value 9 found in n? True/False
del n[0] #remove the first item from the list
print(n)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] True [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
In [40]:
l = []
l.append(2)
print(l[0])
a = [1,2,3,4]
b = a
b[-1] = 6
print(a)
2 [1, 2, 3, 6]
Iterating Over a List with a for Loop¶
In [49]:
years = [0, 1, 2, 3, 4, 5, 6, 7]
r = 5.0
P = 100.0
for n in years:
A = P * (1+r/100)**n
print(f'{n:5d}{A:8.2f}')
0 100.00
1 105.00
2 110.25
3 115.76
4 121.55
5 127.63
6 134.01
7 140.71
Using the function range to loop over indices¶
In [52]:
P = 100
r = 5.0
N = 7
for n in range(N+1):
A = P * (1+r/100)**n
print(f'{n:5d}{A:8.2f}')
0 100.00
1 105.00
2 110.25
3 115.76
4 121.55
5 127.63
6 134.01
7 140.71
Filling a list with values using a for loop¶
In [57]:
P = 100
r = 5.0
N = 7
amounts = [] # start with empty list
for n in range(N+1):
A = P*(1+r/100)**n
amounts.append(A) # add new element to amounts list
print(amounts)
[100.0, 105.0, 110.25, 115.76250000000002, 121.55062500000003, 127.62815625000003, 134.00956406250003, 140.71004226562505]
Mathematical sums are implemented as for loops¶
In [60]:
N = 14
S = 0
for i in range(1, N+1):
S += i**2
How can we change the elements in a list?¶
In [63]:
v = [-1, 1, 10]
for e in v:
e = e + 2
print(v) # unaltered !
[-1, 1, 10] [-1, 1, 10] [-1, 1, 10]
In [65]:
v = [-1, 1, 10]
for i in range(len(v)):
v[i] = v[i] + 2
print(v)
[1, 1, 10] [1, 3, 10] [1, 3, 12]
List comprehension¶
In [76]:
P = 100
r_low = 2.5
r_high = 5.0
N = 7
A_high = []
A_low = []
for n in range(N+1):
A_low.append(P*(1+r_low/100)**n)
A_high.append(P*(1+r_high/100)**n)
In [78]:
P = 100
r_low = 2.5
r_high = 5.0
N = 7
A_low = [P*(1+r_low/100)**n for n in range(N+1)]
A_high = [P*(1+r_high/100)**n for n in range(N+1)]
Traversing multiple lists simultaneously with zip¶
In [80]:
for low, high in zip(A_low, A_high):
print(low, high)
100.0 100.0 102.49999999999999 105.0 105.06249999999999 110.25 107.68906249999996 115.76250000000002 110.38128906249996 121.55062500000003 113.14082128906244 127.62815625000003 115.96934182128899 134.00956406250003 118.86857536682123 140.71004226562505
Nested Lists and List Slicing¶
In [85]:
P = 100
r_low = 2.5
r_high = 5.0
N = 7
A_low = [P*(1+2.5/100)**n for n in range(11)]
A_high = [P*(1+5.0/100)**n for n in range(11)]
amounts = [A_low, A_high] # list of two lists
#print(amounts[0]) # the A_low list
#print(amounts[1]) # the A_high list
print(amounts[1][2]) # the 3rd element in A_high
110.25
In [89]:
L = [[9, 7], [-1, 5, 6]]
for row in L:
for column in row:
#print(column)
a=1
List slicing is used to extract parts of a list¶
In [92]:
a = [2, 3.5, 8, 10]
print(a[2:]) # from index 2 to end of list
print(a[1:-1]) # from index 1 to next last element
[8, 10]
Tuples¶
In [99]:
t = (2, 4, 6, 'temp.pdf') # define a tuple
t = 2, 4, 6, 'temp.pdf' # can skip parentheses
t = t + (-1.0, -2.0) # add two tuples
t[1] # indexing
t[2:] # subtuple/slice
print(6 in t) # membership
True
In [ ]: