3.5 While Loops

How to write a while loop using a counter


Watch this video from 26:29 to 30:40

# To load the video, execute this cell by pressing shift + enter

from IPython.display import YouTubeVideo
from datetime import timedelta
start=int(timedelta(hours=0, minutes=26, seconds=29).total_seconds())
end=int(timedelta(hours=0, minutes=30, seconds=40).total_seconds())

YouTubeVideo("Cvi3dByz9SE",start=start,end=end,width=640,height=360)

The following is a transcript of the video.

So聽that鈥檚 all about for loops. Let鈥檚 learn about聽one other major loop style today, which is the聽while loop, which is very much related to a聽counter.

You can聽read a for loop and it kind of makes sense as聽you use language. You can also use while in the same way, and it鈥檚 basically saying 鈥渨hile鈥澛燼 value, let鈥檚 call it counter is less聽than 25, run the loop.

So a while loop聽continues while the condition is true. Almost聽always a while loop has a Boolean statement. A聽聽Boolean statement is a mathematical statement that聽is either True or False. There鈥檚 no in between. These are very useful, especially because a聽while loop will run while the condition is true.

So if we use our counter, let鈥檚聽set the counter initially equal to two,聽because we鈥檙e going to initialize a聽Fibonacci list with two elements because we need聽to calculate the third. We鈥檙e going to use that one line of code that聽we just made and we have been using to calculate聽the Fibonacci sequence and then when we鈥檙e done with the code in our loop聽we鈥檙e going to use plus equals to make the counter聽go to three, and then we鈥檙e going to run the loop聽again, calculate the code, append the next element聽to our fibonacci list, the counter goes up by one again, and聽eventually the counter will equal 25. When it鈥檚 25 it will no longer be true聽that the counter is less than 25. And when that聽happens the counter, and the while loop, will not proceed further.

# A while loop continues WHILE the condition is TRUE
# A Boolean statement is either TRUE or FALSE

fibonacci = [0,1]
counter = 2

while counter < 25: # Loop continues one more time IF this is TRUE
    
    fibonacci.append(fibonacci[counter-2] + fibonacci[counter-1])
    
    counter += 1

We can check the聽length of our Fibonacci sequence that we produced.聽Remember we have the first two elements and聽then we鈥檙e going up to but not including 25,聽which seems like it鈥檚 24 but remember we鈥檙e聽starting on index position 0. So going between聽0 and 24 there should be 25 elements and that鈥檚聽the case.

# Check length of the sequence
# We used < 25 starting on 0

len(fibonacci)
25

And if we print out our Fibonacci聽sequence with 25 elements you can see again聽we get the Fibonacci sequence back. One is聽the sum of zero and one, two is a sum of one聽and one, three is the sum of one and two,聽five is the sum of two and three, and eight is the聽sum of three and five.

# We get the Fibonacci sequence back

print(fibonacci)
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368]

So what did we learn?

Python learning objectives

  • The range() function and how it produces a series of integers to iterate over in a loop

  • How to write a for loop

  • How to use the += sign as a counter

  • How to create an empty list outside of a loop and fill it with loop outputs

  • How to use a for loop to calculate the Fibonacci sequence

  • How to write a while loop using a counter

Plant learning objectives

  • Plants are computers!

  • They interatively produce outputs (like leaves and flowers), just like loops do

  • Modeling tries to recapitulate phenomena in nature. No model is perfect.

  • The Golden angle that plants use and we will calculate is based on computational mathematics

  • We will use sunflowers and the Golden angle as inspiration to learn from plants.

Thank you!