Key Ideas
The key ideas for this part centre on branching and loops.
Branching
Here's the basic if-else syntax. Any of the three clauses other than the if
clause can be dropped.
if condition:
# do this
do this
elif condition:
# do this
# do this
else:
# do this
# do this
This line always done
Simple comparisons can be combined with boolean operators:
if (a == 2) or (b == 3):
if (a == 2) and (b == 3):
Conditional branching can also be used in so-called 'ternary' assignments:
a = 10
b = "less than 5" if a < 5 else "more than five"
Looping
If you have a condition to reach, use while
:
a = 10
while (a > 5):
a = random.random() * 10
print(a)
If you have a sequence of things, you can use a for-each loop:
names = ("Dale","Albert","Gordon","Tamara","Philip","Chester","Windom")
for name in names:
print(name)
If you have a sequence of things, and you need to count through them because you need to compare with
another sequence, use a for-each loop centred on numbers that can be used as indicies for subscription:
names = ("Dale","Albert","Gordon","Tamara","Philip","Chester","Windom")
surnames = ("Cooper","Rosenfield","Cole","Preston","Jeffries","Desmond","Earle")
for i in range(len(names)):
print(names[i] + " " + surnames[i])
break
drops from a loop clause altogether, whereas continue
drops the current iteration and
returns to loop again if appropriate.
2D Looping
You can nest for-each loops:
for a in [1,2,3]:
for b in [10,100,1000]:
c = a * b
If you know the number of elements in each row, you can alternatively use unpacking, thus:
data = [
[0,1],
[2,3],
[4,5]
]
for i,j in data:
print (str(i) + " " + str(j))
You can use nested loops to construct moving window algorithms. Here's a blurring kernel, which starts and ends one short of each dimension to avoid boundary issues.
data = []
data.append([220,221,222,223,226,230,234,238,241,241,242,243,243,243,244,244])
data.append([221,222,223,224,227,231,234,238,241,241,242,243,243,243,244,244])
data.append([222,223,223,224,228,232,235,239,241,242,242,243,243,243,244,244])
data.append([223,224,224,225,228,232,235,239,242,242,243,243,243,243,243,244])
data.append([226,227,228,228,231,234,237,240,242,243,243,243,243,243,243,244])
data.append([230,231,232,232,235,237,238,240,242,243,243,243,243,243,243,243])
data.append([233,234,235,236,238,239,239,241,242,242,243,243,243,242,243,243])
data.append([237,238,239,240,241,241,241,241,242,242,243,243,242,242,242,243])
data.append([241,242,242,243,243,243,242,242,242,243,243,243,243,242,242,242])
data.append([242,243,243,244,244,243,243,242,243,243,244,243,243,242,242,242])
results = [] # 2 shorter and narrower than data.
results.append([0,0,0,0,0,0,0,0,0,0,0,0,0,0])
results.append([0,0,0,0,0,0,0,0,0,0,0,0,0,0])
results.append([0,0,0,0,0,0,0,0,0,0,0,0,0,0])
results.append([0,0,0,0,0,0,0,0,0,0,0,0,0,0])
results.append([0,0,0,0,0,0,0,0,0,0,0,0,0,0])
results.append([0,0,0,0,0,0,0,0,0,0,0,0,0,0])
results.append([0,0,0,0,0,0,0,0,0,0,0,0,0,0])
results.append([0,0,0,0,0,0,0,0,0,0,0,0,0,0])
for i in range(1,len(data)-1):
for j in range(1,len(data[i])-1):
print(str(i) + " " + str(j))
sum = 0
sum += data[i-1][j-1]
sum += data[i-1][j]
sum += data[i-1][j+1]
sum += data[i][j-1]
sum += data[i][j]
sum += data[i][j+1]
sum += data[i+1][j-1]
sum += data[i+1][j]
sum += data[i+1][j+1]
results[i-1][j-1] = sum / 9
print(results)
print("---------------")
print(data)