Python - Week 6 of 6: Loops and Iteration

Dec 11, 2020 18:26

This is the final week of the Python course.  As before, Week 6 is split into four sections: Introduction, Lecture Materials, Assignment, and Bonus Interviews.  The introduction gives a brief overview of this week's topic and provides the link to Chapter 5 in the textbook and pdfs for the lecture slides.  This week is about loops and iterations which are the other basic programming patterns along with functions.

Lecture Materials
Four lectures this week!  43 minutes worth but most of that is in the final video.  It rather feels like there's a lot being crammed into the last week...
1. Loops and Iteration video (09:37)
2. Definite Loops video (06:28)
3. Finding the Largest Value video (08:25)
4. Loop Idioms video (18:21)

In the first video, we learn that loops and iteration are where computers do repetitive tasks, usually ones that we don't want to do.  Loops are the repeated steps and iterations are how many times the loop is repeated.  The main keywords used to do this are 'while' and 'for' which create indefinite and definite loops respectively.  Indefinite loops run until some condition is met and definite loops go through a specific set of things.  Indefinite loops need an iteration variable to control how many times the loop runs otherwise it will go on indefinitely.  Such as lather, rinse, repeat which has no iteration variable to tell you how many times to repeat and so you are there washing your hair indefinitely! 
In the second video, we learn more about 'for' and definite loops.  These are a lot simpler because you're giving Python a list of things to run through and then it finishes. 
The third video focuses on an example of a loop program in order to find the largest value in a set.   Dr Chuck shows half a dozen numbers on the screen and asks us to find the largest one.  Then he asks what our brain was doing while we were doing that little exercise, and what if we had to do it for a million numbers, and not just simple numbers either, but ones with half a dozen or more digits.  So then he goes through the process that our brain just did, comparing it to how it might work as a computer program.  He actually puts that process into code which is very interesting!  It definitely made sense looking at it that way. 
Finally, the fourth video crams in a whole lot of things that loops can do: counting how many items in a set, totalling a series of values, working out averages (which is basically the same as the total loop, just with the addition of sum/count),  and filtering for a specific criterion.  Then we learn about two more variables: Boolean, and None type.  And finally we learn about 'is' and 'is not' operators.  'is' is like a really really strong equals, and 'is not' is the opposite of 'is'.  Obviously.

Assignment
There's two assignments this week, the first isn't a submitted assignment. It's from the textbook and there's a video showing how Dr Chuck did it. I decided to have a go at working out the code myself before watching the video.
The exercise says: Write a program which repeatedly reads numbers until the user enters “done”. Once “done” is entered, print out the total, count, and average of the numbers. If the user enters anything other than a number, detect their mistake using try and except and print an error message and skip to the next number.
There's several parts to this which Dr Chuck showed how to do in this week's lectures, it's just a case of pulling out the relevant bits of code and fitting them together.  Well, I failed completely on this!  I was working on getting the program to check the input, print an error if it's not a number, and stop when the user enters 'done' but I got it completely wrong.  When I entered 'done', it told me it wasn't a number and carried on running!  I couldn't get out of it!  I had to email support at PythonAnywhere for help.  Thankfully they replied really quickly (within 5 minutes!) and told me that Ctrl+C would kill it.  Phew!  Rather a shame that Dr Chuck didn't provide that information though.
After much trial and error, I managed to get some of the program working.  It would ask for input, check that it was a number and exit when 'done' was entered.  I couldn't get it to do the total, count and average section.  I was running out of time as my access to the course expires on Monday so I had to give up on it and watch the video where Dr Chuck showed how to do it.  I had most of it right which was good to see. I'd put the continue in the wrong place which was why it was giving me an error when it came to doing the calculations.

The second assignment says: Write a program that repeatedly prompts a user for integer numbers until the user enters 'done'. Once 'done' is entered, print out the largest and smallest of the numbers. If the user enters anything other than a valid number catch it with a try/except and put out an appropriate message and ignore the number. Enter 7, 2, bob, 10, and 4 and match the required output.
This is similar to the above exercise, it wants the largest and smallest numbers submitted instead of the average.  I had actually been working on this one before the above assignment because I knew that the video would give me most of the code needed and it felt like cheating or getting extra help. Most of the code was similar for both assignments so I was able to make a couple of tweaks and get it right. My final code was:

largest = None
smallest = None

while True:
number = input("Enter a number: ")
if number == "done" :
break
try:
inum = int(number)
except:
print("Invalid input")
continue
if largest is None or inum > largest :
largest = inum
if smallest is None or inum < smallest :
smallest = inum

print("Maximum is",largest)
print("Minimum is",smallest)

The main errors I made were putting continue in the wrong place and putting the calculations after the loop. Not too bad really.

Bonus Interviews
The final video is fittingly an interview with Guido van Rossum, the creator of Python, called The Modern Era of Python.  There was more about dodgy start-ups and copyright issues than I was expecting.

And that's it!  There's a final video from Dr Chuck giving suggestions on what to do now. His main suggestion is to do another intro class, to build on his initial one by doing lots more intro classes until they're boring and at that point, to do an advanced class. He also suggests going to meet-ups and conferences, especially as it's a good way to make contacts and possibly to get a new job. He also gives various other resources for learning Python which have their own conferences. He ends it by saying that the single most important thing is to keep learning.

This was definitely a great starting point, I feel like I've learned so much. I'm certainly hoping to build on my tiny bit of knowledge and look at some of the resources listed previously as well as working through more of the textbook.

online learnings

Previous post Next post
Up