Python Notes
Python’s for Loops
Python’s for loops are a bit more versatile than pseudocode’s FOR loops.
Notably, it can loop over elements in the array directly!
Here’s a basic loop, like the one you’d write in pseudocode:
array = [0, 2, 4, 6, 8, 10]
for i in range(0, len(array)):
print(array[i])
this would print:
0
2
4
6
8
10
Here is another way of writing it:
array = [0, 2, 4, 6, 8, 10]
for n in array:
print(n)
this also prints:
0
2
4
6
8
10
Notice in the second example, we’re using array directly in the for loop. and so the variable
n gets the values in the array at each iteration. This is useful when we just want the items in
the array and not the index numbers: which is usually the case for totaling and counting.
Input validation
Input validation is a technique where you ensures that the user input is valid.
The basic pattern is:
<variable> = input("Message asking to enter: ")
while <condition checking if input is WRONG>:
<variable> = input("Message asking to enter again: ")
Here are a few examples:
# Input validation for even numbers
number = int(input("Please enter an even number: "))
while number % 2 != 0:
number = int(input("The number must be even; enter again: "))
...
# Input validation for a password that must have be
# between 6 and 15 letters long.
password = input("Please enter a password:" )
while len(password) < 6 or len(password) > 15:
password = input("Please enter a password of valid length: ")
Totaling
Totaling is used when you want to combine all values in the array into a single value.
The pattern is:
total = 0
for n in ...:
total += n
Here are a few examples:
array = [0, 2, 4, 6, 8, 10]
# Basic totaling
total = 0
for n in array:
total += n
print(total)
To total 2D arrays, you’d need a nested loop:
array = [[1,2,3], [4,5,6], [7,8,9]]
# Adding items in a 2-D array
total = 0
for i in range(0, len(array):
row = array[i]
for j in range(0, len(row)):
total += row[j]
print(total)
array = [[1,2,3], [4,5,6], [7,8,9]]
# Doing it the fancy way
total = 0
for row in array:
for n in row:
total += n
print(total)
Condition totaling is used when you only want certain values that passes a condition to be
counted towards the total.
array = [1, 2, 3, 4, 5, 6, 7, 8, 9]
# Example: summing up even numbers in an array
for i in range(0, len(array)):
if array[i] % 2 == 0:
total += array[i]
print(total)
This also works similarly with 2D arrays:
array = [[1,2,3], [4,5,6], [7,8,9]]
# Example: summing up every even numbers in a 2D array
total = 0
for i in range(0, len(array)):
row = array[i]
for j in range(0, len(row)):
if row[j] % 2 == 0:
total += row[j]
print(total)
Counting
Counting is very similar to totaling but instead of adding the value at every loop, it adds one at
every loop.
Basic pattern goes:
counter = 0
for i in ...:
count += 1
Here are a few examples:
array = [1, 2, 3, 4, 5, 6, 7, 8, 9]
# Basic counting
count = 0
for i in range(0, len(array)):
count += 1
print(count)
Conditional counting also works similarly:
array = [1, 2, 3, 4, 5, 6, 7, 8, 9]
# Counting every elements in an array that is even
count = 0
for i in range(0, len(array)):
if array[i] % 2 == 0: # Testing if element is even
count += 1
print(count)
array = [[1,2,3], [4,5,6], [7,8,9]]
# Counting the number of even elements in 2D arrays
count = 0
for i in range(0, len(array)):
row = array[i]
for j in range(0, len(row)):
if row[j] % 2 == 0:
count += 1
print(count)
Finding Minimum and Maximum
The simplest minimum/maximum searching follows this pattern:
Start with the min / max variable, initialized to the first item in the array. (For example,
array[0] )
Loop through