Teaching Python to Beginners.
Teaching Python to a total noob
How hard can it be!
Recently, I had the pleasure of becoming a tutor for someone needing to learn how to program for the very first time. Having been a programmer for over four decades (OAF), I was quickly reminded of how much accumulated knowledge we who are in the know take for granted. Although my knowledge of computer science, and programming languages in general is extensive, I used the occasion to revisit aspects of the python language that are less obvious to a novice.
Loopy Loop
So every new programmer learns how to use a loop to accomplish a repetitive task. The most basic loop starts with an index variable, and loops on a condition - either counting up or counting down thereby satisfying the boolean condition. For example:
count = 0
while count < 10:
print(count)
count +=1
How else might we convey an idiom to loop n
times? I think the while
construct is the easiest
idiom to employ for the novice. From there we could demonstrate looping using ranges.
Ranging
Python has the built-in range
function that expects at least one number (although it takes three parameters,
where the first and last default to 0, and 1 respectively). range
constructs a range of integer values from the
starting value, inclusive, to the ending value, exclusive. For example, summing the number between the range [0, 10):
sum=0
for x in range(10):
sum += x
Suppose we want to add only the even numbers? range
allows us to specify a step (the third parameter). So we can sum the evens by:
sum=0
for x in range(0,10,2):
sum += x
The range
built-in function is very flexible. We can start with a left-side range value greater than the right-side range value.
Using a negative value for the third value will step backwards through the range. For example, start with 10, decrement by 2 and stop at 0.
for i in range(10,0,-2):
print(i)
One thing to keep in mind, regardless of the values of the first and second parameters, the range always is inclusive on the first parameter, and exclusive on the second. So in the above example, 0 will not be part of the range. If we wanted to print out a zero, the range would need to extend past zero.
for i in range(10,-1,-2;
print(i)
Now that we have sufficiently explained ranges and we use them to loop, let’s go further and motivate the use of the range keyword in a comprehension. Well, before we dive into comprehensions, let’s step back and talk about lists.
lists
Python’s List data structure lets us store more than one value (of potentially different types) in a single variable.
x = [1,2,3, "rob"]
Using the built-in list()
function we can convert an iterable type to a list. ranges are iterable, so we can construct a list of
values from a range using the list function:
# create a list of odd numbers between 0 and 100
x = list(range(1,100,2))
So what does the list have to do with looping? We now have another way to iterate through a loop. We could iterate until the list has zero elements. For example:
# loop by removing the last element from our list until the list is empty
while len(x):
x.pop()
Perhaps a more natural way of looping through our list is to treat it as an iterable.
for i in x:
print(i)
Thinking of other ways of using ranges and lists to construct loops lends itelf to more natural choices for constructions that may seem more intuitive.
comprehensions
Okay, we’ve got ranges, lists, and iterables down, so let’s jump into comprehensions. Instead of explicitly constructing a loop to transform a range, we can put it all together in a comprehension. Let’s look at some examples of constructions that don’t use comprehensions, and then we’ll examine the solution with comprehensions.
Suppose we have a list of currencies in US dollars that we want to convert to Euros.
EX_RATE=1.5
usd = [10.00, 15.21, 21.00]
count = 0
euro = []
while count < len(usd):
euro.append(usd[count]*EX_RATE)
count += 1
print(euro)
Now let’s look at the same solution using a comprehension.
EX_RATE=1.5
usd = [10.00, 15.21, 21.00]
euro = [ x * EX_RATE for x in usd]
print(euro)
Comprehensions can be quite clever. We can use them to construct a list of primes. Here are two comprehensions working together:
# print the primes between 0 and 100
noprimes = [j for i in range(2, 8) for j in range(i*2, 100, i)]
primes = [x for x in range(2, 100) if x not in noprimes]
print(primes)
conclusion - teaching is hard
Starting with a basic while loops and progressing to the more complex constructions like comprehensions demonstrates some of the challenges of teaching a beginning programming. The takeaway I got is that you can’t push concepts too early. The beggining programmer needs to become comfortable with the idioms and constructs that they can use to express solutions before introducing ever-more complex idioms.