Skip to content
Home » University of Michigan » Python for Everybody Specialization » Programming for Everybody (Getting Started with Python) » Week 7: Chapter Five: Loops and Iteration

Week 7: Chapter Five: Loops and Iteration

Loops and iteration complete our four basic programming patterns. Loops are the way we tell Python to do something over and over. Loops are the way we build programs that stay with a problem until the problem is solved.


Lecture materials


Video: 5.1 – Loops and Iteration

This video introduces loops and iteration in Python, highlighting their role in automating repetitive tasks and solving problems efficiently.

Key takeaways:

  • Loops: They allow repeating code blocks until a specific condition is met.
  • While loops: Indefinite loops that run continuously until the condition becomes false.
  • Iteration variable: Controls how many times the loop runs (e.g., n in “while n > 0”).
  • Infinite loops: Dangerous loops that run forever due to unchanged iteration variables.
  • Zero-trip loops: Loops that never run because the initial condition is already false.
  • break statement: Exits the loop immediately, jumping to the line after the loop block.
  • continue statement: Skips the remaining code in the current iteration and restarts the loop.
  • For loops: Definite loops that run a specific number of times or iterate over a sequence.

Remember, carefully design loops to avoid infinite loops and ensure proper termination. Next, the video will explore definite loops using “for”.

Welcome to Loops and Iteration. Basically, this is where computers
do repetitive tasks that we humans don’t want to do. This is where the real power and
the real benefit of computers happen. Each time through the loop we
have to do something smart. But we can tell a computer to do it a
million times really, really quite easily. And so the basic idea is that at some
point in the code, you go back up, right. The idea is that you’ve done something,
let’s go do that thing again. And the way we express that in
Python is with a couple of keywords. One of the keywords is the while
keyword and the for keyword. And so we’ll start talking about the while. And in a bit,
we’ll talk about definite loops using for. These are called indefinite loops. So while is a keyword and this is like
an if statement, this is a question. Is n greater than 0? That’s leads to a true or false answer. And if it is true, this code executes and
if it is not true, the code is skipped. And in that respect,
it is exactly the same as an if. You could say if n was greater than 0,
run this code, otherwise skip this code. But that’s where things get
a little bit different. So if it’s true in this case,
n is 5, so it is true. So it runs this. It prints out 5, which prints that out and
then subtracts 1 from n, which makes it 4. But with the while statement,
it goes back up again. And it rechecks the question. That’s another way to say this is here,
re-checks the question. And as long as n greater than 0 remains
true, it will dive in and do this again. So it dives in and prints out 4, and
then it subtracts to 3 and goes up. Checks again, checks again as long as
it’s there, down it goes again, so it prints 3 and then subtracts to 2,
and then prints 2 and subtracts to 1, and then prints 1 and subtracts to 0. So in this case, n right now,
is we printed that and now n is 0, and it comes up. n is 0, and it comes up. Is n greater than 0? Well, it now switched from all
the times yes to now no, okay? And so, when it’s no,
it comes down here and it’s done. So when it comes up here, it skips down
to the next line and then it finishes, so it prints out Blastoff! And we see that, when we came out
of the loop, n was 0. So that was like a residual value of this
little variable n after the last iteration through the loop. And that’s because it was 1 and
then we subtracted 1 from it. And that got us to 0. So, an important part of any loop is what
we call the iteration variable, okay? And that is something that changes, because if we don’t change anything in
the loop, then it’s going to run forever. That’s what we call an infinite loop. So here we have n greater than 5 and
we say while n is greater than 0, which is true, print this, come back up,
check again. Is n greater than 0? Yeah. Print this, check again,
is n greater than 0? Print this. And this is when you read your shampoo and
it says lather, rinse, and repeat. This is what a computer scientist
thinks when they read that. It’s like, that would be an infinite loop. You’re going to run out of water, you’re going to run out of shampoo, you’re
going to run out of something, right? But the technical problem is,
is that n is not changing. n is not changing. And so what happens is there’s no way for
this true to become false. There’s no way, it just stays true,
and so that’s an infinite loop. And that will literally run until
your computer’s battery runs out or you unplug it or
you hit some escape or whatever. If your computer is spinning in a beach
ball, it’s got some little weird little thing, it’s probably in a loop that
is either infinite or nearly infinite. So this is a bad thing to do
because it locks up computers. If you write code to do this and
you run it on your computer, you will probably get some kind
of a spinning beach ball or whatever that your computer,
whichever your operating system does, because it’s not good to put your
computer in an infinite loop. And so we need somehow to have
an iteration variable that changes and in this previous one,
I subtracted 1 from n. And so this variable eventually
went from true, true, true, true, true, false, and so it became false. This question became false,
which gets us out of the loop. This variable that we use to control it
is what we call an iteration variable. Because it controls how
long the iterations run and when the iterations stop. So this is an infinite loop because we’re
not affecting the iteration variable. The iteration variable still is n. And then we have another sort of version
of this loop that shows that these loops are what are called zero-trip loop. And that is when this one comes in,
this one starts out false. And so it skips it. And so this basically
functions like an if statement. So it’s okay, it comes in,
it’s false, skip. Never runs either of these statements. Now, we have some statements that
we can use to get out of a loop. One of them is the break statement. And it’s an executable statement. When it runs,
it basically breaks out of the loop, moves to the line beyond
the end of the loop. So wherever we’re at, when this code runs,
it jumps out of the loop. And so we’re going to have a loop. This is an infinite loop. I just told you not to do infinite loops, but we’re going to use break
to get out of this loop. So while true, remember I said, it starts
out true, can it ever get to false? Well, in this case, it’s never going to
get to false because it’s always true. While true, we’ve constructed an infinite
loop and what does this loop do? It asks for a line of input
with a little arrow sign. And then we type something
like hello there. It goes in, we check to see if
the line we typed was d-o-n-e. If so, we break, that’s not true,
so then we print the line. Then we go up, prompt again, print it. Prompt again and this time,
we type in done. So done gets typed. So this becomes true, so we execute here. And it immediately leaves, so
it doesn’t print the word done here. It prints that string to
indicate that we’ve left. This can be quite a long loop in here. This can be a lot of and you can even
say break a couple of different places. And you usually put
the break inside an if test. And as soon as the break executes,
the loop is done, it is out of the loop. So you can think of it as Here’s the block, here’s the de-indent. So it’s this much, this is a block. The break escapes the block, right? It gets out of that loop block,
the block of code that is the loop. You can think of it as like
a transporter, right? You’re coming in,
you’re doing a bunch of stuff. You’re doing this and
you’re doing this and you’re doing this. Some other thing, some other thing,
and like, oh bang! Hit the break key! Leave. Right? So whatever it is, however much code is in this loop,
the break says get out of this loop. Get to the next line
beyond the end of the loop. It’s not to go to a different
place in the loop. It’s escape the loop directly and
instantaneously. That’s the break statement. The other statement that does
something like this is the continue. And so continue basically says
quit on the current iteration and go to the next iteration. So it skips out of the loop, but it doesn’t skip to the line beyond it,
it skips back up to the top. So continue says, oh,
we’re going to go up to the top. Break says get out and continue says
don’t do the rest of this iteration but go up and do the next iteration. So we have taken this
exact same bit of code and the only bit we added to it was right here. So, we read a line. If the first character is a pound sign,
we continue. If the entire line is done, we break. And so breaking is what
gets us out at this point. Get out and we say done. And what we do is if we find a line like
this one that has pound sign as the first character, we skip back to
the beginning of the loop. We go back down and we do another input. So we don’t print that. But we say something here and
so it does not print the line. So it skips that. So it’s like skipping to the top of
the loop, not skipping out of the loop. So break skips out of the loop and
continue skips to the top of the loop. And again, same kind of thing, you’re running
through this loop, all kinds of ways. You don’t hit the continue. You just happen to stumble and you run
a continue statement and you go back up to the top of the loop, meaning there’s
nothing down here that you run. The continue doesn’t get
you out of the loop, the continue goes to the next
iteration basically. Abandons the current iteration and
goes to the next iteration. So these while loops
are kind of indefinite. You can construct them cleverly. They just go and go and go until some
logic or condition becomes false. Hopefully, you’ve constructed
all the loops except one. I constructed it, so that it would
eventually either exit the loop or the condition would become false. It’s a little hard to sometimes
verify that these things are perfect. Sometimes it’s a little tricky to make
sure that the loop will terminate. Up next, we’ll switch from using while to
using for and talk about definite loops.

Video: 5.2 – Definite Loops

This video dives into “for loops” in Python, exploring how they differ from “while loops” and their benefits for iterating through collections like lists and strings.

Key takeaways:

  • Definite loops: Finite loops iterating a set number of times through a collection.
  • “for” keyword: Introduces the loop, followed by the iteration variable, “in,” and the collection.
  • Iteration variable: Holds the current element during each loop iteration.
  • Benefits: Predictable execution, easier validation, clear iteration count.
  • Examples:
    • Looping through a list of numbers and printing each one.
    • Looping through a list of names and sending greetings.
  • Comparison with “while” loops: While loops rely on a condition to stop, while “for” loops run a set number of times.
  • Next up: Exploring loop idioms for common tasks like finding maximum/minimum values or checking for element presence.

Remember, “for” loops simplify iterating through collections and provide predictable execution, making them a valuable tool in Python programming.

So up to now we’ve been talking
about indefinite loops, and that’s the use of the while keyword
that just runs until some logical condition is
false or you hit a break. Definite loops are finite. They are going over, say, all the lines in the file or
all the items in a list or all the characters
in a string or something. And for this, we use the “for” construct. And it’s more predictable; and it’s easier even to validate them because we have a set of things
that we’re going to go through, and it might be a lot, but we’re going to
go through all the things in the set. So here’s a little loop, the for loop. So the for key – the for is the keyword. And so, while was the keyword for indefinite loops, and for is the key word for definite loops. So the first thing we see in a for loop is we see the iteration variable is explicitly
just part of the syntax. i is, you can pick any variable you like. I happen to pick i, and everyone picks i
for integer iteration variables. in is another Python reserved word. And then we have various forms of collections and we’ll find that files are
collections, lists are collections. This happens to be a list of five integers: 5, 4, 3, 2, 1. And what we’re doing in this for statement
is we’re saying – and again, it’s like, it’s got an indented block. And you got a de-indent, so the block has this stuff. I don’t know, one of these days I’ll
stop writing all those little pictures. I can’t help it. But at some level, what we’re saying is
execute this block five times. And I want i to take on the successive
values: first time through 5, second time through 4, next 3, then 2, then 1. So it executes this one, two, three, four, five times. And the first time, i is 5, print. The second time, i is 4, print. The third time, i is 3, print. 2, 1, and then we’re all done. Blastoff. And so we just, we construct it in a way that we just have
directed Python to do all the work. We have an iteration variable. We have a set of things we want to loop through. And it just guarantees that it’s
going to run this once, code once, for each of these five variables. Or that’s another way to draw that would be, you know, run it once for 5, run it once for 4, run it once for 3, run it once for 2,
and run it once for 1. 5, 4, 3, 2, 1, Blastoff. Right? So, that’s very definite. Python takes care of everything for us. We don’t have to have a logical condition to
stop or anything like that. It’s really quite nice. This doesn’t have to be a list of numbers. This is a list of strings. And so, I put this in a variable. And this is where you’ve got to be careful
because I named it friend and friends, which makes perfect sense, but don’t believe for a moment that
Python understands what plurals are. So, friends is a list of three strings, and friend is an iteration variable
that’s going to go through it. So, I stick this list into friends. So square brackets, list of strings:
Joseph, Glenn, and Sally. And then I have this iteration variable friend that’s going to go through each of the strings in friends, so that means that friend is going to
go once Joseph, the next time it’s going to
run the loop again with Glenn, and the third time it’s going to
run the loop with Sally. And so it says “Happy New Year: Joseph,” “Happy New Year: Glenn,”
“Happy New Year, Sally.” So you know that this code is going to run three times, and friend is going to take on Joseph, Glenn, and Sally in successive iterations through the loop. So you can think of the for statement as
sort of a complex thing. It is dealing with the fact of
when it’s time to continue and when you’re done,
it knows about that. And it also sets i to the new value. So it’s like, are we done yet? No. Move i ahead to the new value. Print it. Are we done yet? No. Move i to the
new value then print. On and on and on. Oh! Now we’re done. All this logic here, this yellow bit, is constructed by you using the for statement. You can tell it what to do, and it just writes the code. You can do this with an i increment or a subtracting, and a while loop, and while i greater
than zero, you could do this. But a for loop just does it for you. So for does a few things for you. And I like, I like in, I think it’s basically saying for
each of the values in 5, 4, 3, 2, 1, have i take on the successive values
and run that loop one time. And that’s the “in”. I think the in is a well-thought-through
Python keyword because that’s how I think of it. If you’re a math person, think of it, you know, the member of function, I think
that’s the member of. Boy, it’s been a while
since I did set theory. I think that’s member of. So, here’s an interesting thing. You can kind of, you certainly can think of this as an iteration, but at some level, it’s really just a contract that says run this code five times with i taking on its,
on the values. So you hardly know the difference
between it saying i equals 5, print i; i equals 4, print i; i equals 3, print i; i equals 2, print i; i equals 1, print i. So, in a sense, this is what happens, meaning that the for loop is telling us
how long we’re going to run and it’s managing the successive values of i to make sure that our happy little line of code gets executed five times with the right value of i. That is the job of the for statement. The for statement takes care of all this for us. So that’s one way to think about the for statement, even though this is a more common way to
think about the for statement. This is sort of equivalent and think of
those two things as equivalent. So, like I said, definite loops are for lists or lines in a file
or characters in a string, and, you know, they iterate through members of a set. Up next, we’re going talk about sort of loop idioms, and that’s how we make use of loops
and how we use loops to do things like find the largest
or find the smallest or check to see if a value is present
or something like that. So that’s what’s coming up next.

Video: 5.3 – Finding the Largest Value

This video delves into the concept of loop idioms in Python, focusing on how to use loops to solve common problems like finding the largest or smallest value in a set.

Key insights:

  • Iterating towards a result: Loops don’t instantly know the answer; they progressively update a variable (e.g., largest_so_far) with each iteration until the final result is obtained.
  • Tracking the “so far” value: A crucial step is initializing a variable (e.g., largest_so_far) before the loop and updating it within the loop based on comparisons with the current element.
  • Sequential approach: Unlike humans who can grasp larger patterns, computers analyze data sequentially, making “so far” variables vital for keeping track of progress.
  • Example: finding the largest number: By initializing “largest_so_far” and comparing it with each element in the loop, we retain the highest value encountered until the loop finishes, revealing the actual largest number.
  • Beyond finding the largest: This pattern applies to various tasks like finding the smallest value, counting occurrences, calculating averages, or summing elements.

Next up: The video will explore further loop idioms for tackling these types of common problems in Python.

Remember: Loops are powerful tools for iteratively approaching computations and accumulating results. By understanding the “so far” concept and implementing it effectively, you can utilize loops to solve diverse problems in Python.

So now we’re going to talk about
what we’re trying to accomplish with a loop, right? We’re trying to,
we know how they mechanically work but what are we looking for? What if we’re looking for
the largest value or checking to see if 42 is
a member of a set or something? Or looking for the largest letter,
like the max function? And so we’re going to construct loops sort
of with an idea of doing something to each value in the set that
we’re iterating through. And then coming up with
some kind of result. And the pattern that we’re going to do
is we’re going to write a for loop. And actually, in this next two segments,
I’m going to do the exact same for loop. But we’re going to do something
before the loop starts, set some variables to initial values. And then we’re going to do something
to every one of the values in our list. And then, we don’t know what the largest
value is while the loop is running. And our goal is, when the loop finally
finishes, that we have something. Whether it’s the maximum,
the minimum, the average, the total, how many things there are,
how many things match. And so, the iterations are getting
us closer to knowing the answer. But they don’t instantly know the answer,
so we we have to work towards the answer. By setting something and
then sort of checking it a bunch of times. And then we have sort of have absolute,
the truth comes out at the bottom. And you’ll see this in a second. So here’s the little loop
that we’re going to do. And I’m going to do this over and
over and over again, and these numbers, you’re going to get
tired of these numbers. For some variable, iteration
variable in 9, 41, 12, 3, 74, 15. We print out a before. So we’re going to do
something before the loop. We’re going to do something during
the loop and after the loop. Right now,
I’m just printing everything out. So you kind of see how
the loop kind of works, okay? Okay, so here’s a little problem. I’m going to show you a number of
numbers and maybe it’ll be a million or maybe it’ll be 6, I don’t know. But you don’t know how many
I’m going to show you. And I want you to tell me when it’s all
said and done what the largest number is. And it’s not going to be too hard. But imagine that you had to do this for
a million numbers as a human being. That you were just sitting there and
numbers going by and by and by and by. How would your brain
truly solve the problem? So here, ready? Here goes some numbers. So did you get it? Did you get the largest number? It’s not so much interesting about
what the largest number was. It wasn’t that many numbers. What was your brain doing to sort of
track that problem, to solve that problem? What was your brain doing? And what if you
really had to do that for a million numbers? And they were not little
tiny numbers either, they were sort of medium-size numbers. And you had to do a million of them,
what would be your technique? Other than running away,
because humans aren’t good at this. But what would be the precise
technique that you’d use? And so here are the numbers. Now actually as a human,
we love looking at these numbers. Like oh, 74 . But then you ask, like how
did your mind exactly find them, right? Our minds go, like [SOUND] there’s 74. Our mind doesn’t look at them
the way a computer looks at them. It just kind of zooms in on 74 and
just kind of [SOUND]. But that’s not how a computer looks them. A computer has to look at them as 3,
41, 12, 9, 74, 15. I conclude at the very end that
74 is the largest number, right? But a human’s just [SOUND] 74. So humans think about this differently. And so we have to realize, the purpose
of that last little exercise, was to think when we construct loops how computers are
going to attack this kind of a problem. They attack it sequentially. They don’t attack it magically
the way we humans do. And the way that you do it is you create
in your head, and you probably did it, some notion of what is the largest
number I’ve seen so far. Like a variable. And we start and say oh, I haven’t seen
any numbers so far, so we’ll stick -1 in there. And then you see 3 and you go like well,
okay, that first number is also the largest I’ve seen so
far, so 3 is pretty good. If we stop now, and I tell you that’s
the last number, the largest number is 3. It’s not the largest number
until I tell you we’re done. We see 41, it’s like whoa,
3 is a bad number, 41 is even better. It’s way better, we’ll keep 41 as our so
far, so far, so good. But we’re not done yet,
we’ve got to keep going. 12, whoa, the 12 is no good because
41 is larger, we’ll keep 41. 9?
9’s no good, we’re in really great shape. Is 41 the largest? No, but it is largest so
far, so we know that much. Now we see 74, whoa, that’s way better
than 41, so keep track of that one. And then we go like, 15, nah
looks like 74 is pretty good. And now we’re all done,
and we see that it’s 74. So what happens is at the end of the loop,
the only thing we knew was the largest we saw so far and
when I tell you we’re all done. Then the largest we saw so far, like
just poof, it is the largest, right? because it’s all that
we’re ever going to see. So that’s how you have to
construct these loops in Python. So here’s a bit of code that does
this logical bit here, okay? And so I’m going to make a variable. And remember that variables can have
underscores in it, and I’m going to call this
variable the largest_so_far. And I’m going to set it
to -1 at the beginning. And before the loop starts,
remember I said there’s stuff we do at the beginning of the loop, and
then stuff we do during the loop. And then our payoff is after the loop,
after the loop. Okay? So before the loop we set
the largest we’ve seen so far, which we’ve seen nothing, so
we say -1, then we print it out. So before the loop largest is so
far is -1. Then we have an iteration variable, the_num is going to go through
successively 9, 41, 12, 3, 74. Run this block of code five times,
six times, once for each of those numbers. And we’re going to ask, if the number
we’re looking at, in this first case 9, is greater than the largest so far, then the largest so far is the number
we just looked at because it’s bigger than the one we saw before. So largest_so_far is
the largest up to this point. And then we kind of grab it basically. So then we print out, for
each iteration, the largest so far. So the first time we get 9 and
the largest we’ve seen so far is 9 because that’s the first one we
saw and it’s the largest we’ve seen. Then we go up, we advance to 41,
41 is the_num. And then 9 is largest_so_far,
41 is greater than it, so then we stick the 41 and
stick in largest_so_far. So the largest we’ve seen so far is 41. The one we just saw was 41,
go up again, now 12 is going to run. And so we see 12 and
the largest so far is still 41. And so we don’t do anything,
go back up, run 3, we get 3, largest so far is 41,
nothing needs to change. Then we run again, we get 74, 74 runs, and
that is the new champion largest number. And then we go back one more time, 15, in this code we don’t know
how many we’re going to do. We could do we could do 5 or 6 or
a million, but we don’t care, we just keep the best we’re
doing is largest so far. So we get 15 and
the largest so far is still 74. And now the for loop says
you’re done and we come out. Now, largest_so_far is actually the largest in the sequence of numbers,
which is 74. So this is how,
that’s a pretty messy slide. This is basically how Python can
go through a list and looking at the largest so far and then when we’re
done with the list, that is the largest. So you get the idea. We set something up before,
we do something to each value, and then at the end we kind of get the payoff of what
we were looking for in the first place. So up next we’re going to talk
about more of these loop idioms and how to find the smallest, and how to
count things, and how to do averages and sums and stuff like that.

Video: 5.4 – Loop Idioms

Summary of the Video on Loop Idioms in Python

This video delves into various loop idioms commonly used in Python programming. It focuses on how to effectively utilize loops to accomplish different tasks like:

– Counting: Iterating through a sequence and keeping track of the number of elements encountered. – Totaling: Accumulating the sum of all elements in a sequence. – Averaging: Calculating the average value of all elements in a sequence. – Filtering: Identifying and processing elements based on specific criteria. – Finding: Searching for a specific element within a sequence.

The video emphasizes the importance of utilizing “loop-carried variables” like counters, totals, and flags to track progress and achieve desired outcomes. Key points covered include:

  • Initializing variables: Setting variables like count and total to 0 before the loop commences.
  • Updating variables: Incrementing counters and adding values to totals within the loop iteration.
  • Conditional statements: Implementing if statements to filter elements or execute specific code based on conditions.
  • None as a flag value: Utilizing None as a placeholder for initial iterations before encountering actual values.
  • is and is not operators: Employing these operators to compare specific values like True, False, or None.

The video concludes by highlighting the versatility of loop idioms in tackling various scenarios and encourages viewers to practice implementing them in their Python code.

Here’s a tutorial on Loop Idioms in Python:

Introduction

  • Loops are fundamental constructs in Python that enable you to execute a block of code repeatedly until a specific condition is met.
  • Loop idioms refer to common patterns and techniques using loops to achieve various tasks effectively.

Types of Loops in Python

  • for loops: Iterate over items in a sequence (e.g., lists, tuples, strings).
for item in sequence:
    # Code to execute for each item
  • while loops: Continue looping as long as a condition remains True.
while condition:
    # Code to execute repeatedly

Common Loop Idioms

  1. Counting:
count = 0
for item in sequence:
    count += 1
print("Total items:", count)
  1. Totaling:
total = 0
for number in numbers:
    total += number
print("Sum of numbers:", total)
  1. Averaging:
total = 0
count = 0
for number in numbers:
    total += number
    count += 1
average = total / count
print("Average:", average)
  1. Filtering:
even_numbers = []
for number in numbers:
    if number % 2 == 0:
        even_numbers.append(number)
print("Even numbers:", even_numbers)
  1. Finding:
found = False
for item in sequence:
    if item == target:
        found = True
        break
if found:
    print("Target found!")
else:
    print("Target not found.")

Key Points to Remember

  • Loop-carried variables: Use variables like counters, totals, and flags to track progress within the loop.
  • Initialization: Set these variables to appropriate values before the loop begins.
  • Updating: Modify them within the loop to reflect changes in each iteration.
  • Conditional statements: Use if statements to make decisions and control code execution based on conditions.

Practice and Experimentation:

  • The best way to master loop idioms is through practice and experimentation.
  • Write code that utilizes loops to solve various problems and explore different scenarios.

So now what we’re going to do is
we’re going to keep working on this. We’re going to do something in the loop,
we’re going to do something before and something after. And we’re going to
accomplish different things. So the first thing that we’re going to
accomplish is counting something, right? I’m counting the number of things that
we are going to be looping through. Now, in this case, it’s kind of silly. But, just, in general,
these are called counters. So, let’s just say we had dot dot dot
dot, a whole bunch of these things. And we didn’t exactly,
we can look at this and say there are six of them,
that’s not the point. The point is, if we have a loop and in loop we want to keep track
of how many we’ve seen. Well, you do this counting thing, okay? So the way it works is
you set a variable to 0. This is the top part, normally this would
be called count, but I’m going to use a bad non-mnemonic value called zork,
just so you don’t get too used to it. So we so far how many have we seen? Well we’ve seen 0. And then we’re going to go it
thing is going to be 9, 41, 12. So thing is going to be
the iteration variable. Each time through the loop,
we’re going to add 1 to zork, increment is another word for
this, zork = zork + 1. We print out the thing
we’re looking at and we print out the current value for zork. So, the first time through 9, and then,
that’s the first thing we saw, 41, the second. So, and each time, this goes up by 1. It doesn’t really matter what these things
are, but then, we run this many times. And when we’re done then we say,
okay, loop is all done, right? There’s the end of the block. And when it’s all said and
done, there were 6 things. Now again, 6 is kind of obvious. We could just look at it and
know that there are 6. But this is a way to keep track of the
number of times that a loop is executed. You set it to 0 at the place, you add 1
to it, and then you print out the end. At the beginning, before it starts set
it to 0, add 1 each time through, and then print out the count at the end. So that’s counting. The next thing we’re going to do
is total up a series of values. So in this case we’re
going to total them up. It makes a lot more sense now and we’re
going to have another variable, zork. I would probably call this total. During the loop,
it’ll be the running total and at the end of loop it will be the total. And so total = 0, before we, that’s
the running total of what we have seen. So now 9 comes out and 0 + 9 is 9, so the running total is 9 and
9 was what we looked at. Then 41 comes in, then we take 9 plus 41. Running total’s 50, we read 41,
go back up, 12 comes in. 50 plus 12 is 62. So, that’s our running total. Next time we get 3. 62 plus 3 is 65. So, this line prints out. Then we get 74. So, 65 plus 74 is 139. So, that’s the running total. We go and do the last one, 15. 139 plus 15 is 154. We print that out. And now it goes back up but the for
loop is done, so out we come. So at the end, when it’s all said and done, this this running total
become the total, 154. So, we know certainly when you
print all that stuff out, right, you wouldn’t print that out. But at the end if you do this. And so the difference between the count
and the total is instead of adding 1 here, you add the thing you’re running
that you’re totaling up. So that’s how we compute totals. And of course,
we can do this with an average, right?` So now we’re going to have
a counter which we set to 0, a running total which we set to 0 in
the variable sum. We’re going to print out 0, 0. So, the count and the total is 0. We run through. Each time through, we add 1. So, it’s going to be 1, 2, 3,
4, 5, 6 each time through. And we have the sum = sum + value,
so, 9, 50, 62, 65, 139, 154. So this is the running total. This is the count. Actually that’s the sum in this case,
the sum variable. And then,
this is the thing that we’re reading in. And then we finally get done. because there was only six things. The for loop dumps this out. And then we got, okay,
how many did we get? We got 6. What was the total?
It was 154. And then divide sum by count and
that should be 25.0. Okay? And so we divide sum by count and
now we have calculated the average, actually this should be 154 divided by 6. I think that should be 25.6666 or
something like that. Okay? So you get the idea, and so you sum and so you can do more than one of these
things in a particular loop. Now, filtering is the idea that we’re looking
for something that meets some criteria. So we’re going to go through and
look at all the things in the loop. And we’re trying to figure out if
something is greater than 20 and we’re going to declare that large number. So this is how you put an if in the loop. So sometimes this loop will do nothing, sometimes this loop will
print out Large number. And so, value is going to be 9 and
that’s false, so it doesn’t do anything. Value is 41, that’s true,
so it prints out this. Value’s 12, so it does nothing. Value’s 3, it does nothing. Value’s 74 and so it prints this out. Value’s 15, so that’s false. Done. Now the for loop knows oh, we’re done,
go on to the next line. Okay? So this is kind of a filtering pattern
where we are going to do some if statement and conditionally run some code based on
the value that we are looking at for now. So that’s like searching for
large numbers in our long list of numbers. Sometimes instead of printing something
out in the middle of the loop, just like in functions, we don’t often
print in functions. We tend to prefer using return values,
sometimes we just want a variable that tells us whether something was
found or not. And so
we’re going to use a boolean variable. So boolean is another type of a variable. You’ve got integer variables, you’ve got string
variables, you’ve got floating point variables. Boolean is another kind of variable. Boolean is a kind of variable that either
has the value True or False, that’s it, it can only have two. And so False is a constant in Python. And so I’m going to say found. found is a mnemonic variable. I use
the word found for this all the time, but don’t get stuck on it. It just happens to be a variable
with the value False. So we print it out before, it’s False. And now we’re going to go through
each of these values again. And we’re looking for 3. And we want to know did we find 3 or not. So we have 9. And it stays False. We have 41, it stays False. We have 12, stays False. Now we have 3,
this all of a sudden clicks to True and so then we change the variable found, which
is the same found here, to be True. So this time when we come out up to 3
we run this code, then we print this, and it’s True. Now at 74, this is False, but
we’ve changed the variable for found, so it doesn’t change back to False. So we run the rest of the loop. And when we’re done, True. And so, at some level,
if you didn’t print all this stuff out. Before, after, that means we found a 3. There could be a million
times that we ran this and all we know when we’re done
is there was a 3 in there. So that’s what it’s saying. Found starts out False, it loops and
loops and loops and loops. And if it ever finds it
then found becomes True and then we can detect by the loop,
yes we found it. Yes, that happened. No matter how many numbers we looked at,
one of them at one point, at least one of them was a 3. Now, if we were a little tricky we
would realize that we could put a break right here. Right there, because once it’s True
it’s not going to go back to False, so we could stop. So we’ve done a bunch of different
things, right? We found the largest, we searched for a value. We did some sums,
we did some averages and now, we’re going to do the smallest value. So, just before we do the smallest
value let’s review what we did for the largest value. Okay, and so, we have this variable
called largest_so_far that we set to -1, and then we
print it out, it starts being -1. And then we’re going to
go through this loop, 9. And the largest so
far is 9 and then we go 41. We like that one better. Then we hit 12. We don’t want that. We went 3, we don’t want that. 74, we keep that and
then 15 doesn’t make any difference. Then it pops out and we get 74. That is the largest, not the largest so
far, but the largest you would get. So the question is, what would you do to change this to make it search for
the smallest value in the list? Now you can look, humans are good at this. Like hey, what’s the smallest value? It’s 3, yeah of course, you know that. No, we’ve got to think like a computer. How are we going to find
the smallest value in the list? What would we change here? What are some of things you might change? There is at least couple things
you would change, right? So the first thing you would do is, let’s just change this from a greater
than sign to a less than sign. And let’s just change
the word large to small. Is that going to fix it? Take a second, if I changed all the word
largest to the word smallest, and I changed the greater than to a less than,
is that going to fix it? If I did that,
what number’s going to come out here? Is it going to be 3?
Is that going to come out? So, like, if I did this? Is that going to make it better? Is it going to print 3? Obviously if I’m staring at you,
probably it’s not going to, this was trick question, right? It was a trick question. This is what happens when you run it. So you start out, smallest_so_far is -1. We come in with 9. Is 9 less than -1? No, it is not, so we skip. So, the smallest we’ve seen so
far is -1, and we go back up. Is 41 less than -1? No, it is not, so we skip. So the smallest so far, is -1. 12? No. 3 is not smaller than -1,
74 is not smaller than -1. 15 is not smaller than -1. So out we come and the
smallest we’ve seen so far is -1. No, it is not. So, what’s the flaw in our logic here? First off, the word smallest so
far doesn’t mean anything. This could be gorp or zap or x. So just changing the variable to say
smallest so far is highly misleading, okay? So, at this point, point right at the screen to the one place that has
the fallacy, the mistake in this code. If you pointed right here,
you’re correct, right? So baked into that when
we were doing largest so far was that these are positive numbers. And smallest_so_far,
if we’re starting at -1. If they are positive numbers,
well smallest_so_far is already smallest than
the smallest possible numbers. So what number might we put
there to make this work better? Well, some of you might suggest that
we would just say, that would be okay. I’ll make that be 100. It’s going to work fine, right? 9 will tick, 41 will ignore,
12 will not work, 3 will tick. And it will work,
it will work, that’s fine. That would work if you made it 100. But then what if there were three-digit
numbers in here, how will that work? Well, let’s make it 1,000. Yeah, that’s it. No, no, no, let’s make it a million,
or like more, right? What’s a big enough number? As a matter of fact, largest so,
that largest so far code that we did, if there were negative numbers, and they’re all negative,
this is not going to work so well. Wait, larger so far, yeah. This is just not going to work very well. Because what if they’re
all negative numbers? We just assumed they were
positive numbers, and it worked just fine, didn’t it? Whoops, whoops, whoops,
whoops, whoops, okay. So, there’s kind of a flaw. We were using this -1,
there’s a word for it. It is kind of like a flag value, it’s not
really the smallest number we have seen. It’s like an indicator that
we haven’t seen any numbers. And we can’t really pick
a number for this. Frankly, either in the largest or the
smallest, it’s just not going to work. So what are we going to do? Okay, so, this is what we do. We have yet another type of variable,
remember I had boolean, True and False, integer, floating point. There is a variable called None type,
None. It only has one constant in it. So booleans have true and false,
integers have a whole bunch, and then floats have a whole bunch, and
None types have one thing, None. We think of it as the absence of a value. The lack of a value, okay? And so, what we’re going to do instead, is in our
smallest variable, that we’re going to do. We’re going to say, you know what, before this loop starts,
the smallest number we’ve seen is nothing. We’ve seen no numbers whatsoever. And that’s going to be our marker to
indicate that we’ve seen no numbers. Okay? This is kind of like the found code
where we set found to be False and then later we set found to be True. But we’re going to use
smallest equals None. None is a variable, a value, that we can
distinctly detect different than numbers. So we can say is the contents
of smallest None? If smallest is None, is is like it’s
more powerful than double equal sign. Means is it exactly the same as. And so if we are asking is smallest None, that’s only true if we’ve
got a None in there. If we put 17 in, smallest is not None. So this is how it works. Start with smallest equals None. And then in the code we have
a little bit more intelligence. If smallest is None,
this means that it’s the first time. If the smallest is None,
smallest is value. So 9 is coming in here. And so, if the smallest that we’ve seen so
far is empty, we have seen nothing, then we’ll grab
the first one as the smallest one. So our 9 becomes the smallest one. And so that’s how we print out the
smallest so far is 9 and we just saw 9. Then we come up. The second time through this loop, this
is going to always be false from now on, because smallest is a number and
number is not None. Okay, so we have this flag
value that’s None just for the first time through the loop. So that’s false, so it’s going to run here, elif value
is less than the smallest, which is 41. Is 41 less than 9 in this case? And the answer is no, it is not. So, it stays 9, then 12, 3 works. So 3 causes this code to run,
so we grab it. So 3 is it and we go through 74 and 15. It runs two more times. And then, when we’re all done, we get 3. This is a flag value that we’re checking and through the loop
it will only be smaller, smallest will only be None the first time through the loop but after
that we’ll just ignore this little part. This is another way to think
about this is this is priming us. This is getting started. Okay? And this technique is a good way to
do the largest and the smallest. To use this None and then have a little
bit of code that triggers the first time through the loop to get your sort
of loop-carried variable setup. Okay, so the is and is not operator,
they’re both operators. Is and is not are like less than or
less than or equal to or not equal to. They don’t hurt their operands and
they return you a true or a false. Okay? So is, None,
and is not is also a logical operator. And there are other times we use it. You shouldn’t use is when you
should be using double equals, usually you’re using them for
a True, False, or None. So that we don’t overuse is, because is,
is a really, really strong equality. So it’s a stronger equality
than double equals to, so the double equals is mathematically
equal to with potential conversion. But is is a stronger thing, and
is not is the opposite of is. Okay, so in this chapter,
we’ve talked about some definite loops, some indefinite loops. break to get out of loops,
continue to pop back up. continue works in for
loops as well as while loops. Iteration variables, how with while loops
you construct them yourself and in for loops, for constructs them for you. And things like largest, smallest,
counting, average, etc., etc., etc. So we looked at some loop idioms where
you do something at the beginning, do something in the middle, and
then you get the payoff at the end.

Review: Chapter 5


Chapter 5 Assignment


Video: Worked Exercise: 5.1

Summary of Exercise 5.1 in Python for Everybody:

Goal: Write a Python program that:

  1. Repeats asking for a number until the user enters “done”.
  2. Calculates the total, count, and average of the entered numbers.
  3. Handles invalid input (anything other than numbers or “done”).

Solution:

  1. Initialize variables:
    • num: Iteration variable for count (starts at 0).
    • tot: Running total of entered numbers (starts at 0.0).
  2. Infinite loop:
    • Prompt user for a number using input.
    • Convert the input to a float (fval).
    • Check for “done”: If fval == "done", break the loop.
    • Error handling: Use a try-except block to catch invalid input (non-numbers).
      • try: Convert fval to float.
      • except: Print “Invalid Input” and continue the loop.
    • Update variables:
      • num += 1 (increment count).
      • tot += fval (add number to running total).
  3. Calculations and output:
    • Print “All done”.
    • Print the total, count, and average (total / count).

Key points:

  • Infinite loop with break keyword for exiting.
  • try-except block for error handling.
  • continue statement to skip faulty input and restart the loop.
  • Accumulator pattern for sum and counter pattern for count.
  • Conditional check for “done” before conversion.

Results:

  • The program prompts for numbers until “done” is entered.
  • Invalid input triggers an “Invalid Input” message and restarts the loop.
  • The program displays the total, count, and average of entered numbers.

Hello and welcome to Python For Everybody, doing work example. My name’s Charles Severance and I’m the instructor for the class. The work example that we’re going to work on right now is in Chapter five and it is exercise one. We’re going to repeat asking for a number until the word done is entered, and then we’re going to print the total. Then, we’re going to print the count, and then we’re going to print the average at the end. We’re going to enter some numbers and I’m going to do some error checking, and we’re going to keep on going. So, we’ll ignore this, we’ll just say invalid input and then we’re going to ignore it. So, I’m going to start from scratch. I’ll start my terminal, starts some Atom. So, I’ve opened the py4e folder and that’s cool because now I can do things like say, New Folder, and say I’d like an ex_05_01. Then, go on ex_05_01 and say File, New File, and then say File, Save As and put it in ex_05_01. Then, name the file ex_05_01.py. I’m going to start from scratch on this one instead of adapting another piece of code. I’ll say print five, and I’m going to do this because now I need to get to the point where I’m in the same folder in this terminal window, cd Desktop/py4e/ex, I can string these together. There I am and I say python3 ex, and there I go. So I’m in good shape. So, there’s a couple of things right now and we’re going to do the total count and average. So, this is just a basic pattern where we’re going to have, we’re going to need a iteration variable for the count. I’ll call that num, we start that at zero, and then tot and I’ll start that at 0.0. So, that’s the running count and the running total. Now, we need to write a loop, and I’m going to write this as an infinite loop, while true with colon and then I’ll indent, and I’ll prompt first string. Remember, input gives us a string, so I’m going to call this sval equals input ‘Enter a number’ colon space. I’m going to deal with the try and except later, but you can just know that the floating point value that we’re going to do is sometimes this little bit of code will fail. I’m just going to take the string value here sval, is for input returns us a string and now I’m going to convert that to float. I’m going to say print fval, so I can print that out. Then, I’m going to do the num equals num plus 1, and tot equals tot plus fval. Now, I do need to deal with the situation where I’m entering the word done. Now, we we want to check that before we convert it to a float because done, well, we can run this. It’s an infinite loop but it’ll only run a little bit, won’t cause us to much problem. If I run Python, let me drag this over here, and I go one, two, three. If I put in something bad, it’s running, I don’t have a way to get out. But you can see that it blew up on line five, it blew up right here in line five. So, what we want to do is we want to say, one, two, three done, but we wanted to detect that we’ve typed in done. So, here, we’ll just say, if the string value that I got back from input is double equal quote done quote break. So, that basically will break us out. Now, print all done. I should be using single quotes here, too much Java coding. Print all done, then I’m going to say print, what do I want to print, the total, the num, and then tot comma num comma tot over num. Now, we’ve got to be careful because we don’t want to divide by zero, but that’ll get us sort of a ways. So, this is going to run, this is going to read these things, it’s going to accumulate here, this is the accumulator pattern and this is a counter pattern, where we’re adding one dual current variable and accumulator pattern, where we’re adding a value to it. So, now, we should be able to see the done four, five, six and then done, and the total of four plus five plus six is 15, the number is three and the average is five. So, that’s really good, the all done prints out. I just did that for yaks and you can see the value that’s coming up, so that’s in pretty good shape. So, I’m going to comment this out and comment that out. So, this is pretty good, it works just the way we want it to work, four, five, six. But we do something other than we’re done, then we’re going to blow up in this float. So, this is where we’re going to have to do a try and except because we just know that this line, line seven is the danger zone. Okay. So, what we’re going to do, is we’re going to put a try in here and then we’re going to indent the part of code that seems strange. Then, we’re going to have some except code. The first thing we have to do in the except code is print out the word Invalid Input. Come back, print Invalid Input. Now, just like in an earlier example, we have to do something here to make sure it doesn’t just keep on going because fval doesn’t work, we’re not going to see the error message that would be the trace back here on line nine. We’re going to run here but we still don’t want to add because fval will be. So, this is where we can use the continue. So, in this code, we’re using both the break to say, if I’m all done break. If I have a problem, I’ll print a message out and then I’ll say continue. So, the continue basically says go back up to the top. So, that is how when we see enter some bad data, we print an invalid input, and without adding anything new, you don’t really see it here, without adding anything new, you go back up to the top and enter a second thing. So, now, if everything is right, I should be able to type dad input four, five, six. Bad input, bad input, done, and I have a total of 15, and three items, and the average is 5.0. So, there we go, that’s what we’re going to get. That roughly achieves the same thing and it’s a combination of a loop with a exit mechanism. We have some sanity checking of our input, so making sure that we have some valid input and we catch it and we use continue to loop back up to run the next iteration of the loop. We have an accumulator pattern and then we can use the accumulated data to print what we want to print. So, I hope that this has been useful to you, exercise 5.1 for Python for Everybody.

Graded App Item: Assignment 5.2

Code

Wrap-up


Video: What’s Next – Dr.Chuck

Summary of the Video on Data Structures in Programming:

This video highlights the importance of data structures in programming and motivates continuing the course despite reaching the halfway point.

Key points:

  • Recap: The first half of the course covered control flow, which tells programs what to do in different situations (e.g., if statements, loops).
  • Next step: The second half focuses on data structures, which are organized ways to store and manipulate data (e.g., variables, lists, dictionaries).
  • Significance: Clever data structures can simplify program logic and make solving problems easier than relying solely on complex control flow.
  • Motivation: Data structures are essential for understanding the full picture of programming. Stopping now would incomplete the understanding.
  • Break option: While taking a short break is acceptable, returning to finish the course on data structures is crucial for comprehensive learning.

Overall message: Data structures are a fundamental and powerful concept in programming. Completing the course and mastering both control flow and data structures will provide a complete understanding of how to create effective programs.

Congratulations. You’re halfway there. So I had to break this course
into two courses, mostly because if I made this course like
14 or 15 weeks long, you’d drop out and then
you’d be all bummed. So we have this moment
in the middle where if you need to take
a break, you’re fine. It’s like in a video game where you’re going through and then you pass a point and then when you
have a problem after that, you come back to that point. This is one of those points. So if you have problems
going forward, you don’t have to come all
the way back and do this. What we’ve covered so far is
what we call control flow. It’s like programs do this, then they do this, then
they have an if statement, and then they have a loop
and they go round and round, and then they do
this other thing and they read some stuff
and they do some stuff. The whole thing, that’s a sequence, it’s
steps, control-flow. What the program is
going to do next, and then what’s it’s
going to do next, and what’s it’s going to do next. So that’s what we talked. We talked assignments,
we talked if statements and for statements
and those kinds of things. We’ve learned how Python navigates through
your program and finds its way through the various steps that
you’ve asked it to do. But that’s only half a
programming. The next thing that
you’re going to learn is about variables and how you can put four in a variable and you can add two to it and then
it becomes six. But there’s much more
that variables can do and we call this data structures. That’s what we
computer scientists call this as data structures. It turns out, in your program, it may not seem like it, but we can solve problems
in a way far more easily with
clever data structures than with clever control flow. Control flow is obvious and
data structures are subtle. So by making
clever data structures, your control flow is simplified.
If that makes any sense. Of course, you
haven’t done it yet. So that’s why data
structures is important. That’s why you
shouldn’t stop now, that’s why you’re
halfway through. But again, if you need to
take a little time off, you can take a break
but come back and finish the next class
data structures, because the two
pieces, algorithms, control flow and
data structures are the two pieces of programming that give you the whole picture. So hope to see you
in the next class.

Bonus: Chapter 5


Video: Interview: Guido van Rossum – The Modern Era of Python

Summary of Guido van Rossum’s Talk on the History of Python:

Early Years (1989-2000):

  • Python created in 1989 by Guido van Rossum at CWI in the Netherlands.
  • Released as open source in 1991, fostering a growing community.
  • Guido worked at CNRI in the US (1995-2000), overseeing Python versions 1.3 to 1.5.2.
  • Disagreements about open source led Guido to join a startup focused on Python development.
  • The startup released Python 2.0 with Unicode and negotiated an awkward open-source license due to CNRI’s claims.
  • The startup quickly failed, leaving Guido and his team jobless but with valuable experience.

Zope and Python’s Growth (2000-2010):

  • The entire Python Labs team from the failed startup was hired by Zope Corporation.
  • Zope supported Guido and the team’s continued work on Python while also developing their own software.
  • Python conferences grew from small workshops to annual events with hundreds of attendees.
  • The community embraced self-organization, including identifying “Python warts” (issues needing improvement).
  • Version 2.1 to 2.3 aimed to fix some warts but faced limitations due to backwards compatibility.

Birth of Python 3 (2000s-present):

  • The idea of Python 3.0 emerged as a way to address numerous wart issues through incompatible changes.
  • Balancing innovation with user retention was a key challenge in developing Python 3.
  • Gradual adoption of Python 3 is evident at annual PyCon conferences, but full transition is still ongoing.
  • Python 3 is expected to ultimately win the race, despite resistance from some users preferring Python 2.

Overall:

The talk provides a personal and insightful perspective on Python’s evolution, highlighting the role of community, challenges, and key milestones in its journey to becoming a globally popular programming language.

[MUSIC] Python was created by
Guido van Rossum in 1989 while working at CWI in the Netherlands. Python was released as
Open Source in February. 1991. The open source community
grew around the world. And the first Python workshop
was hosted by NIST in 1994. >> From 95 to 2000, I worked in the U.S.
in Northern Virginia at CNRI. And so, there we worked through
a lot of growth of the community and the infrastructure. We created the Python website. I think when I started there,
Python 1.3 was about to be released. And then while I was there, we released several subsequent
versions leading up to 1.5.2. Which for some reason, 1.5.0 was nothing, but
1.5.2 remained the sort of, the standard, the gold standard of Python
for a very long time afterwards. Disagreement was brewing between
the people working on Python at CNRI and the CNRI leadership about open source. I was sort of courted by
a small no name startup, and they said, oh,
you gotta come work for us. We’re an open source startup. We’re going to build an
open source portal. And find a number of other
good Python engineers, and we’ll give you all the time you want
to sort of work on Python full time. And I thought, wow, that sounds great! Especially, given that I
already had this sort of discontent at the place where I
had been for five years, and so, reluctantly, I agreed to join these guys. Well, by then, by the time we
actually started, it was 2000. And with, our start date was May 15,
the bubble had actually already burst. I wasn’t even aware that there was
a bubble and that it had burst. I was in Northern Virginia
doing open source software. I wasn’t, I didn’t really know
what a startup was. I didn’t really know how to tell whether
these people were trustworthy or not. Well, we spent the summer in blissful
ignorance working full time on Python. We built and released Python 2,
which was a big deal. It contained Unicode. We also,
because we had to have some kind of graceful exit from my previous employer, who claimed a certain amount of
ownership over the source code. They didn’t claim that it was all theirs,
because it wasn’t theirs to begin with. It was open source before I joined, which
was an incredibly lucky circumstance. But, nevertheless, they sort of claimed ownership over what had been added to it
over the five years I worked there and other people who were also employed there,
also worked there. And so, with negotiations
facilitated by Eric Raymond, and I think also even
more in the background Eben Moglen, the free software lawyer, we negotiated some way to make sure
that Python remained open source, with a license that CNRI’s
lawyers were agreeable to. And that was sort of,
Python still has a very awkward license. And the history of that awkward
license lies in the way I left CNRI. So, this little startup,
we didn’t even move to the west coast. We just sort of, we worked from our homes. And I remember that once a week,
we had a team meeting and a catch-up with the leadership
in California in my living room. And within five months, it was over. They suddenly stopped paying us. And those five months, we had had the time of our lives working
on Python, doing this Python 2 release. We also had to do a Python 1.6 release, that was actually nearly
identical to Python 2.0. That was the first Python 1.0
release with CNRI’s license on it. And sort of over the summer and
the fall of 2000, this startup company began to show
more and more signs of dysfunction. Other teams suddenly disappeared. We later heard that they had been fired. They flew us out to the west
coast to talk to random potential customers,
to sort of show off our technical prowess and somehow, get some kind of deal
where we would do work for a customer. None of those deals ever closed. So they were going deeper and
deeper in the hole. And at the end of the year,
the investors decided to stop funding it. And then suddenly, we were like,
we were out in the streets. We were relatively well off,
because we had been paid well until then. But, yeah, the company was gone. So, what, all we got out of it was,
they had given each of us, they had bought each of us a good
computer to work from home. So, that was our last payment. [LAUGH] Cuz we talked to lawyers or somehow we got advice where someone said,
yeah, sure. Just hold onto that equipment. Nobody’s going to have the power
to take that away from you. They’re not going to go after you. And so, then there was actually,
well I wouldn’t call it a bidding war. But we were negotiating
with both Active State, which was a small software company
in Vancouver, and Zope Corporation, which was a small Python-exclusive
software company in Northern Virginia. And the whole Python labs team,
five of us, ended up working for Zope. And that was an incredibly lucky rescue. >> With the terms and conditions that
pretty much keep working on Python, right? I mean, that
Zope didn’t tell you you had to go- >> No, Zope was very clear. They said, we have no design on
the ownership of that software. We want you guys,
you guys are really good programmers. You guys are the top of
the Python community. You are going to get some time and it wasn’t stated very
precisely how much time. And we were fine with that at the time. We realized that what we had done for this failing startup was not
actually a realistic option. That they would just pay our salaries and
we would only do open source development. Nobody could maintain that, so
we had sort of grown up quickly. We did continue to do a lot of sort of fundamental Python
work at the time at Zope. We also did a lot of
fundamental Zope work. Over time, sort of, the team dispersed. I got a job offer to come work for
a small startup again in California. This time I did my
homework a little better. Python just kept, sort of, growing, and
the community kept self-organizing. And so, what started out as
a workshop we had with about 20, 25 people at most attending in ’94. Within a few years,
we had an annual, what we called the International Python Conference, with
with three or four hundred people attending. I always encouraged
the community to self-organize rather than sort of looking at me for
every decision. And so,
one of the things that gradually appeared was the notion of Python warts. I’m not sure, but I think that Andrew
Kuchling was actually one of the first to talk about Python warts, and sort of
blog about it or give a talk about it. Of course, different people have different
ideas about what’s wrong with Python. But, sort of the idea of
warts is things that keep tripping people up over and over. And so, there is at least some notion of,
these are objectively problems. Every sort of feature release,
2.1, 2.2, 2.3. We tried to fix some of these things. But we also came up against a wall of
things that we could never fix because the fix was sort of, required changing
the language in an incompatible way. Because it’s always
easy to add new syntax, that sort of doesn’t get in
the way of existing syntax. But it’s very hard to
take away a particular syntactic construct that has
an unfortunate but well-defined meaning. And all these things built up and,
sort of, in the middle of the first
decade of the new century, this idea of Python 3000 was born. Like, okay, we have so many things that we
would like to fix. We know roughly how to fix them,
but the fix would be incompatible. Let’s create a new version of
the language that fixes a whole bunch of those things together. While at the same time, not making
the new version of the language so different that it alienates the users. Every year at PyCon,
I sort of take the census of well, who is using Python 3 and
how committed are they? And how happy are they? And sort of,
are there users actually using Python 3? And, I see tremendous
progress there every year. But, we’re still not, we’re not in,
sort of, where we finally want to be. I mean, at the last PyCon conference,
I had to, sort of, hold off a whole bunch of people with very good arguments of
why we should do a Python 2.8 release. And so, Python 3 is winning the race. But it will, the race hasn’t been run yet. We’re still in the race. It’s clear that Python
3 is going to win it. [MUSIC]

Video: Office Hours: Paris, France

Summary of Coursera Office Hours in Paris:

  • Chuck, a Coursera instructor, held office hours in Paris, France.
  • He introduced two students: Seymour and Alex, who greeted the audience in both English and French.
  • Julian also joined the office hours but didn’t introduce himself.
  • Chuck expressed excitement about attending another office hour in Milan after Paris.
  • He jokingly called it a “fashion tour” despite not having the appropriate attire.
  • The video ends with Chuck saying goodbye and promising to see everyone in Milan.

Key Points:

  • The video provides a brief glimpse into Coursera’s in-person office hours with students.
  • It highlights the international reach of the platform and the casual, friendly atmosphere of the session.
  • The ending hints at upcoming office hours in Milan, Italy.

[MUSIC] Hello, Chuck here. We are here in Paris France with yet
another face to face office hours for Coursera, and I’d like you to meet
some of your fellow students. So, say hi and say your first name and if
you wanna say hi in French, go ahead and say hi in French or say hi in English. >> Hi

Bonjour. Seymour. >> Hi.
Sydney. >> Hi, I’m Alex. >> Hi, I’m Julian. >> Okay, there you go. Yet another successful office hours. In a bit I will hop on a plane and
go to Milan. This is called the fashion
tour of Paris and Milan, except that I don’t have the clothes for
that. But, we’ll see you soon in Milan. Bye.